5 .param pmc argv :slurpy
8 options = get_root_global ['_tcl'; 'helpers'; 'subst'], 'options'
10 .local pmc select_switches, switches
11 select_switches = get_root_global ['_tcl'], 'select_switches'
13 switches = select_switches(options, argv)
17 if argc == 0 goto badargs
18 if argc > 1 goto badswitch
20 .local string str, newstr
23 .local int nobackslashes, nocommands, novariables
24 nobackslashes = exists switches['nobackslashes']
25 nocommands = exists switches['nocommands']
26 novariables = exists switches['novariables']
31 .local pmc parse, match, astgrammar, pirgrammar
32 astgrammar = new 'TclExpr::PAST::Grammar'
33 pirgrammar = new 'TclExpr::PIR::Grammar'
35 .local pmc splitNamespace, ns
36 .local string namespace
37 splitNamespace = get_root_global ['_tcl'], 'splitNamespace'
38 ns = splitNamespace('', 2)
43 namespace = join "'; '", ns
44 namespace = "['" . namespace
47 if pos >= len goto done
48 $S0 = substr str, pos, 1
49 if $S0 == '[' goto command
50 if $S0 == '$' goto variable
51 if $S0 == '\' goto backslash
57 if nocommands goto next
58 parse = get_root_global ['parrot'; 'TclExpr::Grammar'], 'subst_command'
62 if novariables goto next
63 parse = get_root_global ['parrot'; 'TclExpr::Grammar'], 'subst_variable'
67 if nobackslashes goto next
68 parse = get_root_global ['parrot'; 'TclExpr::Grammar'], 'subst_backslash'
72 match = parse(str, 'pos'=>pos, 'grammar'=>'TclExpr::Grammar')
74 .local pmc astbuilder, ast
75 astbuilder = astgrammar.'apply'(match)
76 ast = astbuilder.'get'('past')
80 pirbuilder = pirgrammar.'apply'(ast)
81 code = pirbuilder.'get'('result')
87 pir = new 'CodeString'
89 pir.emit(".HLL 'Tcl', ''")
90 pir.emit(".loadlib 'tcl_ops'")
91 pir.emit('.namespace %0', namespace)
92 pir.emit(".include 'languages/tcl/src/returncodes.pasm'")
93 pir.emit(".sub '_anon' :anon")
95 pir.emit(' .return(%0)', ret)
105 substr str, pos, $I1, newstr
118 die 'wrong # args: should be "subst ?-nobackslashes? ?-nocommands? ?-novariables? string"'
122 $S0 = 'bad switch "' . $S0
123 $S0 .= '": must be -nobackslashes, -nocommands, or -novariables'
127 .sub 'anon' :anon :load
129 options = new 'TclList'
130 options[0] = 'nobackslashes'
131 options[1] = 'nocommands'
132 options[2] = 'novariables'
134 set_root_global ['_tcl'; 'helpers'; 'subst'], 'options', options
142 # vim: expandtab shiftwidth=4 ft=pir: