diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2018-11-02 14:38:24 -0700 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2018-11-02 14:39:23 -0700 |
| commit | 307dea18815ba4a06a3098edb170d7ad90708815 (patch) | |
| tree | bce78eb28fa03c9939a92e08e47564afc984c988 /core | |
| parent | d0c3c57f7b25c8d912c426e48cb5ab09cd738f65 (diff) | |
Changed stub convention to (foo 1 baz 2) -> foo_1_baz instead of
foo_1_baz_2, removed "smext", made some cleanup changes.
Diffstat (limited to 'core')
| -rw-r--r-- | core/control_flow.nom | 32 | ||||
| -rw-r--r-- | core/errors.nom | 22 | ||||
| -rw-r--r-- | core/math.nom | 8 | ||||
| -rw-r--r-- | core/metaprogramming.nom | 37 | ||||
| -rw-r--r-- | core/operators.nom | 8 |
5 files changed, 52 insertions, 55 deletions
diff --git a/core/control_flow.nom b/core/control_flow.nom index 0cabf95..bbc98f1 100644 --- a/core/control_flow.nom +++ b/core/control_flow.nom @@ -102,8 +102,20 @@ test: .." # Basic loop control -(do next) compiles to (Lua "goto continue") -(stop) compiles to (Lua "break") +(stop %var) compiles to: + if %var: + return (..) + Lua "goto stop_\((%var.stub if (%var.type == "action") else %var) as lua identifier)" + ..else: return (Lua "break") +(do next %var) compiles to: + if %var: + return (..) + Lua "goto continue_\((%var.stub if (%var.type == "action") else %var) as lua identifier)" + ..else: return (Lua "goto continue") +[===stop %var ===, ---stop %var ---, ***stop %var ***] all compile to (..) + Lua "::stop_\((%var.stub if (%var.type == "action") else %var) as lua identifier)::" +[===next %var ===, ---next %var ---, ***next %var ***] all compile to (..) + Lua "::continue_\((%var.stub if (%var.type == "action") else %var) as lua identifier)::" # While loops test: @@ -177,16 +189,6 @@ test: return %lua -# For loop control flow -(stop %var) compiles to (..) - Lua "goto stop_\((%var.stub if (%var.type == "Action") else %var) as lua identifier)" -(do next %var) compiles to (..) - Lua "goto continue_\((%var.stub if (%var.type == "Action") else %var) as lua identifier)" -[===stop %var ===, ---stop %var ---, ***stop %var ***] all compile to (..) - Lua "::stop_\((%var.stub if (%var.type == "Action") else %var) as lua identifier)::" -[===next %var ===, ---next %var ---, ***next %var ***] all compile to (..) - Lua "::continue_\((%var.stub if (%var.type == "Action") else %var) as lua identifier)::" - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ test: @@ -365,7 +367,7 @@ test: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ test: - if: + when: (1 == 2) (100 < 0): barf "bad conditional" (1 == 0) (1 == 1) %not_a_variable.x: do nothing @@ -377,7 +379,7 @@ test: barf "bad conditional" # Multi-branch conditional (if..elseif..else) -[if %body, when %body] all compile to: +(when %body) compiles to: %code = (Lua "") %clause = "if" %else_allowed = (yes) @@ -512,8 +514,6 @@ test: %d.x = "bad" barf ..then always: %d.x = "good" - ..and if it barfs: do nothing - assume (%d.x == "good") (do %action then always %final_action) compiles to: define mangler diff --git a/core/errors.nom b/core/errors.nom index 70dea0c..0b0a6a3 100644 --- a/core/errors.nom +++ b/core/errors.nom @@ -4,8 +4,7 @@ use "core/metaprogramming.nom" -(barf) compiles to (Lua "error(nil, 0);") -(barf %msg) compiles to (Lua "error(\(%msg as lua expr), 0);") +(barf %msg) compiles to (Lua "error(\(=lua "\%msg and \(%msg as lua expr) or 'nil'"), 0);") (compile error at %tree %msg) compiles to (..) Lua "nomsu:compile_error(\(%tree as lua expr), \(%msg as lua expr))" (compile error at %tree %msg hint %hint) compiles to (..) @@ -66,9 +65,9 @@ test: local ok, ret = xpcall(function() \(%action as lua statements) fell_through = true - end, function(\(%msg as lua expr)) + end, function(\(=lua "\%fallback and \(%msg as lua expr) or ''")) local ok, ret = pcall(function() - \(%fallback as lua statements) + \((=lua "\%fallback or \%msg") as lua statements) end) if not ok then err, erred = ret, true end end) @@ -84,18 +83,19 @@ test: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -[..] - try %action and if it succeeds %success or if it barfs %fallback - try %action and if it barfs %fallback or if it succeeds %success -..all parse as (..) - try %action and if it succeeds %success or if it barfs (=lua "") %fallback +# + [..] + try %action and if it succeeds %success or if it barfs %fallback + try %action and if it barfs %fallback or if it succeeds %success + ..all parse as (..) + try %action and if it succeeds %success or if it barfs (=lua "") %fallback -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ (try %action) parses as (..) try %action and if it succeeds (do nothing) or if it barfs (do nothing) -(try %action and if it barfs %fallback) parses as (..) +#(try %action and if it barfs %fallback) parses as (..) try %action and if it succeeds (do nothing) or if it barfs %fallback (try %action and if it barfs %msg %fallback) parses as (..) diff --git a/core/math.nom b/core/math.nom index 538b068..66f5aba 100644 --- a/core/math.nom +++ b/core/math.nom @@ -80,21 +80,21 @@ externally (%n to the nearest %rounder) means (..) [all of %items, all %items] all compile to: unless (%items.type is "List"): return (Lua value "utils.all(\(%items as lua expr))") - %clauses = (((% as lua expr)::as smext) for % in %items) + %clauses = (((% as lua expr)::text) for % in %items) return (Lua value "(\(%clauses::joined with " and "))") [not all of %items, not all %items] all parse as (not (all of %items)) [any of %items, any %items] all compile to: unless (%items.type is "List"): return (Lua value "utils.any(\(%items as lua expr))") - %clauses = (((% as lua expr)::as smext) for % in %items) + %clauses = (((% as lua expr)::text) for % in %items) return (Lua value "(\(%clauses::joined with " or "))") [none of %items, none %items] all parse as (not (any of %items)) [sum of %items, sum %items] all compile to: unless (%items.type is "List"): return (Lua value "utils.sum(\(%items as lua expr))") - %clauses = (((% as lua expr)::as smext) for % in %items) + %clauses = (((% as lua expr)::text) for % in %items) return (Lua value "(\(%clauses::joined with " + "))") [if all of %items %body, if all of %items then %body] all parse as (..) @@ -139,7 +139,7 @@ externally (%n to the nearest %rounder) means (..) [product of %items, product %items] all compile to: unless (%items.type is "List"): return (Lua value "utils.product(\(%items as lua expr))") - %clauses = (((% as lua expr)::as smext) for % in %items) + %clauses = (((% as lua expr)::text) for % in %items) return (Lua value "(\(%clauses::joined with " * "))") externally [avg of %items, average of %items] all mean (..) diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom index dbc45a4..61c877f 100644 --- a/core/metaprogramming.nom +++ b/core/metaprogramming.nom @@ -3,7 +3,7 @@ This File contains actions for making actions and compile-time actions and some helper functions to make that easier. -lua> "NOMSU_CORE_VERSION = 8" +lua> "NOMSU_CORE_VERSION = 9" lua> "\ ..do local mangle_index = 0 @@ -16,14 +16,14 @@ lua> "\ end end COMPILE_ACTIONS["define mangler"] = function(nomsu, tree) - return LuaCode(tree.source, "local mangle_1 = mangler()") + return LuaCode(tree.source, "local mangle = mangler()") end" lua> "\ - ..COMPILE_ACTIONS["1 -> 2"] = function(nomsu, tree, \%args, \%body) + ..COMPILE_ACTIONS["1 ->"] = function(nomsu, tree, \%args, \%body) local lua = LuaCode.Value(tree.source, "(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 nomsu:compile(a):as_smext() or a end) + local lua_args = table.map(\%args, function(a) return SyntaxTree:is_instance(a) and nomsu:compile(a):text() or a end) lua:concat_append(lua_args, ", ") local body_lua = SyntaxTree:is_instance(\%body) and nomsu:compile(\%body):as_statements("return ") or \%body body_lua:remove_free_vars(lua_args) @@ -63,15 +63,15 @@ test: asdf assume (%tmp is (nil)) or barf "compile to is leaking variables" lua> "\ - ..COMPILE_ACTIONS["1 compiles to 2"] = function(nomsu, tree, \%actions, \%body) + ..COMPILE_ACTIONS["1 compiles to"] = function(nomsu, tree, \%actions, \%body) if \%actions.type ~= "List" then \%actions = {\%actions, type="List"} end - local \%args = {"nomsu", "tree", unpack(table.map(\%actions[1]:get_args(), function(a) return nomsu:compile(a):as_smext() end))} + local \%args = {"nomsu", "tree", unpack(table.map(\%actions[1]:get_args(), function(a) return nomsu:compile(a):text() end))} local lua = LuaCode(tree.source, "COMPILE_ACTIONS[", \%actions[1].stub:as_lua(), "] = ", \(what (%args -> %body) compiles to)) for i=2,#\%actions do local alias = \%actions[i] - local \%alias_args = {"nomsu", "tree", unpack(table.map(alias:get_args(), function(a) return nomsu:compile(a):as_\ - ..smext() end))} + local \%alias_args = {"nomsu", "tree", unpack(table.map(alias:get_args(), function(a) return nomsu:compile(a):text() \ + ..end))} lua:append("\\nCOMPILE_ACTIONS[", alias.stub:as_lua(), "] = ") if utils.equivalent(\%args, \%alias_args) then lua:append("COMPILE_ACTIONS[", \%actions[1].stub:as_lua(), "]") @@ -85,7 +85,7 @@ lua> "\ end return lua end - COMPILE_ACTIONS["1 all compile to 2"] = COMPILE_ACTIONS["1 compiles to 2"]" + COMPILE_ACTIONS["1 all compile to"] = COMPILE_ACTIONS["1 compiles to"]" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -116,14 +116,14 @@ test: lua> "\ ..if \%actions.type ~= "List" then \%actions = {\%actions, type="List"} end local fn_name = \%actions[1].stub:as_lua_id() - local \%args = table.map(\%actions[1]:get_args(), function(a) return nomsu:compile(a):as_smext() end) + local \%args = table.map(\%actions[1]:get_args(), function(a) return nomsu:compile(a):text() end) local lua = LuaCode(tree.source, fn_name, " = ", \(what (%args -> %body) compiles to)) lua:add_free_vars({fn_name}) for i=2,#\%actions do local alias = \%actions[i] local alias_name = alias.stub:as_lua_id() lua:add_free_vars({alias_name}) - local \%alias_args = table.map(alias:get_args(), function(a) return nomsu:compile(a):as_smext() end) + local \%alias_args = table.map(alias:get_args(), function(a) return nomsu:compile(a):text() end) lua:append("\\n", alias_name, " = ") if utils.equivalent(\%args, \%alias_args) then lua:append(fn_name) @@ -151,7 +151,7 @@ test: return lua" test: - assume (((say %)'s meaning) == (=lua "say_1")) + assume (((say %)'s meaning) == (=lua "say")) (%action's meaning) compiles to (Lua value (%action.stub as lua id)) test: @@ -174,7 +174,7 @@ test: ..local replacements = {} if \%actions.type ~= "List" then \%actions = {\%actions, type="List"} end for i,arg in ipairs(\%actions[1]:get_args()) do - replacements[arg[1]] = nomsu:compile(arg):as_smext() + replacements[arg[1]] = nomsu:compile(arg):text() end local function make_tree(t) if SyntaxTree:is_instance(t) and t.type == "Var" then @@ -192,14 +192,14 @@ test: i = i + 1 elseif k == "source" then ret[#ret+1] = k.."= "..tostring(v):as_lua() - elseif lua_type_of_1(k) == 'string' and k:match("[_a-zA-Z][_a-zA-Z0-9]*") then + elseif lua_type_of(k) == 'string' and k:match("[_a-zA-Z][_a-zA-Z0-9]*") then ret[#ret+1] = k.."= "..make_tree(v) else ret[#ret+1] = "["..make_tree(k).."]= "..make_tree(v) end end return "SyntaxTree{"..table.concat(ret, ", ").."}" - elseif lua_type_of_1(t) == 'number' then + elseif lua_type_of(t) == 'number' then return tostring(t) else return t:as_lua() @@ -226,9 +226,6 @@ test: (%tree as lua return) compiles to (..) Lua value "nomsu:compile(\(%tree as lua expr)):as_statements('return ')" -(remove action %action) compiles to (..) - Lua "\(=lua "(\(%action.stub)):as_lua_id()") = nil" - test: assume ("\(\(foo \%x) as nomsu)" == "foo %x") or barf "\ ..action source code failed." @@ -240,11 +237,11 @@ test: externally [%var as lua identifier, %var as lua id] all mean: lua> "\ - ..if lua_type_of_1(\%var) == 'string' then return \%var:as_lua_id() + ..if lua_type_of(\%var) == 'string' then return \%var:as_lua_id() elseif SyntaxTree:is_instance(\%var, 'Var') then return \%var[1]:as_lua_id() elseif SyntaxTree:is_instance(\%var) then local lua = \(%var as lua expr) - if not lua:as_smext():match("^[_a-zA-Z][_a-zA-Z0-9]*$") then + if not lua:text():match("^[_a-zA-Z][_a-zA-Z0-9]*$") then nomsu:compile_error(\%var, "This is not a valid Lua identifier.") end return lua diff --git a/core/operators.nom b/core/operators.nom index bd93cd5..6d574b7 100644 --- a/core/operators.nom +++ b/core/operators.nom @@ -34,7 +34,7 @@ test: lua> "\ ..local lua = LuaCode(tree.source, \%var_lua, ' = ', \%value_lua, ';') if \%var.type == 'Var' then - lua:add_free_vars({nomsu:compile(\%var):as_smext()}) + lua:add_free_vars({nomsu:compile(\%var):text()}) end return lua" @@ -66,7 +66,7 @@ test: %value as text ..) end if \%target.type == "Var" then - lhs:add_free_vars({target_lua:as_smext()}) + lhs:add_free_vars({target_lua:text()}) end if i > 1 then lhs:append(", ") @@ -107,7 +107,7 @@ test: (with external %externs %body) compiles to: %body_lua = (%body as lua statements) lua> "\ - ..\%body_lua:remove_free_vars(table.map(\%externs, function(v) return nomsu:compile(v):as_smext() end))" + ..\%body_lua:remove_free_vars(table.map(\%externs, function(v) return nomsu:compile(v):text() end))" return %body_lua test: @@ -141,7 +141,7 @@ test: lhs:append(target_lua) rhs:append(value_lua) if \%target.type == "Var" then - vars[i] = target_lua:as_smext() + vars[i] = target_lua:text() end end \%lua:remove_free_vars(vars) |
