commit df11fb12eae38d88b7a219399a4fc5c1d81cf67a
parent 70fc3ed776441da3b905f91d1aea7479f3e7953b
Author: Jan Pobrislo <ccx@te2000.cz>
Date: Thu, 24 Apr 2025 15:58:29 +0000
WIP specification with Prolog implementation
Diffstat:
| A | doc/spec.pl | | | 56 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 56 insertions(+), 0 deletions(-)
diff --git a/doc/spec.pl b/doc/spec.pl
@@ -0,0 +1,56 @@
+:- use_module(library(error)).
+:- use_module(library(http/dcg_basics)).
+
+:- op(950, xfx, will_be).
+
+will_be(Value, Predicate) :-
+ freeze(Value, call(Predicate, Value)).
+
+byte(Value) :- must_be(between(0, 255), Value).
+% integer(Value), Value >= 0, Value <= 255.
+
+nonnegative_integer(Value) :- must_be(nonneg, Value).
+
+sequence_of_bytes([]).
+sequence_of_bytes([First|Rest]) :-
+ First will_be byte,
+ Rest will_be sequence_of_bytes.
+% freeze(First, byte(First)),
+% freeze(Rest, sequence_of_bytes(Rest)).
+
+netstring_encoding(netstring(PayloadLength, PayloadBytes), Bytes) :-
+ Bytes will_be sequence_of_bytes,
+ PayloadLength will_be nonnegative_integer,
+ % freeze(Bytes, sequence_of_bytes(Bytes)),
+ % freeze(PayloadLength, integer(PayloadLength)).
+ phrase(netstring_encoding(PayloadLength, PayloadBytes), Bytes).
+%length(Payload, PayloadLength),
+netstring_encoding(PayloadLength, PayloadBytes) -->
+ {length(PayloadBytes, PayloadLength)},
+ netstring_prefix(PayloadLength),
+ PayloadBytes,
+ `,`.
+
+nonzero_digit(Code) --> [Code], {member(Code, `123456789`)}.
+positive_decimal(N) -->
+ {freeze(N, format(codes([C|Cs]), '~w', N))},
+ nonzero_digit(C),
+ digits(Cs),
+ {phrase(number(N), [C|Cs])}.
+netstring_prefix(0) --> `0:`.
+netstring_prefix(N) --> positive_decimal(N), `:`.
+
+netstring_of(Goal, Bytes) :-
+ ground(Goal),
+ !,
+ call(Goal, PayloadBytes),
+ netstring_encoding(netstring(_, PayloadBytes), Bytes).
+
+netstring_of(Goal, Bytes) :-
+ ground(Bytes),
+ !,
+ netstring_encoding(netstring(_, PayloadBytes), Bytes),
+ call(Goal, PayloadBytes).
+
+% :- use_module(library(pldoc/doc_files)).
+% doc_save('spec.pl', [format(html), doc_root(.)]).