Updated to use return values better.

This commit is contained in:
Bruce Hill 2017-09-14 18:18:42 -07:00
parent f8f5e7b653
commit 6225462a1c
3 changed files with 49 additions and 29 deletions

View File

@ -352,10 +352,14 @@ rule "forbid %blacklist to use %fn":
# Error functions
rule "error!":
lua block "compiler:error()"
lua block ".."
|table.remove(compiler.callstack)
|compiler:error()
rule "error %msg":
lua block "compiler:error(vars.msg)"
lua block ".."
|table.remove(compiler.callstack)
|compiler:error(vars.msg)
macro block "test %code yields %expected":
let "generated" = (lua expr "utils.repr(compiler:stringify_tree(vars.code.value.value), true)")

View File

@ -249,11 +249,11 @@ do
if self.debug then
print("RUNNING TEXT:\n" .. tostring(text))
end
local code = self:compile(text)
local code, retval = self:compile(text)
if self.debug then
print("\nGENERATED LUA CODE:\n" .. tostring(code))
end
return code
return retval
end,
parse = function(self, str)
if self.debug then
@ -341,6 +341,7 @@ do
assert(tree, "No tree provided.")
local indent = ""
local buffer = { }
local return_value = nil
local to_lua
to_lua = function(t, kind)
local ret = self:tree_to_lua(t, kind)
@ -358,20 +359,20 @@ do
local _list_0 = tree.value.body.value
for _index_0 = 1, #_list_0 do
local statement = _list_0[_index_0]
local code = to_lua(statement)
local code = to_lua(statement, "Statement")
local lua_thunk, err = load("\n local utils = require('utils')\n return (function(compiler, vars)\n" .. tostring(code) .. "\nend)")
if not lua_thunk then
error("Failed to compile generated code:\n" .. tostring(code) .. "\n\n" .. tostring(err) .. "\n\nProduced by statement:\n" .. tostring(utils.repr(statement)))
end
local ok
ok, err = pcall(lua_thunk)
local ok, value = pcall(lua_thunk)
if not ok then
error(err)
error(value)
end
ok, err = pcall(err, self, vars)
ok, value = pcall(value, self, vars)
if not ok then
self:error(err)
error()
end
return_value = value
add(code)
end
add([[ return ret
@ -496,7 +497,7 @@ do
error("Unknown/unimplemented thingy: " .. tostring(tree.type))
end
buffer = table.concat(buffer, "\n")
return buffer
return buffer, return_value
end,
fn_name_from_tree = function(self, tree)
assert(tree.type == "FunctionCall", "Attempt to get fn name from non-functioncall tree: " .. tostring(tree.type))
@ -663,12 +664,12 @@ do
end
local tree = self:parse(src)
assert(tree, "Tree failed to compile: " .. tostring(src))
local code = self:tree_to_lua(tree)
local code, retval = self:tree_to_lua(tree)
if output_file then
local output = io.open(output_file, "w")
output:write(code)
end
return code
return code, retval
end,
error = function(self, ...)
print(...)
@ -676,6 +677,8 @@ do
for i = #self.callstack, 1, -1 do
print(" " .. tostring(self.callstack[i]))
end
print(" <top level>")
self.callstack = { }
return error()
end,
test = function(self, src, expected)
@ -836,7 +839,7 @@ if arg and arg[1] then
nop = function() end
print, io.write = nop, nop
end
local code = c:run(input)
local code, retval = c:compile(input)
if arg[2] then
local output
if arg[2] == "-" then
@ -853,11 +856,12 @@ if arg and arg[1] then
end
local NomsuCompiler = require('nomsu')
local c = NomsuCompiler()
load()(c, {})
return load()(c, {})
]])
end
elseif arg then
local c = NomsuCompiler()
c:run('run file "core.nom"')
while true do
local buff = ""
while true do
@ -871,7 +875,12 @@ elseif arg then
if #buff == 0 then
break
end
c:run(buff)
local ok, ret = pcall(function()
return c:run(buff)
end)
if ok and ret ~= nil then
print("= " .. utils.repr(ret, true))
end
end
end
return NomsuCompiler

View File

@ -158,10 +158,10 @@ class NomsuCompiler
if @debug
print "RUNNING TEXT:\n#{text}"
-- This will execute each chunk as it goes along
code = @compile(text)
code, retval = @compile(text)
if @debug
print "\nGENERATED LUA CODE:\n#{code}"
return code
return retval
parse: (str)=>
if @debug
@ -248,6 +248,7 @@ class NomsuCompiler
assert tree, "No tree provided."
indent = ""
buffer = {}
return_value = nil
to_lua = (t,kind)->
ret = @tree_to_lua(t,kind)
@ -261,7 +262,7 @@ class NomsuCompiler
local ret]]
vars = {}
for statement in *tree.value.body.value
code = to_lua(statement)
code = to_lua(statement, "Statement")
-- Run the fuckers as we go
-- TODO: clean up repeated loading of utils?
lua_thunk, err = load("
@ -269,10 +270,11 @@ class NomsuCompiler
return (function(compiler, vars)\n#{code}\nend)")
if not lua_thunk
error("Failed to compile generated code:\n#{code}\n\n#{err}\n\nProduced by statement:\n#{utils.repr(statement)}")
ok,err = pcall(lua_thunk)
if not ok then error(err)
ok,err = pcall(err, self, vars)
if not ok then @error(err)
ok,value = pcall(lua_thunk)
if not ok then error(value)
ok,value = pcall(value, self, vars)
if not ok then error!
return_value = value
add code
add [[
return ret
@ -365,7 +367,7 @@ class NomsuCompiler
-- TODO: make indentation clean
buffer = table.concat(buffer, "\n")
return buffer
return buffer, return_value
@comma_separated_items: (open, items, close)=>
utils.accumulate "\n", ->
@ -483,17 +485,19 @@ class NomsuCompiler
print "COMPILING:\n#{src}"
tree = @parse(src)
assert tree, "Tree failed to compile: #{src}"
code = @tree_to_lua(tree)
code, retval = @tree_to_lua(tree)
if output_file
output = io.open(output_file, "w")
output\write(code)
return code
return code, retval
error: (...)=>
print(...)
print("Callstack:")
for i=#@callstack,1,-1
print " #{@callstack[i]}"
print " <top level>"
@callstack = {}
error!
test: (src, expected)=>
@ -583,7 +587,7 @@ if arg and arg[1]
export print
nop = ->
print, io.write = nop, nop
code = c\run(input)
code, retval = c\compile(input)
if arg[2]
output = if arg[2] == "-"
export print
@ -601,11 +605,12 @@ if arg and arg[1]
end
local NomsuCompiler = require('nomsu')
local c = NomsuCompiler()
load()(c, {})
return load()(c, {})
]]
elseif arg
-- REPL:
c = NomsuCompiler()
c\run('run file "core.nom"')
while true
buff = ""
while true
@ -616,6 +621,8 @@ elseif arg
buff ..= line
if #buff == 0
break
c\run(buff)
ok, ret = pcall(-> c\run(buff))
if ok and ret != nil
print "= "..utils.repr(ret, true)
return NomsuCompiler