Added 'for (%k,%v) in ...' alias for 'for %k = %v in ...'

This commit is contained in:
Bruce Hill 2018-05-30 14:08:03 -07:00
parent 624fcdcc7e
commit b3df63eb10
3 changed files with 26 additions and 9 deletions

View File

@ -79,7 +79,10 @@ immediately
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
parse [..]
%expression for %key = %value in %iterable
%expression for (%key,%value) in %iterable
..as
result of
%comprehension <- []
for %key = %value in %iterable
@ -87,14 +90,20 @@ immediately
return %comprehension
# Dict comprehensions
parse [%key = %value for %item in %iterable] as
parse [..]
%key = %value for %item in %iterable
(%key,%value) for %item in %iterable
..as
result of
%comprehension <- {}
for %item in %iterable
%comprehension.%key <- %value
return %comprehension
parse [%key = %value for %src_key = %src_value in %iterable] as
parse [..]
%key = %value for %src_key = %src_value in %iterable
(%key,%value) for (%src_key,%src_value) in %iterable
..as
result of
%comprehension <- {}
for %src_key = %src_value in %iterable
@ -133,7 +142,10 @@ immediately
# Sorting
immediately
compile [sort %items] to: Lua "table.sort(\(%items as lua expr));"
parse [sort %items by %item = %key_expr] as
parse [..]
sort %items by %item = %key_expr
sort %items by %item -> %key_expr
..as
do
%keys <- ({} with fallback %item -> %key_expr)
lua> "table.sort(\%items, function(x,y) return \%keys[x] < \%keys[y] end)"
@ -143,7 +155,10 @@ immediately
%copy <- (% for % in %items)
sort %copy
return %copy
action [%items sorted by %item = %key]
action [..]
%items sorted by %item = %key
%items sorted by %item -> %key
..
%copy <- (% for % in %items)
sort %copy by %item = %key
return %copy

View File

@ -76,10 +76,10 @@ immediately
(function()
for \(%subtree as lua expr) in coroutine.wrap(function() \(%tree as lua expr):map(coroutine.yield) end) do
if \(%condition as lua expr) then
return true;
return true
end
end
return false;
return false
end)()
# While loops
@ -207,7 +207,10 @@ immediately
# Dict iteration (lua's "pairs()")
immediately
compile [for %key = %value in %iterable %body] to
compile [..]
for %key = %value in %iterable %body
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"
assume (%value.type is "Var") or barf "Loop expected variable, not: \%value"

View File

@ -55,7 +55,6 @@ STDIN, STDOUT, STDERR = "/dev/fd/0", "/dev/fd/1", "/dev/fd/2"
string.as_lua_id = (str)->
"_"..(str\gsub("%W", (c)-> if c == "_" then "__" else ("_%x")\format(c\byte!)))
-- TODO:
-- consider non-linear codegen, rather than doing thunks for things like comprehensions
-- type checking?