aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2017-10-10 00:52:07 -0700
committerBruce Hill <bitbucket@bruce-hill.com>2017-10-10 00:52:07 -0700
commitd0b0cfcf397fea5d71e986d5e1a2e3b7aab5fcfe (patch)
tree45f97fc9b2ad36931e9a72ff14da0c759d9f6f89
parenta858775a6831e5d880d9c0f88470f12a7e9b91f9 (diff)
Added an option to pass in vars to run()
-rw-r--r--nomsu.lua14
-rwxr-xr-xnomsu.moon9
2 files changed, 12 insertions, 11 deletions
diff --git a/nomsu.lua b/nomsu.lua
index 496cd28..11dd792 100644
--- a/nomsu.lua
+++ b/nomsu.lua
@@ -332,7 +332,10 @@ do
end
return tree
end,
- run = function(self, src, filename, max_operations)
+ run = function(self, src, filename, vars, max_operations)
+ if vars == nil then
+ vars = { }
+ end
if max_operations == nil then
max_operations = nil
end
@@ -348,7 +351,6 @@ do
assert(tree, "Tree failed to compile: " .. tostring(src))
assert(tree.type == "File", "Attempt to run non-file: " .. tostring(tree.type))
local buffer = { }
- local vars = { }
local return_value = nil
local _list_0 = tree.value
for _index_0 = 1, #_list_0 do
@@ -394,15 +396,15 @@ do
end
insert(buffer, tostring(statements or '') .. "\n" .. tostring(expr and "ret = " .. tostring(expr) or ''))
end
+ if max_operations then
+ debug.sethook()
+ end
local lua_code = ([[ return (function(nomsu, vars)
local ret;
%s
return ret;
end);]]):format(concat(buffer, "\n"))
- if max_operations then
- debug.sethook()
- end
- return return_value, lua_code
+ return return_value, lua_code, vars
end,
tree_to_value = function(self, tree, vars)
local code = "return (function(nomsu, vars)\nreturn " .. tostring(self:tree_to_lua(tree)) .. ";\nend);"
diff --git a/nomsu.moon b/nomsu.moon
index bf5c979..1695906 100755
--- a/nomsu.moon
+++ b/nomsu.moon
@@ -274,7 +274,7 @@ class NomsuCompiler
@print_tree tree, " "
return tree
- run: (src, filename, max_operations=nil)=>
+ run: (src, filename, vars={}, max_operations=nil)=>
if max_operations
timeout = ->
debug.sethook!
@@ -285,7 +285,6 @@ class NomsuCompiler
assert tree.type == "File", "Attempt to run non-file: #{tree.type}"
buffer = {}
- vars = {}
return_value = nil
for statement in *tree.value
if @debug
@@ -320,15 +319,15 @@ class NomsuCompiler
@error(ret)
insert buffer, "#{statements or ''}\n#{expr and "ret = #{expr}" or ''}"
+ if max_operations
+ debug.sethook!
lua_code = ([[
return (function(nomsu, vars)
local ret;
%s
return ret;
end);]])\format(concat(buffer, "\n"))
- if max_operations
- debug.sethook!
- return return_value, lua_code
+ return return_value, lua_code, vars
tree_to_value: (tree, vars)=>
code = "return (function(nomsu, vars)\nreturn #{@tree_to_lua(tree)};\nend);"