aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2018-04-17 14:18:23 -0700
committerBruce Hill <bitbucket@bruce-hill.com>2018-04-17 14:19:07 -0700
commit54fc7fc4404c02df2c38a7ae121e61e9b8bca78c (patch)
tree15142e627201978116985f56d05ab88bf237c59b /core
parent25e06d1fce660e7c8144425b440f7b1c698e2fb7 (diff)
Moving tree_to_lua into each of the Nomsu tree types, which are now in
their own file.
Diffstat (limited to 'core')
-rw-r--r--core/control_flow.nom192
-rw-r--r--core/metaprogramming.nom55
-rw-r--r--core/operators.nom54
3 files changed, 143 insertions, 158 deletions
diff --git a/core/control_flow.nom b/core/control_flow.nom
index 3f6c59c..5a3a775 100644
--- a/core/control_flow.nom
+++ b/core/control_flow.nom
@@ -13,30 +13,19 @@ immediately:
# Conditionals
immediately:
compile [if %condition %if_body] to:
- %if_body <- (%if_body as lua)
- return {..}
- locals: %if_body.locals
- statements:".."
- if \(%condition as lua expr) then
- \(%if_body.statements or "\(%if_body.expr);")
- end
+ Lua ".."
+ if \(%condition as lua expr) then
+ \(%if_body as lua statements)
+ end
parse [unless %condition %unless_body] as: if (not %condition) %unless_body
compile [if %condition %if_body else %else_body, unless %condition %else_body else %if_body] to:
- %if_body <- (%if_body as lua)
- %else_body <- (%else_body as lua)
- lua> ".."
- local \%locals = {unpack(\%if_body.locals or {})};
- for i,loc in ipairs(\%else_body.locals or {}) do table.insert(\%locals, loc); end
- utils.deduplicate(\%locals);
- return {..}
- locals:%locals
- statements:".."
- if \(%condition as lua expr) then
- \(%if_body.statements or "\(%if_body.expr);")
- else
- \(%else_body.statements or "\(%else_body.expr);")
- end
+ Lua ".."
+ if \(%condition as lua expr) then
+ \(%if_body as lua statements)
+ else
+ \(%else_body as lua statements)
+ end
# Conditional expression (ternary operator)
#.. Note: this uses a function instead of "(condition and if_expr or else_expr)"
@@ -51,14 +40,15 @@ immediately
#.. If %when_true_expr is guaranteed to be truthy, we can use Lua's idiomatic
equivalent of a conditional expression: (cond and if_true or if_false)
if: %when_true_expr.type in {Text:yes, List:yes, Dict:yes, Number:yes}
- return {..}
- expr:"(\(%condition as lua expr) and \(%when_true_expr as lua expr) or \(%when_false_expr as lua expr))"
+ return:
+ Lua ".."
+ (\(%condition as lua expr) and \(%when_true_expr as lua expr) or \(%when_false_expr as lua expr))
..else:
#.. Otherwise, need to do an anonymous inline function (yuck, too bad lua
doesn't have a proper ternary operator!)
To see why this is necessary consider: (random()<.5 and false or 99)
- return {..}
- expr: ".."
+ return:
+ Lua ".."
(function()
if \(%condition as lua expr) then
return \(%when_true_expr as lua expr);
@@ -82,67 +72,59 @@ immediately:
# Helper function
immediately:
compile [if %tree has subtree %subtree where %condition %body] to:
- %body_lua <- (%body as lua)
- %body_statements <- (%body_lua.statements or "\(%body_lua.expr);")
- return {..}
- locals: %body_lua.locals
- statements:".."
- for \(%subtree as lua expr) in coroutine.wrap(function() nomsu:walk_tree(\(%tree as lua expr)) end) do
- if Types.is_node(\(%subtree as lua expr)) then
- if \(%condition as lua expr) then
- \%body_statements
- break;
- end
+ Lua ".."
+ for \(%subtree as lua expr) in coroutine.wrap(function() nomsu:walk_tree(\(%tree as lua expr)) end) do
+ if Types.is_node(\(%subtree as lua expr)) then
+ if \(%condition as lua expr) then
+ \(%body as lua statements)
+ break;
end
end
+ end
# While loops
immediately:
compile [do next repeat] to {statements:"goto continue_repeat;"}
compile [stop repeating] to {statements:"goto stop_repeat;"}
compile [repeat while %condition %body] to
- %body_lua <- (%body as lua)
- %body_statements <- (%body_lua.statements or "\(%body_lua.expr);")
+ %lua <-: Lua "while \(%condition as lua expr) do"
if %body has subtree % where:
(%.type = "FunctionCall") and ((%'s stub) is "do next repeat")
- ..: %body_statments +<- "\n::continue_repeat::;"
- %code <- ".."
- while \(%condition as lua expr) do
- \%body_statements
- end --while-loop
+ ..:
+ %lua +<- "\n::continue_repeat::;"
+ %lua +<- "\n"
+ %lua +<- (%body as lua statements)
+ %lua +<- "\nend --while-loop"
if %body has subtree % where:
(%.type = "FunctionCall") and ((%'s stub) is "stop repeating")
..:
- %code <- ".."
+ %lua <- ".."
do -- scope of "stop repeating" label
- \%code
+ \%lua
::stop_repeat::;
end -- end of "stop repeating" label scope
- return {statements:%code, locals:%body_lua.locals}
+ return %lua
parse [repeat %body] as: repeat while (yes) %body
parse [repeat until %condition %body] as: repeat while (not %condition) %body
compile [..]
repeat %n times %body
..to:
- %body_lua <- (%body as lua)
- %body_statements <- (%body_lua.statements or "\(%body_lua.expr);")
+ %lua <-: Lua "for i=1,\(%n as lua expr) do"
if %body has subtree % where
(%.type = "FunctionCall") and ((%'s stub) is "do next repeat")
- ..: %body_statements +<- "\n::continue_repeat::;"
- %code <- ".."
- for i=1,\(%n as lua expr) do
- \%body_statements
- end --numeric for-loop
+ ..: %lua +<- "\n::continue_repeat::;"
+ %lua +<- "\n\(%body as lua statements)\nend --numeric for-loop"
if %body has subtree % where:
(%.type = "FunctionCall") and ((%'s stub) is "stop repeating")
..:
- %code <- ".."
- do -- scope of "stop repeating" label
- \%code
- ::stop_repeat::;
- end -- end of "stop repeating" label scope
- return {statements:%code, locals:%body_lua.locals}
+ %lua <-:
+ Lua ".."
+ do -- scope of "stop repeating" label
+ \%lua
+ ::stop_repeat::;
+ end -- end of "stop repeating" label scope
+ return %lua
# For loop control flow:
immediately:
@@ -157,33 +139,30 @@ immediately:
for %var from %start to %stop by %step %body
for %var from %start to %stop via %step %body
..to:
- %body_lua <- (%body as lua)
- %body_statements <- (%body_lua.statements or "\(%body_lua.expr);")
+ # This uses Lua's approach of only allowing loop-scoped variables in a loop
+ assume (%var.type is "Var") or barf "Loop expected variable, not: \(%var's source code)"
+ %lua <-
+ Lua ".."
+ for \(%var as lua expr)=\(%start as lua expr),\(%n as lua expr) do
if %body has subtree % where:
(%.type = "FunctionCall") and
((%'s stub) is "do next %") and
%.value.3.value is %var.value
- ..: %body_statements +<- "\n::continue_\(%var as lua identifier)::;"
-
- # This uses Lua's approach of only allowing loop-scoped variables in a loop
- assume (%var.type is "Var") or barf "Loop expected variable, not: \(%var's source code)"
- %code <- ".."
- for \(%var as lua expr)=\(%start as lua expr),\(%stop as lua expr),\(%step as lua expr) do
- \%body_statements
- end --numeric for-loop
+ ..: %lua write code "\n::continue_\(%var as lua identifier)::;"
+ %lua write code "\n\(%body as lua statements)\nend --numeric for-loop"
if %body has subtree % where:
(%.type = "FunctionCall") and:
((%'s stub) is "stop %") and:
%.value.2.value is %var.value
..:
- %code <- ".."
+ %lua write code ".."
do -- scope for stopping for-loop
- \%code
+ \%lua
::stop_\(%var as lua identifier)::;
end -- end of scope for stopping for-loop
- return {statements:%code, locals:%body_lua.locals}
+ return %lua
immediately:
parse [for %var from %start to %stop %body] as: for %var from %start to %stop via 1 %body
@@ -196,57 +175,49 @@ immediately:
# For-each loop (lua's "ipairs()")
immediately:
compile [for %var in %iterable %body] to:
- %body_lua <- (%body as lua)
- %body_statements <- (%body_lua.statements or "\(%body_lua.expr);")
+ # This uses Lua's approach of only allowing loop-scoped variables in a loop
+ assume (%var.type is "Var") or barf "Loop expected variable, not: \(%var's source code)"
+ %lua <-: Lua "for i,\(%var as lua identifier) in ipairs(\(%iterable as lua expr)) do"
if %body has subtree % where:
(%.type = "FunctionCall") and
((%'s stub) is "do next %") and
%.value.3.value is %var.value
- ..: %body_statements +<- "\n::continue_\(%var as lua identifier)::;"
- # This uses Lua's approach of only allowing loop-scoped variables in a loop
- assume (%var.type is "Var") or barf "Loop expected variable, not: \(%var's source code)"
- %code <- ".."
- for i,\(%var as lua expr) in ipairs(\(%iterable as lua expr)) do
- \%body_statements
- end --foreach-loop
+ ..: %lua +<- (Lua "\n::continue_\(%var as lua identifier)::;")
+ %lua +<- (Lua "\n\(%body as lua statements)\nend --foreach-loop")
if %body has subtree % where:
(%.type = "FunctionCall") and
((%'s stub) is "stop %") and
%.value.2.value is %var.value
..:
- %code <- ".."
- do -- scope for stopping for-loop
- \%code
- ::stop_\(%var as lua identifier)::;
- end -- end of scope for stopping for-loop
- return {statements:%code, locals:%body_lua.locals}
+ %lua <-
+ Lua ".."
+ do -- scope for stopping for-loop
+ \%lua
+ ::stop_\(%var as lua identifier)::;
+ end -- end of scope for stopping for-loop
+ return %lua
parse [for all %iterable %body] as: for % in %iterable %body
# Dict iteration (lua's "pairs()")
immediately:
compile [for %key = %value in %iterable %body] to:
- %body_lua <- (%body as lua)
- %body_statements <- (%body_lua.statements or "\(%body_lua.expr);")
+ # This uses Lua's approach of only allowing loop-scoped variables in a loop
+ assume (%key.type is "Var") or barf "Loop expected variable, not: \(%key's source code)"
+ assume (%value.type is "Var") or barf "Loop expected variable, not: \(%value's source code)"
+ %lua <- (Lua "for \(%key as lua identifier),\(%value as lua identifier) in pairs(\(%iterable as lua expr)) do")
if %body has subtree % where:
(%.type = "FunctionCall") and
((%'s stub) is "do next %") and
%.value.3.value is %key.value
- ..: %body_statements +<- "\n::continue_\(%key as lua identifier)::;"
+ ..: %lua +<- (Lua "\n::continue_\(%key as lua identifier)::;")
if %body has subtree % where:
(%.type = "FunctionCall") and
((%'s stub) is "do next %") and
%.value.3.value is %value.value
- ..: %body_statements +<- "\n::continue_\(%value as lua identifier)::;"
-
- # This uses Lua's approach of only allowing loop-scoped variables in a loop
- assume (%key.type is "Var") or barf "Loop expected variable, not: \(%key's source code)"
- assume (%value.type is "Var") or barf "Loop expected variable, not: \(%value's source code)"
- %code <- ".."
- for \(%key as lua expr),\(%value as lua expr) in pairs(\(%iterable as lua expr)) do
- \%body_statements
- end --foreach-loop
+ ..: %lua +<- (Lua "\n::continue_\(%value as lua identifier)::;")
+ %lua +<- (Lua "\n\(%body as lua statements)\nend --foreach-loop")
%stop_labels <- ""
if %body has subtree % where:
@@ -262,11 +233,12 @@ immediately:
..: %stop_labels +<- "\n::stop_\(%value as lua identifier)::;"
if: %stop_labels is not ""
- %code <- ".."
- do -- scope for stopping for % = % loop
- \%code\%stop_labels
- end
- return {statements:%code, locals:%body_lua.locals}
+ %lua <-
+ Lua ".."
+ do -- scope for stopping for % = % loop
+ \%lua\%stop_labels
+ end
+ return %lua
# Switch statement/multi-branch if
immediately:
@@ -279,11 +251,11 @@ immediately:
for %func_call in %body.value:
assume (%func_call.type is "FunctionCall") or barf ".."
Invalid format for 'when' statement. Only '*' blocks are allowed.
- with [..]
- %tokens <- %func_call.value
- %star <- (1st in %tokens)
- %condition <- (2nd in %tokens)
- %action <- (3rd in %tokens)
+ with {..}
+ %tokens: %func_call.value
+ %star: %tokens.1
+ %condition: %tokens.2
+ %action: %tokens.3
..:
assume (=lua "\%star and \%star.type == 'Word' and \%star.value == '*'") or barf ".."
Invalid format for 'when' statement. Lines must begin with '*'
@@ -334,7 +306,7 @@ immediately:
assume (%func_call.type is "FunctionCall") or barf ".."
Invalid format for 'when' statement. Only '*' blocks are allowed.
%tokens <- %func_call.value
- with [%star<-(1st in %tokens), %condition<-(2nd in %tokens), %action<-(3rd in %tokens)]:
+ with {%star:%tokens.1, %condition:%tokens.2, %action:%tokens.3}:
assume (=lua "\%star and \%star.type == 'Word' and \%star.value == '*'") or barf ".."
Invalid format for 'when' statement. Lines must begin with '*'
assume %condition or barf ".."
diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom
index 8b1546b..9c7f166 100644
--- a/core/metaprogramming.nom
+++ b/core/metaprogramming.nom
@@ -9,7 +9,7 @@ immediately:
local lua = Lua(tree.source, "nomsu:define_compile_action(");
local stubs = {};
for i, action in ipairs(\%actions.value) do
- stubs[i] = nomsu:tree_to_named_stub(action);
+ stubs[i] = action:get_stub(true);
end
lua:append(repr(stubs), ", ", repr(tree.source:get_line()), ", function(tree");
local args = {};
@@ -23,7 +23,8 @@ immediately:
lua:append(", ");
lua:append(arg);
end
- local body_lua = nomsu:tree_to_lua(\%lua):as_statements("return ");
+ local body_lua = \%lua:as_lua(nomsu);
+ body_lua:convert_to_statements("return ");
body_lua:declare_locals(args);
lua:append(")\\n ", body_lua, "\\nend);")
return lua;
@@ -36,7 +37,7 @@ immediately:
local lua = Lua(tree.source, "nomsu:define_action(");
local stubs = {};
for i, action in ipairs(\%actions.value) do
- stubs[i] = nomsu:tree_to_named_stub(action);
+ stubs[i] = action:get_stub(true);
end
lua:append(repr(stubs), ", ", repr(tree.source:get_line()), ", function(");
local args = {};
@@ -47,7 +48,8 @@ immediately:
lua:append(arg);
if i < #args then lua:append(", ") end
end
- local body_lua = nomsu:tree_to_lua(\%body):as_statements("return ");
+ local body_lua = \%body:as_lua(nomsu);
+ body_lua:convert_to_statements("return ");
body_lua:declare_locals(args);
lua:append(")\\n ", body_lua, "\\nend);")
return lua;
@@ -59,7 +61,7 @@ immediately:
local lua = Lua(tree.source, "nomsu:define_compile_action(");
local stubs = {};
for i, action in ipairs(\%shorthand.value) do
- stubs[i] = nomsu:tree_to_named_stub(action);
+ stubs[i] = action:get_stub(true);
end
lua:append(repr(stubs), ", ", repr(tree.source:get_line()), ", function(tree");
local args = {};
@@ -84,7 +86,7 @@ immediately:
lua:append([[)
local template = nomsu:parse(]]..template..[[, ]]..repr(tree.source.filename)..[[);
local replacement = nomsu:tree_with_replaced_vars(template, ]]..replacements..[[);
- return nomsu:tree_to_lua(replacement, nomsu.compilestack[#nomsu.compilestack].source.filename);
+ return replacement:as_lua(nomsu);
end);
]]);
return lua;
@@ -101,11 +103,11 @@ action [remove action %stub]:
immediately:
action [%tree as lua]:
- =lua "nomsu:tree_to_lua(\%tree)"
+ =lua "\%tree:as_lua(nomsu)"
action [%tree as lua expr]:
lua> ".."
- local lua = nomsu:tree_to_lua(\%tree);
+ local lua = \%tree:as_lua(nomsu);
if not lua.is_value then
error("Invalid thing to convert to lua expr: "..\%tree.source:get_text());
end
@@ -113,7 +115,8 @@ immediately:
action [%tree as lua statements]:
lua> ".."
- local lua = nomsu:tree_to_lua(\%tree):as_statements();
+ local lua = \%tree:as_lua(nomsu);
+ lua:convert_to_statements();
lua:declare_locals();
return lua;
@@ -124,14 +127,17 @@ immediately:
=lua "nomsu:tree_to_stub(\%tree)"
immediately:
- compile [%tree's source code, %tree' source code] to: LuaValue "\(%tree as lua expr).source:get_text()"
+ parse [%var write code %code] as: lua> "\%var:append(\%code);"
- compile [repr %obj] to: LuaValue "repr(\(%obj as lua expr))"
- compile [type of %obj] to: LuaValue "type(\(%obj as lua expr))"
+immediately:
+ compile [%tree's source code, %tree' source code] to: Lua value "\(%tree as lua expr).source:get_text()"
+
+ compile [repr %obj] to: Lua value "repr(\(%obj as lua expr))"
+ compile [type of %obj] to: Lua value "type(\(%obj as lua expr))"
immediately:
- compile [nomsu] to: LuaValue "nomsu"
- compile [%var as lua identifier] to: LuaValue "nomsu:var_to_lua_identifier(\(%var as lua expr))"
+ compile [nomsu] to: Lua value "nomsu"
+ compile [%var as lua identifier] to: Lua value "nomsu:var_to_lua_identifier(\(%var as lua expr))"
action [action %names metadata]:
=lua "nomsu.action_metadata[ACTIONS[\%names]]"
@@ -149,7 +155,7 @@ action [help %action]:
# Compiler tools
immediately:
compile [run %code] to:
- LuaValue ".."
+ Lua value ".."
nomsu:run(\(%code as lua expr), '\
=lua "nomsu:get_line_number(nomsu.compilestack[#nomsu.compilestack])"
..')
@@ -159,7 +165,7 @@ immediately:
immediately:
compile [show lua %block] to:
lua> ".."
- local \%lua = nomsu:tree_to_lua(\%block);
+ local \%lua = \%block:as_lua(nomsu);
return Lua(\%block.source, "print(", repr(tostring(\%lua)), ");");
immediately:
@@ -172,6 +178,13 @@ immediately:
return Lua(tree.source, "print(stringify(", \(%message as lua expr), "));");
end
+immediately:
+ compile [source] to: Lua value (=lua "tree.source") "tree.source"
+
+immediately:
+ action [Lua %]: Lua (=lua "nomsu.compilestack[#nomsu.compilestack]") %
+ action [Lua value %]: Lua value (=lua "nomsu.compilestack[#nomsu.compilestack]") %
+
# Return
immediately:
#.. Return statement is wrapped in a do..end block because Lua is unhappy if you
@@ -181,8 +194,8 @@ immediately:
# Error functions
immediately:
- compile [barf] to: LuaValue "error(nil, 0);"
- compile [barf %msg] to: LuaValue "error(\(%msg as lua expr), 0);"
+ compile [barf] to: Lua value "error(nil, 0);"
+ compile [barf %msg] to: Lua value "error(\(%msg as lua expr), 0);"
compile [assume %condition] to:
lua> "local \%assumption = 'Assumption failed: '..\%condition.source:get_text();"
return:
@@ -200,7 +213,7 @@ immediately:
# Literals
immediately:
- compile [yes] to: LuaValue "true"
- compile [no] to: LuaValue "false"
- compile [nothing, nil, null] to: LuaValue "nil"
+ compile [yes] to: Lua value "true"
+ compile [no] to: Lua value "false"
+ compile [nothing, nil, null] to: Lua value "nil"
diff --git a/core/operators.nom b/core/operators.nom
index 358962d..a6d9977 100644
--- a/core/operators.nom
+++ b/core/operators.nom
@@ -18,31 +18,31 @@ immediately:
# Comparison Operators
immediately:
- compile [%x < %y] to: LuaValue "(\(%x as lua expr) < \(%y as lua expr))"
- compile [%x > %y] to: LuaValue "(\(%x as lua expr) > \(%y as lua expr))"
- compile [%x <= %y] to: LuaValue "(\(%x as lua expr) <= \(%y as lua expr))"
- compile [%x >= %y] to: LuaValue "(\(%x as lua expr) >= \(%y as lua expr))"
+ compile [%x < %y] to: Lua value "(\(%x as lua expr) < \(%y as lua expr))"
+ compile [%x > %y] to: Lua value "(\(%x as lua expr) > \(%y as lua expr))"
+ compile [%x <= %y] to: Lua value "(\(%x as lua expr) <= \(%y as lua expr))"
+ compile [%x >= %y] to: Lua value "(\(%x as lua expr) >= \(%y as lua expr))"
# TODO: optimize case of [%x,%y] = [1,2]
compile [%a is %b, %a = %b, %a == %b] to:
lua> ".."
local safe = {Text=true, Number=true};
local a_lua, b_lua = nomsu:tree_to_lua(\%a), nomsu:tree_to_lua(\%b);
if safe[\%a.type] or safe[\%b.type] then
- return LuaValue(tree.source, "(", a_lua, " == ", b_lua, ")");
+ return Lua.Value(tree.source, "(", a_lua, " == ", b_lua, ")");
else
- return LuaValue(tree.source, "utils.equivalent(", a_lua, ", ", b_lua, ")");
+ return Lua.Value(tree.source, "utils.equivalent(", a_lua, ", ", b_lua, ")");
end
compile [%a isn't %b, %a is not %b, %a not= %b, %a != %b] to:
lua> ".."
local safe = {Text=true, Number=true};
local a_lua, b_lua = nomsu:tree_to_lua(\%a), nomsu:tree_to_lua(\%b);
if safe[\%a.type] or safe[\%b.type] then
- return LuaValue(tree.source, "(", a_lua, " ~= ", b_lua, ")");
+ return Lua.Value(tree.source, "(", a_lua, " ~= ", b_lua, ")");
else
- return LuaValue(tree.source, "(not utils.equivalent(", a_lua, ", ", b_lua, "))");
+ return Lua.Value(tree.source, "(not utils.equivalent(", a_lua, ", ", b_lua, "))");
end
# For strict identity checking, use (%x's id) is (%y's id)
- compile [%'s id, id of %] to: LuaValue "nomsu.ids[\(% as lua expr)]"
+ compile [%'s id, id of %] to: Lua value "nomsu.ids[\(% as lua expr)]"
# Variable assignment operator
immediately:
@@ -72,7 +72,7 @@ immediately:
local value_lua = nomsu:tree_to_lua(value);
if not value_lua.is_value then error("Invalid value for assignment: "..value:get_src()); end
if target.type == "Var" then
- lhs:add_free_var(nomsu:var_to_lua_identifier(target.value));
+ lhs:add_free_vars(nomsu:var_to_lua_identifier(target.value));
end
if i > 1 then
lhs:append(", ");
@@ -104,12 +104,12 @@ immediately:
immediately:
# Math Operators
- compile [%x + %y] to: LuaValue "(\(%x as lua expr) + \(%y as lua expr))"
- compile [%x - %y] to: LuaValue "(\(%x as lua expr) - \(%y as lua expr))"
- compile [%x * %y] to: LuaValue "(\(%x as lua expr) * \(%y as lua expr))"
- compile [%x / %y] to: LuaValue "(\(%x as lua expr) / \(%y as lua expr))"
- compile [%x ^ %y] to: LuaValue "(\(%x as lua expr) ^ \(%y as lua expr))"
- compile [%x wrapped around %y, %x mod %y] to: LuaValue "(\(%x as lua expr) % \(%y as lua expr))"
+ compile [%x + %y] to: Lua value "(\(%x as lua expr) + \(%y as lua expr))"
+ compile [%x - %y] to: Lua value "(\(%x as lua expr) - \(%y as lua expr))"
+ compile [%x * %y] to: Lua value "(\(%x as lua expr) * \(%y as lua expr))"
+ compile [%x / %y] to: Lua value "(\(%x as lua expr) / \(%y as lua expr))"
+ compile [%x ^ %y] to: Lua value "(\(%x as lua expr) ^ \(%y as lua expr))"
+ compile [%x wrapped around %y, %x mod %y] to: Lua value "(\(%x as lua expr) % \(%y as lua expr))"
# 3-part chained comparisons
# (uses a lambda to avoid re-evaluating middle value, while still being an expression)
@@ -124,22 +124,22 @@ immediately:
# TODO: optimize for common case where x,y,z are all either variables or number literals
# Boolean Operators
- compile [%x and %y] to: LuaValue "(\(%x as lua expr) and \(%y as lua expr))"
- compile [%x or %y] to: LuaValue "(\(%x as lua expr) or \(%y as lua expr))"
+ compile [%x and %y] to: Lua value "(\(%x as lua expr) and \(%y as lua expr))"
+ compile [%x or %y] to: Lua value "(\(%x as lua expr) or \(%y as lua expr))"
# Bitwise Operators
- compile [%a OR %b, %a | %b] to: LuaValue "bit32.bor(\(%a as lua expr), \(%b as lua expr))"
- compile [%a XOR %b] to: LuaValue "bit32.bxor(\(%a as lua expr), \(%b as lua expr))"
- compile [%a AND %b, %a & %b] to: LuaValue "bit32.band(\(%a as lua expr), \(%b as lua expr))"
- compile [NOT %, ~ %] to: LuaValue "bit32.bnot(\(% as lua expr))"
- compile [%x LSHIFT %shift, %x << %shift] to: LuaValue "bit32.lshift(\(%x as lua expr), \(%shift as lua expr))"
- compile [%x RSHIFT %shift, %x >>> %shift] to: LuaValue "bit32.rshift(\(%x as lua expr), \(%shift as lua expr))"
- compile [%x ARSHIFT %shift, %x >> %shift] to: LuaValue "bit32.arshift(\(%x as lua expr), \(%shift as lua expr))"
+ compile [%a OR %b, %a | %b] to: Lua value "bit32.bor(\(%a as lua expr), \(%b as lua expr))"
+ compile [%a XOR %b] to: Lua value "bit32.bxor(\(%a as lua expr), \(%b as lua expr))"
+ compile [%a AND %b, %a & %b] to: Lua value "bit32.band(\(%a as lua expr), \(%b as lua expr))"
+ compile [NOT %, ~ %] to: Lua value "bit32.bnot(\(% as lua expr))"
+ compile [%x LSHIFT %shift, %x << %shift] to: Lua value "bit32.lshift(\(%x as lua expr), \(%shift as lua expr))"
+ compile [%x RSHIFT %shift, %x >>> %shift] to: Lua value "bit32.rshift(\(%x as lua expr), \(%shift as lua expr))"
+ compile [%x ARSHIFT %shift, %x >> %shift] to: Lua value "bit32.arshift(\(%x as lua expr), \(%shift as lua expr))"
# TODO: implement OR, XOR, AND for multiple operands?
# Unary operators
- compile [- %] to: LuaValue "(- \(% as lua expr))"
- compile [not %] to: LuaValue "(not \(% as lua expr))"
+ compile [- %] to: Lua value "(- \(% as lua expr))"
+ compile [not %] to: Lua value "(not \(% as lua expr))"
# Update operators
immediately: