aboutsummaryrefslogtreecommitdiff
path: root/lib/core/metaprogramming.nom
diff options
context:
space:
mode:
Diffstat (limited to 'lib/core/metaprogramming.nom')
-rw-r--r--lib/core/metaprogramming.nom84
1 files changed, 53 insertions, 31 deletions
diff --git a/lib/core/metaprogramming.nom b/lib/core/metaprogramming.nom
index 8fe09ca..7bf04f2 100644
--- a/lib/core/metaprogramming.nom
+++ b/lib/core/metaprogramming.nom
@@ -1,5 +1,6 @@
-#!/usr/bin/env nomsu -V6.15.13.8
-#
+#!/usr/bin/env nomsu -V7.0.0
+
+###
This File contains actions for making actions and compile-time actions and some helper
functions to make that easier.
@@ -55,6 +56,28 @@ lua> ("
end
COMPILE_RULES["->"] = COMPILE_RULES["1 ->"]
COMPILE_RULES["for"] = COMPILE_RULES["1 ->"]
+
+ COMPILE_RULES["`"] = function(\(nomsu environment), _tree, escaped)
+ local function escape(t)
+ if t.type == "Action" and t:get_stub() == "`" and #t == 2 then
+ return \(nomsu environment):compile(t[2])
+ else
+ local bits = {}
+ table.insert(bits, "type="..t.type:as_lua())
+ if t.source then
+ table.insert(bits, "source="..t.source:as_lua())
+ end
+ for i,b in ipairs(t) do
+ table.insert(bits, lua_type_of(b) == 'string' and b:as_lua() or escape(b))
+ end
+ local lua = LuaCode:from(t.source, "SyntaxTree{")
+ lua:concat_add(bits, ", ")
+ lua:add("}")
+ return lua
+ end
+ end
+ return escape(escaped)
+ end
")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -95,7 +118,8 @@ lua> ("
(\$action.type == "EscapedNomsu" and \$action[1].type == "Action") or
\$action.type == "MethodCall") then
at_1_fail(\$action.source, "Compile error: "..
- "This first argument to (* compiles to *) is neither an action nor an escaped action (it's a "..\$action.type.."). "..
+ "This first argument to (* compiles to *) is neither an action nor an escaped \
+ ..action (it's a "..\$action.type.."). "..
"Hint: This should probably be an action like:\\n"
.."(foo $x) compiles to \\"(\\\\($x as lua) + 1)\\"")
end
@@ -104,25 +128,23 @@ lua> ("
if a.type == "EscapedNomsu" then \$args:add(a[1]) end
end
return LuaCode("COMPILE_RULES[", \($action as lua), ":get_stub()] = ",
- \(\($args -> $body) as lua))
+ \(`(`$args -> `$body) as lua))
else
for _,a in ipairs(\$action:get_args()) do \$args:add(a) end
return LuaCode("COMPILE_RULES[", \$action:get_stub():as_lua(),
- "] = ", \(\($args -> $body) as lua))
+ "] = ", \(`(`$args -> `$body) as lua))
end
end
")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-(` $) compiles to (Lua (=lua "\$ or SyntaxTree{type='Action'}", as lua))
-
($actions all compile to $body) compiles to:
lua> ("
if \$actions.type ~= "List" then
at_1_fail(\$actions, "Compile error: This should be a list of actions.")
end
- local lua = \(\($actions.1 compiles to $body) as lua)
+ local lua = \(`(`$actions.1 compiles to `$body) as lua)
local \$args = a_List{"\(nomsu environment)", "\(this tree)", unpack(\$actions[1]:get_args())}
local \$compiled_args = a_List{"\(nomsu environment)", "\(this tree)"};
for i=3,#\$args do \$compiled_args[i] = \(nomsu environment):compile(\$args[i]) end
@@ -165,7 +187,6 @@ test:
($action means $body) compiles to:
lua> ("
-
local lua = LuaCode()
if \$action.type == "MethodCall" then
lua:add(\(nomsu environment):compile(\$action[1]), ".", \$action[2]:get_stub():as_lua_id())
@@ -175,15 +196,16 @@ test:
else
at_1_fail(\$action, "Compile error: This is not an action or method call.")
end
- lua:add(" = ", \(\($action -> $body) as lua), ";")
+ lua:add(" = ", \(`(`$action -> `$body) as lua), ";")
return lua
")
($actions all mean $body) compiles to:
lua> ("
- local lua = \(\($actions.1 means $body) as lua)
+ local lua = \(`(`$actions.1 means `$body) as lua)
local first_def = (\$actions[1].type == "MethodCall"
- and LuaCode(\(nomsu environment):compile(\$actions[1][1]), ".", \$actions[1][2]:get_stub():as_lua_id())
+ and LuaCode(\(nomsu environment):compile(\$actions[1][1]), ".", \$actions[1][2]:get_\
+ ..stub():as_lua_id())
or LuaCode(\$actions[1]:get_stub():as_lua_id()))
local \$args = a_List(\$actions[1]:get_args())
for i=2,#\$actions do
@@ -199,7 +221,7 @@ test:
if \$args == \$alias_args then
lua:add(" = ", first_def, ";")
else
- lua:add(" = ", \(\($alias_args -> $actions.1) as lua), ";")
+ lua:add(" = ", \(`(`$alias_args -> `$actions.1) as lua), ";")
end
end
return lua
@@ -301,15 +323,13 @@ test:
local \$new_body = LuaCode:from(\$body.source,
"local mangle = mangler()",
"\\nreturn ", make_tree(\$body))
- local ret = \(\($actions all compile to $new_body) as lua)
- return ret
+ return \(`(`$actions all compile to `$new_body) as lua)
")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[$action parses as $body] all parse as ([$action] all parse as $body)
-
-((nomsu environment), $tree as lua expr) means:
+(nomsu environment, $tree as lua expr) means:
lua> ("
local tree_lua = \(nomsu environment):compile(\$tree)
if \$tree.type == 'Block' and #\$tree > 1 then
@@ -318,9 +338,8 @@ test:
return tree_lua
")
-# Need to make sure the proper environment is used for compilation (i.e. the caller's environment)
-($tree as lua expr) compiles to
- =lua "SyntaxTree{type='MethodCall', \(\(nomsu environment)), \(\($tree as lua expr))}"
+### Need to make sure the proper environment is used for compilation (i.e. the caller's environment)
+($tree as lua expr) compiles to `((nomsu environment), `$tree as lua expr)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -337,11 +356,11 @@ external:
")
test:
- (num args (*extra arguments*)) means (#(*extra arguments*))
+ (num args (*extra arguments*)) means #(*extra arguments*)
assume (num args 1 2 3) == 3
(extra args (*extra arguments*)) means [*extra arguments*]
assume (extra args 1 2 3) == [1, 2, 3]
- (third arg (*extra arguments*)) means ((*extra arguments*).3)
+ (third arg (*extra arguments*)) means (*extra arguments*).3
assume (third arg 5 6 7 8) == 7
(*extra arguments*) compiles to "..."
@@ -405,7 +424,7 @@ external:
local lua_type = \(lua type of $)
return 'a '..lua_type:capitalized()
")
-
+
($ is $type) means:
lua> ("
local class = getmetatable(\$)
@@ -425,13 +444,15 @@ external:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test:
- assume ("Action" tree with "foo" ("Var" tree with "x")) == \(foo \$x)
+ assume ("Action" tree with "foo" ("Var" tree with "x")) == `(foo $x)
external:
($type tree with (*extra arguments*)) means
SyntaxTree (=lua "{type=\$type, ...}")
+
($type tree from $source) means
SyntaxTree (=lua "{type=\$type, source=\$source}")
+
($type tree from $source with (*extra arguments*)) means
SyntaxTree (=lua "{type=\$type, source=\$source, ...}")
@@ -440,7 +461,7 @@ test:
return 100 200 300
assume (select 2 (foo)) == 200
-# Return statement is wrapped in a do..end block because Lua is unhappy if you
+### 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.
(return (*extra arguments*)) compiles to:
lua> ("
@@ -453,10 +474,10 @@ test:
return lua
")
-# Convenience helper:
-(return Lua (*extra arguments*)) compiles to \(return \(Lua (*extra arguments*)))
+### Convenience helper:
+(return Lua (*extra arguments*)) compiles to `(return (Lua `(*extra arguments*)))
-# Literals
+### Literals
(yes) compiles to "(true)"
(no) compiles to "(false)"
[nothing, nil, null] all compile to "(nil)"
@@ -466,7 +487,7 @@ test:
(at compilation $expr) compiles to:
lua> ("
- local value = \(nomsu environment):run(\(\(return $expr)))
+ local value = \(nomsu environment):run(\(`(return `$expr)))
if lua_type_of(value) == 'table' or lua_type_of(value) == 'string' and value.as_lua then
return LuaCode(value:as_lua())
else
@@ -490,8 +511,9 @@ test:
return lua
")
-~~~~
-# TODO: Remove shim
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+### TODO: Remove shim
($tree with $t -> $replacement) parses as
$tree, with ($t -> $replacement)