From a104f80e61f7e6a110a209cf7cc5ffd09d6b5b4f Mon Sep 17 00:00:00 2001 From: "Konstantin Kirik (snegovick)" Date: Sat, 13 Dec 2025 14:45:13 +0300 Subject: Add variable substitution --- appl/lib/sh9parser.b | 94 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 87 insertions(+), 7 deletions(-) (limited to 'appl/lib/sh9parser.b') diff --git a/appl/lib/sh9parser.b b/appl/lib/sh9parser.b index 8df4cfc..54c26b5 100644 --- a/appl/lib/sh9parser.b +++ b/appl/lib/sh9parser.b @@ -26,13 +26,87 @@ GrammarNode.print_expr(gn: self ref GrammarNode) } } +ShModule.find_var(m: self ref ShModule, name: string): ref ModVar +{ + l:= len m.vars; + vars := m.vars; + for (i:=0; iprint("%s: %s\n", v.name, v.val); + vars = tl vars; + } +} + +ShModule.set_var(m: self ref ShModule, v: ref ModVar) +{ + m.vars = v :: m.vars; +} + ParserCtx.add_module(ctx: self ref ParserCtx, name: string) { m:= ref ShModule; m.name = name; + if (ctx.current_module == nil) { + sys->print("Set current module %s\n", name); + ctx.current_module = name; + } ctx.modules = m :: ctx.modules; } +ParserCtx.find_module(ctx: self ref ParserCtx, name: string): ref ShModule +{ + l:= len ctx.modules; + mods := ctx.modules; + for (i:=0; iPATH; @@ -127,24 +201,30 @@ parse_toks(toks: array of ref TokNode, g: array of ref GrammarNode): array of re do { lt := len toks; - #sys->print("Loop %d: ", ctr); - #print_toks_short(toks); + sys->print("Loop %d: ", ctr); + print_toks_short(toks); ctr ++; changed = 0; - fast: for (i := 0; i <= lt; i ++) { + fast: for (i := 0; i < lt; i ++) { for (j := 0; j < lgns; j++) { gj:= g[j]; - if (check_grammar_node_match(toks[lt - i:], gj) == 1) { + if (check_grammar_node_match(toks[i:], gj) == 1) { #sys->print("Something matched !\n"); #gj.print_expr(); #sys->print("Before replace: "); #print_toks_short(toks); - gj.callback(gj.ctx, toks[lt-i: lt-i+len gj.expr]); + if ((i+len gj.expr) > lt) { + continue; + } + #sys->print("len toks: %d, i: %d, len gj.expr: %d\n", len toks, i, len gj.expr); + result := gj.callback(gj.ctx, toks[i:i+len gj.expr]); if (gj.transform == S_NONE) { - toks = replace_toks(toks, lt-i, len gj.expr, array[0] of ref TokNode); + toks = replace_toks(toks, i, len gj.expr, array[0] of ref TokNode); + } else if (gj.transform == nil) { + toks = replace_toks(toks, i, len gj.expr, result); } else { - toks = replace_toks(toks, lt-i, len gj.expr, array[] of {mk_tok(toks[lt - i].start, toks[lt - i].line, "", gj.transform)}); + toks = replace_toks(toks, i, len gj.expr, array[] of {mk_tok(toks[i].start, toks[i].line, "", gj.transform)}); } #sys->print("After replace: "); changed = 1; -- cgit v1.2.3