Removed "for all"-style iteration and changed "for % from 1 to 10"-style

to "for % in 1 to 10" for consistency.
This commit is contained in:
Bruce Hill 2018-05-15 20:32:22 -07:00
parent 0a95a264e5
commit 3ffeaf1f5d
9 changed files with 41 additions and 75 deletions

View File

@ -66,33 +66,18 @@ immediately
%comprehension.%i <- %expression %comprehension.%i <- %expression
return %comprehension return %comprehension
parse [%expression for all %iterable] as
%expression for % in %iterable
parse [..] parse [..]
%expression for %index from %start to %stop via %step %expression for %index in %start to %stop via %step
%expression for %index from %start to %stop by %step %expression for %index in %start to %stop by %step
..as ..as
result of result of
%comprehension <- [] %comprehension <- []
for %index from %start to %stop via %step for %index in %start to %stop via %step
add %expression to %comprehension add %expression to %comprehension
return %comprehension return %comprehension
parse [%expression for all %iterable] as parse [%expression for %var in %start to %stop] as
%expression for % in %iterable %expression for %var in %start to %stop via 1
parse [%expression for %var from %start to %stop] as
%expression for %var from %start to %stop via 1
parse [..]
%expression for all %start to %stop by %step
%expression for all %start to %stop via %step
..as
%expression for % from %start to %stop via %step
parse [%expression for all %start to %stop] as
%expression for all %start to %stop via 1
parse [%expression for %key = %value in %iterable] as parse [%expression for %key = %value in %iterable] as
result of result of
@ -109,9 +94,6 @@ immediately
%comprehension.%key <- %value %comprehension.%key <- %value
return %comprehension return %comprehension
parse [%key = %value for all %iterable] as
%key = %value for % in %iterable
parse [%key = %value for %src_key = %src_value in %iterable] as parse [%key = %value for %src_key = %src_value in %iterable] as
result of result of
%comprehension <- {} %comprehension <- {}
@ -142,18 +124,18 @@ immediately
immediately immediately
action [%items sorted, sorted %items] action [%items sorted, sorted %items]
%copy <- (% for all %items) %copy <- (% for % in %items)
sort %copy sort %copy
return %copy return %copy
action [%items sorted by %key] action [%items sorted by %key]
%copy <- (% for all %items) %copy <- (% for % in %items)
sort %copy by %key sort %copy by %key
return %copy return %copy
action [unique %items] action [unique %items]
%unique <- [] %unique <- []
%seen <- {} %seen <- {}
for all %items for % in %items
unless: % in %seen unless: % in %seen
add % to %unique add % to %unique
(% in %seen) <- (yes) (% in %seen) <- (yes)

View File

@ -140,8 +140,8 @@ immediately
# Numeric range for loops # Numeric range for loops
immediately immediately
compile [..] compile [..]
for %var from %start to %stop by %step %body for %var in %start to %stop by %step %body
for %var from %start to %stop via %step %body for %var in %start to %stop via %step %body
..to ..to
# This uses Lua's approach of only allowing loop-scoped variables in a loop # 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)" assume (%var.type is "Var") or barf "Loop expected variable, not: \(%var's source code)"
@ -173,12 +173,7 @@ immediately
return %lua return %lua
immediately immediately
parse [for %var from %start to %stop %body] as: for %var from %start to %stop via 1 %body parse [for %var in %start to %stop %body] as: for %var in %start to %stop via 1 %body
parse [..]
for all %start to %stop by %step %body
for all %start to %stop via %step %body
..as: for % from %start to %stop via %step %body
parse [for all %start to %stop %body] as: for all %start to %stop via 1 %body
# For-each loop (lua's "ipairs()") # For-each loop (lua's "ipairs()")
immediately immediately
@ -210,8 +205,6 @@ immediately
end -- end of scope for stopping for-loop end -- end of scope for stopping for-loop
return %lua return %lua
parse [for all %iterable %body] as: for % in %iterable %body
# Dict iteration (lua's "pairs()") # Dict iteration (lua's "pairs()")
immediately immediately
compile [for %key = %value in %iterable %body] to compile [for %key = %value in %iterable %body] to

View File

@ -42,27 +42,31 @@ compile [all of %items, all %items] to
unless: (%items' "type") is "List" unless: (%items' "type") is "List"
return: Lua value "utils.all(\(%items as lua expr))" return: Lua value "utils.all(\(%items as lua expr))"
%clauses <- [] %clauses <- []
for all (%items' "value"): lua> "table.insert(\%clauses, \(% as lua expr));" for % in (%items' "value")
lua> "table.insert(\%clauses, \(% as lua expr));"
return: Lua value "(\(%clauses joined with " and "))" return: Lua value "(\(%clauses joined with " and "))"
parse [not all of %items, not all %items] as: not (all of %items) parse [not all of %items, not all %items] as: not (all of %items)
compile [any of %items, any %items] to compile [any of %items, any %items] to
unless: (%items' "type") is "List" unless: (%items' "type") is "List"
return: Lua value "utils.any(\(%items as lua expr))" return: Lua value "utils.any(\(%items as lua expr))"
%clauses <- [] %clauses <- []
for all (%items' "value"): lua> "table.insert(\%clauses, \(% as lua expr));" for % in (%items' "value")
lua> "table.insert(\%clauses, \(% as lua expr));"
return: Lua value "(\(%clauses joined with " or "))" return: Lua value "(\(%clauses joined with " or "))"
parse [none of %items, none %items] as: not (any of %items) parse [none of %items, none %items] as: not (any of %items)
compile [sum of %items, sum %items] to compile [sum of %items, sum %items] to
unless: (%items' "type") is "List" unless: (%items' "type") is "List"
return: Lua value "utils.sum(\(%items as lua expr))" return: Lua value "utils.sum(\(%items as lua expr))"
%clauses <- [] %clauses <- []
for all (%items' "value"): lua> "table.insert(\%clauses, \(% as lua expr));" for % in (%items' "value")
lua> "table.insert(\%clauses, \(% as lua expr));"
return: Lua value "(\(%clauses joined with " + "))" return: Lua value "(\(%clauses joined with " + "))"
compile [product of %items, product %items] to compile [product of %items, product %items] to
unless: (%items' "type") is "List" unless: (%items' "type") is "List"
return: Lua value "utils.product(\(%items as lua expr))" return: Lua value "utils.product(\(%items as lua expr))"
%clauses <- [] %clauses <- []
for all (%items' "value"): lua> "table.insert(\%clauses, \(% as lua expr));" for % in (%items' "value")
lua> "table.insert(\%clauses, \(% as lua expr));"
return: Lua value "(\(%clauses joined with " * "))" return: Lua value "(\(%clauses joined with " * "))"
action [avg of %items, average of %items] action [avg of %items, average of %items]
=lua "(utils.sum(\%items)/#\%items)" =lua "(utils.sum(\%items)/#\%items)"

View File

@ -38,6 +38,7 @@ say "Hello world!"
Strings can contain a backslash followed by a variable, list, dict, or parenthesized Strings can contain a backslash followed by a variable, list, dict, or parenthesized
expression. This escaped value will be converted to a readable string, like so: expression. This escaped value will be converted to a readable string, like so:
The value of %x is \%x, isn't that nice? The value of %x is \%x, isn't that nice?
These are some numbers: \[1+1,2+1,3+1]
The sum of 2 and 4 is \(2 + 4). The sum of 2 and 4 is \(2 + 4).
If you need to use a plain ol' backslash, you can do \\ <-- that If you need to use a plain ol' backslash, you can do \\ <-- that
%format_str2 <- "Single-line strings can contain escape sequences like \", \\, \n, \065, and \x0A" %format_str2 <- "Single-line strings can contain escape sequences like \", \\, \n, \065, and \x0A"
@ -109,20 +110,15 @@ when 3 = ?
%list <- [1,2,3] %list <- [1,2,3]
for %x in %list for %x in %list
say "For %x loop #\%x" say "For %x loop #\%x"
# There's also a slightly more concise version that automatically populates a loop variable "%"
for all %list
say "For all loop #\%"
# How do I loop over a number range? # How do I loop over a number range?
# This is inclusive, so it will loop over 1,2, and 3 # This is inclusive, so it will loop over 1,2, and 3
for %i from 1 to 3 for %i in 1 to 3
say "For %i from 1 to 3 loop #\%i" say "For %i in 1 to 3 loop #\%i"
for all 1 to 3
say "For all 1 to 3 loop #\%"
# This will print 0,2, and 4 # This will print 0,2, and 4
for %even from 0 to 5 by 2 for %even in 0 to 5 by 2
say "Even #\%even" say "Even #\%even"
for %backwards from 3 to 1 by -1 for %backwards in 3 to 1 by -1
say "Backwards #\%backwards" say "Backwards #\%backwards"
# How do I do a 'while' loop? # How do I do a 'while' loop?
@ -289,7 +285,7 @@ if (1 > (TWENTY)) on opposite day
# Well... it's always *possible* to fall back to Lua behavior for something like this: # Well... it's always *possible* to fall back to Lua behavior for something like this:
action [best of %items according to %key_fn] action [best of %items according to %key_fn]
<- {%best:nil, %best_key:nil} <- {%best:nil, %best_key:nil}
for all %items for % in %items
%key <- (=lua "\%key_fn(\%)") %key <- (=lua "\%key_fn(\%)")
if: (%best is (nil)) or (%key > %best_key) if: (%best is (nil)) or (%key > %best_key)
<- {%best:%, %best_key:%key} <- {%best:%, %best_key:%key}
@ -312,7 +308,7 @@ immediately
parse [best of %items according to %key_expr] as parse [best of %items according to %key_expr] as
result of result of
<- {%best:nil, %best_key:nil} <- {%best:nil, %best_key:nil}
for all %items for % in %items
%key <- %key_expr %key <- %key_expr
if: (%best is (nil)) or (%key > %best_key) if: (%best is (nil)) or (%key > %best_key)
<- {%best:%, %best_key:%key} <- {%best:%, %best_key:%key}

View File

@ -35,7 +35,7 @@ compile [function %args %body, lambda %args %body] to
to %lua write ")\n " to %lua write ")\n "
%body <-: %body as lua %body <-: %body as lua
lua> "\%body:convert_to_statements('return ');" lua> "\%body:convert_to_statements('return ');"
for all %args.value: lua> "\%body:remove_free_vars(\%);" for % in %args.value: lua> "\%body:remove_free_vars(\%);"
to %lua write %body to %lua write %body
to %lua write "\nend)" to %lua write "\nend)"
return %lua return %lua

View File

@ -741,7 +741,7 @@ do
local self = _class_0 local self = _class_0
stub_defs = { stub_defs = {
space = (P(' ') + P('\n..')) ^ 0, space = (P(' ') + P('\n..')) ^ 0,
word = (NOMSU_DEFS.ident_char ^ 1 + NOMSU_DEFS.operator ^ 1), word = (NOMSU_DEFS.ident_char ^ 1 + NOMSU_DEFS.operator),
varname = (R('az', 'AZ', '09') + P('_') + NOMSU_DEFS.utf8_char) ^ 0 varname = (R('az', 'AZ', '09') + P('_') + NOMSU_DEFS.utf8_char) ^ 0
} }
stub_pattern = re.compile([=[ {~ (%space->'') (('%' (%varname->'')) / %word)? ((%space->' ') (('%' (%varname->'')) / %word))* (%space->'') ~} stub_pattern = re.compile([=[ {~ (%space->'') (('%' (%varname->'')) / %word)? ((%space->' ') (('%' (%varname->'')) / %word))* (%space->'') ~}

View File

@ -294,7 +294,7 @@ class NomsuCompiler
stub_defs = { stub_defs = {
space:(P(' ') + P('\n..'))^0 space:(P(' ') + P('\n..'))^0
word:(NOMSU_DEFS.ident_char^1 + NOMSU_DEFS.operator^1) word:(NOMSU_DEFS.ident_char^1 + NOMSU_DEFS.operator)
varname:(R('az','AZ','09') + P('_') + NOMSU_DEFS.utf8_char)^0 varname:(R('az','AZ','09') + P('_') + NOMSU_DEFS.utf8_char)^0
} }
stub_pattern = re.compile [=[ stub_pattern = re.compile [=[

View File

@ -19,10 +19,10 @@ pop from %list
assume ((last in %list) = 5) assume ((last in %list) = 5)
remove index 1 from %list remove index 1 from %list
assume ((first in %list) = 2) assume ((first in %list) = 2)
assume (((% * %) for all [1,2,3]) = [1,4,9]) assume (((% * %) for % in [1,2,3]) = [1,4,9])
assume ((%k = (%v * %v) for %k = %v in {x:1,y:2,z:3}) = {x:1,y:4,z:9}) assume ((%k = (%v * %v) for %k = %v in {x:1,y:2,z:3}) = {x:1,y:4,z:9})
assume ((%k for %k = %v in {x:1}) = ["x"]) assume ((%k for %k = %v in {x:1}) = ["x"])
assume ((% = (% * %) for all [1,2,3]) = {1:1,2:4,3:9}) assume ((% = (% * %) for % in [1,2,3]) = {1:1,2:4,3:9})
assume (([[1,2],[3,4]] flattened) = [1,2,3,4]) assume (([[1,2],[3,4]] flattened) = [1,2,3,4])
assume ((entries in {x:1}) = [{key:"x",value:1}]) assume ((entries in {x:1}) = [{key:"x",value:1}])
assume ((keys in {x:1}) = ["x"]) assume ((keys in {x:1}) = ["x"])
@ -38,7 +38,7 @@ sort %x by (% in %keys)
assume (%x = [2,3,1]) assume (%x = [2,3,1])
assume ((unique [1,2,1,3,2,3]) = [1,2,3]) assume ((unique [1,2,1,3,2,3]) = [1,2,3])
%c <- (new counter) %c <- (new counter)
for all ["x","y","x","x","y"] for % in ["x","y","x","x","y"]
(% in %c) +<- 1 (% in %c) +<- 1
assume (%c = {x:3,y:2}) assume (%c = {x:3,y:2})

View File

@ -1,4 +1,4 @@
#.. #
Tests for the stuff defined in lib/control_flow.nom Tests for the stuff defined in lib/control_flow.nom
use "core" use "core"
@ -40,11 +40,6 @@ for %x in [1,2,3]
%tot +<- %x %tot +<- %x
assume (%tot = 6) or barf "for-loop failed" assume (%tot = 6) or barf "for-loop failed"
%tot <- 0
for all [1,2,3]
%tot +<- %
assume (%tot = 6) or barf "for-all-loop failed"
%x <- 0 %x <- 0
repeat repeat
%x +<- 1 %x +<- 1
@ -58,7 +53,7 @@ repeat 5 times
assume (%x = 5) or barf "Failed to repeat 5 times" assume (%x = 5) or barf "Failed to repeat 5 times"
<- {%x:0,%y:0} <- {%x:0,%y:0}
for all [1,2,3] for % in [1,2,3]
repeat 5 times repeat 5 times
do next repeat do next repeat
%x +<- 1 %x +<- 1
@ -66,7 +61,7 @@ for all [1,2,3]
assume ([%x,%y] = [0,3]) or barf "Failed to continue repeat" assume ([%x,%y] = [0,3]) or barf "Failed to continue repeat"
<- {%x:0,%y:0} <- {%x:0,%y:0}
for all [1,2,3] for % in [1,2,3]
repeat 5 times repeat 5 times
do next % do next %
%x +<- 1 %x +<- 1
@ -74,7 +69,7 @@ for all [1,2,3]
assume ([%x,%y] = [0,0]) or barf "Failed to continue for" assume ([%x,%y] = [0,0]) or barf "Failed to continue for"
<- {%x:0,%y:0} <- {%x:0,%y:0}
for all [1,2,3] for % in [1,2,3]
repeat 5 times repeat 5 times
stop repeating stop repeating
%x +<- 1 %x +<- 1
@ -82,7 +77,7 @@ for all [1,2,3]
assume ([%x,%y] = [0,3]) or barf "Failed to stop repeat" assume ([%x,%y] = [0,3]) or barf "Failed to stop repeat"
<- {%x:0,%y:0} <- {%x:0,%y:0}
for all [1,2,3] for % in [1,2,3]
repeat 5 times repeat 5 times
stop % stop %
%x +<- 1 %x +<- 1
@ -100,17 +95,13 @@ repeat until: %x = 10
assume (%x = 10) or barf "repeat-until failed" assume (%x = 10) or barf "repeat-until failed"
%x <- 0 %x <- 0
for %i from 1 to 3: %x +<- %i for %i in 1 to 3: %x +<- %i
assume (%x = 6) or barf "Numeric for range failed" assume (%x = 6) or barf "Numeric for range failed"
%x <- 0 %x <- 0
for %i from 3 to 1 via -1: %x +<- %i for %i in 3 to 1 via -1: %x +<- %i
assume (%x = 6) or barf "backwards numeric for range failed" assume (%x = 6) or barf "backwards numeric for range failed"
%x <- 0
for all 1 to 3: %x +<- %
assume (%x = 6) or barf "terse numeric for range failed"
%result <- {} %result <- {}
for %key = %value in {x:1,y:2} for %key = %value in {x:1,y:2}
(%result's ("\%key\%key")) <- (%value * 11) (%result's ("\%key\%key")) <- (%value * 11)
@ -197,12 +188,12 @@ assume
(..) (..)
result of result of
%n <- 0 %n <- 0
for all [1,2,3]: %n +<- % for % in [1,2,3]: %n +<- %
return %n return %n
..= 6 ..= 6
%nums <- [] %nums <- []
for all for % in
values values
-> 4 -> 4
-> 5 -> 5