From 79d4bd5125de7ff220fbf8a8a5493d437ed16963 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 18 Sep 2018 19:48:58 -0700 Subject: Got rid of repr() use and replaced with :as_lua() or :as_nomsu() in as many places as possible. --- core/metaprogramming.nom | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'core/metaprogramming.nom') diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom index c50f783..51831ec 100644 --- a/core/metaprogramming.nom +++ b/core/metaprogramming.nom @@ -34,7 +34,7 @@ lua> "\ lua> "\ ..COMPILE_ACTIONS["compile as 1"] = function(nomsu, tree, \%action) - local lua = LuaCode.Value(tree.source, "COMPILE_ACTIONS[", repr(\%action.stub), "](") + local lua = LuaCode.Value(tree.source, "COMPILE_ACTIONS[", \%action.stub:as_lua(), "](") local lua_args = table.map(\%action:get_args(), function(a) return nomsu:compile(a) end) table.insert(lua_args, 1, "nomsu") table.insert(lua_args, 2, "tree") @@ -66,19 +66,19 @@ lua> "\ ..COMPILE_ACTIONS["compile 1 to 2"] = function(nomsu, tree, \%actions, \%body) local \%args = {"nomsu", "tree", unpack(table.map(\%actions[1]:get_args(), function(a) return tostring(nomsu:compile(\ ..a)) end))} - local lua = LuaCode(tree.source, "COMPILE_ACTIONS[", repr(\%actions[1].stub), + local lua = LuaCode(tree.source, "COMPILE_ACTIONS[", \%actions[1].stub:as_lua(), "] = ", \(compile as (%args -> %body))) for i=2,#\%actions do local alias = \%actions[i] local \%alias_args = {"nomsu", "tree", unpack(table.map(alias:get_args(), function(a) return tostring(nomsu:compile(\ ..a)) end))} - lua:append("\\nCOMPILE_ACTIONS[", repr(alias.stub), "] = ") + lua:append("\\nCOMPILE_ACTIONS[", alias.stub:as_lua(), "] = ") if utils.equivalent(\%args, \%alias_args) then - lua:append("COMPILE_ACTIONS[", repr(\%actions[1].stub), "]") + lua:append("COMPILE_ACTIONS[", \%actions[1].stub:as_lua(), "]") else lua:append("function(") lua:concat_append(\%alias_args, ", ") - lua:append(")\\n return COMPILE_ACTIONS[", repr(\%actions[1].stub), "](") + lua:append(")\\n return COMPILE_ACTIONS[", \%actions[1].stub:as_lua(), "](") lua:concat_append(\%args, ", ") lua:append(")\\nend") end @@ -173,7 +173,7 @@ compile [parse %actions as %body] to (..) if replacements[t[1]] then return replacements[t[1]] else - return t.type.."{mangle("..repr(t[1]).."), source="..repr(tostring(t.source)).."}" + return t.type.."{mangle("..t[1]:as_lua().."), source="..tostring(t.source):as_lua().."}" end elseif AST.is_syntax_tree(t) then local ret = {} @@ -183,7 +183,7 @@ compile [parse %actions as %body] to (..) ret[#ret+1] = make_tree(t[i]) i = i + 1 elseif k == "source" then - ret[#ret+1] = k.."= "..repr(tostring(v)) + ret[#ret+1] = k.."= "..tostring(v):as_lua() elseif type(k) == 'string' and k:match("[_a-zA-Z][_a-zA-Z0-9]*") then ret[#ret+1] = k.."= "..make_tree(v) else @@ -191,8 +191,10 @@ compile [parse %actions as %body] to (..) end end return t.type.."{"..table.concat(ret, ", ").."}" + elseif type(t) == 'number' then + return tostring(t) else - return repr(t) + return t:as_lua() end end local \%new_body = LuaCode(\%body.source, @@ -257,7 +259,7 @@ action [%tree with vars %replacements] (..) compile [tree %tree with vars %replacements] to (..) Lua value "\ - ..\(=lua "repr(\%tree)"):map(function(t) + ..\(=lua "(\%tree):as_lua()"):map(function(t) if t.type == "Var" then return \(%replacements as lua expr)[t[1]] end @@ -312,7 +314,7 @@ test: ..one "two"" ..== "\"one\\n\\\"two\\\"\"" -compile [quote %s] to (Lua value "repr(\(%s as lua expr))") +compile [quote %s] to (Lua value "tostring(\(%s as lua expr)):as_lua()") test: assume ((type of {}) == "table") or barf "type of failed." @@ -340,7 +342,7 @@ test: compile [run %nomsu_code] to (..) Lua value "\ ..nomsu:run(NomsuCode(\(..) - =lua "repr(tostring(\(%nomsu_code.source)))" + =lua "tostring(\(%nomsu_code.source)):as_lua()" .., \(%nomsu_code as lua expr)))" test: -- cgit v1.2.3 From f2048235f5cc7ff02db39a0e2fe5c79c7f390e0b Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 21 Sep 2018 00:30:28 -0700 Subject: Incremental checkin, currently not working, just saving progress. --- core/metaprogramming.nom | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) (limited to 'core/metaprogramming.nom') diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom index 51831ec..1503905 100644 --- a/core/metaprogramming.nom +++ b/core/metaprogramming.nom @@ -145,7 +145,7 @@ compile [action %actions %body] to (..) test: assume ((action (say %)) == (=lua "say_1")) -compile [action %action] to (Lua value (%action as lua id)) +compile [action %action] to (Lua value (%action.stub as lua id)) test: parse [swap %x and %y] as (..) @@ -233,7 +233,12 @@ action [%var as lua identifier, %var as lua id] (..) lua> "\ ..if type(\%var) == 'string' then return \%var:as_lua_id() elseif AST.is_syntax_tree(\%var, 'Var') then return \%var[1]:as_lua_id() - elseif AST.is_syntax_tree(\%var, 'Action') then return \%var.stub:as_lua_id() + elseif AST.is_syntax_tree(\%var) then + local lua = \(%var as lua expr) + if not tostring(lua):match("^[_a-zA-Z][_a-zA-Z0-9]*$") then + \(compile error at %var "This is not a valid Lua identifier.") + end + return lua else error("Unknown type: "..tostring(\%var)) end" @@ -317,8 +322,29 @@ test: compile [quote %s] to (Lua value "tostring(\(%s as lua expr)):as_lua()") test: - assume ((type of {}) == "table") or barf "type of failed." -compile [type of %obj] to (Lua value "type(\(%obj as lua expr))") + assume (lua type of {}) == "Lua table" + assume (type of {}) == "Dict" + assume ({} is a "Dict") + assume ("" is text) + assume ("" isn't a "Dict") + +%dict_mt = (=lua "getmetatable(\{})") +%list_mt = (=lua "getmetatable(\[])") + +action [% is text] (=lua "\(lua type of %) == 'string'") +action [% is not text, % isn't text] (=lua "\(lua type of %) ~= 'string'") +action [type of %]: + lua> "\ + local lua_type = \(lua type of %) + if lua_type == 'string' then return 'Text' + elseif lua_type == 'table' then + local mt = getmetatable(\%) + if mt and mt.__type then return mt.__type end + return 'Lua table' + else return lua_type end" +parse [% is a %type, % is an %type] as ((type of %) == %type) +parse [% isn't a %type, % isn't an %type, % is not a %type, % is not an %type] +..as ((type of %) == %type) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- cgit v1.2.3 From 692fae5416ce1f2702b599ffb27b2e3d2235eba7 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 26 Sep 2018 12:45:08 -0700 Subject: Incremental fixes and more nomnom ports. --- core/metaprogramming.nom | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'core/metaprogramming.nom') diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom index 1503905..8b75ccd 100644 --- a/core/metaprogramming.nom +++ b/core/metaprogramming.nom @@ -184,14 +184,14 @@ compile [parse %actions as %body] to (..) i = i + 1 elseif k == "source" then ret[#ret+1] = k.."= "..tostring(v):as_lua() - elseif type(k) == 'string' and k:match("[_a-zA-Z][_a-zA-Z0-9]*") then + elseif lua_type_of_1(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 t.type.."{"..table.concat(ret, ", ").."}" - elseif type(t) == 'number' then + elseif lua_type_of_1(t) == 'number' then return tostring(t) else return t:as_lua() @@ -231,12 +231,12 @@ compile [%tree as inline nomsu] to (..) action [%var as lua identifier, %var as lua id] (..) lua> "\ - ..if type(\%var) == 'string' then return \%var:as_lua_id() + ..if lua_type_of_1(\%var) == 'string' then return \%var:as_lua_id() elseif AST.is_syntax_tree(\%var, 'Var') then return \%var[1]:as_lua_id() elseif AST.is_syntax_tree(\%var) then local lua = \(%var as lua expr) if not tostring(lua):match("^[_a-zA-Z][_a-zA-Z0-9]*$") then - \(compile error at %var "This is not a valid Lua identifier.") + compile_error_at_1_2(\%var, "This is not a valid Lua identifier.") end return lua else error("Unknown type: "..tostring(\%var)) @@ -328,9 +328,6 @@ test: assume ("" is text) assume ("" isn't a "Dict") -%dict_mt = (=lua "getmetatable(\{})") -%list_mt = (=lua "getmetatable(\[])") - action [% is text] (=lua "\(lua type of %) == 'string'") action [% is not text, % isn't text] (=lua "\(lua type of %) ~= 'string'") action [type of %]: -- cgit v1.2.3 From b43432e647fbb3bb76aa2836e3899d5e407c50f9 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 26 Sep 2018 13:05:28 -0700 Subject: Fixed all syntax errors, got original (non-nomnom) tests passing. --- core/metaprogramming.nom | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'core/metaprogramming.nom') diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom index 8b75ccd..f92fb0c 100644 --- a/core/metaprogramming.nom +++ b/core/metaprogramming.nom @@ -236,7 +236,7 @@ action [%var as lua identifier, %var as lua id] (..) elseif AST.is_syntax_tree(\%var) then local lua = \(%var as lua expr) if not tostring(lua):match("^[_a-zA-Z][_a-zA-Z0-9]*$") then - compile_error_at_1_2(\%var, "This is not a valid Lua identifier.") + nomsu:compile_error(\%var, "This is not a valid Lua identifier.") end return lua else error("Unknown type: "..tostring(\%var)) @@ -322,7 +322,7 @@ test: compile [quote %s] to (Lua value "tostring(\(%s as lua expr)):as_lua()") test: - assume (lua type of {}) == "Lua table" + assume (lua type of {}) == "table" assume (type of {}) == "Dict" assume ({} is a "Dict") assume ("" is text) @@ -341,7 +341,7 @@ action [type of %]: else return lua_type end" parse [% is a %type, % is an %type] as ((type of %) == %type) parse [% isn't a %type, % isn't an %type, % is not a %type, % is not an %type] -..as ((type of %) == %type) +..as ((type of %) != %type) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- cgit v1.2.3 From dcff9ecfcf0c3f81cc22bacd082472ea744c9070 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 26 Sep 2018 13:57:13 -0700 Subject: Fix for calling function with non-literal list. --- core/metaprogramming.nom | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'core/metaprogramming.nom') diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom index f92fb0c..00151d5 100644 --- a/core/metaprogramming.nom +++ b/core/metaprogramming.nom @@ -91,7 +91,11 @@ lua> "\ compile [call %fn with %args] to: lua> "\ ..local lua = LuaCode.Value(tree.source, nomsu:compile(\%fn), "(") - lua:concat_append(table.map(\%args, function(a) return nomsu:compile(a) end), ", ") + if \%args.type == 'List' then + lua:concat_append(table.map(\%args, function(a) return nomsu:compile(a) end), ", ") + else + lua:append('unpack(', nomsu:compile(\%args), ')') + end lua:append(")") return lua" -- cgit v1.2.3 From 63d8b1cd3f34b15bf86210b99209e8b57e7019bb Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 28 Sep 2018 22:15:06 -0700 Subject: Fully working, I think? (with a lot of shims) --- core/metaprogramming.nom | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'core/metaprogramming.nom') diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom index 00151d5..6a457d9 100644 --- a/core/metaprogramming.nom +++ b/core/metaprogramming.nom @@ -23,7 +23,7 @@ lua> "\ ..COMPILE_ACTIONS["1 -> 2"] = function(nomsu, tree, \%args, \%body) local lua = LuaCode.Value(tree.source, "(function(") if AST.is_syntax_tree(\%args, "Action") then \%args = \%args:get_args() end - local lua_args = table.map(\%args, function(a) return AST.is_syntax_tree(a) and tostring(nomsu:compile(a)) or a end) + local lua_args = table.map(\%args, function(a) return AST.is_syntax_tree(a) and nomsu:compile(a):as_smext() or a end) lua:concat_append(lua_args, ", ") local body_lua = AST.is_syntax_tree(\%body) and nomsu:compile(\%body):as_statements("return ") or \%body body_lua:remove_free_vars(lua_args) @@ -64,14 +64,14 @@ test: assume (%tmp is (nil)) or barf "compile to is leaking variables" lua> "\ ..COMPILE_ACTIONS["compile 1 to 2"] = function(nomsu, tree, \%actions, \%body) - local \%args = {"nomsu", "tree", unpack(table.map(\%actions[1]:get_args(), function(a) return tostring(nomsu:compile(\ - ..a)) end))} + local \%args = {"nomsu", "tree", unpack(table.map(\%actions[1]:get_args(), function(a) return nomsu:compile(\ + ..a):as_smext() end))} local lua = LuaCode(tree.source, "COMPILE_ACTIONS[", \%actions[1].stub:as_lua(), "] = ", \(compile as (%args -> %body))) for i=2,#\%actions do local alias = \%actions[i] - local \%alias_args = {"nomsu", "tree", unpack(table.map(alias:get_args(), function(a) return tostring(nomsu:compile(\ - ..a)) end))} + local \%alias_args = {"nomsu", "tree", unpack(table.map(alias:get_args(), function(a) return nomsu:compile(\ + ..a):as_smext() 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(), "]") @@ -114,14 +114,14 @@ test: compile [local action %actions %body] to: lua> "\ ..local fn_name = \%actions[1].stub:as_lua_id() - local \%args = table.map(\%actions[1]:get_args(), function(a) return tostring(nomsu:compile(a)) end) + local \%args = table.map(\%actions[1]:get_args(), function(a) return nomsu:compile(a):as_smext() end) local lua = LuaCode(tree.source, fn_name, " = ", \(compile as (%args -> %body))) 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 tostring(nomsu:compile(a)) end) + local \%alias_args = table.map(alias:get_args(), function(a) return nomsu:compile(a):as_smext() end) lua:append("\\n", alias_name, " = ") if utils.equivalent(\%args, \%alias_args) then lua:append(fn_name) @@ -170,7 +170,7 @@ compile [parse %actions as %body] to (..) lua> "\ ..local replacements = {} for i,arg in ipairs(\%actions[1]:get_args()) do - replacements[arg[1]] = tostring(nomsu:compile(arg)) + replacements[arg[1]] = nomsu:compile(arg):as_smext() end local function make_tree(t) if AST.is_syntax_tree(t, "Var") then @@ -239,7 +239,7 @@ action [%var as lua identifier, %var as lua id] (..) elseif AST.is_syntax_tree(\%var, 'Var') then return \%var[1]:as_lua_id() elseif AST.is_syntax_tree(\%var) then local lua = \(%var as lua expr) - if not tostring(lua):match("^[_a-zA-Z][_a-zA-Z0-9]*$") then + if not lua:as_smext():match("^[_a-zA-Z][_a-zA-Z0-9]*$") then nomsu:compile_error(\%var, "This is not a valid Lua identifier.") end return lua -- cgit v1.2.3 From 23abab4f809e8d4b825746580082292db700036b Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 29 Oct 2018 13:00:08 -0700 Subject: Some cleanups and fixes, made the parser more permissive of prematurely terminated files. --- core/metaprogramming.nom | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/metaprogramming.nom') diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom index 6a457d9..d85c1ad 100644 --- a/core/metaprogramming.nom +++ b/core/metaprogramming.nom @@ -231,7 +231,7 @@ compile [%tree as nomsu] to (..) Lua value "nomsu:tree_to_nomsu(\(%tree as lua expr))" compile [%tree as inline nomsu] to (..) - Lua value "nomsu:tree_to_nomsu(\(%tree as lua expr), true)" + Lua value "nomsu:tree_to_inline_nomsu(\(%tree as lua expr), true)" action [%var as lua identifier, %var as lua id] (..) lua> "\ -- cgit v1.2.3 From ea3197aaffba00318920ed5e1e33ca5f2a5e6c5c Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 30 Oct 2018 23:42:04 -0700 Subject: Fully working version of (action [foo]: baz) -> ((foo) means: baz) refactor and misc other changes. --- core/metaprogramming.nom | 151 +++++++++++++++++++++++++---------------------- 1 file changed, 80 insertions(+), 71 deletions(-) (limited to 'core/metaprogramming.nom') diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom index d85c1ad..a2f15a1 100644 --- a/core/metaprogramming.nom +++ b/core/metaprogramming.nom @@ -1,4 +1,4 @@ -#!/usr/bin/env nomsu -V4.8.8.6 +#!/usr/bin/env nomsu -V4.8.10 # This File contains actions for making actions and compile-time actions and some helper functions to make that easier. @@ -33,7 +33,7 @@ lua> "\ end" lua> "\ - ..COMPILE_ACTIONS["compile as 1"] = function(nomsu, tree, \%action) + ..COMPILE_ACTIONS["what 1 compiles to"] = function(nomsu, tree, \%action) local lua = LuaCode.Value(tree.source, "COMPILE_ACTIONS[", \%action.stub:as_lua(), "](") local lua_args = table.map(\%action:get_args(), function(a) return nomsu:compile(a) end) table.insert(lua_args, 1, "nomsu") @@ -46,32 +46,32 @@ lua> "\ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ test: - compile [five] to (Lua value "5") + (five) compiles to (Lua value "5") test: assume ((five) == 5) or barf "Compile to expression failed." - compile [loc x] to (Lua "local x = 99;") + (loc x) compiles to (Lua "local x = 99;") test: lua> "do" loc x assume (%x is 99) or barf "Compile to statements with locals failed." lua> "end" assume (%x is (nil)) or barf "Failed to properly localize a variable." - compile [asdf] to: + (asdf) compiles to: %tmp = "" return (Lua %tmp) test: asdf assume (%tmp is (nil)) or barf "compile to is leaking variables" lua> "\ - ..COMPILE_ACTIONS["compile 1 to 2"] = function(nomsu, tree, \%actions, \%body) - local \%args = {"nomsu", "tree", unpack(table.map(\%actions[1]:get_args(), function(a) return nomsu:compile(\ - ..a):as_smext() end))} + ..COMPILE_ACTIONS["1 compiles to 2"] = 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 lua = LuaCode(tree.source, "COMPILE_ACTIONS[", \%actions[1].stub:as_lua(), - "] = ", \(compile as (%args -> %body))) + "] = ", \(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):as_\ + ..smext() 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(), "]") @@ -84,11 +84,12 @@ lua> "\ end end return lua - end" + end + COMPILE_ACTIONS["1 all compile to 2"] = COMPILE_ACTIONS["1 compiles to 2"]" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -compile [call %fn with %args] to: +(call %fn with %args) compiles to: lua> "\ ..local lua = LuaCode.Value(tree.source, nomsu:compile(\%fn), "(") if \%args.type == 'List' then @@ -100,22 +101,23 @@ compile [call %fn with %args] to: return lua" test: - local action [foo %x]: return "outer" + (foo %x) means (return "outer") with local [action (foo %)]: - local action [foo %x]: + (foo %x) means: %y = (%x + 1) return %y assume ((foo 10) == 11) or barf "Action didn't work." assume (%y is (nil)) or barf "Action leaked a local into globals." - parse [baz %] as (foo %) + (baz %) parses as (foo %) assume ((foo 1) == "outer") -compile [local action %actions %body] to: +[%actions means %body, %actions all mean %body] all compile to: lua> "\ - ..local fn_name = \%actions[1].stub:as_lua_id() + ..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 lua = LuaCode(tree.source, fn_name, " = ", \(compile as (%args -> %body))) + 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] @@ -136,23 +138,24 @@ compile [local action %actions %body] to: return lua" test: - action [baz1]: return "baz1" - action [baz2] "baz2" + externally (baz1) means: return "baz1" + externally (baz2) means "baz2" test: assume ((baz1) == "baz1") assume ((baz2) == "baz2") -compile [action %actions %body] to (..) +[externally %actions means %body, externally %actions all mean %body] all compile to: lua> "\ - ..local lua = \(compile as (local action %actions %body)) + ..local lua = \(what (%actions means %body) compiles to) + if \%actions.type ~= "List" then \%actions = {\%actions, type="List"} end lua:remove_free_vars(table.map(\%actions, function(a) return a.stub:as_lua_id() end)) return lua" test: assume ((action (say %)) == (=lua "say_1")) -compile [action %action] to (Lua value (%action.stub as lua id)) +(action %action) compiles to (Lua value (%action.stub as lua id)) test: - parse [swap %x and %y] as (..) + (swap %x and %y) parses as (..) do: %tmp = %x %x = %y @@ -166,9 +169,10 @@ test: swap %tmp and %tmp2 assume ((%tmp == 2) and (%tmp2 == 1)) or barf "\ ..'parse % as %' variable mangling failed." -compile [parse %actions as %body] to (..) +[%actions parses as %body, %actions all parse as %body] all compile to: lua> "\ ..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() end @@ -204,36 +208,37 @@ compile [parse %actions as %body] to (..) local \%new_body = LuaCode(\%body.source, "local mangle = mangler()", "\\nreturn ", make_tree(\%body)) - local ret = \(compile as (compile %actions to %new_body)) + local ret = \(what (%actions compiles to %new_body) compiles to) return ret" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # TODO: add check for .is_value -compile [%tree as lua expr] to (Lua value "nomsu:compile(\(=lua "nomsu:compile(\%tree, nil, true)"), nil, true)") +(%tree as lua expr) compiles to (..) + Lua value "nomsu:compile(\(=lua "nomsu:compile(\%tree, nil, true)"), nil, true)" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -compile [%tree as lua] to (Lua value "nomsu:compile(\(%tree as lua expr))") -compile [%tree as lua statements] to (..) +(%tree as lua) compiles to (Lua value "nomsu:compile(\(%tree as lua expr))") +(%tree as lua statements) compiles to (..) Lua value "nomsu:compile(\(%tree as lua expr)):as_statements()" -compile [%tree as lua return] to (..) +(%tree as lua return) compiles to (..) Lua value "nomsu:compile(\(%tree as lua expr)):as_statements('return ')" -compile [remove action %action] to (..) +(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." -compile [%tree as nomsu] to (..) +(%tree as nomsu) compiles to (..) Lua value "nomsu:tree_to_nomsu(\(%tree as lua expr))" -compile [%tree as inline nomsu] to (..) +(%tree as inline nomsu) compiles to (..) Lua value "nomsu:tree_to_inline_nomsu(\(%tree as lua expr), true)" -action [%var as lua identifier, %var as lua id] (..) +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() elseif AST.is_syntax_tree(\%var, 'Var') then return \%var[1]:as_lua_id() @@ -248,17 +253,17 @@ action [%var as lua identifier, %var as lua id] (..) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -compile [% is syntax tree] to (Lua value "AST.is_syntax_tree(\(% as lua expr))") -compile [% is %kind syntax tree] to (..) +(% is syntax tree) compiles to (Lua value "AST.is_syntax_tree(\(% as lua expr))") +(% is %kind syntax tree) compiles to (..) Lua value "AST.is_syntax_tree(\(% as lua expr), \(%kind as lua expr))" -compile [%tree with %t -> %replacement] to (..) +(%tree with %t -> %replacement) compiles to (..) Lua value "\ ..\(%tree as lua expr):map(function(\(%t as lua expr)) \(%replacement as lua return) end)" -action [%tree with vars %replacements] (..) +externally (%tree with vars %replacements) means (..) =lua "\ ..\%tree:map(function(\%t) if \%t.type == "Var" then @@ -266,7 +271,7 @@ action [%tree with vars %replacements] (..) end end)" -compile [tree %tree with vars %replacements] to (..) +(tree %tree with vars %replacements) compiles to (..) Lua value "\ ..\(=lua "(\%tree):as_lua()"):map(function(t) if t.type == "Var" then @@ -274,7 +279,7 @@ compile [tree %tree with vars %replacements] to (..) end end)" -compile [%tree has subtree %match_tree] to (..) +(%tree has subtree %match_tree) compiles to (..) Lua value "\ ..(function() local match_tree = \(%match_tree as lua expr) @@ -283,7 +288,7 @@ compile [%tree has subtree %match_tree] to (..) end end)()" -action [match %tree with %patt]: +externally (match %tree with %patt) means: lua> "\ ..if \%patt.type == "Var" then return _Dict{[\%patt[1]]=\%tree} end if \%patt.type == "Action" and \%patt.stub ~= \%tree.stub then return nil end @@ -301,7 +306,7 @@ action [match %tree with %patt]: end return matches" -action [%tree with %patt ~> %replacement]: +externally (%tree with %patt ~> %replacement) means: lua> "\ ..return \%tree:map(function(\%t) local \%vars = \(match %t with %patt) @@ -323,7 +328,7 @@ test: ..one "two"" ..== "\"one\\n\\\"two\\\"\"" -compile [quote %s] to (Lua value "tostring(\(%s as lua expr)):as_lua()") +(quote %s) compiles to (Lua value "tostring(\(%s as lua expr)):as_lua()") test: assume (lua type of {}) == "table" @@ -331,21 +336,23 @@ test: assume ({} is a "Dict") assume ("" is text) assume ("" isn't a "Dict") +externally (% is text) means (=lua "\(lua type of %) == 'string'") +externally [% is not text, % isn't text] all mean (..) + =lua "\(lua type of %) ~= 'string'" -action [% is text] (=lua "\(lua type of %) == 'string'") -action [% is not text, % isn't text] (=lua "\(lua type of %) ~= 'string'") -action [type of %]: +externally (type of %) means: lua> "\ - local lua_type = \(lua type of %) + ..local lua_type = \(lua type of %) if lua_type == 'string' then return 'Text' elseif lua_type == 'table' then local mt = getmetatable(\%) if mt and mt.__type then return mt.__type end return 'Lua table' else return lua_type end" -parse [% is a %type, % is an %type] as ((type of %) == %type) -parse [% isn't a %type, % isn't an %type, % is not a %type, % is not an %type] -..as ((type of %) != %type) + +[% is a %type, % is an %type] all parse as ((type of %) == %type) +[% isn't a %type, % isn't an %type, % is not a %type, % is not an %type] all parse as (..) + (type of %) != %type ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -354,8 +361,8 @@ test: %a = (parse "\\1") %b = \(\(1)) assume ((parse "\\1") == \(\(1))) -compile [parse %text] to (Lua value "nomsu:parse(\(%text as lua expr))") -compile [parse %text from %filename] to (..) +(parse %text) compiles to (Lua value "nomsu:parse(\(%text as lua expr))") +(parse %text from %filename) compiles to (..) Lua value "\ ..nomsu:parse(NomsuCode(Source(\(%filename as lua expr), 1, #\(%text as lua expr)), \(..) %text as lua expr @@ -366,37 +373,39 @@ test: external %passed = (no) run "external %passed = (yes)" assume %passed -compile [run %nomsu_code] to (..) +(run %nomsu_code) compiles to (..) Lua value "\ - ..nomsu:run(NomsuCode(\(..) - =lua "tostring(\(%nomsu_code.source)):as_lua()" - .., \(%nomsu_code as lua expr)))" + ..nomsu:run(NomsuCode(\(=lua "tostring(\(%nomsu_code.source)):as_lua()"), \(..) + %nomsu_code as lua expr + ..))" test: assume ((\(\(5) + \(5)) as value) == 10) or barf "%tree as value failed." -compile [run tree %tree, %tree as value] to (Lua value "nomsu:run(\(%tree as lua expr))") -compile [compile %block, compiled %block, %block compiled] to (..) +[run tree %tree, %tree as value] all compile to (..) + Lua value "nomsu:run(\(%tree as lua expr))" + +[compile %block, compiled %block, %block compiled] all compile to (..) Lua value "nomsu:compile(\(%block as lua))" # Return statement is wrapped in a do..end block because Lua is unhappy if you put code after a return statement, unless you wrap it in a block. -compile [return] to (Lua "do return; end") -compile [return %return_value] to (..) +(return) compiles to (Lua "do return; end") +(return %return_value) compiles to (..) Lua "do return \(%return_value as lua expr) end" # Literals -compile [yes] to (Lua value "true") -compile [no] to (Lua value "false") -compile [nothing, nil, null] to (Lua value "nil") -compile [Nomsu syntax version] to (Lua value "NOMSU_SYNTAX_VERSION") -compile [Nomsu compiler version] to (Lua value "NOMSU_COMPILER_VERSION") -compile [core version] to (Lua value "NOMSU_CORE_VERSION") -compile [lib version] to (Lua value "NOMSU_LIB_VERSION") -compile [command line args] to (Lua value "arg") +(yes) compiles to (Lua value "true") +(no) compiles to (Lua value "false") +[nothing, nil, null] all compile to (Lua value "nil") +(Nomsu syntax version) compiles to (Lua value "NOMSU_SYNTAX_VERSION") +(Nomsu compiler version) compiles to (Lua value "NOMSU_COMPILER_VERSION") +(core version) compiles to (Lua value "NOMSU_CORE_VERSION") +(lib version) compiles to (Lua value "NOMSU_LIB_VERSION") +(command line args) compiles to (Lua value "arg") ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -compile [with local compile actions %body] to (..) +(with local compile actions %body) compiles to (..) Lua "\ ..do local nomsu = nomsu:fork() @@ -404,7 +413,7 @@ compile [with local compile actions %body] to (..) \(%body as lua statements) end" -action [Nomsu version]: +externally (Nomsu version) means: use "lib/version.nom" return "\ ..\(Nomsu syntax version).\(core version).\(Nomsu compiler version).\(lib version)" -- cgit v1.2.3 From 7a35e38d8778670fe0662f203e82638355db3bba Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 31 Oct 2018 15:05:17 -0700 Subject: Renamed (action %) -> (%'s meaning) --- core/metaprogramming.nom | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'core/metaprogramming.nom') diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom index a2f15a1..c999191 100644 --- a/core/metaprogramming.nom +++ b/core/metaprogramming.nom @@ -102,7 +102,7 @@ lua> "\ test: (foo %x) means (return "outer") - with local [action (foo %)]: + with local [(foo %)'s meaning]: (foo %x) means: %y = (%x + 1) return %y @@ -151,8 +151,8 @@ test: return lua" test: - assume ((action (say %)) == (=lua "say_1")) -(action %action) compiles to (Lua value (%action.stub as lua id)) + assume (((say %)'s meaning) == (=lua "say_1")) +(%action's meaning) compiles to (Lua value (%action.stub as lua id)) test: (swap %x and %y) parses as (..) -- cgit v1.2.3 From d0c3c57f7b25c8d912c426e48cb5ab09cd738f65 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 31 Oct 2018 15:54:18 -0700 Subject: Simplified AST to just use a single moonscript class called "SyntaxTree" instead of a different metatable for each type of syntax tree. --- core/metaprogramming.nom | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'core/metaprogramming.nom') diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom index c999191..dbc45a4 100644 --- a/core/metaprogramming.nom +++ b/core/metaprogramming.nom @@ -22,10 +22,10 @@ lua> "\ lua> "\ ..COMPILE_ACTIONS["1 -> 2"] = function(nomsu, tree, \%args, \%body) local lua = LuaCode.Value(tree.source, "(function(") - if AST.is_syntax_tree(\%args, "Action") then \%args = \%args:get_args() end - local lua_args = table.map(\%args, function(a) return AST.is_syntax_tree(a) and nomsu:compile(a):as_smext() or a end) + 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) lua:concat_append(lua_args, ", ") - local body_lua = AST.is_syntax_tree(\%body) and nomsu:compile(\%body):as_statements("return ") or \%body + local body_lua = SyntaxTree:is_instance(\%body) and nomsu:compile(\%body):as_statements("return ") or \%body body_lua:remove_free_vars(lua_args) body_lua:declare_locals() lua:append(")\\n ", body_lua, "\\nend)") @@ -177,13 +177,13 @@ test: replacements[arg[1]] = nomsu:compile(arg):as_smext() end local function make_tree(t) - if AST.is_syntax_tree(t, "Var") then + if SyntaxTree:is_instance(t) and t.type == "Var" then if replacements[t[1]] then return replacements[t[1]] else - return t.type.."{mangle("..t[1]:as_lua().."), source="..tostring(t.source):as_lua().."}" + return "SyntaxTree{mangle("..t[1]:as_lua().."), type="..t.type:as_lua()..", source="..tostring(t.source):as_lua().."}" end - elseif AST.is_syntax_tree(t) then + elseif SyntaxTree:is_instance(t) then local ret = {} local i = 1 for k, v in pairs(t) do @@ -198,7 +198,7 @@ test: ret[#ret+1] = "["..make_tree(k).."]= "..make_tree(v) end end - return t.type.."{"..table.concat(ret, ", ").."}" + return "SyntaxTree{"..table.concat(ret, ", ").."}" elseif lua_type_of_1(t) == 'number' then return tostring(t) else @@ -241,8 +241,8 @@ 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() - elseif AST.is_syntax_tree(\%var, 'Var') then return \%var[1]:as_lua_id() - elseif AST.is_syntax_tree(\%var) then + 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 nomsu:compile_error(\%var, "This is not a valid Lua identifier.") @@ -253,9 +253,9 @@ externally [%var as lua identifier, %var as lua id] all mean: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -(% is syntax tree) compiles to (Lua value "AST.is_syntax_tree(\(% as lua expr))") +(% is syntax tree) compiles to (Lua value "SyntaxTree:is_instance(\(% as lua expr))") (% is %kind syntax tree) compiles to (..) - Lua value "AST.is_syntax_tree(\(% as lua expr), \(%kind as lua expr))" + Lua value "SyntaxTree:is_instance(\(% as lua expr), \(%kind as lua expr))" (%tree with %t -> %replacement) compiles to (..) Lua value "\ @@ -295,7 +295,7 @@ externally (match %tree with %patt) means: if #\%patt ~= #\%tree then return nil end local matches = _Dict{} for \%i=1,#\%patt do - if AST.is_syntax_tree(\%tree[\%i]) then + if SyntaxTree:is_instance(\%tree[\%i]) then local submatch = \(match %tree.%i with %patt.%i) if not submatch then return nil end for k,v in pairs(submatch) do -- cgit v1.2.3 From 307dea18815ba4a06a3098edb170d7ad90708815 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 2 Nov 2018 14:38:24 -0700 Subject: Changed stub convention to (foo 1 baz 2) -> foo_1_baz instead of foo_1_baz_2, removed "smext", made some cleanup changes. --- core/metaprogramming.nom | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) (limited to 'core/metaprogramming.nom') 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 -- cgit v1.2.3