diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/collections.nom | 44 | ||||
| -rw-r--r-- | core/math.nom | 26 | ||||
| -rw-r--r-- | core/metaprogramming.nom | 19 | ||||
| -rw-r--r-- | core/operators.nom | 16 |
4 files changed, 65 insertions, 40 deletions
diff --git a/core/collections.nom b/core/collections.nom index 7922a4f..81ff3d2 100644 --- a/core/collections.nom +++ b/core/collections.nom @@ -113,23 +113,39 @@ immediately parse [keys in %dict] as: %k for %k = %v in %dict parse [values in %dict] as: %v for %k = %v in %dict +# Metatable stuff +immediately + compile [set %dict's metatable to %metatable] to + Lua "setmetatable(\(%dict as lua expr), \(%metatable as lua expr));" + + compile [%dict with fallback %key -> %value] to + Lua value ".." + setmetatable(\(%dict as lua expr), {__index=function(self, \(%key as lua expr)) + local value = \(%value as lua expr) + self[\(%key as lua expr)] = value + return value + end}) + +immediately + parse [new counter] as: {} with fallback % -> 0 + parse [new default dict] as: {} with fallback % -> {} + # Sorting immediately compile [sort %items] to: Lua "table.sort(\(%items as lua expr));" - compile [sort %items by %key_expr] to - Lua ".." - utils.sort(\(%items as lua expr), function(_) - return \(%key_expr as lua expr); - end); + parse [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)" immediately action [%items sorted, sorted %items] %copy <- (% for % in %items) sort %copy return %copy - action [%items sorted by %key] + action [%items sorted by %item = %key] %copy <- (% for % in %items) - sort %copy by %key + sort %copy by %item = %key return %copy action [unique %items] @@ -141,19 +157,5 @@ immediately (% in %seen) <- (yes) return %unique -immediately - # Metatable stuff - compile [set %dict's metatable to %metatable] to - Lua "setmetatable(\(%dict as lua expr), \(%metatable as lua expr));" - - compile [new counter] to: Lua value "setmetatable({}, {__index=function() return 0; end})" - - compile [new default dict] to - Lua value ".." - setmetatable({}, {__index=function(self, key) - t = {}; - self[key] = t; - return t; - end}) # TODO: maybe make a generator/coroutine? diff --git a/core/math.nom b/core/math.nom index ed2cdb6..677230b 100644 --- a/core/math.nom +++ b/core/math.nom @@ -74,16 +74,22 @@ compile [min of %items, smallest of %items, lowest of %items] to Lua value "utils.min(\(%items as lua expr))" compile [max of %items, biggest of %items, largest of %items, highest of %items] to Lua value "utils.max(\(%items as lua expr))" -compile [min of %items by %value_expr] to - Lua value ".." - utils.min(\(%items as lua expr), function(_) - return \(%value_expr as lua expr) - end) -compile [max of %items by %value_expr] to - Lua value ".." - utils.max(\(%items as lua expr), function(_) - return \(%value_expr as lua expr) - end) +parse [min of %items by %item = %value_expr] as + result of + <- {%best:nil, %best_key:nil} + for %item in %items + %key <- %value_expr + if: (%best = (nil)) or (%key < %best_key) + <- {%best:%item, %best_key:%key} + return %best +parse [max of %items by %item = %value_expr] as + result of + <- {%best:nil, %best_key:nil} + for %item in %items + %key <- %value_expr + if: (%best = (nil)) or (%key > %best_key) + <- {%best:%item, %best_key:%key} + return %best # Random functions action [seed random with %] diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom index 76ba731..717690d 100644 --- a/core/metaprogramming.nom +++ b/core/metaprogramming.nom @@ -86,11 +86,14 @@ immediately lua:append(", ", lua_var) end end + MANGLE_INDEX = (MANGLE_INDEX or 0) + 1 local function make_tree(t) if type(t) ~= 'table' and type(t) ~= 'userdata' then return repr(t) elseif t.type == 'Var' and replacements[t.value] then return replacements[t.value] + elseif t.type == 'Var' then + return t.type.."("..repr(t.value.."#"..tostring(MANGLE_INDEX))..", "..repr(t.source)..")" elseif t.is_multi then local bits = {} for i, entry in ipairs(t.value) do @@ -104,14 +107,16 @@ immediately lua:append(")\n local tree = ", make_tree(\%longhand), "\n return nomsu:tree_to_lua(tree)\nend);") return lua -action [remove action %stub] - lua> ".." - local fn = ACTIONS[\%stub] - local stubs = ARG_ORDERS[fn] - for stub in pairs(stubs) do - ACTIONS[stub] = nil +compile [remove action %action] to + Lua ".." + do + local fn = ACTIONS[\(=lua "repr(\%action:get_stub())")] + local stubs = ARG_ORDERS[fn] + for stub in pairs(stubs) do + ACTIONS[stub] = nil + end + ARG_ORDERS[fn] = nil end - ARG_ORDERS[fn] = nil immediately action [%tree as nomsu] diff --git a/core/operators.nom b/core/operators.nom index 7f5c772..c3b56c6 100644 --- a/core/operators.nom +++ b/core/operators.nom @@ -47,9 +47,16 @@ immediately # Variable assignment operator immediately compile [%var <- %value] to - lua> "local \%var_lua = \(%var as lua);" + lua> "local \%var_lua = \(%var as lua)" assume %var_lua.is_value or barf "Invalid target for assignment: \%var" - lua> "local \%value_lua = \(%value as lua);" + lua> ".." + \%value = \%value:map(function(t) + if Action:is_instance(t) and t:get_stub() == "?" then + return \%var + end + end) + local \%value_lua = \(%value as lua) + assume %value_lua.is_value or barf "Invalid value for assignment: \%value" lua> ".." local lua = Lua(tree.source, \%var_lua, ' = ', \%value_lua, ';') @@ -67,6 +74,11 @@ immediately local lhs, rhs = Lua(tree.source), Lua(tree.source) for i, item in ipairs(\%assignments.value) do local \%target, \%value = item.value[1], item.value[2] + \%value = \%value:map(function(t) + if Action:is_instance(t) and t:get_stub() == "?" then + return \%target + end + end) local target_lua = \(%target as lua) if not target_lua.is_value then error("Invalid target for assignment: "..\(%target as text)) end local value_lua = \(%value as lua) |
