From 4df19e3a19ca7572d895ff4da589bc23850eab16acc16b7a5fdd1ac2f0ca204e Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Fri, 4 Apr 2025 21:22:25 +0300 Subject: [PATCH] More compact commands encoding --- c/lib/schema.c | 18 ++--- go/schema/check.go | 18 ++--- spec/schema/cmds.texi | 162 +++++++++++++++++++++--------------------- tcl/schema2bin | 18 ++--- 4 files changed, 108 insertions(+), 108 deletions(-) diff --git a/c/lib/schema.c b/c/lib/schema.c index 6324b6b..2d8c1eb 100644 --- a/c/lib/schema.c +++ b/c/lib/schema.c @@ -21,15 +21,15 @@ #include "items.h" #include "schema.h" -static const char CmdEach[] = "EACH"; -static const char CmdExists[] = "EXISTS"; -static const char CmdGT[] = "GT"; -static const char CmdLT[] = "LT"; -static const char CmdNotExists[] = "!EXISTS"; -static const char CmdSchema[] = "SCHEMA"; -static const char CmdTake[] = "TAKE"; -static const char CmdTimeMaxPrec[] = "TIMEMAXPREC"; -static const char CmdType[] = "TYPE"; +static const char CmdEach[] = "*"; +static const char CmdExists[] = "E"; +static const char CmdGT[] = ">"; +static const char CmdLT[] = "<"; +static const char CmdNotExists[] = "!E"; +static const char CmdSchema[] = "S"; +static const char CmdTake[] = "."; +static const char CmdTimeMaxPrec[] = "TMP"; +static const char CmdType[] = "T"; static const char TypeBin[] = "BIN"; static const char TypeBlob[] = "BLOB"; diff --git a/go/schema/check.go b/go/schema/check.go index a147234..df779be 100644 --- a/go/schema/check.go +++ b/go/schema/check.go @@ -29,15 +29,15 @@ import ( ) const ( - CmdTake = "TAKE" - CmdEach = "EACH" - CmdExists = "EXISTS" - CmdNotExists = "!EXISTS" - CmdType = "TYPE" - CmdLT = "LT" - CmdGT = "GT" - CmdSchema = "SCHEMA" - CmdTimeMaxPrec = "TIMEMAXPREC" + CmdEach = "*" + CmdExists = "E" + CmdGT = ">" + CmdLT = "<" + CmdNotExists = "!E" + CmdSchema = "S" + CmdTake = "." + CmdTimeMaxPrec = "TMP" + CmdType = "T" Magic = "schema" ) diff --git a/spec/schema/cmds.texi b/spec/schema/cmds.texi index 4487ced..4d1d90e 100644 --- a/spec/schema/cmds.texi +++ b/spec/schema/cmds.texi @@ -14,50 +14,50 @@ generated from higher level schema descriptions. @table @code -@item TAKE k -Choose the value of the "k" key in the map, if "k" is a string. +@item . k +Take/choose the value of the "k" key in the map, if "k" is a string. If "k" is integer, then choose the k-th value in a list. If "k" equals to ".", then choose the element you are currently in (current map or list). Command never fails, but key can be non-existent. -@item EXISTS -Check that TAKEn element exists. +@item E +Check that chosen element exists. -@item !EXISTS -Check that TAKEn element does not exist. +@item !E +Check that chosen element does not exist. -@item EACH -Execute the next command against every element of the TAKEn list, of -every value of the map. Do nothing if taken list/map is empty. - -@item TYPE T0 [T1 ...] -Check that TAKEn element's type is in (T0[, T1 ...]) set. -Do nothing if no element was taken. +@item * +Execute the next command against every element of the chosen (if it +exists) list, or every value of the map. +@item T T0 [T1 ...] +Check that chosen (if it exists) element's type is in (T0[, T1 ...]) set. Possible types: BIN, BLOB, BOOL, HEXLET, INT, LIST, MAGIC, MAP, NIL, STR, TAI64. -@item GT n -Check that TAKEn integer value is greater than "n". If taken value is -either list or map, then check their length. If the value is a string, -then check its length. Do nothing if no element was taken. - -@item LT n -Same as @code{GT}, but check that value is less than "n". - -@item SCHEMA s -Check TAKEn element against schema with the name equal to "s". -Do nothing if no element was taken. - -@item TIMEMAXPREC p -Check maximal allowable time precision. "p" is integer with following -possible values: 0 -- only full seconds allowed, no parts; -3 -- only up to milliseconds; -6 -- only up to microseconds; -9 -- only up to nanoseconds; -12 -- only up to picoseconds; -15 -- only up to femtoseconds; -18 -- up to attoseconds; +@item > n +Check that chosen (if it exists) integer value is greater than "n". +If chosen value is either list or map, then check their length. +If the value is a string, then check its length. + +@item < n +Same as @code{>}, but check that value is less than "n". + +@item S s +Check chosen (if it exists) element against schema named "s". + +@item TMP p +Check that chosen (if it exists) element, of time type, has value below +maximal time precision. "p" is integer with following possible values: + @itemize + @item 0 -- only full seconds allowed, no parts; + @item 3 -- only up to milliseconds; + @item 6 -- only up to microseconds; + @item 9 -- only up to nanoseconds; + @item 12 -- only up to picoseconds; + @item 15 -- only up to femtoseconds; + @item 18 -- up to attoseconds; + @end itemize @end table @@ -75,29 +75,29 @@ Corresponding schema can be: @verbatim {"our": [ - ["TAKE", "a"], - ["EXISTS"], - ["TAKE", "a"], - ["TYPE", "STR"], - ["TAKE", "a"], - ["GT", 0], - - ["TAKE", "v"], - ["EXISTS"], - ["TAKE", "v"], - ["TYPE", "BIN", "STR"], - - ["TAKE", "fpr"], - ["EXISTS"], - ["TAKE", "fpr"], - ["TYPE", "BIN"], - ["TAKE", "fpr"], - ["GT", 31], - ["TAKE", "fpr"], - ["LT", 33], - - ["TAKE", "comment"], - ["TYPE", "STR"], + [".", "a"], + ["E"], + [".", "a"], + ["T", "STR"], + [".", "a"], + [">", 0], + + [".", "v"], + ["E"], + [".", "v"], + ["T", "BIN", "STR"], + + [".", "fpr"], + ["E"], + [".", "fpr"], + ["T", "BIN"], + [".", "fpr"], + [">", 31], + [".", "fpr"], + ["<", 33], + + [".", "comment"], + ["T", "STR"], ]} @end verbatim @@ -113,32 +113,32 @@ wheres = [+ where] @verbatim { "where": [ - ["TAKE", "."], - ["TYPE", "LIST"], - ["TAKE", "."], - ["GT", 1], - ["TAKE", "."], - ["LT", 3], - ["TAKE", "."], - ["EACH"], - ["TAKE", "INT"], - ["TAKE", 0], - ["GT", -91], - ["TAKE", 0], - ["LT", 91], - ["TAKE", 1], - ["GT", -181], - ["TAKE", 1], - ["LT", 181], + [".", "."], + ["T", "LIST"], + [".", "."], + [">", 1], + [".", "."], + ["<", 3], + [".", "."], + ["*"], + [".", "INT"], + [".", 0], + [">", -91], + [".", 0], + ["<", 91], + [".", 1], + [">", -181], + [".", 1], + ["<", 181], ], "wheres": [ - ["TAKE", "."], - ["TYPE", "LIST"], - ["TAKE", "."], - ["GT", 0], - ["TAKE", "."], - ["EACH"], - ["SCHEMA", "where"], + [".", "."], + ["T", "LIST"], + [".", "."], + [">", 0], + [".", "."], + ["*"], + ["S", "where"], ], } @end verbatim diff --git a/tcl/schema2bin b/tcl/schema2bin index e345f48..6df461e 100755 --- a/tcl/schema2bin +++ b/tcl/schema2bin @@ -23,22 +23,22 @@ proc TAKE {v} { } { set v [list STR $v] } - subst {{LIST {{STR TAKE} {$v}}}} + subst {{LIST {{STR .} {$v}}}} } -proc EXISTS {} {subst {{LIST {{STR EXISTS}}}}} -proc !EXISTS {} {subst {{LIST {{STR !EXISTS}}}}} -proc EACH {} {subst {{LIST {{STR EACH}}}}} +proc EXISTS {} {return {{LIST {{STR E}}}}} +proc !EXISTS {} {return {{LIST {{STR !E}}}}} +proc EACH {} {return {{LIST {{STR *}}}}} proc TYPE {vs} { - set l {{STR TYPE}} + set l {{STR T}} foreach v $vs { lappend l "STR $v" } subst {{LIST {$l}}} } -proc GT {v} {subst {{LIST {{STR GT} {INT $v}}}}} -proc LT {v} {subst {{LIST {{STR LT} {INT $v}}}}} -proc SCHEMA {v} {subst {{LIST {{STR SCHEMA} {STR $v}}}}} -proc TIMEMAXPREC {v} {subst {{LIST {{STR TIMEMAXPREC} {INT $v}}}}} +proc GT {v} {subst {{LIST {{STR >} {INT $v}}}}} +proc LT {v} {subst {{LIST {{STR <} {INT $v}}}}} +proc SCHEMA {v} {subst {{LIST {{STR S} {STR $v}}}}} +proc TIMEMAXPREC {v} {subst {{LIST {{STR TMP} {INT $v}}}}} proc evals {cmds} { set rv {} -- 2.48.1