aboutsummaryrefslogtreecommitdiff
path: root/core/metaprogramming.nom
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2018-10-31 15:54:18 -0700
committerBruce Hill <bruce@bruce-hill.com>2018-10-31 15:54:51 -0700
commitd0c3c57f7b25c8d912c426e48cb5ab09cd738f65 (patch)
treebc783d59d13c859343a9225f003062adb455ecac /core/metaprogramming.nom
parentec92b0fccd70f06b5348fa355f49557aa71fdb3c (diff)
Simplified AST to just use a single moonscript class called "SyntaxTree"
instead of a different metatable for each type of syntax tree.
Diffstat (limited to 'core/metaprogramming.nom')
-rw-r--r--core/metaprogramming.nom24
1 files changed, 12 insertions, 12 deletions
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