aboutsummaryrefslogtreecommitdiff
path: root/lib/operators.nom
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2018-01-26 20:20:12 -0800
committerBruce Hill <bitbucket@bruce-hill.com>2018-01-26 20:20:38 -0800
commit90c56d31352a0eeccd382ef5921baf3af4971040 (patch)
tree5167eafb5785c94b48458b18b0454222ca70c749 /lib/operators.nom
parentd5aa4e52983712f9f4c5b23528d0c2dab12b0b33 (diff)
Added a ton of tests for virtually all the functionality. Helped me find
and fix a lot of latent problems.
Diffstat (limited to 'lib/operators.nom')
-rw-r--r--lib/operators.nom69
1 files changed, 47 insertions, 22 deletions
diff --git a/lib/operators.nom b/lib/operators.nom
index 8be0ea0..670e7b7 100644
--- a/lib/operators.nom
+++ b/lib/operators.nom
@@ -36,6 +36,7 @@ immediately
compile [%x > %y] to {expr:"(\(%x as lua expr) > \(%y as lua expr))"}
compile [%x <= %y] to {expr:"(\(%x as lua expr) <= \(%y as lua expr))"}
compile [%x >= %y] to {expr:"(\(%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};
@@ -69,6 +70,30 @@ immediately
locals: =lua "(\%var.type == 'Var' and {\%var_lua.expr} or nil)"
immediately
+ # Simultaneous mutli-assignments like: x,y,z = 1,x,3;
+ compile [<- %assignments] to
+ %locals <- []
+ %targets <- []
+ %values <- []
+ assume ((%assignments' "type") is "Dict") or barf ".."
+ Expected a Dict for the assignments part of '<- %' statement, not \(%assignments' source code)
+ lua> ".."
+ for i, item in ipairs(\%assignments.value) do
+ local target, value = item.dict_key, item.dict_value;
+ local target_lua = nomsu:tree_to_lua(target);
+ if not target_lua.expr then error("Invalid target for assignment: "..target:get_src()); end
+ local value_lua = nomsu:tree_to_lua(value);
+ if not value_lua.expr then error("Invalid value for assignment: "..value:get_src()); end
+ if target.type == "Var" then
+ table.insert(\%locals, target_lua.expr);
+ end
+ table.insert(\%targets, target_lua.expr);
+ table.insert(\%values, value_lua.expr);
+ end
+ utils.deduplicate(\%locals);
+ return {locals=\%locals, statements=(table.concat(\%targets, ", ").." = "..table.concat(\%values, ", ")..";")};
+
+immediately
compile [export %var <- %value] to
%var_lua <- (%var as lua)
assume (%var_lua's "expr") or barf "Invalid target for assignment: \(%var's source code)"
@@ -76,6 +101,21 @@ immediately
assume (%value_lua's "expr") or barf "Invalid value for assignment: \(%value's source code)"
return {statements:"\(%var_lua's "expr") = \(%value_lua's "expr");"}
+ compile [exporting %exported %body] to
+ %body_lua <- (%body as lua)
+ %leftover_locals <- (=lua "{unpack(\%body_lua.locals or {})}")
+ assume ((%exported's "type") = "List") or barf ".."
+ Expected a List for the export part of 'exporting' statement, not \(%exported's source code)
+ lua> ".."
+ for i, item in ipairs(\%exported.value) do
+ if item.type ~= "Var" then
+ error("'exporting' statement expects Vars, not: "..item:get_src());
+ end
+ local var = nomsu:tree_to_lua(item).expr;
+ utils.remove_from_list(\%leftover_locals, var);
+ end
+ return {locals:%leftover_locals, statements:=lua "\%body_lua.statements or (\%body_lua.expr..';')"}
+
compile [with %assignments %body] to
%body_lua <- (%body as lua)
%locals <- []
@@ -118,21 +158,6 @@ immediately
%s
end]]):format(locals_code, declaration_code, \%body_lua.statements or (\%body_lua.expr..";"))};
- compile [exporting %exported %body] to
- %body_lua <- (%body as lua)
- %leftover_locals <- (=lua "{unpack(\%body_lua.locals or {})}")
- assume ((%exported's "type") = "List") or barf ".."
- Expected a List for the export part of 'exporting' statement, not \(%exported's source code)
- lua> ".."
- for i, item in ipairs(\%exported.value) do
- if item.type ~= "Var" then
- error("'exporting' statement expects Vars, not: "..item:get_src());
- end
- local var = nomsu:tree_to_lua(item).expr;
- utils.remove_from_list(leftover_locals, var);
- end
- return {locals:%leftover_locals, statements:=lua "\%body_lua.statements or (\%body_lua.expr..';')"}
-
immediately
# Math Operators
compile [%x + %y] to {expr:"(\(%x as lua expr) + \(%y as lua expr))"}
@@ -174,11 +199,11 @@ immediately
# Update operators
immediately
- parse [<- %var + %] as: %var <- (%var + %)
- parse [<- %var - %] as: %var <- (%var - %)
- parse [<- %var * %] as: %var <- (%var * %)
- parse [<- %var / %] as: %var <- (%var / %)
- parse [<- %var ^ %] as: %var <- (%var ^ %)
- parse [<- %var and %] as: %var <- (%var and %)
- parse [<- %var or %] as: %var <- (%var or %)
+ parse [%var + <- %, %var +<- %] as: %var <- (%var + %)
+ parse [%var - <- %, %var -<- %] as: %var <- (%var - %)
+ parse [%var * <- %, %var *<- %] as: %var <- (%var * %)
+ parse [%var / <- %, %var /<- %] as: %var <- (%var / %)
+ parse [%var ^ <- %, %var ^<- %] as: %var <- (%var ^ %)
+ parse [%var and <- %] as: %var <- (%var and %)
+ parse [%var or <- %] as: %var <- (%var or %)
parse [wrap %var around %] as: %var <- (%var wrapped around %)