diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2018-01-24 12:37:52 -0800 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2018-01-24 12:38:09 -0800 |
| commit | d173e9ae88380bb1217b3f775e6c3cec71a6606c (patch) | |
| tree | a385dbe31e3a71cae667c64458a837efc25d5b3d | |
| parent | 566b7d09c860bb719e69c54ea0618da7c90c2dff (diff) | |
Fixed id for nil/NaN and cleaned up collection metatable stuff.
| -rw-r--r-- | lib/collections.nom | 34 | ||||
| -rw-r--r-- | nomsu.lua | 7 | ||||
| -rwxr-xr-x | nomsu.moon | 4 |
3 files changed, 17 insertions, 28 deletions
diff --git a/lib/collections.nom b/lib/collections.nom index 1a36aae..15f27ed 100644 --- a/lib/collections.nom +++ b/lib/collections.nom @@ -9,10 +9,6 @@ use "lib/operators.nom" # List/dict functions: # Indexing -parse [..] - %index st in %list, %index nd in %list, %index rd in %list - %index th in %list -..as: %list -> %index 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 @@ -47,15 +43,6 @@ compile [..] compile [length of %list, size of %list, size %list, number of %list, len %list] to "utils.size(\(%list as lua))" -# Chained lookup -compile [%list ->* %indices] to - assume ((%indices's "type") is "List") or barf ".." - Expected List for chained lookup, not \(%indices's "type") - %ret <- "\(%list as lua)" - for %index in (%indices's "value") - <- %ret + "[\(%index as lua)]" - return "\%ret" - compile [append %item to %list, add %item to %list] to "table.insert(\(%list as lua), \(%item as lua))" @@ -73,9 +60,6 @@ action [flatten %lists] add %item to %flat return %flat -action [dict %items] - (%->1)=(%->2) for all %items - action [entries in %dict] [{key:%k, value:%v} for %k=%v in %dict] @@ -163,22 +147,16 @@ action [unique %items] [%k for %k=%v in (%=(yes) for all %items)] # Metatable stuff -compile [counter] to "setmetatable({}, {__index=function() return 0; end})" -compile [default dict] to ".." +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})" -action [chain %dict to %fallback] - when (type of %fallback) is ? - * "table" - =lua "setmetatable(\%dict, \%fallback)" - * "function" - =lua "setmetatable(\%dict, {__index=function(self, key) return (\%fallback)(nomsu, {['']=key, self=self}); end})" - * else - =lua "setmetatable(\%dict, {__index=function(self, key) return (\%fallback); end})" - # TODO: maybe make a generator/coroutine? - @@ -1430,9 +1430,16 @@ do self.write_err = function(self, ...) return io.stderr:write(...) end + local NaN_surrogate = { } + local nil_surrogate = { } self.ids = setmetatable({ }, { __mode = "k", __index = function(self, key) + if key == nil then + return self[nil_surrogate] + elseif key ~= key then + return self[NaN_surrogate] + end local id = new_uuid() self[key] = id return id @@ -136,9 +136,13 @@ class NomsuCompiler @write = (...)=> io.write(...) @write_err = (...)=> io.stderr\write(...) -- Weak-key mapping from objects to randomly generated unique IDs + NaN_surrogate = {} + nil_surrogate = {} @ids = setmetatable({}, { __mode: "k" __index: (key)=> + if key == nil then return @[nil_surrogate] + elseif key != key then return @[NaN_surrogate] id = new_uuid! @[key] = id return id |
