diff options
Diffstat (limited to 'lib/collections.nom')
| -rw-r--r-- | lib/collections.nom | 123 |
1 files changed, 67 insertions, 56 deletions
diff --git a/lib/collections.nom b/lib/collections.nom index 15f27ed..c31bbee 100644 --- a/lib/collections.nom +++ b/lib/collections.nom @@ -12,7 +12,7 @@ use "lib/operators.nom" compile [..] %index st to last in %list, %index nd to last in %list, %index rd to last in %list %index th to last in %list -..to "utils.nth_to_last(\(%list as lua), \(%index as lua))" +..to {expr:"utils.nth_to_last(\(%list as lua expr), \(%index as lua expr))"} parse [first in %list, first %list] as: 1 st in %list parse [last in %list, last %list] as: 1 st to last in %list @@ -32,25 +32,28 @@ action [..] if (%key is %item): return (no) return (yes) -compile [%list has key %index, %list has index %index] to ".." - ((\(%list as lua))[\(%index as lua)] ~= nil) +# Note: it's important to have the space after "[" to prevent confusion if %index is a string +compile [%list has key %index, %list has index %index] to {..} + expr: ".." + ((\(%list as lua expr))[ \(%index as lua expr)] ~= nil) +# Note: it's important to have the space after "[" to prevent confusion if %index is a string compile [..] %list doesn't have key %index, %list does not have key %index %list doesn't have index %index, %list does not have index %index -..to "((\(%list as lua))[\(%index as lua)] ~= nil)" +..to {expr:"((\(%list as lua expr))[ \(%index as lua expr)] ~= nil)"} compile [length of %list, size of %list, size %list, number of %list, len %list] to - "utils.size(\(%list as lua))" + {expr:"utils.size(\(%list as lua expr))"} compile [append %item to %list, add %item to %list] to - "table.insert(\(%list as lua), \(%item as lua))" + {statements:"table.insert(\(%list as lua expr), \(%item as lua expr))"} compile [pop from %list, remove last from %list] to - "table.remove(\(%list as lua))" + {statements:"table.remove(\(%list as lua expr))"} compile [remove index %index from %list] to - "table.remove(\(%list as lua), \(%index as lua))" + {statements:"table.remove(\(%list as lua expr), \(%index as lua expr))"} action [flatten %lists] @@ -74,14 +77,15 @@ immediately compile [%expression for %item in %iterable] to assume ((%item's "type") is "Var") or barf ".." List comprehension has the wrong type for the loop variable. Expected Var, but got: \(%item's "type") - return ".." - (function() - local comprehension = {}; - for i,\(%item as lua) in ipairs(\(%iterable as lua)) do - comprehension[i] = \(%expression as lua); - end - return comprehension; - end)() + return {..} + expr:".." + (function() + local comprehension = {}; + for i,\(%item as lua expr) in ipairs(\(%iterable as lua expr)) do + comprehension[i] = \(%expression as lua expr); + end + return comprehension; + end)() parse [%expression for all %iterable] as: %expression for % in %iterable compile [%expression for %key = %value in %iterable] to @@ -89,28 +93,31 @@ immediately List comprehension has the wrong type for the key loop variable. Expected Var, but got: \(%key's "type") assume ((%value's "type") is "Var") or barf ".." List comprehension has the wrong type for the value loop variable. Expected Var, but got: \(%value's "type") - return ".." - (function() - local comprehension = {}; - for \(%key as lua), \(%value as lua) in pairs(\(%iterable as lua)) do - comprehension[i] = \(%expression as lua) - end - return comprehension; - end)() + return {..} + expr: ".." + (function() + local comprehension = {}; + for \(%key as lua expr), \(%value as lua expr) in pairs(\(%iterable as lua expr)) do + comprehension[i] = \(%expression as lua expr) + end + return comprehension; + end)() # Dict comprehensions immediately compile [%key = %value for %item in %iterable] to assume ((%item's "type") is "Var") or barf ".." Dict comprehension has the wrong type for the loop variable. Expected Var, but got: \(%item's "type") - return ".." - (function() - local comprehension = {}; - for i,\(%item as lua) in ipairs(\(%iterable as lua)) do - comprehension[\(%key as lua)] = \(%value as lua) - end - return comprehension; - end)() + # Note: it's important to have the space after "[" to prevent confusion if %key is a string + return {..} + expr: ".." + (function() + local comprehension = {}; + for i,\(%item as lua expr) in ipairs(\(%iterable as lua expr)) do + comprehension[ \(%key as lua expr)] = \(%value as lua expr) + end + return comprehension; + end)() parse [%key = %value for all %iterable] as: %key = %value for % in %iterable compile [%key = %value for %src_key = %src_value in %iterable] to @@ -118,21 +125,24 @@ immediately Dict comprehension has the wrong type for the key loop variable. Expected Var, but got: \(%src_key's "type") assume ((%src_value's "type") is "Var") or barf ".." Dict comprehension has the wrong type for the value loop variable. Expected Var, but got: \(%src_value's "type") - return ".." - (function() - local comprehension = {}; - for \(%src_key as lua), \(%src_value as lua) in pairs(\(%iterable as lua)) do - comprehension[\(%key as lua)] = \(%value as lua); - end - return comprehension; - end)() + # Note: it's important to have the space after "[" to prevent confusion if %key is a string + return {..} + expr: ".." + (function() + local comprehension = {}; + for \(%src_key as lua expr), \(%src_value as lua expr) in pairs(\(%iterable as lua expr)) do + comprehension[ \(%key as lua expr)] = \(%value as lua expr); + end + return comprehension; + end)() # Sorting: -compile [sort %items] to "table.sort(\(%items as lua))" -compile [sort %items by %key_expr] to ".." - utils.sort(\(%items as lua), function(\(\% as lua)) - return \(%key_expr as lua); - end) +compile [sort %items] to {statements:"table.sort(\(%items as lua expr))"} +compile [sort %items by %key_expr] to {..} + statements: ".." + utils.sort(\(%items as lua expr), function(\(\% as lua expr)) + return \(%key_expr as lua expr); + end) action [%items sorted] %copy <- (% for all %items) @@ -147,16 +157,17 @@ action [unique %items] [%k for %k=%v in (%=(yes) for all %items)] # Metatable stuff -compile [set %dict's metatable to %metatable] to code ".." - setmetatable(\(%dict as lua), \(%metatable as lua)); - -compile [new counter] to "setmetatable({}, {__index=function() return 0; end})" - -compile [new default dict] to ".." - setmetatable({}, {__index=function(self, key) - t = {}; - self[key] = t; - return t; - end})" +compile [set %dict's metatable to %metatable] to {..} + statements: "setmetatable(\(%dict as lua expr), \(%metatable as lua expr));" + +compile [new counter] to {expr:"setmetatable({}, {__index=function() return 0; end})"} + +compile [new default dict] to {..} + expr: ".." + setmetatable({}, {__index=function(self, key) + t = {}; + self[key] = t; + return t; + end}) # TODO: maybe make a generator/coroutine? |
