diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/control_flow.nom | 1 | ||||
| -rw-r--r-- | core/io.nom | 8 | ||||
| -rw-r--r-- | core/math.nom | 8 | ||||
| -rw-r--r-- | core/metaprogramming.nom | 33 | ||||
| -rw-r--r-- | core/operators.nom | 11 | ||||
| -rw-r--r-- | core/text.nom | 22 |
6 files changed, 37 insertions, 46 deletions
diff --git a/core/control_flow.nom b/core/control_flow.nom index cba0777..ff1a7c1 100644 --- a/core/control_flow.nom +++ b/core/control_flow.nom @@ -4,7 +4,6 @@ like "if" statements and loops. use "core/metaprogramming.nom" -use "core/text.nom" use "core/operators.nom" use "core/errors.nom" diff --git a/core/io.nom b/core/io.nom index edeeab4..2df0999 100644 --- a/core/io.nom +++ b/core/io.nom @@ -9,17 +9,17 @@ use "core/metaprogramming.nom" (say %message) compiles to: lua> "\ ..if \%message.type == "Text" then - return LuaCode(tree.source, "print(", \(%message as lua expr), ");"); + return LuaCode("print(", \(%message as lua expr), ");"); else - return LuaCode(tree.source, "print(tostring(", \(%message as lua expr), "));"); + return LuaCode("print(tostring(", \(%message as lua expr), "));"); end" (ask %prompt) compiles to: lua> "\ ..if \%prompt.type == "Text" then - return LuaCode(tree.source, "(io.write(", \(%prompt as lua expr), ") and io.read())"); + return LuaCode("(io.write(", \(%prompt as lua expr), ") and io.read())"); else - return LuaCode(tree.source, "(io.write(tostring(", \(..) + return LuaCode("(io.write(tostring(", \(..) %prompt as lua expr .., ")) and io.read())"); end" diff --git a/core/math.nom b/core/math.nom index 1c7306e..8951721 100644 --- a/core/math.nom +++ b/core/math.nom @@ -85,7 +85,7 @@ externally [all of %items, all %items] all mean: return (yes) [all of %items, all %items] all compile to: unless (%items.type is "List"): - return %tree + return \(all of %items) if ((size of %items) == 0): return (Lua "true") %lua = (Lua "(") %lua::add ((% as lua expr) for % in %items) joined with " and " @@ -99,7 +99,7 @@ externally [any of %items, any %items] all mean: return (no) [any of %items, any %items] all compile to: unless (%items.type is "List"): - return %tree + return \(any of %items) if ((size of %items) == 0): return (Lua "false") %lua = (Lua "(") %lua::add ((% as lua expr) for % in %items) joined with " or " @@ -114,7 +114,7 @@ externally [sum of %items, sum %items] all mean: return %total [sum of %items, sum %items] all compile to: unless (%items.type is "List"): - return %tree + return \(sum of %items) if ((size of %items) == 0): return (Lua "0") %lua = (Lua "(") %lua::add ((% as lua expr) for % in %items) joined with " + " @@ -127,7 +127,7 @@ externally [product of %items, product %items] all mean: return %prod [product of %items, product %items] all compile to: unless (%items.type is "List"): - return %tree + return \(product of %items) if ((size of %items) == 0): return (Lua "1") %lua = (Lua "(") %lua::add ((% as lua expr) for % in %items) joined with " * " diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom index 1f787c2..fc81e15 100644 --- a/core/metaprogramming.nom +++ b/core/metaprogramming.nom @@ -17,13 +17,13 @@ lua> "\ end end end - compile.action["define mangler"] = function(compile, tree) - return LuaCode(tree.source, "local mangle = mangler()") + compile.action["define mangler"] = function(compile) + return LuaCode("local mangle = mangler()") end" lua> "\ - ..compile.action["1 ->"] = function(compile, tree, \%args, \%body) - local lua = LuaCode(tree.source, "(function(") + ..compile.action["1 ->"] = function(compile, \%args, \%body) + local lua = LuaCode("(function(") if SyntaxTree:is_instance(\%args) and \%args.type == "Action" then \%args = \%args:get_args() end local lua_args = table.map(\%args, function(a) return SyntaxTree:is_instance(a) and compile(a):text() or a end) lua:concat_append(lua_args, ", ") @@ -36,11 +36,10 @@ lua> "\ end" lua> "\ - ..compile.action["what 1 compiles to"] = function(compile, tree, \%action) - local lua = LuaCode(tree.source, "compile.action[", \%action.stub:as_lua(), "](") + ..compile.action["what 1 compiles to"] = function(compile, \%action) + local lua = LuaCode("compile.action[", \%action.stub:as_lua(), "](") local lua_args = table.map(\%action:get_args(), function(a) return compile(a) end) table.insert(lua_args, 1, "compile") - table.insert(lua_args, 2, "tree") lua:concat_append(lua_args, ", ") lua:append(")") return lua @@ -66,12 +65,12 @@ test: asdf assume (%tmp is (nil)) or barf "compile to is leaking variables" lua> "\ - ..compile.action["1 compiles to"] = function(compile, tree, \%action, \%body) - local \%args = List{\(\%compile), \(\%tree), unpack(\%action:get_args())} + ..compile.action["1 compiles to"] = function(compile, \%action, \%body) + local \%args = List{\(\%compile), unpack(\%action:get_args())} if \%body.type == "Text" then \%body = SyntaxTree{source=\%body.source, type="Action", "Lua", \%body} end - return LuaCode(tree.source, "compile.action[", \%action.stub:as_lua(), + return LuaCode("compile.action[", \%action.stub:as_lua(), "] = ", \(what (%args -> %body) compiles to)) end" @@ -82,11 +81,11 @@ lua> "\ ..if \%actions.type ~= "List" then compile_error(\%actions, "This should be a list of actions.") end - local lua = LuaCode(tree.source, \(what (%actions.1 compiles to %body) compiles to)) - local \%args = List{\(\%compile), \(\%tree), unpack(\%actions[1]:get_args())} + local lua = \(what (%actions.1 compiles to %body) compiles to) + local \%args = List{\(\%compile), unpack(\%actions[1]:get_args())} for i=2,#\%actions do local alias = \%actions[i] - local \%alias_args = List{\(\%compile), \(\%tree), unpack(alias:get_args())} + local \%alias_args = List{\(\%compile), unpack(alias:get_args())} lua:append("\\ncompile.action[", alias.stub:as_lua(), "] = ") if \%alias_args == \%args then lua:append("compile.action[", \%actions[1].stub:as_lua(), "]") @@ -100,7 +99,7 @@ lua> "\ (call %fn with %args) compiles to: lua> "\ - ..local lua = LuaCode(tree.source, compile(\%fn), "(") + ..local lua = LuaCode(compile(\%fn), "(") if \%args.type == 'List' then lua:concat_append(table.map(\%args, function(a) return compile(a) end), ", ") else @@ -126,14 +125,14 @@ test: lua> "\ ..local fn_name = \%action.stub:as_lua_id() local \%args = \%action:get_args() - local lua = LuaCode(tree.source, fn_name, " = ", \(what (%args -> %body) compiles to)) + local lua = LuaCode(fn_name, " = ", \(what (%args -> %body) compiles to)) lua:add_free_vars({fn_name}) return lua" (%actions all mean %body) compiles to: lua> "\ ..local fn_name = \%actions[1].stub:as_lua_id() local \%args = List(\%actions[1]:get_args()) - local lua = LuaCode(tree.source, \(what (%actions.1 means %body) compiles to)) + local lua = \(what (%actions.1 means %body) compiles to) for i=2,#\%actions do local alias = \%actions[i] local alias_name = alias.stub:as_lua_id() @@ -222,7 +221,7 @@ test: return t:as_lua() end end - local \%new_body = LuaCode(\%body.source, + local \%new_body = LuaCode:from(\%body.source, "local mangle = mangler()", "\\nreturn ", make_tree(\%body)) local ret = \(what (%actions all compile to %new_body) compiles to) diff --git a/core/operators.nom b/core/operators.nom index 38fc9a8..19a61d7 100644 --- a/core/operators.nom +++ b/core/operators.nom @@ -31,7 +31,7 @@ test: lua> "\ ..local \%var_lua = \(%var as lua expr) local \%value_lua = \(%value as lua expr) - local lua = LuaCode(tree.source, \%var_lua, ' = ', \%value_lua, ';') + local lua = LuaCode(\%var_lua, ' = ', \%value_lua, ';') if \%var.type == 'Var' then lua:add_free_vars({compile(\%var):text()}) end @@ -49,7 +49,7 @@ test: assume (%assignments.type is "Dict") or barf "\ ..Expected a Dict for the assignments part of '<- %' statement, not \%assignments" lua> "\ - ..local lhs, rhs = LuaCode(tree.source), LuaCode(tree.source) + ..local lhs, rhs = LuaCode(), LuaCode() for i, item in ipairs(\%assignments) do local \%target, \%value = item[1], item[2] \%value = \%value:map(function(t) @@ -69,7 +69,7 @@ test: lhs:append(target_lua) rhs:append(value_lua) end - return LuaCode(tree.source, lhs, " = ", rhs, ";")" + return LuaCode(lhs, " = ", rhs, ";")" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -111,7 +111,7 @@ test: (with %assignments %body) compiles to: %lua = (%body as lua) lua> "\ - ..local lhs, rhs = LuaCode(tree.source), LuaCode(tree.source) + ..local lhs, rhs = LuaCode(), LuaCode() local vars = {} for i, item in ipairs(\%assignments) do local \%target, \%value = item[1], item[2] @@ -233,8 +233,7 @@ test: test: assume ((size of [1, 2, 3]) == 3) -[size of %list, size of %list, size of %list, size of %list] all compile to (..) - "(#\(%list as lua expr))" +(size of %list) compiles to "(#\(%list as lua expr))" (%list is empty) compiles to "(#\(%list as lua expr) == 0)" diff --git a/core/text.nom b/core/text.nom index 2d72f01..d4cd227 100644 --- a/core/text.nom +++ b/core/text.nom @@ -4,6 +4,8 @@ color codes. use "core/metaprogramming.nom" +use "core/operators.nom" +use "core/control_flow.nom" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -47,17 +49,9 @@ test: assume "\n" == (newline) # Text literals -lua> "\ - ..do - local escapes = { - nl="\\\\n", newline="\\\\n", tab="\\\\t", bell="\\\\a", cr="\\\\r", - ["carriage return"]="\\\\r", backspace="\\\\b", ["form feed"]="\\\\f", - formfeed="\\\\f", ["vertical tab"]="\\\\v", - }; - for name, e in pairs(escapes) do - local lua = "'"..e.."'" - compile.action[name] = function(compile, tree) - return LuaCode(tree.source, lua) - end - end - end" +%escapes = {..} + nl:"\n", newline:"\n", tab:"\t", bell:"\a", cr:"\r", "carriage return":"\r", + backspace:"\b", "form feed":"\f", formfeed:"\f", "vertical tab":"\v" +for %name = %str in %escapes: + with {%lua: Lua (quote %str)}: + %compile.action.%name = ([]-> %lua) |
