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

View File

@ -140,8 +140,8 @@ immediately
# Numeric range for loops
immediately
compile [..]
for %var from %start to %stop by %step %body
for %var from %start to %stop via %step %body
for %var in %start to %stop by %step %body
for %var in %start to %stop via %step %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)"
@ -173,12 +173,7 @@ immediately
return %lua
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
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
parse [for %var in %start to %stop %body] as: for %var in %start to %stop via 1 %body
# For-each loop (lua's "ipairs()")
immediately
@ -210,8 +205,6 @@ immediately
end -- end of scope for stopping for-loop
return %lua
parse [for all %iterable %body] as: for % in %iterable %body
# Dict iteration (lua's "pairs()")
immediately
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"
return: Lua value "utils.all(\(%items as lua expr))"
%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 "))"
parse [not all of %items, not all %items] as: not (all of %items)
compile [any of %items, any %items] to
unless: (%items' "type") is "List"
return: Lua value "utils.any(\(%items as lua expr))"
%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 "))"
parse [none of %items, none %items] as: not (any of %items)
compile [sum of %items, sum %items] to
unless: (%items' "type") is "List"
return: Lua value "utils.sum(\(%items as lua expr))"
%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 " + "))"
compile [product of %items, product %items] to
unless: (%items' "type") is "List"
return: Lua value "utils.product(\(%items as lua expr))"
%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 " * "))"
action [avg of %items, average of %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
expression. This escaped value will be converted to a readable string, like so:
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).
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"
@ -109,20 +110,15 @@ when 3 = ?
%list <- [1,2,3]
for %x in %list
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?
# This is inclusive, so it will loop over 1,2, and 3
for %i from 1 to 3
say "For %i from 1 to 3 loop #\%i"
for all 1 to 3
say "For all 1 to 3 loop #\%"
for %i in 1 to 3
say "For %i in 1 to 3 loop #\%i"
# 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"
for %backwards from 3 to 1 by -1
for %backwards in 3 to 1 by -1
say "Backwards #\%backwards"
# 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:
action [best of %items according to %key_fn]
<- {%best:nil, %best_key:nil}
for all %items
for % in %items
%key <- (=lua "\%key_fn(\%)")
if: (%best is (nil)) or (%key > %best_key)
<- {%best:%, %best_key:%key}
@ -312,7 +308,7 @@ immediately
parse [best of %items according to %key_expr] as
result of
<- {%best:nil, %best_key:nil}
for all %items
for % in %items
%key <- %key_expr
if: (%best is (nil)) or (%key > %best_key)
<- {%best:%, %best_key:%key}

View File

@ -35,7 +35,7 @@ compile [function %args %body, lambda %args %body] to
to %lua write ")\n "
%body <-: %body as lua
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 "\nend)"
return %lua

View File

@ -741,7 +741,7 @@ do
local self = _class_0
stub_defs = {
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
}
stub_pattern = re.compile([=[ {~ (%space->'') (('%' (%varname->'')) / %word)? ((%space->' ') (('%' (%varname->'')) / %word))* (%space->'') ~}

View File

@ -294,7 +294,7 @@ class NomsuCompiler
stub_defs = {
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
}
stub_pattern = re.compile [=[

View File

@ -19,10 +19,10 @@ pop from %list
assume ((last in %list) = 5)
remove index 1 from %list
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 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 ((entries in {x:1}) = [{key:"x",value:1}])
assume ((keys in {x:1}) = ["x"])
@ -38,7 +38,7 @@ sort %x by (% in %keys)
assume (%x = [2,3,1])
assume ((unique [1,2,1,3,2,3]) = [1,2,3])
%c <- (new counter)
for all ["x","y","x","x","y"]
for % in ["x","y","x","x","y"]
(% in %c) +<- 1
assume (%c = {x:3,y:2})

View File

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