aboutsummaryrefslogtreecommitdiff
path: root/lib/control_flow.nom
diff options
context:
space:
mode:
Diffstat (limited to 'lib/control_flow.nom')
-rw-r--r--lib/control_flow.nom66
1 files changed, 33 insertions, 33 deletions
diff --git a/lib/control_flow.nom b/lib/control_flow.nom
index 5adcbd5..d06de4f 100644
--- a/lib/control_flow.nom
+++ b/lib/control_flow.nom
@@ -55,9 +55,9 @@ immediately:
compile [do next repeat-loop] to code: "goto continue_repeat;"
compile [stop repeat-loop] to code: "goto stop_repeat;"
compile [repeat while %condition %body] to code:
- %continue_labels = (..)
+ set %continue_labels = (..)
"\n::continue_repeat::;" if (tree %body has function call \(do next repeat-loop)) else ""
- %code = ".."
+ set %code = ".."
while \(%condition as lua) do
\(%body as lua statements)\
..\%continue_labels
@@ -92,19 +92,19 @@ immediately:
for %var from %start to %stop via %step %body
..to code:
lua> "local \%continue_labels, \%code, \%stop_labels"
- %continue_labels = ""
+ set %continue_labels = ""
if (tree %body has function call \(do next for-loop)):
%continue_labels join= "\n::continue_for::;"
if (tree %body has function call (tree \(do next %) with {""=%var})):
%continue_labels join= "\n::continue_\(nomsu "var_to_lua_identifier" [%var])::;"
# This trashes the loop variables, just like in Python.
- %code = ".."
+ set %code = ".."
for i=\(%start as lua),\(%stop as lua),\(%step as lua) do
\(%var as lua) = i;
\(%body as lua statements)\
..\%continue_labels
end --numeric for-loop
- %stop_labels = ""
+ set %stop_labels = ""
if (tree %body has function call \(stop for-loop)):
%stop_labels join= "\n::stop_for::;"
if (tree %body has function call (tree \(stop %) with {""=%var})):
@@ -128,25 +128,25 @@ immediately:
immediately:
compile [for %var in %iterable %body] to code:
lua> "local \%continue_labels, \%stop_labels, \%code, \%stop_labels"
- %continue_labels = ""
+ set %continue_labels = ""
if (tree %body has function call \(do next for-loop)):
%continue_labels join= "\n::continue_for::;"
if (tree %body has function call (tree \(do next %) with {""=%var})):
%continue_labels join= "\n::continue_\(nomsu "var_to_lua_identifier" [%var])::;"
# This trashes the loop variables, just like in Python.
- %code = ".."
+ set %code = ".."
for i,value in ipairs(\(%iterable as lua)) do
\(%var as lua) = value;
\(%body as lua statements)\
..\%continue_labels
end --foreach-loop
- %stop_labels = ""
+ set %stop_labels = ""
if (tree %body has function call \(stop for-loop)):
%stop_labels join= "\n::stop_for::;"
if (tree %body has function call (tree \(stop %) with {""=%var})):
%stop_labels join= "\n::stop_\(nomsu "var_to_lua_identifier" [%var])::;"
if %stop_labels != "":
- %code = ".."
+ set %code = ".."
do --for-loop label scope
\%code\%stop_labels
end --for-loop label scope
@@ -156,7 +156,7 @@ immediately:
# Dict iteration (lua's "pairs()")
immediately:
compile [for %key = %value in %iterable %body] to code:
- %continue_labels = ""
+ set %continue_labels = ""
if (tree %body has function call \(do next for-loop)):
%continue_labels join= "\n::continue_for::;"
if (tree %body has function call (tree \(do next %x) with {x=%key})):
@@ -164,13 +164,13 @@ immediately:
if (tree %body has function call (tree \(do next %) with {""=%value})):
%continue_labels join= "\n::continue_\(nomsu "var_to_lua_identifier" [%value])::;"
# This trashes the loop variables, just like in Python.
- %code = ".."
+ set %code = ".."
for key,value in ipairs(\(%iterable as lua)) do
\(%key as lua), \(%value as lua) = key, value;
\(%body as lua statements)\
..\%continue_labels
end --foreach-loop
- %stop_labels = ""
+ set %stop_labels = ""
if (tree %body has function call \(stop for-loop)):
%stop_labels join= "\n::stop_for::;"
if (tree %body has function call (tree \(stop %) with {""=%key})):
@@ -189,23 +189,23 @@ immediately:
immediately:
compile [when %body] to code:
lua> "local \%result, \%fallthroughs, \%first"
- %result = ""
- %fallthroughs = []
- %first = (yes)
+ set %result = ""
+ set %fallthroughs = []
+ set %first = (yes)
for %func_call in (%body's "value"):
lua> "local \%tokens, \%star, \%condition, \%action"
assert ((%func_call's "type") == "FunctionCall") ".."
Invalid format for 'when' statement. Only '*' blocks are allowed.
- %tokens = (%func_call's "value")
- %star = (%tokens -> 1)
+ set %tokens = (%func_call's "value")
+ set %star = (%tokens -> 1)
assert (=lua "\%star and \%star.type == 'Word' and \%star.value == '*'") ".."
Invalid format for 'when' statement. Lines must begin with '*'
- %condition = (%tokens -> 2)
+ set %condition = (%tokens -> 2)
assert %condition ".."
Invalid format for 'when' statement. Lines must begin with '*' and have a condition or the word "else"
- %action = (%tokens -> 3)
+ set %action = (%tokens -> 3)
if (%action == (nil)):
lua do> "table.insert(\%fallthroughs, \%condition)"
do next %func_call
@@ -217,7 +217,7 @@ immediately:
\(%action as lua statements)
stop for-loop
..else:
- %condition = (%condition as lua)
+ set %condition = (%condition as lua)
for all %fallthroughs:
%condition join= " or \(% as lua)"
%result join= ".."
@@ -225,8 +225,8 @@ immediately:
\("if" if %first else "elseif") \%condition then
\(%action as lua statements)
- %fallthroughs = []
- %first = (no)
+ set %fallthroughs = []
+ set %first = (no)
if (%result != ""):
%result join= "\nend"
@@ -235,22 +235,22 @@ immediately:
# Switch statement
immediately:
compile [when %branch_value == ? %body] to code:
- %result = ""
- %fallthroughs = []
- %first = (yes)
+ set %result = ""
+ set %fallthroughs = []
+ set %first = (yes)
for %func_call in (%body's "value"):
assert ((%func_call's "type") == "FunctionCall") ".."
Invalid format for 'when' statement. Only '*' blocks are allowed.
- %tokens = (%func_call's "value")
- %star = (%tokens -> 1)
+ set %tokens = (%func_call's "value")
+ set %star = (%tokens -> 1)
assert (=lua "\%star and \%star.type == 'Word' and \%star.value == '*'") ".."
Invalid format for 'when' statement. Lines must begin with '*'
- %condition = (%tokens -> 2)
+ set %condition = (%tokens -> 2)
assert %condition ".."
Invalid format for 'when' statement. Lines must begin with '*' and have a condition or the word "else"
- %action = (%tokens -> 3)
+ set %action = (%tokens -> 3)
if (%action == (nil)):
lua> "table.insert(\%fallthroughs, \%condition)"
do next %func_call
@@ -262,7 +262,7 @@ immediately:
\(%action as lua statements)
stop for-loop
..else:
- %condition = "branch_value == (\(%condition as lua))"
+ set %condition = "branch_value == (\(%condition as lua))"
for all %fallthroughs:
%condition join= " or (branch_value == \(% as lua))"
%result join= ".."
@@ -270,11 +270,11 @@ immediately:
\("if" if %first else "elseif") \%condition then
\(%action as lua statements)
- %fallthroughs = []
- %first = (no)
+ set %fallthroughs = []
+ set %first = (no)
if (%result != ""):
- %result = ".."
+ set %result = ".."
do --when == ?
local branch_value = \(%branch_value as lua);\
..\%result