aboutsummaryrefslogtreecommitdiff
path: root/core/control_flow.nom
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2018-11-26 16:21:42 -0800
committerBruce Hill <bruce@bruce-hill.com>2018-11-26 16:22:04 -0800
commit8e5f1b9e1e912f781738a564843e024b0e0b2e96 (patch)
treed0ff38fc1f791d784ef2c968940365f04d3b6eee /core/control_flow.nom
parentcbbe6b1c14faf85c20dee4c481723cf0e468e0e8 (diff)
Simplifying the control flow API and deduplicating the code.
Diffstat (limited to 'core/control_flow.nom')
-rw-r--r--core/control_flow.nom125
1 files changed, 27 insertions, 98 deletions
diff --git a/core/control_flow.nom b/core/control_flow.nom
index f01d948..4fe94e4 100644
--- a/core/control_flow.nom
+++ b/core/control_flow.nom
@@ -87,29 +87,29 @@ test:
assume (%i == 0)
(--- %label ---) compiles to "\
- ..::label_\((%label.stub if (%label.type == "Action") else %label) as lua identifier)::"
+ ..::label_\((%label.stub::as lua id) if (%label.type == "Action") else (%label as lua identifier))::"
(go to %label) compiles to "\
- ..goto label_\((%label.stub if (%label.type == "Action") else %label) as lua identifier)"
+ ..goto label_\((%label.stub::as lua id) if (%label.type == "Action") else (%label as lua identifier))"
# Basic loop control
(stop %var) compiles to:
if %var:
- return (Lua "goto stop_\((%var.stub if (%var.type == "action") else %var) as lua identifier)")
+ return (Lua "goto stop_\(%var as lua identifier)")
..else:
return (Lua "break")
(do next %var) compiles to:
if %var:
- return (Lua "goto continue_\((%var.stub if (%var.type == "action") else %var) as lua identifier)")
+ return (Lua "goto continue_\(%var as lua identifier)")
..else:
return (Lua "goto continue")
(---stop %var ---) compiles to "\
- ..::stop_\((%var.stub if (%var.type == "action") else %var) as lua identifier)::"
+ ..::stop_\(%var as lua identifier)::"
(---next %var ---) compiles to "\
- ..::continue_\((%var.stub if (%var.type == "action") else %var) as lua identifier)::"
+ ..::continue_\(%var as lua identifier)::"
# While loops
test:
@@ -117,8 +117,6 @@ test:
repeat while (%x < 10): %x += 1
assume (%x == 10)
repeat while (%x < 20): stop
- repeat while (%x < 20):
- stop repeating
assume (%x == 10)
repeat while (%x < 20):
%x += 1
@@ -126,57 +124,14 @@ test:
do next
barf "Failed to 'do next'"
assume (%x == 20)
- repeat while (%x < 30):
- %x += 1
- if (yes):
- do next repeat
- barf "Failed to 'do next repeat'"
- assume (%x == 30)
-(do next repeat) compiles to "goto continue_repeat"
-(stop repeating) compiles to "goto stop_repeat"
(repeat while %condition %body) compiles to:
%lua = (Lua "while \(%condition as lua expr) do\n \(%body as lua)")
if (%body has subtree \(do next)):
%lua::append "\n ::continue::"
-
- if (%body has subtree \(do next repeat)):
- %lua::append "\n ::continue_repeat::"
-
%lua::append "\nend --while-loop"
- if (%body has subtree \(stop repeating)):
- %inner_lua = %lua
- %lua = (Lua "do -- scope of 'stop repeating' label\n ")
- %lua::append %inner_lua "\
- ..
- ::stop_repeat::
- end -- end of 'stop repeating' label scope"
return %lua
(repeat %body) parses as (repeat while (yes) %body)
(repeat until %condition %body) parses as (repeat while (not %condition) %body)
-test:
- %x = 0
- repeat 10 times: %x += 1
- assume (%x == 10)
-
-(repeat %n times %body) compiles to:
- define mangler
- %lua = (Lua "for \(mangle "i")=1,\(%n as lua expr) do\n ")
- %lua::append (%body as lua)
- if (%body has subtree \(do next)):
- %lua::append "\n ::continue::"
-
- if (%body has subtree \(do next repeat)):
- %lua::append "\n ::continue_repeat::"
-
- %lua::append "\nend --numeric for-loop"
- if (%body has subtree \(stop repeating)):
- %inner_lua = %lua
- %lua = (Lua "do -- scope of 'stop repeating' label\n ")
- %lua::append %inner_lua "\
- ..
- ::stop_repeat::
- end -- end of 'stop repeating' label scope"
- return %lua
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -206,22 +161,20 @@ test:
for %var in %start to %stop via %step %body
..all compile to:
# This uses Lua's approach of only allowing loop-scoped variables in a loop
- unless (%var.type is "Var"):
- compile error at %var "Expected a variable here, not a \(%var.type)"
- %lua = (Lua "for \(%var as lua expr)=\(%start as lua expr),\(%stop as lua expr),\(%step as lua expr) do")
+ %lua = (Lua "for \(%var as lua identifier)=\(%start as lua expr),\(%stop as lua expr),\(%step as lua expr) do")
%lua::append "\n " (%body as lua)
if (%body has subtree \(do next)):
%lua::append "\n ::continue::"
if (%body has subtree \(do next %var)):
- %lua::append "\n " (what (---next %var ---) compiles to)
+ %lua::append "\n " (\(---next %var ---) as lua)
%lua::append "\nend --numeric for-loop"
if (%body has subtree \(stop %var)):
%inner_lua = %lua
%lua = (Lua "do -- scope for stopping for-loop\n ")
%lua::append %inner_lua "\n "
- %lua::append (what (---stop %var ---) compiles to)
+ %lua::append (\(---stop %var ---) as lua)
%lua::append "\nend -- end of scope for stopping for-loop"
return %lua
@@ -231,6 +184,12 @@ test:
for %var in %start to %stop via 1 %body
test:
+ %x = 0
+ repeat 5 times: %x += 1
+ assume %x == 5
+(repeat %n times %body) parses as (for (=lua "_XXX_") in 1 to %n %body)
+
+test:
%a = [10, 20, 30, 40, 50]
%b = []
for %x in %a:
@@ -248,31 +207,6 @@ test:
assume (%b == [20, 30, 40])
# For-each loop (lua's "ipairs()")
-(for %var in %iterable %body) compiles to:
- define mangler
-
- # This uses Lua's approach of only allowing loop-scoped variables in a loop
- %lua = (..)
- Lua "\
- ..for \(mangle "i"),\(%var as lua identifier) in ipairs(\(%iterable as lua expr)) do
- "
- %lua::append (%body as lua)
- if (%body has subtree \(do next)):
- %lua::append "\n ::continue::"
-
- if (%body has subtree \(do next %var)):
- %lua::append "\n " (what (---next %var ---) compiles to)
-
- %lua::append "\nend --foreach-loop"
- if (%body has subtree \(stop %var)):
- %inner_lua = %lua
- %lua = (Lua "do -- scope for stopping for-loop\n ")
- %lua::append %inner_lua "\n "
- %lua::append (what (---stop %var ---) compiles to)
- %lua::append "\nend -- end of scope for stopping for-loop"
- return %lua
-
-# TODO: reduce code duplication
(for %var in %iterable at %i %body) compiles to:
# This uses Lua's approach of only allowing loop-scoped variables in a loop
%lua = (..)
@@ -284,17 +218,19 @@ test:
%lua::append "\n ::continue::"
if (%body has subtree \(do next %var)):
- %lua::append "\n " (what (---next %var ---) compiles to)
+ %lua::append "\n " (\(---next %var ---) as lua)
- %lua::append "\nend --foreach-loop"
+ %lua::append "\nend --for \(%var as lua identifier) loop"
if (%body has subtree \(stop %var)):
%inner_lua = %lua
%lua = (Lua "do -- scope for stopping for-loop\n ")
%lua::append %inner_lua "\n "
- %lua::append (what (---stop %var ---) compiles to)
+ %lua::append (\(---stop %var ---) as lua)
%lua::append "\nend -- end of scope for stopping for-loop"
return %lua
+(for %var in %iterable %body) parses as (for %var in %iterable at (=lua "__") %body)
+
test:
%d = {a: 10, b: 20, c: 30, d: 40, e: 50}
%result = []
@@ -311,31 +247,24 @@ test:
# Dict iteration (lua's "pairs()")
[for %key = %value in %iterable %body, for %key %value in %iterable %body] \
..all compile to:
- # This uses Lua's approach of only allowing loop-scoped variables in a loop
- unless (%key.type is "Var"):
- compile error at %key "Expected a variable here, not a \(%key.type)"
-
- unless (%value.type is "Var"):
- compile error at %value "Expected a variable here, not a \(%value.type)"
-
%lua = (Lua "for \(%key as lua identifier),\(%value as lua identifier) in pairs(\(%iterable as lua expr)) do")
%lua::append "\n " (%body as lua)
if (%body has subtree \(do next)):
%lua::append "\n ::continue::"
if (%body has subtree \(do next %key)):
- %lua::append "\n " (what (---next %key ---) compiles to)
+ %lua::append "\n " (\(---next %key ---) as lua)
if (%body has subtree \(do next %value)):
- %lua::append "\n " (what (---next %value ---) compiles to)
+ %lua::append "\n " (\(---next %value ---) as lua)
%lua::append "\nend --foreach-loop"
%stop_labels = (Lua "")
if (%body has subtree \(stop %key)):
- %stop_labels::append "\n" (what (---stop %key ---) compiles to)
+ %stop_labels::append "\n" (\(---stop %key ---) as lua)
if (%body has subtree \(stop %value)):
- %stop_labels::append "\n" (what (---stop %value ---) compiles to)
+ %stop_labels::append "\n" (\(---stop %value ---) as lua)
if ((size of "\%stop_labels") > 0):
%inner_lua = %lua
@@ -506,7 +435,7 @@ test:
assume ((result of: return 99) == 99)
# Inline thunk:
-(result of %body) compiles to "\(what (-> %body) compiles to)()"
+(result of %body) compiles to "\(\(-> %body) as lua)()"
test:
%t = [1, [2, [[3], 4], 5, [[[6]]]]]
%flat = []
@@ -537,10 +466,10 @@ test:
%lua::append "\n ::continue::"
if (%body has subtree \(do next %var)):
- %lua::append "\n \(what (---next %var ---) compiles to)"
+ %lua::append "\n \(\(---next %var ---) as lua)"
%lua::append "\n end -- Recursive loop"
if (%body has subtree \(stop %var)):
- %lua::append "\n \(what (---stop %var ---) compiles to)"
+ %lua::append "\n \(\(---stop %var ---) as lua)"
%lua::append "\nend -- Recursive scope"
return %lua