Minor cleanups changes.

This commit is contained in:
Bruce Hill 2018-05-15 14:53:37 -07:00
parent 3be20840ca
commit ae979c3718
8 changed files with 57 additions and 102 deletions

View File

@ -262,7 +262,7 @@ do
end end
self.__str = nil self.__str = nil
end, end,
convert_to_statements = function(self, prefix, suffix) as_statements = function(self, prefix, suffix)
if prefix == nil then if prefix == nil then
prefix = "" prefix = ""
end end
@ -270,14 +270,17 @@ do
suffix = ";" suffix = ";"
end end
if not (self.is_value) then if not (self.is_value) then
return return self
end end
local statements = Lua(self.source)
if prefix ~= "" then if prefix ~= "" then
self:prepend(prefix) statements:append(prefix)
end end
statements:append(self)
if suffix ~= "" then if suffix ~= "" then
return self:append(suffix) statements:append(suffix)
end end
return statements
end, end,
declare_locals = function(self, to_declare) declare_locals = function(self, to_declare)
if to_declare == nil then if to_declare == nil then

View File

@ -167,13 +167,16 @@ class Lua extends Code
stack[#stack+1] = b stack[#stack+1] = b
@__str = nil @__str = nil
convert_to_statements: (prefix="", suffix=";")=> as_statements: (prefix="", suffix=";")=>
unless @is_value unless @is_value
return return self
statements = Lua(@source)
if prefix != "" if prefix != ""
@prepend prefix statements\append prefix
statements\append self
if suffix != "" if suffix != ""
@append suffix statements\append suffix
return statements
declare_locals: (to_declare=nil)=> declare_locals: (to_declare=nil)=>
if to_declare == nil if to_declare == nil

View File

@ -27,7 +27,7 @@ immediately
lua:append(arg); lua:append(arg);
end end
local body_lua = \%lua:as_lua(nomsu); local body_lua = \%lua:as_lua(nomsu);
body_lua:convert_to_statements("return "); body_lua = body_lua:as_statements("return ");
body_lua:remove_free_vars(args); body_lua:remove_free_vars(args);
body_lua:declare_locals(); body_lua:declare_locals();
lua:append(")\n ", body_lua, "\nend);"); lua:append(")\n ", body_lua, "\nend);");
@ -59,7 +59,7 @@ immediately
if i < #args then lua:append(", ") end if i < #args then lua:append(", ") end
end end
local body_lua = \%body:as_lua(nomsu); local body_lua = \%body:as_lua(nomsu);
body_lua:convert_to_statements("return "); body_lua = body_lua:as_statements("return ");
body_lua:remove_free_vars(args); body_lua:remove_free_vars(args);
body_lua:declare_locals(); body_lua:declare_locals();
lua:append(")\n ", body_lua, "\nend);") lua:append(")\n ", body_lua, "\nend);")
@ -122,10 +122,7 @@ immediately
return lua; return lua;
action [%tree as lua statements] action [%tree as lua statements]
lua> ".." =lua "\%tree:as_lua(nomsu):as_statements()"
local lua = \%tree:as_lua(nomsu);
lua:convert_to_statements();
return lua;
action [%tree with vars %vars] action [%tree with vars %vars]
=lua "nomsu:tree_with_replaced_vars(\%tree, \%vars)" =lua "nomsu:tree_with_replaced_vars(\%tree, \%vars)"
@ -183,7 +180,7 @@ immediately
end end
immediately immediately
compile [source] to: Lua value (=lua "tree.source") "tree.source" compile [source] to: Lua value "tree.source"
#.. #..
immediately immediately

View File

@ -377,8 +377,7 @@ do
end end
local tree = self:parse(nomsu_code) local tree = self:parse(nomsu_code)
assert(tree, "Failed to parse: " .. tostring(nomsu_code)) assert(tree, "Failed to parse: " .. tostring(nomsu_code))
local lua = tree:as_lua(self) local lua = tree:as_lua(self):as_statements()
lua:convert_to_statements()
lua:declare_locals() lua:declare_locals()
lua:prepend("-- File: " .. tostring(nomsu_code.source or "") .. "\n") lua:prepend("-- File: " .. tostring(nomsu_code.source or "") .. "\n")
if compile_fn then if compile_fn then
@ -545,14 +544,13 @@ do
initialize_core = function(self) initialize_core = function(self)
local nomsu = self local nomsu = self
self:define_compile_action("immediately %block", function(self, _block) self:define_compile_action("immediately %block", function(self, _block)
local lua = _block:as_lua(nomsu) local lua = _block:as_lua(nomsu):as_statements()
lua:convert_to_statements()
lua:declare_locals() lua:declare_locals()
nomsu:run_lua(lua) nomsu:run_lua(lua)
return Lua(self.source, "if IMMEDIATE then\n ", lua, "\nend") return Lua(self.source, "if IMMEDIATE then\n ", lua, "\nend")
end) end)
local add_lua_bits local add_lua_string_bits
add_lua_bits = function(lua, code) add_lua_string_bits = function(lua, code)
if code.type ~= "Text" then if code.type ~= "Text" then
lua:append(", ", code:as_lua(nomsu)) lua:append(", ", code:as_lua(nomsu))
return return
@ -575,34 +573,19 @@ do
end end
self:define_compile_action("Lua %code", function(self, _code) self:define_compile_action("Lua %code", function(self, _code)
local lua = Lua.Value(self.source, "Lua(", tostring(_code.source)) local lua = Lua.Value(self.source, "Lua(", tostring(_code.source))
add_lua_bits(lua, _code) add_lua_string_bits(lua, _code)
lua:append(")")
return lua
end)
self:define_compile_action("Lua %source %code", function(self, _source, _code)
local lua = Lua.Value(self.source, "Lua(", _source:as_lua(nomsu))
add_lua_bits(lua, _code)
lua:append(")") lua:append(")")
return lua return lua
end) end)
self:define_compile_action("Lua value %code", function(self, _code) self:define_compile_action("Lua value %code", function(self, _code)
local lua = Lua.Value(self.source, "Lua.Value(", tostring(_code.source)) local lua = Lua.Value(self.source, "Lua.Value(", tostring(_code.source))
add_lua_bits(lua, _code) add_lua_string_bits(lua, _code)
lua:append(")") lua:append(")")
return lua return lua
end) end)
self:define_compile_action("Lua value %source %code", function(self, _source, _code) local add_lua_bits
local lua = Lua.Value(self.source, "Lua.Value(", _source:as_lua(nomsu)) add_lua_bits = function(lua, code)
add_lua_bits(lua, _code) local _list_0 = code.value
lua:append(")")
return lua
end)
self:define_compile_action("lua> %code", function(self, _code)
if _code.type ~= "Text" then
return Lua.Value(self.source, "nomsu:run_lua(Lua(", repr(_code.source), ", ", repr(tostring(_code:as_lua(nomsu))), "))")
end
local lua = Lua(_code.source)
local _list_0 = _code.value
for _index_0 = 1, #_list_0 do for _index_0 = 1, #_list_0 do
local bit = _list_0[_index_0] local bit = _list_0[_index_0]
if type(bit) == "string" then if type(bit) == "string" then
@ -617,27 +600,18 @@ do
end end
end end
return lua return lua
end
self:define_compile_action("lua> %code", function(self, _code)
if _code.type ~= "Text" then
return Lua(self.source, "nomsu:run_lua(", _code:as_lua(nomsu), ");")
end
return add_lua_bits(Lua(_code.source), _code)
end) end)
self:define_compile_action("=lua %code", function(self, _code) self:define_compile_action("=lua %code", function(self, _code)
if _code.type ~= "Text" then if _code.type ~= "Text" then
return Lua.Value(self.source, "nomsu:run_lua(Lua(", repr(_code.source), ", ", repr(tostring(_code:as_lua(nomsu))), "))") return Lua.Value(self.source, "nomsu:run_lua(", _code:as_lua(nomsu), ":as_statements('return '))")
end end
local lua = Lua.Value(self.source) return add_lua_bits(Lua.Value(_code.source), _code)
local _list_0 = _code.value
for _index_0 = 1, #_list_0 do
local bit = _list_0[_index_0]
if type(bit) == "string" then
lua:append(bit)
else
local bit_lua = bit:as_lua(nomsu)
if not (lua.is_value) then
local line, src = bit.source:get_line(), bit.source:get_text()
error(tostring(line) .. ": Cannot use " .. tostring(colored.yellow(src)) .. " as a string interpolation value, since it's not an expression.", 0)
end
lua:append(bit_lua)
end
end
return lua
end) end)
return self:define_compile_action("use %path", function(self, _path) return self:define_compile_action("use %path", function(self, _path)
local path = nomsu:tree_to_value(_path) local path = nomsu:tree_to_value(_path)

View File

@ -336,8 +336,7 @@ class NomsuCompiler
if #tostring(nomsu_code) == 0 then return nil if #tostring(nomsu_code) == 0 then return nil
tree = @parse(nomsu_code) tree = @parse(nomsu_code)
assert tree, "Failed to parse: #{nomsu_code}" assert tree, "Failed to parse: #{nomsu_code}"
lua = tree\as_lua(@) lua = tree\as_lua(@)\as_statements!
lua\convert_to_statements!
lua\declare_locals! lua\declare_locals!
lua\prepend "-- File: #{nomsu_code.source or ""}\n" lua\prepend "-- File: #{nomsu_code.source or ""}\n"
if compile_fn if compile_fn
@ -447,13 +446,12 @@ class NomsuCompiler
-- Sets up some core functionality -- Sets up some core functionality
nomsu = self nomsu = self
@define_compile_action "immediately %block", (_block)=> @define_compile_action "immediately %block", (_block)=>
lua = _block\as_lua(nomsu) lua = _block\as_lua(nomsu)\as_statements!
lua\convert_to_statements!
lua\declare_locals! lua\declare_locals!
nomsu\run_lua(lua) nomsu\run_lua(lua)
return Lua(@source, "if IMMEDIATE then\n ", lua, "\nend") return Lua(@source, "if IMMEDIATE then\n ", lua, "\nend")
add_lua_bits = (lua, code)-> add_lua_string_bits = (lua, code)->
if code.type != "Text" if code.type != "Text"
lua\append ", ", code\as_lua(nomsu) lua\append ", ", code\as_lua(nomsu)
return return
@ -470,35 +468,18 @@ class NomsuCompiler
@define_compile_action "Lua %code", (_code)=> @define_compile_action "Lua %code", (_code)=>
lua = Lua.Value(@source, "Lua(", tostring(_code.source)) lua = Lua.Value(@source, "Lua(", tostring(_code.source))
add_lua_bits(lua, _code) add_lua_string_bits(lua, _code)
lua\append ")"
return lua
@define_compile_action "Lua %source %code", (_source, _code)=>
lua = Lua.Value(@source, "Lua(", _source\as_lua(nomsu))
add_lua_bits(lua, _code)
lua\append ")" lua\append ")"
return lua return lua
@define_compile_action "Lua value %code", (_code)=> @define_compile_action "Lua value %code", (_code)=>
lua = Lua.Value(@source, "Lua.Value(", tostring(_code.source)) lua = Lua.Value(@source, "Lua.Value(", tostring(_code.source))
add_lua_bits(lua, _code) add_lua_string_bits(lua, _code)
lua\append ")" lua\append ")"
return lua return lua
@define_compile_action "Lua value %source %code", (_source, _code)=> add_lua_bits = (lua, code)->
lua = Lua.Value(@source, "Lua.Value(", _source\as_lua(nomsu)) for bit in *code.value
add_lua_bits(lua, _code)
lua\append ")"
return lua
@define_compile_action "lua> %code", (_code)=>
if _code.type != "Text"
return Lua.Value @source, "nomsu:run_lua(Lua(",repr(_code.source),
", ",repr(tostring(_code\as_lua(nomsu))),"))"
lua = Lua(_code.source)
for bit in *_code.value
if type(bit) == "string" if type(bit) == "string"
lua\append bit lua\append bit
else else
@ -509,22 +490,15 @@ class NomsuCompiler
lua\append bit_lua lua\append bit_lua
return lua return lua
@define_compile_action "lua> %code", (_code)=>
if _code.type != "Text"
return Lua @source, "nomsu:run_lua(", _code\as_lua(nomsu), ");"
return add_lua_bits(Lua(_code.source), _code)
@define_compile_action "=lua %code", (_code)=> @define_compile_action "=lua %code", (_code)=>
if _code.type != "Text" if _code.type != "Text"
return Lua.Value @source, "nomsu:run_lua(Lua(",repr(_code.source), return Lua.Value @source, "nomsu:run_lua(", _code\as_lua(nomsu), ":as_statements('return '))"
", ",repr(tostring(_code\as_lua(nomsu))),"))" return add_lua_bits(Lua.Value(_code.source), _code)
lua = Lua.Value(@source)
for bit in *_code.value
if type(bit) == "string"
lua\append bit
else
bit_lua = bit\as_lua(nomsu)
unless lua.is_value
line, src = bit.source\get_line!, bit.source\get_text!
error "#{line}: Cannot use #{colored.yellow src} as a string interpolation value, since it's not an expression.", 0
lua\append bit_lua
return lua
@define_compile_action "use %path", (_path)=> @define_compile_action "use %path", (_path)=>
path = nomsu\tree_to_value(_path) path = nomsu\tree_to_value(_path)

View File

@ -79,8 +79,7 @@ Tree("Block", {
if i > 1 then if i > 1 then
lua:append("\n") lua:append("\n")
end end
line_lua:convert_to_statements() lua:append(line_lua:as_statements())
lua:append(line_lua)
end end
return lua return lua
end, end,

View File

@ -54,8 +54,7 @@ Tree "Block",
line_lua = line\as_lua(nomsu) line_lua = line\as_lua(nomsu)
if i > 1 if i > 1
lua\append "\n" lua\append "\n"
line_lua\convert_to_statements! lua\append line_lua\as_statements!
lua\append line_lua
return lua return lua
as_nomsu: (inline=false)=> as_nomsu: (inline=false)=>

View File

@ -55,3 +55,9 @@ assume (("x" as lua identifier) = (\%x as lua identifier)) or barf "converting t
assume ((run "return 99") = 99) or barf "run % failed." assume ((run "return 99") = 99) or barf "run % failed."
say "Metaprogramming test passed." say "Metaprogramming test passed."
%code <-: Lua "global_x = true;"
lua> %code
assume (=lua "global_x") or barf "Running lua from a variable failed."
%code <-: Lua value "global_x"
assume (=lua %code) or barf "Running lua from a variable failed."