From a1b559a3a269bbee1ae9a33061b08a868ea52f5c Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 25 Jan 2019 15:49:29 -0800 Subject: Added metatables for bool, number, function, coroutine. Added run-time check to make sure precompiled code used the same version of Lua. Methods can now be used in (* compiles to *), etc. --- lib/core/metaprogramming.nom | 27 +++++++++++++++++++-------- lib/core/operators.nom | 26 +++++++++++++++++++------- 2 files changed, 38 insertions(+), 15 deletions(-) (limited to 'lib/core') diff --git a/lib/core/metaprogramming.nom b/lib/core/metaprogramming.nom index da89665..096c62c 100644 --- a/lib/core/metaprogramming.nom +++ b/lib/core/metaprogramming.nom @@ -4,7 +4,7 @@ functions to make that easier. lua> "NOMSU_CORE_VERSION = 15" -lua> "NOMSU_LIB_VERSION = 8" +lua> "NOMSU_LIB_VERSION = 9" lua> (" do local mangle_index = 0 @@ -93,8 +93,9 @@ lua> (" if \$body.type == "Text" then \$body = SyntaxTree{source=\$body.source, type="Action", "Lua", \$body} end - if not (\$action.type == "Action" or (\$action.type == "EscapedNomsu" and \$action[1]\ - ...type == "Action")) then + if not (\$action.type == "Action" or + (\$action.type == "EscapedNomsu" and \$action[1].type == "Action") or + \$action.type == "MethodCall") then at_1_fail(\$action.source, "Compile error: ".. "This is neither an action nor an escaped action. ".. "Hint: This should probably be an action like:\\n" @@ -182,7 +183,7 @@ test: lua> (" local lua = \(\($actions.1 means $body) as lua) local first_def = (\$actions[1].type == "MethodCall" - and LuaCode(\(nomsu environment):compile(\$actions[1][1]), ".", \$actions[1]:get_stub():as_lua_id()) + and LuaCode(\(nomsu environment):compile(\$actions[1][1]), ".", \$actions[1][2]:get_stub():as_lua_id()) or LuaCode(\$actions[1]:get_stub():as_lua_id())) local \$args = a_List(\$actions[1]:get_args()) for i=2,#\$actions do @@ -190,7 +191,7 @@ test: local \$alias_args = a_List(alias:get_args()) lua:add("\\n") if alias.type == "MethodCall" then - lua:add(\(nomsu environment):compile(alias[1]), ".", alias:get_stub():as_lua_id()) + lua:add(\(nomsu environment):compile(alias[1]), ".", alias[2]:get_stub():as_lua_id()) else lua:add(alias:get_stub():as_lua_id()) lua:add_free_vars({alias_name}) @@ -481,9 +482,9 @@ test: ") # Literals -(yes) compiles to "true" -(no) compiles to "false" -[nothing, nil, null] all compile to "nil" +(yes) compiles to "(true)" +(no) compiles to "(false)" +[nothing, nil, null] all compile to "(nil)" (Nomsu syntax version) compiles to "NOMSU_SYNTAX_VERSION" (Nomsu compiler version) compiles to "NOMSU_COMPILER_VERSION" (core version) compiles to "NOMSU_CORE_VERSION" @@ -492,6 +493,16 @@ test: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +(at compilation $expr) compiles to: + lua> (" + local value = \(nomsu environment):run(\(\(return $expr))) + if lua_type_of(value) == 'table' or lua_type_of(value) == 'string' and value.as_lua then + return LuaCode(value:as_lua()) + else + return LuaCode(tostring(value)) + end + ") + test: using compile rules: (yes) compiles to "3" diff --git a/lib/core/operators.nom b/lib/core/operators.nom index c2e6b57..4bdb5bc 100644 --- a/lib/core/operators.nom +++ b/lib/core/operators.nom @@ -159,6 +159,18 @@ test: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Bitwise Operators +# These can break if running the precompiled code from another Lua version: +lua> (" + if \(at compilation $(LUA VERSION)) ~= \$(LUA VERSION) then + \( + fail (" + This code was precompiled with \(at compilation $(LUA VERSION)), but it is being run with \$(LUA VERSION)\ + ... This will cause problems with bitwise operations. + ") + ) + end +") + # TODO: implement OR, XOR, AND for multiple operands? test: assume ((~ (~ 5)) == 5) @@ -170,22 +182,22 @@ test: # Lua 5.3 introduced bit operators like | and &. Use them when possible, otherwise fall back to bit.bor(), bit.band(), etc. -lua> "if \((is jit) or ((Lua version) == "Lua 5.2")) then" -[NOT $, ~ $] all compile to "bit.bnot(\($ as lua expr))" +lua> "if \((is jit) or ($(LUA API) == "Lua 5.2")) then" +[NOT $, ~ $] all compile to "Bit.bnot(\($ as lua expr))" [$x OR $y, $x | $y] all compile to - "bit.bor(\($x as lua expr), \($y as lua expr))" + "Bit.bor(\($x as lua expr), \($y as lua expr))" [$x XOR $y, $x ~ $y] all compile to - "bit.bxor(\($x as lua expr), \($y as lua expr))" + "Bit.bxor(\($x as lua expr), \($y as lua expr))" [$x AND $y, $x & $y] all compile to - "bit.band(\($x as lua expr), \($y as lua expr))" + "Bit.band(\($x as lua expr), \($y as lua expr))" [$x LSHIFT $shift, $x << $shift] all compile to - "bit.lshift(\($x as lua expr), \($shift as lua expr))" + "Bit.lshift(\($x as lua expr), \($shift as lua expr))" [$x RSHIFT $shift, $x >> $shift] all compile to - "bit.rshift(\($x as lua expr), \($shift as lua expr))" + "Bit.rshift(\($x as lua expr), \($shift as lua expr))" lua> "else" [NOT $, ~ $] all compile to "~(\($ as lua expr))" -- cgit v1.2.3