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 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'lib/core/metaprogramming.nom') 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" -- cgit v1.2.3