Added an option to pass in vars to run()
This commit is contained in:
parent
a858775a68
commit
d0b0cfcf39
14
nomsu.lua
14
nomsu.lua
@ -332,7 +332,10 @@ do
|
|||||||
end
|
end
|
||||||
return tree
|
return tree
|
||||||
end,
|
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
|
if max_operations == nil then
|
||||||
max_operations = nil
|
max_operations = nil
|
||||||
end
|
end
|
||||||
@ -348,7 +351,6 @@ do
|
|||||||
assert(tree, "Tree failed to compile: " .. tostring(src))
|
assert(tree, "Tree failed to compile: " .. tostring(src))
|
||||||
assert(tree.type == "File", "Attempt to run non-file: " .. tostring(tree.type))
|
assert(tree.type == "File", "Attempt to run non-file: " .. tostring(tree.type))
|
||||||
local buffer = { }
|
local buffer = { }
|
||||||
local vars = { }
|
|
||||||
local return_value = nil
|
local return_value = nil
|
||||||
local _list_0 = tree.value
|
local _list_0 = tree.value
|
||||||
for _index_0 = 1, #_list_0 do
|
for _index_0 = 1, #_list_0 do
|
||||||
@ -394,15 +396,15 @@ do
|
|||||||
end
|
end
|
||||||
insert(buffer, tostring(statements or '') .. "\n" .. tostring(expr and "ret = " .. tostring(expr) or ''))
|
insert(buffer, tostring(statements or '') .. "\n" .. tostring(expr and "ret = " .. tostring(expr) or ''))
|
||||||
end
|
end
|
||||||
|
if max_operations then
|
||||||
|
debug.sethook()
|
||||||
|
end
|
||||||
local lua_code = ([[ return (function(nomsu, vars)
|
local lua_code = ([[ return (function(nomsu, vars)
|
||||||
local ret;
|
local ret;
|
||||||
%s
|
%s
|
||||||
return ret;
|
return ret;
|
||||||
end);]]):format(concat(buffer, "\n"))
|
end);]]):format(concat(buffer, "\n"))
|
||||||
if max_operations then
|
return return_value, lua_code, vars
|
||||||
debug.sethook()
|
|
||||||
end
|
|
||||||
return return_value, lua_code
|
|
||||||
end,
|
end,
|
||||||
tree_to_value = function(self, tree, vars)
|
tree_to_value = function(self, tree, vars)
|
||||||
local code = "return (function(nomsu, vars)\nreturn " .. tostring(self:tree_to_lua(tree)) .. ";\nend);"
|
local code = "return (function(nomsu, vars)\nreturn " .. tostring(self:tree_to_lua(tree)) .. ";\nend);"
|
||||||
|
@ -274,7 +274,7 @@ class NomsuCompiler
|
|||||||
@print_tree tree, " "
|
@print_tree tree, " "
|
||||||
return tree
|
return tree
|
||||||
|
|
||||||
run: (src, filename, max_operations=nil)=>
|
run: (src, filename, vars={}, max_operations=nil)=>
|
||||||
if max_operations
|
if max_operations
|
||||||
timeout = ->
|
timeout = ->
|
||||||
debug.sethook!
|
debug.sethook!
|
||||||
@ -285,7 +285,6 @@ class NomsuCompiler
|
|||||||
assert tree.type == "File", "Attempt to run non-file: #{tree.type}"
|
assert tree.type == "File", "Attempt to run non-file: #{tree.type}"
|
||||||
|
|
||||||
buffer = {}
|
buffer = {}
|
||||||
vars = {}
|
|
||||||
return_value = nil
|
return_value = nil
|
||||||
for statement in *tree.value
|
for statement in *tree.value
|
||||||
if @debug
|
if @debug
|
||||||
@ -320,15 +319,15 @@ class NomsuCompiler
|
|||||||
@error(ret)
|
@error(ret)
|
||||||
insert buffer, "#{statements or ''}\n#{expr and "ret = #{expr}" or ''}"
|
insert buffer, "#{statements or ''}\n#{expr and "ret = #{expr}" or ''}"
|
||||||
|
|
||||||
|
if max_operations
|
||||||
|
debug.sethook!
|
||||||
lua_code = ([[
|
lua_code = ([[
|
||||||
return (function(nomsu, vars)
|
return (function(nomsu, vars)
|
||||||
local ret;
|
local ret;
|
||||||
%s
|
%s
|
||||||
return ret;
|
return ret;
|
||||||
end);]])\format(concat(buffer, "\n"))
|
end);]])\format(concat(buffer, "\n"))
|
||||||
if max_operations
|
return return_value, lua_code, vars
|
||||||
debug.sethook!
|
|
||||||
return return_value, lua_code
|
|
||||||
|
|
||||||
tree_to_value: (tree, vars)=>
|
tree_to_value: (tree, vars)=>
|
||||||
code = "return (function(nomsu, vars)\nreturn #{@tree_to_lua(tree)};\nend);"
|
code = "return (function(nomsu, vars)\nreturn #{@tree_to_lua(tree)};\nend);"
|
||||||
|
Loading…
Reference in New Issue
Block a user