]> Cypherpunks repositories - keks.git/commitdiff
Package KEKS and KEKS::Schema
authorSergey Matveev <stargrave@stargrave.org>
Thu, 10 Apr 2025 08:10:25 +0000 (11:10 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Thu, 10 Apr 2025 08:17:55 +0000 (11:17 +0300)
c/lib/cm/default.schema.keks.do
go/cm/default.schema.keks.do
spec/schema/tcl.texi
tcl/keks.tcl
tcl/pkgIndex.tcl [new file with mode: 0644]
tcl/schema.tcl [moved from tcl/schema-marshal with 84% similarity]
tcl/test-vector.tcl

index b361b4004c3430fe7eeeb5add9a47331398df39a0b35bcc44f3b20c69ad44b4c..4134f0b79f06d182e307fa9340bb04281f17e2aa21f2764d8e0de7b54d4abe23 100644 (file)
@@ -1,7 +1,7 @@
 n=${2##*/}.tcl
 cd ../../../tcl/schemas
 {
-    echo ../schema-marshal $n
+    echo ../schema.tcl $n
     sed -n "s/^schema-include \(.*\)$/\1/p" <$n
 } | xargs redo-ifchange
-../schema-marshal $n | xxd -r -p
+TCLLIBPATH=.. ../schema.tcl $n | xxd -r -p
index cd2826b009c82bc5388b0bfbdffa627bd8db58656942523850436f4332d83186..fb10673f456bcd1848d9adfdec004352842c14a3dc37fecffdd1c1904fe51640 100644 (file)
@@ -1,7 +1,7 @@
 n=${2##*/}.tcl
 cd ../../tcl/schemas
 {
-    echo  ../schema-marshal $n
+    echo  ../schema.tcl $n
     sed -n "s/^schema-include \(.*\)$/\1/p" <$n
 } | xargs redo-ifchange
-../schema-marshal $n | xxd -r -p
+TCLLIBPATH=.. ../schema.tcl $n | xxd -r -p
index 758e6b55cc9ed5676955aad979797de545f581a3a2fd1cd294ec98e056bd6922..886a1b78d9c4351e33d599e6823a12718ab899543d6af1c82993ac39b2b035fd 100644 (file)
@@ -6,7 +6,7 @@
 
 Validation commands are pretty low-level and are inconvenient to write
 by hand, at least because of huge quantity of TAKEs.
-@command{tcl/schema-marshal} utility gives ability to convert much more
+@command{tcl/schema.tcl} utility gives ability to convert much more
 nicer schemas written on Tcl language to the KEKS-encoded commands. We
 call that Tcl-written schemas KEKS/Schema.
 
@@ -30,7 +30,7 @@ and @ref{cm-pub, cm/pub} as:
 @verbatiminclude ../tcl/schemas/pub-load.tcl
 @verbatiminclude ../tcl/schemas/pub-sig-tbs.tcl
 
-@command{schema-marshal} calls @code{schemas@{s0 cmds0 s1 cmds1 ...@}}
+@command{schema.tcl} calls @code{schemas@{s0 cmds0 s1 cmds1 ...@}}
 commands to produce an encoded map with @code{cmds*} commands for
 @code{s*} schemas. There is @code{field} command that helps creation of
 commands related to the field.
index aa15b61c40e859ac831119e78fe2431bea6aef2376c45f6af16e82bb8eea972a..5f661d39a574fcaaff4b59c0959067db24d72dc9e0648928312ec5e8b9ad52f1 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/env tclsh
-# TclKEKS -- Tcl KEKS encoder implementation
+# keks.tcl -- Tcl KEKS encoder implementation
 # Copyright (C) 2024-2025 Sergey Matveev <stargrave@stargrave.org>
 #
 # This program is free software: you can redistribute it and/or modify
@@ -16,6 +16,8 @@
 
 namespace eval KEKS {
 
+set version 0.1.0
+
 proc add {v} {
     upvar buf buf
     set buf [string cat $buf $v]
@@ -301,6 +303,8 @@ namespace export LIST MAP SET LenFirstSort BLOB
 
 }
 
+package provide KEKS $KEKS::version
+
 if {[info exists argv0] && ([file tail [info script]] eq [file tail $argv0])} {
     namespace import KEKS::*
     set buf ""
diff --git a/tcl/pkgIndex.tcl b/tcl/pkgIndex.tcl
new file mode 100644 (file)
index 0000000..9b73629
--- /dev/null
@@ -0,0 +1,2 @@
+package ifneeded KEKS 0.1.0 [list source [file join $dir keks.tcl]]
+package ifneeded KEKS::Schema 0.1.0 [list source [file join $dir schema.tcl]]
similarity index 84%
rename from tcl/schema-marshal
rename to tcl/schema.tcl
index e26a130ff4103ad69ea5b4a6f2f3ce5ba5bff1ef7be823083fb4cc0cb6c84bc5..35f7671a6cf66fa86bf1c39406782c4b9ba2c96d5073cd6cd81fb14e64762d93 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/env tclsh
-# schema-marshal -- Convert Tcl schemas to KEKS representation
+# schema.tcl -- Convert Tcl schemas to KEKS representation
 # Copyright (C) 2024-2025 Sergey Matveev <stargrave@stargrave.org>
 #
 # This program is free software: you can redistribute it and/or modify
 # You should have received a copy of the GNU Lesser General Public
 # License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-source [file join [file dirname $::argv0] keks.tcl]
-namespace import KEKS::*
+package require KEKS
+
+namespace eval KEKS::Schema {
+
+set version 0.1.0
 
 proc TAKE {k} {
     if {[string is digit $k]} {
@@ -42,33 +45,11 @@ proc SCHEMA {s} {subst {LIST {{STR S} {STR $s}}}}
 proc TIMEPREC {p} {subst {LIST {{STR TP} {INT $p}}}}
 proc UTC {} {return {LIST {{STR UTC}}}}
 
-proc schema-process {v} {
-    upvar _pairs _pairs
-    foreach {name cmds} $v {
-        set _cmds {}
-        if {$name == "schema-include"} {
-            set fd [open [file join $cmds]]
-            set inc [read $fd]
-            close $fd
-            schema-process $inc
-            continue
-        }
-        foreach cmd $cmds {eval $cmd}
-        lappend _pairs $name [list LIST $_cmds]
-    }
-}
-
-proc schemas {v} {
-    set _pairs {}
-    schema-process $v
-    MAP $_pairs
-}
-
 set timeprecArgs [dict create s 0 ms 3 us 6 ns 9 ps 12 fs 15 as 18]
 set knownTypes {bin blob bool hexlet int list magic map nil set str tai}
 
 proc field {k types args} {
-    upvar _cmds _cmds
+    upvar _cmds _cmds buf buf
     if {[lindex $types 0] == "with"} {
         lappend _cmds [TAKE $k]
         lappend _cmds [SCHEMA [lindex $types 1]]
@@ -115,7 +96,7 @@ proc field {k types args} {
     set i [lsearch -glob $args "prec=*"]
     if {$i != -1} {
         set p [string range [lindex $args $i] 5 end]
-        global timeprecArgs
+        variable timeprecArgs
         set p [dict get $timeprecArgs $p]
         lappend _cmds [TAKE $k]
         lappend _cmds [TIMEPREC $p]
@@ -134,7 +115,7 @@ proc field {k types args} {
         set s [lindex [lindex $args $i] 1]
         lappend _cmds [TAKE $k]
         lappend _cmds [EACH]
-        global knownTypes
+        variable knownTypes
         if {[lsearch -exact $knownTypes $s] == -1} {
             lappend _cmds [SCHEMA $s]
         } {
@@ -143,8 +124,43 @@ proc field {k types args} {
     }
 }
 
-set _schema {}
-foreach f $::argv {lappend _schema schema-include $f}
-MAGIC schema
-schemas $_schema
-puts [binary encode hex $::KEKS::buf]
+proc process {v} {
+    upvar _pairs _pairs buf buf
+    foreach {name cmds} $v {
+        set _cmds {}
+        if {$name == "schema-include"} {
+            set fd [open [file join $cmds]]
+            set inc [read $fd]
+            close $fd
+            process $inc
+            continue
+        }
+        foreach cmd $cmds {eval $cmd}
+        lappend _pairs $name [list LIST $_cmds]
+    }
+}
+
+proc do {v} {
+    upvar buf buf
+    set _pairs {}
+    process $v
+    MAP $_pairs
+}
+
+namespace export TAKE EXISTS !EXISTS EACH EQ TYPE GT LT SCHEMA TIMEPREC UTC
+namespace export do process field
+
+}
+
+package provide KEKS::Schema $KEKS::Schema::version
+
+namespace import KEKS::*
+
+if {[info exists argv0] && ([file tail [info script]] eq [file tail $argv0])} {
+    set _schema {}
+    foreach f $::argv {lappend _schema schema-include $f}
+    set buf ""
+    MAGIC schema
+    KEKS::Schema::do $_schema
+    puts [binary encode hex $buf]
+}
index 2b471357c401bcf9b38ba08701ca9a3b125945758b4da5b0a77d0c929ef0b0a5..3dbcf08f5808b3453227d81430a8a74f8e5bfabff78dbc2c70fc07fe9ab34993 100644 (file)
@@ -1,4 +1,4 @@
-source keks.tcl
+package require KEKS
 namespace import KEKS::*
 set buf ""