diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2018-05-16 19:08:16 -0700 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2018-05-16 19:08:59 -0700 |
| commit | ad94ed3653e2b7a9f68855670a32617aa80a637c (patch) | |
| tree | bfde4d9e82635aea25336bfda4bf73e819347a93 /core | |
| parent | 6f6c4377b236902566794c3d06820f3fdd7ec28c (diff) | |
Moved all the tree->lua and tree->nomsu code back into single functions
in nomsu.moon, and cleaned up how Vars are treated, since they are not
atomic.
Diffstat (limited to 'core')
| -rw-r--r-- | core/metaprogramming.nom | 34 | ||||
| -rw-r--r-- | core/operators.nom | 84 | ||||
| -rw-r--r-- | core/text.nom | 2 |
3 files changed, 60 insertions, 60 deletions
diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom index 41491c7..9d6e1e6 100644 --- a/core/metaprogramming.nom +++ b/core/metaprogramming.nom @@ -20,12 +20,12 @@ immediately lua:append("function(tree") local args = {} for i,tok in ipairs(\%actions[1]) do - if tok.type == "Var" then args[#args+1] = tok:as_lua(nomsu) end + if tok.type == "Var" then args[#args+1] = tok end end for i, arg in ipairs(args) do - lua:append(", ", arg) + lua:append(", ", nomsu:tree_to_lua(arg)) end - local body_lua = \%lua:as_lua(nomsu):as_statements("return ") + local body_lua = nomsu:tree_to_lua(\%lua):as_statements("return ") body_lua:remove_free_vars(args) body_lua:declare_locals() lua:append(")\n ", body_lua, "\nend);") @@ -50,13 +50,13 @@ immediately lua:append("function(") local args = {} for i,tok in ipairs(\%actions[1]) do - if tok.type == "Var" then args[#args+1] = tok:as_lua(nomsu) end + if tok.type == "Var" then args[#args+1] = tok end end for i, arg in ipairs(args) do - lua:append(arg) + lua:append(nomsu:tree_to_lua(arg)) if i < #args then lua:append(", ") end end - local body_lua = \%body:as_lua(nomsu):as_statements("return ") + local body_lua = nomsu:tree_to_lua(\%body):as_statements("return ") body_lua:remove_free_vars(args) body_lua:declare_locals() lua:append(")\n ", body_lua, "\nend);") @@ -81,7 +81,7 @@ immediately local replacements = {} for i,tok in ipairs(\%shorthand[1]) do if tok.type == "Var" then - local lua_var = tostring(tok:as_lua(nomsu)) + local lua_var = tostring(nomsu:tree_to_lua(tok)) replacements[tok] = lua_var lua:append(", ", lua_var) end @@ -104,7 +104,7 @@ immediately lua:append([[) local tree = ]], make_tree(\%longhand), [[ - return tree:as_lua(nomsu) + return nomsu:tree_to_lua(tree) end);]]) return lua @@ -119,27 +119,27 @@ action [remove action %stub] immediately action [%tree as nomsu] - =lua "\%tree:as_nomsu()" + =lua "nomsu:tree_to_nomsu(\%tree)" action [%tree as inline nomsu] - =lua "\%tree:as_nomsu(true)" + =lua "nomsu:tree_to_nomsu(\%tree, true)" action [%tree as lua] - =lua "\%tree:as_lua(nomsu)" + =lua "nomsu:tree_to_lua(\%tree)" action [%tree as lua expr] lua> ".." - local lua = \%tree:as_lua(nomsu) + local lua = nomsu:tree_to_lua(\%tree) if not lua.is_value then error("Invalid thing to convert to lua expr: "..\%tree) end return lua action [%tree as lua statements] - =lua "\%tree:as_lua(nomsu):as_statements()" + =lua "nomsu:tree_to_lua(\%tree):as_statements()" action [%tree with vars %vars] - =lua "nomsu:tree_with_replacements(\%tree, \%vars)" + =lua "\%tree:map(\%vars)" compile [declare locals in %code] to Lua value "\(%code as lua expr):declare_locals()" @@ -168,7 +168,7 @@ immediately immediately compile [nomsu] to: Lua value "nomsu" - compile [%var as lua identifier] to: Lua value "\(%var as lua expr):as_lua(nomsu)" + compile [%var as lua identifier] to: Lua value "nomsu:tree_to_lua(\(%var as lua expr))" # Compiler tools immediately @@ -178,7 +178,7 @@ immediately immediately compile [show lua %block] to lua> ".." - local \%lua = \%block:as_lua(nomsu); + local \%lua = nomsu:tree_to_lua(\%block); return Lua(nil, "print(", repr(tostring(\%lua)), ");"); immediately @@ -202,7 +202,7 @@ immediately compile [barf] to: Lua "error(nil, 0);" compile [barf %msg] to: Lua "error(\(%msg as lua expr), 0);" compile [assume %condition] to - lua> "local \%assumption = 'Assumption failed: '..tostring(\%condition:as_nomsu());" + lua> "local \%assumption = 'Assumption failed: '..tostring(nomsu:tree_to_nomsu(\%condition));" return Lua ".." if not \(%condition as lua expr) then diff --git a/core/operators.nom b/core/operators.nom index adfeeb5..62a3225 100644 --- a/core/operators.nom +++ b/core/operators.nom @@ -25,21 +25,21 @@ immediately # TODO: optimize case of [%x,%y] = [1,2] compile [%a is %b, %a = %b, %a == %b] to lua> ".." - local safe = {Text=true, Number=true}; - local a_lua, b_lua = \%a:as_lua(nomsu), \%b:as_lua(nomsu); + local safe = {Text=true, Number=true} + local a_lua, b_lua = \(%a as lua), \(%b as lua) if safe[\%a.type] or safe[\%b.type] then - return Lua.Value(nil, "(", a_lua, " == ", b_lua, ")"); + return Lua.Value(nil, "(", a_lua, " == ", b_lua, ")") else - return Lua.Value(nil, "utils.equivalent(", a_lua, ", ", b_lua, ")"); + return Lua.Value(nil, "utils.equivalent(", a_lua, ", ", b_lua, ")") end compile [%a isn't %b, %a is not %b, %a not= %b, %a != %b] to lua> ".." - local safe = {Text=true, Number=true}; - local a_lua, b_lua = \%a:as_lua(nomsu), \%b:as_lua(nomsu); + local safe = {Text=true, Number=true} + local a_lua, b_lua = \(%a as lua), \(%b as lua) if safe[\%a.type] or safe[\%b.type] then - return Lua.Value(nil, "(", a_lua, " ~= ", b_lua, ")"); + return Lua.Value(nil, "(", a_lua, " ~= ", b_lua, ")") else - return Lua.Value(nil, "(not utils.equivalent(", a_lua, ", ", b_lua, "))"); + return Lua.Value(nil, "(not utils.equivalent(", a_lua, ", ", b_lua, "))") end # For strict identity checking, use (%x's id) is (%y's id) compile [%'s id, id of %] to: Lua value "nomsu.ids[\(% as lua expr)]" @@ -47,14 +47,14 @@ immediately # Variable assignment operator immediately compile [%var <- %value] to - lua> "local \%var_lua = \%var:as_lua(nomsu);" + lua> "local \%var_lua = \(%var as lua);" assume %var_lua.is_value or barf "Invalid target for assignment: \%var" - lua> "local \%value_lua = \%value:as_lua(nomsu);" + lua> "local \%value_lua = \(%value as lua);" assume %value_lua.is_value or barf "Invalid value for assignment: \%value" lua> ".." - local lua = Lua(nil, \%var_lua, ' = ', \%value_lua, ';'); + local lua = Lua(nil, \%var_lua, ' = ', \%value_lua, ';') if \%var.type == 'Var' then - lua:add_free_vars({\%var}); + lua:add_free_vars({\%var}) end return lua; @@ -64,24 +64,24 @@ immediately assume ((%assignments' "type") is "Dict") or barf ".." Expected a Dict for the assignments part of '<- %' statement, not \%assignments lua> ".." - local lhs, rhs = Lua(), Lua(); + local lhs, rhs = Lua(), Lua() for i, item in ipairs(\%assignments) do - local target, value = item[1], item[2]; - local target_lua = target:as_lua(nomsu); - if not target_lua.is_value then error("Invalid target for assignment: "..target:get_src()); end - local value_lua = value:as_lua(nomsu); - if not value_lua.is_value then error("Invalid value for assignment: "..value:get_src()); end - if target.type == "Var" then - lhs:add_free_vars({target}); + local \%target, \%value = item[1], item[2] + local target_lua = \(%target as lua) + if not target_lua.is_value then error("Invalid target for assignment: "..\(%target as text)) end + local value_lua = \(%value as lua) + if not value_lua.is_value then error("Invalid value for assignment: "..\(%value as text)) end + if \%target.type == "Var" then + lhs:add_free_vars({\%target}) end if i > 1 then - lhs:append(", "); - rhs:append(", "); + lhs:append(", ") + rhs:append(", ") end - lhs:append(target_lua); - rhs:append(value_lua); + lhs:append(target_lua) + rhs:append(value_lua) end - return Lua(nil, lhs, " = ", rhs, ";"); + return Lua(nil, lhs, " = ", rhs, ";") immediately compile [external %var <- %value] to @@ -99,31 +99,31 @@ immediately compile [with %assignments %body] to %lua <- (%body as lua statements) lua> ".." - local lhs, rhs = Lua(), Lua(); - local vars = {}; + local lhs, rhs = Lua(), Lua() + local vars = {} for i, item in ipairs(\%assignments) do - local target, value = item[1], item[2]; - if not target.type == "Var" then - error("Invalid target for 'with' assignment: "..tostring(target)); + local \%target, \%value = item[1], item[2] + if not \%target.type == "Var" then + error("Invalid target for 'with' assignment: "..tostring(\%target)) end - local target_lua = target:as_lua(nomsu); - local value_lua = value:as_lua(nomsu); + local target_lua = \(%target as lua) + local value_lua = \(%value as lua) if not value_lua.is_value then - error("Invalid value for assignment: "..tostring(value)); + error("Invalid value for assignment: "..tostring(\%value)) end - if target.type == "Var" then - lhs:add_free_vars({target}); + if \%target.type == "Var" then + lhs:add_free_vars({\%target}) end if i > 1 then - lhs:append(", "); - rhs:append(", "); + lhs:append(", ") + rhs:append(", ") end - lhs:append(target_lua); - rhs:append(value_lua); - vars[i] = tostring(target_lua); + lhs:append(target_lua) + rhs:append(value_lua) + vars[i] = \%target end - \%lua:remove_free_vars(vars); - \%lua:prepend("local ", lhs, " = ", rhs, ";\n"); + \%lua:remove_free_vars(vars) + \%lua:prepend("local ", lhs, " = ", rhs, ";\n") return Lua ".." do diff --git a/core/text.nom b/core/text.nom index 7b945cd..b7fd2cf 100644 --- a/core/text.nom +++ b/core/text.nom @@ -44,7 +44,7 @@ lua> ".." local reset = "'"..colors["reset color"].."'"; nomsu:define_compile_action(name, function(tree) return Lua.Value(nil, color); end); nomsu:define_compile_action(name.." %", function(\%) - return Lua.Value(nil, color, "..", \%:as_lua(nomsu), "..", reset); + return Lua.Value(nil, color, "..", \(% as lua), "..", reset); end); end end |
