aboutsummaryrefslogtreecommitdiff
path: root/core/control_flow.nom
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2018-04-25 16:30:49 -0700
committerBruce Hill <bitbucket@bruce-hill.com>2018-04-25 16:31:09 -0700
commit5d59d510cd6fdc8553250e5ec10f19a285e8878c (patch)
tree69d9a2646ed59746500fc9c62a04294a1019d381 /core/control_flow.nom
parent50a092e4b5cb82131c0c8f6f46c7d21a42198858 (diff)
Removing end-of-line ":" and "(..)" for blocks (they just use
indentation now).
Diffstat (limited to 'core/control_flow.nom')
-rw-r--r--core/control_flow.nom126
1 files changed, 63 insertions, 63 deletions
diff --git a/core/control_flow.nom b/core/control_flow.nom
index 35e5fb5..b51ecdf 100644
--- a/core/control_flow.nom
+++ b/core/control_flow.nom
@@ -7,19 +7,19 @@ use "core/text.nom"
use "core/operators.nom"
# No-Op
-immediately:
+immediately
compile [do nothing] to: Lua ""
# Conditionals
-immediately:
- compile [if %condition %if_body] to:
+immediately
+ compile [if %condition %if_body] to
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:
+ compile [if %condition %if_body else %else_body, unless %condition %else_body else %if_body] to
Lua ".."
if \(%condition as lua expr) then
\(%if_body as lua statements)
@@ -36,18 +36,18 @@ immediately
%when_true_expr if %condition otherwise %when_false_expr
%when_false_expr unless %condition else %when_true_expr
%when_false_expr unless %condition then %when_true_expr
- ..to:
+ ..to
#.. 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:
+ return
Lua value ".."
(\(%condition as lua expr) and \(%when_true_expr as lua expr) or \(%when_false_expr as lua expr))
- ..else:
+ ..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:
+ return
Lua value ".."
(function()
if \(%condition as lua expr) then
@@ -58,20 +58,20 @@ immediately
end)()
# GOTOs
-immediately:
+immediately
compile [=== %label ===, --- %label ---, *** %label ***] to
Lua "::label_\(%label as lua identifier)::;"
compile [go to %label] to
Lua "goto label_\(%label as lua identifier);"
# Basic loop control
-immediately:
+immediately
compile [do next] to: Lua "continue;"
compile [stop] to: Lua "break;"
# Helper function
-immediately:
- compile [if %tree has subtree %subtree where %condition %body] to:
+immediately
+ compile [if %tree has subtree %subtree where %condition %body] to
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
@@ -83,7 +83,7 @@ immediately:
end
# While loops
-immediately:
+immediately
compile [do next repeat] to: Lua "goto continue_repeat;"
compile [stop repeating] to: Lua "goto stop_repeat;"
compile [repeat while %condition %body] to
@@ -91,14 +91,14 @@ immediately:
Lua ".."
while \(%condition as lua expr) do
\(%body as lua statements)
- if %body has subtree % where:
+ if %body has subtree % where
(%.type = "Action") and ((%'s stub) is "do next repeat")
- ..:
+ ..
to %lua write "\n ::continue_repeat::;"
to %lua write "\nend --while-loop"
- if %body has subtree % where:
+ if %body has subtree % where
(%.type = "Action") and ((%'s stub) is "stop repeating")
- ..:
+ ..
%lua <-
Lua ".."
do -- scope of "stop repeating" label
@@ -111,7 +111,7 @@ immediately:
compile [..]
repeat %n times %body
- ..to:
+ ..to
%lua <-
Lua ".."
for i=1,\(%n as lua expr) do
@@ -120,10 +120,10 @@ immediately:
(%.type = "Action") and ((%'s stub) is "do next repeat")
..: to %lua write "\n ::continue_repeat::;"
to %lua write "\nend --numeric for-loop"
- if %body has subtree % where:
+ if %body has subtree % where
(%.type = "Action") and ((%'s stub) is "stop repeating")
- ..:
- %lua <-:
+ ..
+ %lua <-
Lua ".."
do -- scope of "stop repeating" label
\%lua
@@ -131,37 +131,37 @@ immediately:
end -- end of "stop repeating" label scope
return %lua
-# For loop control flow:
-immediately:
+# For loop control flow
+immediately
compile [stop %var] to
Lua "goto stop_\(%var as lua identifier);"
compile [do next %var] to
Lua "goto continue_\(%var as lua identifier);"
# Numeric range for loops
-immediately:
+immediately
compile [..]
for %var from %start to %stop by %step %body
for %var from %start to %stop via %step %body
- ..to:
+ ..to
# 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),\(%stop as lua expr),\(%step as lua expr) do
\(%body as lua statements)
- if %body has subtree % where:
+ if %body has subtree % where
(%.type = "Action") and
((%'s stub) is "do next %") and
%.value.3.value is %var.value
..: to %lua write "\n ::continue_\(%var as lua identifier)::;"
to %lua write "\nend --numeric for-loop"
- if %body has subtree % where:
- (%.type = "Action") and:
- ((%'s stub) is "stop %") and:
+ if %body has subtree % where
+ (%.type = "Action") and
+ ((%'s stub) is "stop %") and
%.value.2.value is %var.value
- ..:
+ ..
%lua <-
Lua ".."
do -- scope for stopping for-loop
@@ -171,7 +171,7 @@ immediately:
return %lua
-immediately:
+immediately
parse [for %var from %start to %stop %body] as: for %var from %start to %stop via 1 %body
parse [..]
for all %start to %stop by %step %body
@@ -180,25 +180,25 @@ immediately:
parse [for all %start to %stop %body] as: for all %start to %stop via 1 %body
# For-each loop (lua's "ipairs()")
-immediately:
- compile [for %var in %iterable %body] to:
+immediately
+ compile [for %var in %iterable %body] to
# 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
\(%body as lua statements)
- if %body has subtree % where:
+ if %body has subtree % where
(%.type = "Action") and
((%'s stub) is "do next %") and
%.value.3.value is %var.value
..: to %lua write (Lua "\n ::continue_\(%var as lua identifier)::;")
to %lua write "\nend --foreach-loop"
- if %body has subtree % where:
+ if %body has subtree % where
(%.type = "Action") and
((%'s stub) is "stop %") and
%.value.2.value is %var.value
- ..:
+ ..
%lua <-
Lua ".."
do -- scope for stopping for-loop
@@ -210,8 +210,8 @@ immediately:
parse [for all %iterable %body] as: for % in %iterable %body
# Dict iteration (lua's "pairs()")
-immediately:
- compile [for %key = %value in %iterable %body] to:
+immediately
+ compile [for %key = %value in %iterable %body] to
# 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)"
@@ -219,13 +219,13 @@ immediately:
Lua ".."
for \(%key as lua identifier),\(%value as lua identifier) in pairs(\(%iterable as lua expr)) do
\(%body as lua statements)
- if %body has subtree % where:
+ if %body has subtree % where
(%.type = "Action") and
((%'s stub) is "do next %") and
%.value.3.value is %key.value
..: to %lua write (Lua "\n ::continue_\(%key as lua identifier)::;")
- if %body has subtree % where:
+ if %body has subtree % where
(%.type = "Action") and
((%'s stub) is "do next %") and
%.value.3.value is %value.value
@@ -233,13 +233,13 @@ immediately:
to %lua write "\nend --foreach-loop"
%stop_labels <- (Lua "")
- if %body has subtree % where:
+ if %body has subtree % where
(%.type = "Action") and
((%'s stub) is "stop %") and
%.value.2.value is %key.value
..: to %stop_labels write "\n::stop_\(%key as lua identifier)::;"
- if %body has subtree % where:
+ if %body has subtree % where
(%.type = "Action") and
((%'s stub) is "stop %") and
%.value.2.value is %value.value
@@ -255,13 +255,13 @@ immediately:
return %lua
# Switch statement/multi-branch if
-immediately:
- compile [when %body] to:
+immediately
+ compile [when %body] to
%code <- (Lua "")
%fallthroughs <- []
%is_first <- (yes)
%seen_else <- (no)
- for %func_call in %body.value:
+ for %func_call in %body.value
assume (%func_call.type is "Action") or barf ".."
Invalid format for 'when' statement. Only '*' blocks are allowed.
%tokens <- %func_call.value
@@ -269,7 +269,7 @@ immediately:
%star: %tokens.1
%condition: %tokens.2
%action: %tokens.3
- ..:
+ ..
assume ((%star and (%star.type is "Word")) and (%star.value is "*")) or barf ".."
Invalid format for 'when' statement. Lines must begin with '*'
assume %condition or barf ".."
@@ -283,11 +283,11 @@ immediately:
to %code write "\nelse\n "
to %code write: %action as lua statements
%seen_else <- (yes)
- ..else:
+ ..else
assume (not %seen_else) or barf "'else' clause needs to be last in 'when' block"
lua> "table.insert(\%fallthroughs, \(%condition as lua expr));"
to %code write "\("if" if %is_first else "\nelseif") "
- for %i=%condition in %fallthroughs:
+ for %i=%condition in %fallthroughs
if (%i > 1): to %code write " or "
to %code write %condition
to %code write " then\n "
@@ -302,17 +302,17 @@ immediately:
return %code
# Switch statement
-immediately:
- compile [when %branch_value = ? %body, when %branch_value is ? %body] to:
+immediately
+ compile [when %branch_value = ? %body, when %branch_value is ? %body] to
%code <- (Lua "")
%fallthroughs <- []
%is_first <- (yes)
%seen_else <- (no)
- for %func_call in %body.value:
+ for %func_call in %body.value
assume (%func_call.type is "Action") or barf ".."
Invalid format for 'when' statement. Only '*' blocks are allowed.
%tokens <- %func_call.value
- with {%star:%tokens.1, %condition:%tokens.2, %action:%tokens.3}:
+ with {%star:%tokens.1, %condition:%tokens.2, %action:%tokens.3}
assume ((%star and (%star.type is "Word")) and (%star.value is "*")) or barf ".."
Invalid format for 'when' statement. Lines must begin with '*'
assume %condition or barf ".."
@@ -325,7 +325,7 @@ immediately:
assume (not %is_first) or barf "'else' clause cannot be first in 'when % = ?' block"
to %code write "\nelse\n "
to %code write: %action as lua statements
- ..else:
+ ..else
assume (not %seen_else) or barf "'else' clause needs to be last in 'when % = ?' block"
to %code write "\("if" if %is_first else "\nelseif") "
lua> "table.insert(\%fallthroughs, \(%condition as lua expr));"
@@ -334,7 +334,7 @@ immediately:
to %code write " or "
if: (%.type is "Text") or (%.type is "Number")
to %code write "branch_value == \%"
- ..else:
+ ..else
to %code write "utils.equivalent(branch_value, \%)"
to %code write "then\n "
to %code write (%action as lua statements)
@@ -354,11 +354,11 @@ immediately:
return %code
# Try/except
-immediately:
+immediately
compile [..]
try %action and if it succeeds %success or if it barfs %fallback
try %action and if it barfs %fallback or if it succeeds %success
- ..to:
+ ..to
Lua ".."
do
local fell_through = false;
@@ -375,24 +375,24 @@ immediately:
return ret;
end
end
- parse [try %action] as:
+ parse [try %action] as
try %action and if it succeeds: do nothing
..or if it barfs: do nothing
- parse [try %action and if it barfs %fallback] as:
+ parse [try %action and if it barfs %fallback] as
try %action and if it succeeds: do nothing
..or if it barfs %fallback
- parse [try %action and if it succeeds %success] as:
+ parse [try %action and if it succeeds %success] as
try %action and if it succeeds %success or if it barfs: do nothing
-# Do/finally:
-immediately:
- compile [do %action] to:
+# Do/finally
+immediately
+ compile [do %action] to
Lua ".."
do
\(%action as lua statements)
end --do
- compile [do %action then always %final_action] to:
+ compile [do %action then always %final_action] to
Lua ".."
do
local fell_through = false;