Added _1_as_text(), which converts true -> yes, false -> no.

This commit is contained in:
Bruce Hill 2019-01-23 15:42:38 -08:00
parent 9441a81011
commit fb20a1f48a
3 changed files with 57 additions and 31 deletions

View File

@ -273,7 +273,7 @@ test:
return replacements[t:as_var()] return replacements[t:as_var()]
else else
return "SyntaxTree{mangle("..t:as_var():as_lua().."), type="..t.type:as_lua(\ 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 end
elseif SyntaxTree:is_instance(t) then elseif SyntaxTree:is_instance(t) then
local ret = {} local ret = {}
@ -283,7 +283,7 @@ test:
ret[#ret+1] = make_tree(t[i]) ret[#ret+1] = make_tree(t[i])
i = i + 1 i = i + 1
elseif k == "source" then 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 elseif lua_type_of(k) == 'string' and k:is_a_lua_id() then
ret[#ret+1] = k.."= "..make_tree(v) ret[#ret+1] = k.."= "..make_tree(v)
else else
@ -292,7 +292,7 @@ test:
end end
return "SyntaxTree{"..table.concat(ret, ", ").."}" return "SyntaxTree{"..table.concat(ret, ", ").."}"
elseif lua_type_of(t) == 'number' then elseif lua_type_of(t) == 'number' then
return tostring(t) return _1_as_text(t)
else else
return t:as_lua() return t:as_lua()
end end
@ -419,7 +419,9 @@ test:
") ")
) == "\"one\\n\\\"two\\\"\"" ) == "\"one\\n\\\"two\\\"\""
(quote $s) compiles to "tostring(\($s as lua expr)):as_lua()" external:
(quote $) means ($ as text, as lua)
test: test:
assume (lua type of {}) == "table" assume (lua type of {}) == "table"
assume (type of {}) == "a Dict" assume (type of {}) == "a Dict"

View File

@ -78,6 +78,16 @@ Importer = function(t, imports)
t._IMPORTS = _module_imports[t] t._IMPORTS = _module_imports[t]
return setmetatable(t, _importer_mt) return setmetatable(t, _importer_mt)
end 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 local nomsu_environment
nomsu_environment = Importer({ nomsu_environment = Importer({
NOMSU_COMPILER_VERSION = 13, NOMSU_COMPILER_VERSION = 13,
@ -101,7 +111,7 @@ nomsu_environment = Importer({
tostring = tostring, tostring = tostring,
string = string, string = string,
xpcall = xpcall, xpcall = xpcall,
say = print, print = print,
loadfile = loadfile, loadfile = loadfile,
rawset = rawset, rawset = rawset,
_VERSION = _VERSION, _VERSION = _VERSION,
@ -147,6 +157,7 @@ nomsu_environment = Importer({
_1_as_inline_nomsu = tree_to_inline_nomsu, _1_as_inline_nomsu = tree_to_inline_nomsu,
compile = compile, compile = compile,
at_1_fail = fail_at, at_1_fail = fail_at,
_1_as_text = _1_as_text,
exit = os.exit, exit = os.exit,
quit = os.exit, quit = os.exit,
_1_parsed = function(nomsu_code, syntax_version) _1_parsed = function(nomsu_code, syntax_version)
@ -251,7 +262,10 @@ nomsu_environment = Importer({
code = NomsuCode:from(Source(path, 1, #code), code) code = NomsuCode:from(Source(path, 1, #code), code)
end end
_currently_running_files:add(path) _currently_running_files:add(path)
mod:run(code) local ret = mod:run(code)
if ret ~= nil then
mod = ret
end
_currently_running_files:pop() _currently_running_files:pop()
package.nomsuloaded[package_name] = mod package.nomsuloaded[package_name] = mod
package.nomsuloaded[path] = mod package.nomsuloaded[path] = mod
@ -264,8 +278,10 @@ nomsu_environment = Importer({
imports[k] = v imports[k] = v
end end
local cr_imports = assert(_module_imports[self.COMPILE_RULES]) local cr_imports = assert(_module_imports[self.COMPILE_RULES])
for k, v in pairs(mod.COMPILE_RULES) do if mod.COMPILE_RULES then
cr_imports[k] = v for k, v in pairs(mod.COMPILE_RULES) do
cr_imports[k] = v
end
end end
return mod return mod
end, end,
@ -283,14 +299,16 @@ nomsu_environment = Importer({
end end
end end
local cr_imports = assert(_module_imports[self.COMPILE_RULES]) local cr_imports = assert(_module_imports[self.COMPILE_RULES])
for k, v in pairs(_module_imports[mod.COMPILE_RULES]) do if mod.COMPILE_RULES then
if rawget(cr_imports, k) == nil then for k, v in pairs(_module_imports[mod.COMPILE_RULES]) do
cr_imports[k] = v if rawget(cr_imports, k) == nil then
cr_imports[k] = v
end
end end
end for k, v in pairs(mod.COMPILE_RULES) do
for k, v in pairs(mod.COMPILE_RULES) do if rawget(self.COMPILE_RULES, k) == nil then
if rawget(self.COMPILE_RULES, k) == nil then self.COMPILE_RULES[k] = v
self.COMPILE_RULES[k] = v end
end end
end end
return mod return mod
@ -377,7 +395,7 @@ nomsu_environment = Importer({
end end
return run_lua_fn() return run_lua_fn()
else 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
end, end,
new_environment = function() new_environment = function()

View File

@ -43,6 +43,11 @@ Importer = (t, imports)->
t._IMPORTS = _module_imports[t] t._IMPORTS = _module_imports[t]
return setmetatable(t, _importer_mt) 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 local nomsu_environment
nomsu_environment = Importer{ nomsu_environment = Importer{
NOMSU_COMPILER_VERSION: 13, NOMSU_SYNTAX_VERSION: max_parser_version 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, yield:coroutine.yield, resume:coroutine.resume, coroutine_status_of:coroutine.status,
coroutine_wrap:coroutine.wrap, coroutine_from: coroutine.create, coroutine_wrap:coroutine.wrap, coroutine_from: coroutine.create,
:error, :package, :os, :require, :tonumber, :tostring, :string, :xpcall, :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, :table, :assert, :dofile, :loadstring, lua_type_of:type, :select, :math, :io, :load,
:pairs, :ipairs, :jit, :_VERSION :pairs, :ipairs, :jit, :_VERSION
bit: (jit or _VERSION == "Lua 5.2") and require('bitops') or nil bit: (jit or _VERSION == "Lua 5.2") and require('bitops') or nil
@ -69,7 +74,7 @@ nomsu_environment = Importer{
-- Nomsu functions: -- Nomsu functions:
_1_as_nomsu:tree_to_nomsu, _1_as_inline_nomsu:tree_to_inline_nomsu, _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, exit:os.exit, quit:os.exit,
_1_parsed: (nomsu_code, syntax_version)-> _1_parsed: (nomsu_code, syntax_version)->
@ -133,7 +138,9 @@ nomsu_environment = Importer{
else else
code = NomsuCode\from(Source(path, 1, #code), code) code = NomsuCode\from(Source(path, 1, #code), code)
_currently_running_files\add path _currently_running_files\add path
mod\run(code) ret = mod\run(code)
if ret != nil
mod = ret
_currently_running_files\pop! _currently_running_files\pop!
package.nomsuloaded[package_name] = mod package.nomsuloaded[package_name] = mod
package.nomsuloaded[path] = mod package.nomsuloaded[path] = mod
@ -145,8 +152,9 @@ nomsu_environment = Importer{
for k,v in pairs(mod) for k,v in pairs(mod)
imports[k] = v imports[k] = v
cr_imports = assert _module_imports[@COMPILE_RULES] cr_imports = assert _module_imports[@COMPILE_RULES]
for k,v in pairs(mod.COMPILE_RULES) if mod.COMPILE_RULES
cr_imports[k] = v for k,v in pairs(mod.COMPILE_RULES)
cr_imports[k] = v
return mod return mod
export: (package_name)=> export: (package_name)=>
@ -160,15 +168,13 @@ nomsu_environment = Importer{
--if k != "_G" and k != "_ENV" and k != "COMPILE_RULES" and k != "MODULE_NAME" --if k != "_G" and k != "_ENV" and k != "COMPILE_RULES" and k != "MODULE_NAME"
@[k] = v @[k] = v
cr_imports = assert _module_imports[@COMPILE_RULES] cr_imports = assert _module_imports[@COMPILE_RULES]
for k,v in pairs(_module_imports[mod.COMPILE_RULES]) if mod.COMPILE_RULES
if rawget(cr_imports, k) == nil for k,v in pairs(_module_imports[mod.COMPILE_RULES])
cr_imports[k] = v if rawget(cr_imports, k) == nil
for k,v in pairs(mod.COMPILE_RULES) cr_imports[k] = v
if rawget(@COMPILE_RULES, k) == nil for k,v in pairs(mod.COMPILE_RULES)
@COMPILE_RULES[k] = v if rawget(@COMPILE_RULES, k) == nil
--for k,v in pairs(mod.TESTS) @COMPILE_RULES[k] = v
-- if rawget(@TESTS, k) == nil
-- @TESTS[k] = v
return mod return mod
run: (to_run)=> run: (to_run)=>
@ -230,7 +236,7 @@ nomsu_environment = Importer{
@SOURCE_MAP[source_key] = map @SOURCE_MAP[source_key] = map
return run_lua_fn! return run_lua_fn!
else else
error("Attempt to run unknown thing: "..tostring(to_run)) error("Attempt to run unknown thing: ".._1_as_lua(to_run))
new_environment: -> new_environment: ->
env = Importer({}, {k,v for k,v in pairs(nomsu_environment)}) env = Importer({}, {k,v for k,v in pairs(nomsu_environment)})