diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/control_flow.nom | 38 | ||||
| -rw-r--r-- | core/math.nom | 8 | ||||
| -rw-r--r-- | core/metaprogramming.nom | 61 | ||||
| -rw-r--r-- | core/operators.nom | 40 | ||||
| -rw-r--r-- | core/text.nom | 6 |
5 files changed, 73 insertions, 80 deletions
diff --git a/core/control_flow.nom b/core/control_flow.nom index 8e7306d..129f236 100644 --- a/core/control_flow.nom +++ b/core/control_flow.nom @@ -144,7 +144,7 @@ immediately for %var in %start to %stop via %step %body ..to # This uses Lua's approach of only allowing loop-scoped variables in a loop - assume (%var.type is "Var") or barf "Loop expected variable, not: \(%var's source code)" + assume (%var.type is "Var") or barf "Loop expected variable, not: \%var" %lua <- Lua ".." for \(%var as lua expr)=\(%start as lua expr),\(%stop as lua expr),\(%step as lua expr) do @@ -153,7 +153,7 @@ immediately %body has subtree % where (%.type = "Action") and ((%'s stub) is "do next %") and - %.value.3.value is %var.value + %.3 = %var ..: to %lua write "\n ::continue_\(%var as lua identifier)::;" to %lua write "\nend --numeric for-loop" @@ -161,7 +161,7 @@ immediately %body has subtree % where (%.type = "Action") and ((%'s stub) is "stop %") and - %.value.2.value is %var.value + %.2 = %var .. %lua <- Lua ".." @@ -179,7 +179,7 @@ immediately immediately compile [for %var in %iterable %body] to # This uses Lua's approach of only allowing loop-scoped variables in a loop - assume (%var.type is "Var") or barf "Loop expected variable, not: \(%var's source code)" + assume (%var.type is "Var") or barf "Loop expected variable, not: \%var" %lua <- Lua ".." for i,\(%var as lua identifier) in ipairs(\(%iterable as lua expr)) do @@ -188,14 +188,14 @@ immediately %body has subtree % where (%.type = "Action") and ((%'s stub) is "do next %") and - %.value.3.value is %var.value + %.3 = %var ..: to %lua write (Lua "\n ::continue_\(%var as lua identifier)::;") to %lua write "\nend --foreach-loop" if %body has subtree % where (%.type = "Action") and ((%'s stub) is "stop %") and - %.value.2.value is %var.value + %.2 = %var .. %lua <- Lua ".." @@ -209,8 +209,8 @@ immediately immediately compile [for %key = %value in %iterable %body] to # This uses Lua's approach of only allowing loop-scoped variables in a loop - assume (%key.type is "Var") or barf "Loop expected variable, not: \(%key's source code)" - assume (%value.type is "Var") or barf "Loop expected variable, not: \(%value's source code)" + assume (%key.type is "Var") or barf "Loop expected variable, not: \%key" + assume (%value.type is "Var") or barf "Loop expected variable, not: \%value" %lua <- Lua ".." for \(%key as lua identifier),\(%value as lua identifier) in pairs(\(%iterable as lua expr)) do @@ -219,14 +219,14 @@ immediately %body has subtree % where (%.type = "Action") and ((%'s stub) is "do next %") and - %.value.3.value is %key.value + %.3 = %key ..: to %lua write (Lua "\n ::continue_\(%key as lua identifier)::;") if %body has subtree % where (%.type = "Action") and ((%'s stub) is "do next %") and - %.value.3.value is %value.value + %.3 = %value ..: to %lua write (Lua "\n ::continue_\(%value as lua identifier)::;") to %lua write "\nend --foreach-loop" @@ -235,14 +235,14 @@ immediately %body has subtree % where (%.type = "Action") and ((%'s stub) is "stop %") and - %.value.2.value is %key.value + %.2 = %key ..: to %stop_labels write "\n::stop_\(%key as lua identifier)::;" if %body has subtree % where (%.type = "Action") and ((%'s stub) is "stop %") and - %.value.2.value is %value.value + %.2 = %value ..: to %stop_labels write "\n::stop_\(%value as lua identifier)::;" if: (length of %stop_labels) > 0 @@ -262,15 +262,14 @@ immediately %is_first <- (yes) %seen_else <- (no) %branches <- - %body.value if (%body.type = "Block") else [%body] + %body if (%body.type = "Block") else [%body] for %func_call in %branches assume (%func_call.type is "Action") or barf ".." Invalid format for 'when' statement. Only '*' blocks are allowed. - %tokens <- %func_call.value with {..} - %star: %tokens.1 - %condition: %tokens.2 - %action: %tokens.3 + %star: %func_call.1 + %condition: %func_call.2 + %action: %func_call.3 .. assume ((%star and (%star.type is "Word")) and (%star.value is "*")) or barf ".." Invalid format for 'when' statement. Lines must begin with '*' @@ -311,12 +310,11 @@ immediately %is_first <- (yes) %seen_else <- (no) %branches <- - %body.value if (%body.type = "Block") else [%body] + %body if (%body.type = "Block") else [%body] for %func_call in %branches assume (%func_call.type is "Action") or barf ".." Invalid format for 'when' statement. Only '*' blocks are allowed. - %tokens <- %func_call.value - with {%star:%tokens.1, %condition:%tokens.2, %action:%tokens.3} + with {%star:%func_call.1, %condition:%func_call.2, %action:%func_call.3} assume ((%star and (%star.type is "Word")) and (%star.value is "*")) or barf ".." Invalid format for 'when' statement. Lines must begin with '*' assume %condition or barf ".." diff --git a/core/math.nom b/core/math.nom index c507b87..120c210 100644 --- a/core/math.nom +++ b/core/math.nom @@ -42,7 +42,7 @@ compile [all of %items, all %items] to unless: (%items' "type") is "List" return: Lua value "utils.all(\(%items as lua expr))" %clauses <- [] - for % in (%items' "value") + for % in %items lua> "table.insert(\%clauses, \(% as lua expr));" return: Lua value "(\(%clauses joined with " and "))" parse [not all of %items, not all %items] as: not (all of %items) @@ -50,7 +50,7 @@ compile [any of %items, any %items] to unless: (%items' "type") is "List" return: Lua value "utils.any(\(%items as lua expr))" %clauses <- [] - for % in (%items' "value") + for % in %items lua> "table.insert(\%clauses, \(% as lua expr));" return: Lua value "(\(%clauses joined with " or "))" parse [none of %items, none %items] as: not (any of %items) @@ -58,14 +58,14 @@ compile [sum of %items, sum %items] to unless: (%items' "type") is "List" return: Lua value "utils.sum(\(%items as lua expr))" %clauses <- [] - for % in (%items' "value") + for % in %items lua> "table.insert(\%clauses, \(% as lua expr));" return: Lua value "(\(%clauses joined with " + "))" compile [product of %items, product %items] to unless: (%items' "type") is "List" return: Lua value "utils.product(\(%items as lua expr))" %clauses <- [] - for % in (%items' "value") + for % in %items lua> "table.insert(\%clauses, \(% as lua expr));" return: Lua value "(\(%clauses joined with " * "))" action [avg of %items, average of %items] diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom index 0cc3859..41491c7 100644 --- a/core/metaprogramming.nom +++ b/core/metaprogramming.nom @@ -6,9 +6,9 @@ immediately lua> ".." nomsu:define_compile_action("compile %actions to %lua", function(tree, \%actions, \%lua) - local lua = Lua(tree.source, "nomsu:define_compile_action(") + local lua = Lua(nil, "nomsu:define_compile_action(") local stubs = {} - for i, action in ipairs(\%actions.value) do + for i, action in ipairs(\%actions) do stubs[i] = action:get_stub(true) end stubs = repr(stubs) @@ -19,7 +19,7 @@ immediately end lua:append("function(tree") local args = {} - for i,tok in ipairs(\%actions.value[1].value) do + for i,tok in ipairs(\%actions[1]) do if tok.type == "Var" then args[#args+1] = tok:as_lua(nomsu) end end for i, arg in ipairs(args) do @@ -36,9 +36,9 @@ immediately immediately compile [action %actions %body] to lua> ".." - local lua = Lua(tree.source, "nomsu:define_action(") + local lua = Lua(nil, "nomsu:define_action(") local stubs = {} - for i, action in ipairs(\%actions.value) do + for i, action in ipairs(\%actions) do stubs[i] = action:get_stub(true) end stubs = repr(stubs) @@ -49,7 +49,7 @@ immediately end lua:append("function(") local args = {} - for i,tok in ipairs(\%actions.value[1].value) do + for i,tok in ipairs(\%actions[1]) do if tok.type == "Var" then args[#args+1] = tok:as_lua(nomsu) end end for i, arg in ipairs(args) do @@ -66,9 +66,9 @@ immediately immediately compile [parse %shorthand as %longhand] to lua> ".." - local lua = Lua(tree.source, "nomsu:define_compile_action(") + local lua = Lua(nil, "nomsu:define_compile_action(") local stubs = {} - for i, action in ipairs(\%shorthand.value) do + for i, action in ipairs(\%shorthand) do stubs[i] = action:get_stub(true) end stubs = repr(stubs) @@ -79,24 +79,24 @@ immediately end lua:append("function(tree") local replacements = {} - for i,tok in ipairs(\%shorthand.value[1].value) do + for i,tok in ipairs(\%shorthand[1]) do if tok.type == "Var" then local lua_var = tostring(tok:as_lua(nomsu)) - replacements[tok.value] = lua_var + replacements[tok] = lua_var lua:append(", ", lua_var) end end local function make_tree(t) - if Tuple:is_instance(t) then + if replacements[t] then + return replacements[t] + elseif type(t) ~= 'table' and type(t) ~= 'userdata' then + return repr(t) + elseif t.is_multi then local bits = {} for i, entry in ipairs(t) do bits[i] = make_tree(entry) end - return "Tuple("..table.concat(bits, ", ")..")" - elseif type(t) ~= 'table' and type(t) ~= 'userdata' then - return repr(t) - elseif t.type == "Var" and replacements[t.value] then - return replacements[t.value] + return t.type.."("..table.concat(bits, ", ")..")" else return t.type.."("..make_tree(t.value)..")" end @@ -118,6 +118,12 @@ action [remove action %stub] ARG_ORDERS[fn] = nil immediately + action [%tree as nomsu] + =lua "\%tree:as_nomsu()" + + action [%tree as inline nomsu] + =lua "\%tree:as_nomsu(true)" + action [%tree as lua] =lua "\%tree:as_lua(nomsu)" @@ -125,7 +131,7 @@ immediately lua> ".." local lua = \%tree:as_lua(nomsu) if not lua.is_value then - error("Invalid thing to convert to lua expr: "..\%tree.source:get_text()) + error("Invalid thing to convert to lua expr: "..\%tree) end return lua @@ -156,9 +162,6 @@ immediately parse [to %var write %code] as: lua> "\%var:append(\%code);" immediately - action [%tree's source code, %tree' source code] - =lua "\%tree.source:get_text()" - compile [repr %obj] to: Lua value "repr(\(%obj as lua expr))" compile [%obj as text] to: Lua value "tostring(\(%obj as lua expr))" compile [type of %obj] to: Lua value "type(\(%obj as lua expr))" @@ -170,31 +173,23 @@ immediately # Compiler tools immediately compile [run %code] to - Lua value "nomsu:run(Nomsu(\(=lua "tostring(tree.source)"), \(%code as lua expr)))" + Lua value "nomsu:run(Nomsu(nil, \(%code as lua expr)))" immediately compile [show lua %block] to lua> ".." local \%lua = \%block:as_lua(nomsu); - return Lua(\%block.source, "print(", repr(tostring(\%lua)), ");"); + return Lua(nil, "print(", repr(tostring(\%lua)), ");"); immediately compile [say %message] to lua> ".." if \%message.type == "Text" then - return Lua(tree.source, "print(", \(%message as lua expr), ");"); + return Lua(nil, "print(", \(%message as lua expr), ");"); else - return Lua(tree.source, "print(stringify(", \(%message as lua expr), "));"); + return Lua(nil, "print(stringify(", \(%message as lua expr), "));"); end -immediately - compile [source] to: Lua value "tree.source" - -# - immediately - action [Lua %]: Lua (=lua "tree.source") % - action [Lua value %]: Lua value (=lua "tree.source") % - # Return immediately # Return statement is wrapped in a do..end block because Lua is unhappy if you @@ -207,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.source:get_text());" + lua> "local \%assumption = 'Assumption failed: '..tostring(\%condition:as_nomsu());" return Lua ".." if not \(%condition as lua expr) then diff --git a/core/operators.nom b/core/operators.nom index e3b41e1..adfeeb5 100644 --- a/core/operators.nom +++ b/core/operators.nom @@ -28,18 +28,18 @@ immediately local safe = {Text=true, Number=true}; local a_lua, b_lua = \%a:as_lua(nomsu), \%b:as_lua(nomsu); if safe[\%a.type] or safe[\%b.type] then - return Lua.Value(tree.source, "(", a_lua, " == ", b_lua, ")"); + return Lua.Value(nil, "(", a_lua, " == ", b_lua, ")"); else - return Lua.Value(tree.source, "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); if safe[\%a.type] or safe[\%b.type] then - return Lua.Value(tree.source, "(", a_lua, " ~= ", b_lua, ")"); + return Lua.Value(nil, "(", a_lua, " ~= ", b_lua, ")"); else - return Lua.Value(tree.source, "(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)]" @@ -48,11 +48,11 @@ immediately immediately compile [%var <- %value] to lua> "local \%var_lua = \%var:as_lua(nomsu);" - assume %var_lua.is_value or barf "Invalid target for assignment: \(%var's source code)" + assume %var_lua.is_value or barf "Invalid target for assignment: \%var" lua> "local \%value_lua = \%value:as_lua(nomsu);" - assume %value_lua.is_value or barf "Invalid value for assignment: \(%value's source code)" + assume %value_lua.is_value or barf "Invalid value for assignment: \%value" lua> ".." - local lua = Lua(tree.source, \%var_lua, ' = ', \%value_lua, ';'); + local lua = Lua(nil, \%var_lua, ' = ', \%value_lua, ';'); if \%var.type == 'Var' then lua:add_free_vars({\%var}); end @@ -62,11 +62,11 @@ immediately # Simultaneous mutli-assignments like: x,y,z = 1,x,3; compile [<- %assignments] to assume ((%assignments' "type") is "Dict") or barf ".." - Expected a Dict for the assignments part of '<- %' statement, not \(%assignments' source code) + Expected a Dict for the assignments part of '<- %' statement, not \%assignments lua> ".." - local lhs, rhs = Lua(tree.source), Lua(\%assignments.source); - for i, item in ipairs(\%assignments.value) do - local target, value = item.value[1], item.value[2]; + 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); @@ -81,35 +81,35 @@ immediately lhs:append(target_lua); rhs:append(value_lua); end - return Lua(tree.source, lhs, " = ", rhs, ";"); + return Lua(nil, lhs, " = ", rhs, ";"); immediately compile [external %var <- %value] to %var_lua <- (%var as lua) - assume %var_lua.is_value or barf "Invalid target for assignment: \(%var's source code)" + assume %var_lua.is_value or barf "Invalid target for assignment: \%var" %value_lua <- (%value as lua) - assume %value_lua.is_value or barf "Invalid value for assignment: \(%value's source code)" + assume %value_lua.is_value or barf "Invalid value for assignment: \%value" return: Lua "\(%var_lua) = \(%value_lua);" compile [with external %externs %body] to %body_lua <- (%body as lua statements) - lua> "\%body_lua:remove_free_vars(\(%externs.value));" + lua> "\%body_lua:remove_free_vars(\(%externs));" return %body_lua compile [with %assignments %body] to %lua <- (%body as lua statements) lua> ".." - local lhs, rhs = Lua(tree.source), Lua(\%assignments.source); + local lhs, rhs = Lua(), Lua(); local vars = {}; - for i, item in ipairs(\%assignments.value) do - local target, value = item.value[1], item.value[2]; + 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.source:get_text())); + error("Invalid target for 'with' assignment: "..tostring(target)); end local target_lua = target:as_lua(nomsu); local value_lua = value:as_lua(nomsu); if not value_lua.is_value then - error("Invalid value for assignment: "..tostring(value.source:get_text())); + error("Invalid value for assignment: "..tostring(value)); end if target.type == "Var" then lhs:add_free_vars({target}); diff --git a/core/text.nom b/core/text.nom index e1aaa9d..7b945cd 100644 --- a/core/text.nom +++ b/core/text.nom @@ -27,7 +27,7 @@ lua> ".." }; for name, e in pairs(escapes) do local lua = "'"..e.."'"; - nomsu:define_compile_action(name, function(tree) return Lua.Value(tree.source, lua); end); + nomsu:define_compile_action(name, function(tree) return Lua.Value(nil, lua); end); end local colors = { ["reset color"]="\\\\27[0m", bright="\\\\27[1m", dim="\\\\27[2m", underscore="\\\\27[4m", @@ -42,9 +42,9 @@ lua> ".." for name, c in pairs(colors) do local color = "'"..c.."'"; local reset = "'"..colors["reset color"].."'"; - nomsu:define_compile_action(name, function(tree) return Lua.Value(tree.source, color); end); + nomsu:define_compile_action(name, function(tree) return Lua.Value(nil, color); end); nomsu:define_compile_action(name.." %", function(\%) - return Lua.Value(tree.source, color, "..", \%:as_lua(nomsu), "..", reset); + return Lua.Value(nil, color, "..", \%:as_lua(nomsu), "..", reset); end); end end |
