diff --git a/core/collections.nom b/core/collections.nom index 91c0c10..8e7576d 100644 --- a/core/collections.nom +++ b/core/collections.nom @@ -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 diff --git a/core/control_flow.nom b/core/control_flow.nom index 5eabefd..e54f10a 100644 --- a/core/control_flow.nom +++ b/core/control_flow.nom @@ -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" diff --git a/nomsu.moon b/nomsu.moon index 1d2bff9..59c1251 100755 --- a/nomsu.moon +++ b/nomsu.moon @@ -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?