dialect
lib/dialect.vklib/dialect.vk
Pure-Veko homoiconic block walker, structural pattern matcher, and AST/DSL rewriter.
Designed for building custom Veko dialects over quote [...] blocks.
Zero capabilities required.
Rule = struct { name str, pattern block, transform fn[map->any] }
Describes a single structural rewrite rule (pattern -> transform fn[map -> any]).
rewrite-tree = fn [node any, visitor fn[any->any]] any
Recursively visits every node in a quoted/data block, applying visitor fn[any -> any]
bottom-up to produce a transformed block tree.
use [d = dialect]
tree = [1 [2 3] 4]
out = d/rewrite-tree tree fn [node] [
if (type-of node) == .i64_val [ node * 10 ] else [ node ]
]
if out != [10 [20 30] 40] [ err 'rewrite-tree' ] else [ .ok ]
collect = fn [tree any, pred fn[any->bool]] block
Collects every node inside tree for which (pred node) returns true.
match-pattern = fn [pattern block, target block] map
Matches a flat or nested pattern against target block.
String placeholders starting with '$' (e.g., '$proto', '$port') bind values
into the returned map, and '*' matches any single element.
Returns a bindings map on match, or err 'dialect/match-pattern: no match'.
use [d = dialect]
m = d/match-pattern [.allow '$proto' '$port'] [.allow .tcp 443]
if (map/get m 'proto') != .tcp or (map/get m 'port') != 443 [ err 'match-pattern' ] else [ .ok ]
apply-rules = fn [clauses block[block], rules block[Rule]] block
Evaluates a sequence of dialect clauses (block[block]) against an ordered list of Rules,
returning a block of transformed results (or err on the first unmatched clause).
use [d = dialect]
rules = [
d/Rule {
name = 'allow'
pattern = [.allow '$proto' '$port']
transform = fn [b] [ { action = .allow, proto = (map/get b 'proto'), port = (map/get b 'port') } ]
}
]
res = d/apply-rules [[.allow .tcp 443] [.allow .udp 53]] rules
if (len res) != 2 or res[1].port != 53 [ err 'apply-rules' ] else [ .ok ]
split-clauses = fn [items block, delim any] block
Splits a flat DSL token/item block into sub-blocks whenever delim appears.
use [d = dialect]
parts = d/split-clauses [.get '/a' .end .post '/b'] .end
if (len parts) != 2 or parts[1][0] != .post [ err 'split-clauses' ] else [ .ok ]