From fb20a1f48aae0fd1235529fa9caad45a502f5944 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 23 Jan 2019 15:42:38 -0800 Subject: [PATCH] Added _1_as_text(), which converts true -> yes, false -> no. --- lib/core/metaprogramming.nom | 10 +++++---- nomsu_environment.lua | 42 +++++++++++++++++++++++++----------- nomsu_environment.moon | 36 ++++++++++++++++++------------- 3 files changed, 57 insertions(+), 31 deletions(-) diff --git a/lib/core/metaprogramming.nom b/lib/core/metaprogramming.nom index c7d3787..da89665 100644 --- a/lib/core/metaprogramming.nom +++ b/lib/core/metaprogramming.nom @@ -273,7 +273,7 @@ test: return replacements[t:as_var()] else return "SyntaxTree{mangle("..t:as_var():as_lua().."), type="..t.type:as_lua(\ - ..)..", source="..tostring(t.source):as_lua().."}" + ..)..", source=".._1_as_text(t.source):as_lua().."}" end elseif SyntaxTree:is_instance(t) then local ret = {} @@ -283,7 +283,7 @@ test: ret[#ret+1] = make_tree(t[i]) i = i + 1 elseif k == "source" then - ret[#ret+1] = k.."= "..tostring(v):as_lua() + ret[#ret+1] = k.."= ".._1_as_text(v):as_lua() elseif lua_type_of(k) == 'string' and k:is_a_lua_id() then ret[#ret+1] = k.."= "..make_tree(v) else @@ -292,7 +292,7 @@ test: end return "SyntaxTree{"..table.concat(ret, ", ").."}" elseif lua_type_of(t) == 'number' then - return tostring(t) + return _1_as_text(t) else return t:as_lua() end @@ -419,7 +419,9 @@ test: ") ) == "\"one\\n\\\"two\\\"\"" -(quote $s) compiles to "tostring(\($s as lua expr)):as_lua()" +external: + (quote $) means ($ as text, as lua) + test: assume (lua type of {}) == "table" assume (type of {}) == "a Dict" diff --git a/nomsu_environment.lua b/nomsu_environment.lua index 08fd662..0fdf1cf 100644 --- a/nomsu_environment.lua +++ b/nomsu_environment.lua @@ -78,6 +78,16 @@ Importer = function(t, imports) t._IMPORTS = _module_imports[t] return setmetatable(t, _importer_mt) end +local _1_as_text +_1_as_text = function(x) + if x == true then + return "yes" + end + if x == false then + return "no" + end + return tostring(x) +end local nomsu_environment nomsu_environment = Importer({ NOMSU_COMPILER_VERSION = 13, @@ -101,7 +111,7 @@ nomsu_environment = Importer({ tostring = tostring, string = string, xpcall = xpcall, - say = print, + print = print, loadfile = loadfile, rawset = rawset, _VERSION = _VERSION, @@ -147,6 +157,7 @@ nomsu_environment = Importer({ _1_as_inline_nomsu = tree_to_inline_nomsu, compile = compile, at_1_fail = fail_at, + _1_as_text = _1_as_text, exit = os.exit, quit = os.exit, _1_parsed = function(nomsu_code, syntax_version) @@ -251,7 +262,10 @@ nomsu_environment = Importer({ code = NomsuCode:from(Source(path, 1, #code), code) end _currently_running_files:add(path) - mod:run(code) + local ret = mod:run(code) + if ret ~= nil then + mod = ret + end _currently_running_files:pop() package.nomsuloaded[package_name] = mod package.nomsuloaded[path] = mod @@ -264,8 +278,10 @@ nomsu_environment = Importer({ imports[k] = v end local cr_imports = assert(_module_imports[self.COMPILE_RULES]) - for k, v in pairs(mod.COMPILE_RULES) do - cr_imports[k] = v + if mod.COMPILE_RULES then + for k, v in pairs(mod.COMPILE_RULES) do + cr_imports[k] = v + end end return mod end, @@ -283,14 +299,16 @@ nomsu_environment = Importer({ end end local cr_imports = assert(_module_imports[self.COMPILE_RULES]) - for k, v in pairs(_module_imports[mod.COMPILE_RULES]) do - if rawget(cr_imports, k) == nil then - cr_imports[k] = v + if mod.COMPILE_RULES then + for k, v in pairs(_module_imports[mod.COMPILE_RULES]) do + if rawget(cr_imports, k) == nil then + cr_imports[k] = v + end end - end - for k, v in pairs(mod.COMPILE_RULES) do - if rawget(self.COMPILE_RULES, k) == nil then - self.COMPILE_RULES[k] = v + for k, v in pairs(mod.COMPILE_RULES) do + if rawget(self.COMPILE_RULES, k) == nil then + self.COMPILE_RULES[k] = v + end end end return mod @@ -377,7 +395,7 @@ nomsu_environment = Importer({ end return run_lua_fn() else - return error("Attempt to run unknown thing: " .. tostring(to_run)) + return error("Attempt to run unknown thing: " .. _1_as_lua(to_run)) end end, new_environment = function() diff --git a/nomsu_environment.moon b/nomsu_environment.moon index 1ace227..8cd3228 100644 --- a/nomsu_environment.moon +++ b/nomsu_environment.moon @@ -43,6 +43,11 @@ Importer = (t, imports)-> t._IMPORTS = _module_imports[t] return setmetatable(t, _importer_mt) +_1_as_text = (x)-> + if x == true then return "yes" + if x == false then return "no" + return tostring(x) + local nomsu_environment nomsu_environment = Importer{ NOMSU_COMPILER_VERSION: 13, NOMSU_SYNTAX_VERSION: max_parser_version @@ -51,7 +56,7 @@ nomsu_environment = Importer{ yield:coroutine.yield, resume:coroutine.resume, coroutine_status_of:coroutine.status, coroutine_wrap:coroutine.wrap, coroutine_from: coroutine.create, :error, :package, :os, :require, :tonumber, :tostring, :string, :xpcall, - say:print, :loadfile, :rawset, :_VERSION, :collectgarbage, :rawget, :rawlen, + :print, :loadfile, :rawset, :_VERSION, :collectgarbage, :rawget, :rawlen, :table, :assert, :dofile, :loadstring, lua_type_of:type, :select, :math, :io, :load, :pairs, :ipairs, :jit, :_VERSION bit: (jit or _VERSION == "Lua 5.2") and require('bitops') or nil @@ -69,7 +74,7 @@ nomsu_environment = Importer{ -- Nomsu functions: _1_as_nomsu:tree_to_nomsu, _1_as_inline_nomsu:tree_to_inline_nomsu, - compile: compile, at_1_fail:fail_at, + compile: compile, at_1_fail:fail_at, _1_as_text:_1_as_text, exit:os.exit, quit:os.exit, _1_parsed: (nomsu_code, syntax_version)-> @@ -133,7 +138,9 @@ nomsu_environment = Importer{ else code = NomsuCode\from(Source(path, 1, #code), code) _currently_running_files\add path - mod\run(code) + ret = mod\run(code) + if ret != nil + mod = ret _currently_running_files\pop! package.nomsuloaded[package_name] = mod package.nomsuloaded[path] = mod @@ -145,8 +152,9 @@ nomsu_environment = Importer{ for k,v in pairs(mod) imports[k] = v cr_imports = assert _module_imports[@COMPILE_RULES] - for k,v in pairs(mod.COMPILE_RULES) - cr_imports[k] = v + if mod.COMPILE_RULES + for k,v in pairs(mod.COMPILE_RULES) + cr_imports[k] = v return mod export: (package_name)=> @@ -160,15 +168,13 @@ nomsu_environment = Importer{ --if k != "_G" and k != "_ENV" and k != "COMPILE_RULES" and k != "MODULE_NAME" @[k] = v cr_imports = assert _module_imports[@COMPILE_RULES] - for k,v in pairs(_module_imports[mod.COMPILE_RULES]) - if rawget(cr_imports, k) == nil - cr_imports[k] = v - for k,v in pairs(mod.COMPILE_RULES) - if rawget(@COMPILE_RULES, k) == nil - @COMPILE_RULES[k] = v - --for k,v in pairs(mod.TESTS) - -- if rawget(@TESTS, k) == nil - -- @TESTS[k] = v + if mod.COMPILE_RULES + for k,v in pairs(_module_imports[mod.COMPILE_RULES]) + if rawget(cr_imports, k) == nil + cr_imports[k] = v + for k,v in pairs(mod.COMPILE_RULES) + if rawget(@COMPILE_RULES, k) == nil + @COMPILE_RULES[k] = v return mod run: (to_run)=> @@ -230,7 +236,7 @@ nomsu_environment = Importer{ @SOURCE_MAP[source_key] = map return run_lua_fn! else - error("Attempt to run unknown thing: "..tostring(to_run)) + error("Attempt to run unknown thing: ".._1_as_lua(to_run)) new_environment: -> env = Importer({}, {k,v for k,v in pairs(nomsu_environment)})