diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2017-12-04 17:35:47 -0800 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2017-12-04 17:35:47 -0800 |
| commit | b3b8c4d731b0983d5b12c56fc245a8d7c1d631f4 (patch) | |
| tree | 21c1bf182440b26edb621e76cf8e730d7dc6849e /lib/utils2.nom | |
| parent | 8c0816995afaaf34cbfe903e6da68d8b6d8e8c39 (diff) | |
Some stuff changed to allow escaped args and some other ports from the
two_defs branch.
Diffstat (limited to 'lib/utils2.nom')
| -rw-r--r-- | lib/utils2.nom | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/lib/utils2.nom b/lib/utils2.nom index 2562471..415f8c3 100644 --- a/lib/utils2.nom +++ b/lib/utils2.nom @@ -2,9 +2,51 @@ require "lib/metaprogramming.nom" require "lib/utils.nom" require "lib/control_flow.nom" require "lib/operators.nom" +require "lib/collections.nom" + compile [say %str] to: if ((%str's "type") == "String"): "nomsu:writeln(\(%str as lua))" ..else: "nomsu:writeln(nomsu:stringify(\(%str as lua)))" + +compile [do %action] to code: + if ((%action's "type") == "Thunk"): + %action as lua statements + ..else: + "(\(%action as lua))(nomsu, vars);" + +# With statement +compile [with %assignments %action] to code: + %data = [] + for %i = %assignment in (%assignments' "value"): + %tokens = (%assignment's "value") + %var = (%tokens -> 1) + %eq = (%tokens -> 2) + assert (=lua "vars.eq and vars.eq.type == 'Word' and vars.eq.value == '='") ".." + |Invalid format for 'with' statement. List entries must have the form %var = (value) + %value = (%tokens -> 3) + add (d{i=%i; var=%var; value=%value}) to %data + %foo = (..) + join (..) + "local old_value\(%->"i") = \((%->"var") as lua); \((%->"var") as lua) = \((%->"value") as lua);" + ..for all %data + ..with glue "\n " + ".." + |do + | \(%foo) + | local fell_through = false; + | local ok, ret1, ret2 = pcall(function(nomsu, vars) + | \(%action as lua statements); + | fell_through = true; + | end, nomsu, vars); + | \(join ("\((%->"var") as lua) = old_value\(%->"i");" for all %data) with glue "\n ") + | if not ok then nomsu:error(ret1); end + | if not fell_through then + | return ret1, ret2; + | end + |end +parse [with %thing = %value %action] as: with [%thing = %value] %action + + |
