aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--code_obj.lua11
-rw-r--r--code_obj.moon11
-rw-r--r--core/metaprogramming.nom11
-rw-r--r--nomsu.lua60
-rwxr-xr-xnomsu.moon54
-rw-r--r--nomsu_tree.lua3
-rw-r--r--nomsu_tree.moon3
-rw-r--r--tests/metaprogramming.nom6
8 files changed, 57 insertions, 102 deletions
diff --git a/code_obj.lua b/code_obj.lua
index 4c9eac0..2e1ce5e 100644
--- a/code_obj.lua
+++ b/code_obj.lua
@@ -262,7 +262,7 @@ do
end
self.__str = nil
end,
- convert_to_statements = function(self, prefix, suffix)
+ as_statements = function(self, prefix, suffix)
if prefix == nil then
prefix = ""
end
@@ -270,14 +270,17 @@ do
suffix = ";"
end
if not (self.is_value) then
- return
+ return self
end
+ local statements = Lua(self.source)
if prefix ~= "" then
- self:prepend(prefix)
+ statements:append(prefix)
end
+ statements:append(self)
if suffix ~= "" then
- return self:append(suffix)
+ statements:append(suffix)
end
+ return statements
end,
declare_locals = function(self, to_declare)
if to_declare == nil then
diff --git a/code_obj.moon b/code_obj.moon
index caccd2b..ec5bc46 100644
--- a/code_obj.moon
+++ b/code_obj.moon
@@ -167,13 +167,16 @@ class Lua extends Code
stack[#stack+1] = b
@__str = nil
- convert_to_statements: (prefix="", suffix=";")=>
+ as_statements: (prefix="", suffix=";")=>
unless @is_value
- return
+ return self
+ statements = Lua(@source)
if prefix != ""
- @prepend prefix
+ statements\append prefix
+ statements\append self
if suffix != ""
- @append suffix
+ statements\append suffix
+ return statements
declare_locals: (to_declare=nil)=>
if to_declare == nil
diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom
index a63ec32..eafe583 100644
--- a/core/metaprogramming.nom
+++ b/core/metaprogramming.nom
@@ -27,7 +27,7 @@ immediately
lua:append(arg);
end
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:declare_locals();
lua:append(")\n ", body_lua, "\nend);");
@@ -59,7 +59,7 @@ immediately
if i < #args then lua:append(", ") end
end
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:declare_locals();
lua:append(")\n ", body_lua, "\nend);")
@@ -122,10 +122,7 @@ immediately
return lua;
action [%tree as lua statements]
- lua> ".."
- local lua = \%tree:as_lua(nomsu);
- lua:convert_to_statements();
- return lua;
+ =lua "\%tree:as_lua(nomsu):as_statements()"
action [%tree with vars %vars]
=lua "nomsu:tree_with_replaced_vars(\%tree, \%vars)"
@@ -183,7 +180,7 @@ immediately
end
immediately
- compile [source] to: Lua value (=lua "tree.source") "tree.source"
+ compile [source] to: Lua value "tree.source"
#..
immediately
diff --git a/nomsu.lua b/nomsu.lua
index 7329c2e..1231b57 100644
--- a/nomsu.lua
+++ b/nomsu.lua
@@ -377,8 +377,7 @@ do
end
local tree = self:parse(nomsu_code)
assert(tree, "Failed to parse: " .. tostring(nomsu_code))
- local lua = tree:as_lua(self)
- lua:convert_to_statements()
+ local lua = tree:as_lua(self):as_statements()
lua:declare_locals()
lua:prepend("-- File: " .. tostring(nomsu_code.source or "") .. "\n")
if compile_fn then
@@ -545,14 +544,13 @@ do
initialize_core = function(self)
local nomsu = self
self:define_compile_action("immediately %block", function(self, _block)
- local lua = _block:as_lua(nomsu)
- lua:convert_to_statements()
+ local lua = _block:as_lua(nomsu):as_statements()
lua:declare_locals()
nomsu:run_lua(lua)
return Lua(self.source, "if IMMEDIATE then\n ", lua, "\nend")
end)
- local add_lua_bits
- add_lua_bits = function(lua, code)
+ local add_lua_string_bits
+ add_lua_string_bits = function(lua, code)
if code.type ~= "Text" then
lua:append(", ", code:as_lua(nomsu))
return
@@ -575,34 +573,19 @@ do
end
self:define_compile_action("Lua %code", function(self, _code)
local lua = Lua.Value(self.source, "Lua(", tostring(_code.source))
- add_lua_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)
+ add_lua_string_bits(lua, _code)
lua:append(")")
return lua
end)
self:define_compile_action("Lua value %code", function(self, _code)
local lua = Lua.Value(self.source, "Lua.Value(", tostring(_code.source))
- add_lua_bits(lua, _code)
- lua:append(")")
- return lua
- end)
- self:define_compile_action("Lua value %source %code", function(self, _source, _code)
- local lua = Lua.Value(self.source, "Lua.Value(", _source:as_lua(nomsu))
- add_lua_bits(lua, _code)
+ add_lua_string_bits(lua, _code)
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
+ local add_lua_bits
+ add_lua_bits = function(lua, code)
+ local _list_0 = code.value
for _index_0 = 1, #_list_0 do
local bit = _list_0[_index_0]
if type(bit) == "string" then
@@ -617,27 +600,18 @@ do
end
end
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)
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))), "))")
+ return Lua.Value(self.source, "nomsu:run_lua(", _code:as_lua(nomsu), ":as_statements('return '))")
end
- local lua = Lua.Value(self.source)
- 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
+ return add_lua_bits(Lua.Value(_code.source), _code)
end)
return self:define_compile_action("use %path", function(self, _path)
local path = nomsu:tree_to_value(_path)
diff --git a/nomsu.moon b/nomsu.moon
index c22136d..79ecd63 100755
--- a/nomsu.moon
+++ b/nomsu.moon
@@ -336,8 +336,7 @@ class NomsuCompiler
if #tostring(nomsu_code) == 0 then return nil
tree = @parse(nomsu_code)
assert tree, "Failed to parse: #{nomsu_code}"
- lua = tree\as_lua(@)
- lua\convert_to_statements!
+ lua = tree\as_lua(@)\as_statements!
lua\declare_locals!
lua\prepend "-- File: #{nomsu_code.source or ""}\n"
if compile_fn
@@ -447,13 +446,12 @@ class NomsuCompiler
-- Sets up some core functionality
nomsu = self
@define_compile_action "immediately %block", (_block)=>
- lua = _block\as_lua(nomsu)
- lua\convert_to_statements!
+ lua = _block\as_lua(nomsu)\as_statements!
lua\declare_locals!
nomsu\run_lua(lua)
return Lua(@source, "if IMMEDIATE then\n ", lua, "\nend")
- add_lua_bits = (lua, code)->
+ add_lua_string_bits = (lua, code)->
if code.type != "Text"
lua\append ", ", code\as_lua(nomsu)
return
@@ -470,35 +468,18 @@ class NomsuCompiler
@define_compile_action "Lua %code", (_code)=>
lua = Lua.Value(@source, "Lua(", tostring(_code.source))
- add_lua_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)
+ add_lua_string_bits(lua, _code)
lua\append ")"
return lua
@define_compile_action "Lua value %code", (_code)=>
lua = Lua.Value(@source, "Lua.Value(", tostring(_code.source))
- add_lua_bits(lua, _code)
- lua\append ")"
- return lua
-
- @define_compile_action "Lua value %source %code", (_source, _code)=>
- lua = Lua.Value(@source, "Lua.Value(", _source\as_lua(nomsu))
- add_lua_bits(lua, _code)
+ add_lua_string_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
+ add_lua_bits = (lua, code)->
+ for bit in *code.value
if type(bit) == "string"
lua\append bit
else
@@ -509,22 +490,15 @@ class NomsuCompiler
lua\append bit_lua
return lua
- @define_compile_action "=lua %code", (_code)=>
+ @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))),"))"
+ return Lua @source, "nomsu:run_lua(", _code\as_lua(nomsu), ");"
+ return add_lua_bits(Lua(_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 "=lua %code", (_code)=>
+ if _code.type != "Text"
+ return Lua.Value @source, "nomsu:run_lua(", _code\as_lua(nomsu), ":as_statements('return '))"
+ return add_lua_bits(Lua.Value(_code.source), _code)
@define_compile_action "use %path", (_path)=>
path = nomsu\tree_to_value(_path)
diff --git a/nomsu_tree.lua b/nomsu_tree.lua
index c800c65..1fd1687 100644
--- a/nomsu_tree.lua
+++ b/nomsu_tree.lua
@@ -79,8 +79,7 @@ Tree("Block", {
if i > 1 then
lua:append("\n")
end
- line_lua:convert_to_statements()
- lua:append(line_lua)
+ lua:append(line_lua:as_statements())
end
return lua
end,
diff --git a/nomsu_tree.moon b/nomsu_tree.moon
index fc71219..f63f036 100644
--- a/nomsu_tree.moon
+++ b/nomsu_tree.moon
@@ -54,8 +54,7 @@ Tree "Block",
line_lua = line\as_lua(nomsu)
if i > 1
lua\append "\n"
- line_lua\convert_to_statements!
- lua\append line_lua
+ lua\append line_lua\as_statements!
return lua
as_nomsu: (inline=false)=>
diff --git a/tests/metaprogramming.nom b/tests/metaprogramming.nom
index c6aa719..395f4c6 100644
--- a/tests/metaprogramming.nom
+++ b/tests/metaprogramming.nom
@@ -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."
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."