From b3b8c4d731b0983d5b12c56fc245a8d7c1d631f4 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 4 Dec 2017 17:35:47 -0800 Subject: Some stuff changed to allow escaped args and some other ports from the two_defs branch. --- lib/utils2.nom | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'lib/utils2.nom') 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 + + -- cgit v1.2.3