diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2017-12-13 16:29:15 -0800 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2017-12-13 16:29:15 -0800 |
| commit | 536a3ba64931946f81140e6a6d13f612a47a41d9 (patch) | |
| tree | fdf6975057207af248d5c345671e09be79498318 /lib/collections.nom | |
| parent | 0c1c406ce0d1c19508653181d8cef75f976677a5 (diff) | |
Got it working.
Diffstat (limited to 'lib/collections.nom')
| -rw-r--r-- | lib/collections.nom | 62 |
1 files changed, 31 insertions, 31 deletions
diff --git a/lib/collections.nom b/lib/collections.nom index 02729c5..41710a6 100644 --- a/lib/collections.nom +++ b/lib/collections.nom @@ -20,12 +20,12 @@ parse [last in %list, last %list] as: 1 st to last in %list # Dict iteration convenience function. This could also be accomplished with: for all (entries in %dict): ... compile [for %key = %value in %dict %body] to code: ".." - |do; - | for k, v in pairs(\(%dict as lua)) do; - | \(%key as lua), \(%value as lua) = k, v; - | \(%body as lua statements) - | end; - |end; + do + for k, v in pairs(\(%dict as lua)) do + \(%key as lua), \(%value as lua) = k, v; + \(%body as lua statements) + end + end # Membership testing rule [%item is in %list, %list contains %item, %list has %item] =: @@ -43,7 +43,7 @@ rule [..] return (yes) compile [%list has key %index, %list has index %index] to: ".." - |((\(%list as lua))[\(%index as lua)] ~= nil) + ((\(%list as lua))[\(%index as lua)] ~= nil) compile [..] %list doesn't have key %index, %list does not have key %index @@ -56,7 +56,7 @@ compile [length of %list, size of %list, size %list, number of %list, len %list] # Chained lookup compile [%list ->* %indices] to: assert ((%indices's "type") == "List") ".." - |Expected List for chained lookup, not \(%indices's "type") + Expected List for chained lookup, not \(%indices's "type") %ret = "\(%list as lua)" for %index in (%indices's "value"): %ret join= "[\(%index as lua)]" @@ -98,20 +98,20 @@ compile [dict %items, d %items] to: %item_codes = [] for %func_call in (%items's "value"): assert ((%func_call's "type") == "FunctionCall") ".." - |Invalid format for 'dict' expression. Only literals are allowed. + Invalid format for 'dict' expression. Only literals are allowed. %tokens = (%func_call's "value") %equals = (%tokens -> 2) assert (=lua "#\(%tokens) == 3 and \(%equals) and \(%equals).type == 'Word' and \(%equals).value == '='") ".." - |Invalid format for 'dict' expression. Lines must only have the "% = %" format, not \(%func_call's "src") + Invalid format for 'dict' expression. Lines must only have the "% = %" format, not \(%func_call's "src") %key = (%tokens -> 1) lua> ".." - |if \(%key).type == "Word" and \(%key).value:match("^[a-zA-Z_][a-zA-Z0-9_]*$") then - | \(%key_code) = \(%key).value; - |elseif \(%key).type == "Word" then - | \(%key_code) = "["..nomsu:repr(\(%key).value).."]"; - |else - | \(\{%key_code = "[\((%key as lua))]"} as lua statements) - |end + if \(%key).type == "Word" and \(%key).value:match("^[a-zA-Z_][a-zA-Z0-9_]*$") then + \(%key_code) = \(%key).value; + elseif \(%key).type == "Word" then + \(%key_code) = "["..nomsu:repr(\(%key).value).."]"; + else + \(\{%key_code = "[\((%key as lua))]"} as lua statements) + end add "\(%key_code) = \((%tokens -> 3) as lua)" to %item_codes return "{\(join %item_codes with glue ",\n")}" ..else: @@ -139,16 +139,16 @@ rule [values in %dict] =: # List Comprehension compile [%expression for %var in %iterable] to: assert ((%var's "type") == "Var") ".." - |List comprehension has the wrong type for the loop variable. Expected Var, but got: \(%var's "type") + List comprehension has the wrong type for the loop variable. Expected Var, but got: \(%var's "type") ".." - |(function(game, vars); - | local comprehension = {}; - | for i,value in ipairs(\(%iterable as lua)) do; - | \(%var as lua) = value; - | comprehension[i] = \(%expression as lua); - | end; - | return comprehension; - |end)(game, setmetatable({}, {__index=vars})) + (function(game, vars); + local comprehension = {}; + for i,value in ipairs(\(%iterable as lua)) do; + \(%var as lua) = value; + comprehension[i] = \(%expression as lua); + end; + return comprehension; + end)(game, setmetatable({}, {__index=vars})) parse [%expression for all %iterable] as: %expression for % in %iterable rule [%items sorted] =: @@ -165,11 +165,11 @@ rule [unique %items] =: # Metatable stuff compile [counter] to: "setmetatable({}, {__index=function() return 0; end})" compile [default dict] to: ".." - |setmetatable({}, {__index=function(self, key) - | t = {}; - | self[key] = t; - | return t; - |end})" + setmetatable({}, {__index=function(self, key) + t = {}; + self[key] = t; + return t; + end})" rule [chain %dict to %fallback] =: when (type of %fallback) == ?: * "table": |
