Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to #225, added better error reporting for internal compiler errors. #329

Merged
merged 29 commits into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
6a08daa
added tag_matches and tag_error functions to typedecl module
srijan-paul Sep 16, 2020
fd0d9ef
added a test file, and replaced all error("impossible") calls in type…
srijan-paul Sep 16, 2020
b8ee216
added proper error messages in ast.lua, print_ir.lua and checker.lua
srijan-paul Sep 16, 2020
95b4d3d
added proper error reporting to 'to_ir.lua'
srijan-paul Sep 16, 2020
7f74d2e
fixed typo
srijan-paul Sep 16, 2020
f02aaf7
added error reporting to 'coder.lua' and 'constant_propagation.lua'
srijan-paul Sep 16, 2020
a06532b
updated error_tag error message
srijan-paul Sep 16, 2020
7a0729b
removed uneccessary whitespace after doc comments. added usage exampl…
srijan-paul Sep 17, 2020
ca72031
removed uneccessary whitespace after doc comments. added usage exampl…
srijan-paul Sep 17, 2020
2134fe5
replaced all uses of tag_is_type(tag) with tag_matches(tag, "types.T"
srijan-paul Sep 17, 2020
6910a0d
replaced old uses of string.match with typedecl.tag_matches for check…
srijan-paul Sep 17, 2020
b6091ac
removed uneccessary error messages.
srijan-paul Sep 17, 2020
706aed2
better default error message
srijan-paul Sep 17, 2020
29c9bef
updated doc comment
srijan-paul Sep 17, 2020
edd073a
fixed incorrect require path
srijan-paul Sep 17, 2020
e99c0dd
fixed bug with assert guards, now passes all tests
srijan-paul Sep 17, 2020
07350ca
Merge branch 'better-errors' into master
srijan-paul Sep 17, 2020
81a5676
updated calls to match_tag , removed the "." at end of each call argu…
srijan-paul Sep 17, 2020
88b1f02
Merge branch 'better-errors' into master
srijan-paul Sep 17, 2020
51123e9
replaced the remaining string.matches with tag_matches
srijan-paul Sep 17, 2020
1827abb
changed function tag_matches to match_tag, now returns the captured s…
srijan-paul Sep 17, 2020
619c89d
updated match_tag to not treat the 2nd argument as regex
srijan-paul Sep 18, 2020
93d2669
updated a previously missed assert guard
srijan-paul Sep 18, 2020
9367f29
added some test cases to typedecl_spec
srijan-paul Sep 18, 2020
f7f553f
Merge branch 'better-errors' into master
srijan-paul Sep 18, 2020
e99a95b
removed uneccessary comment
srijan-paul Sep 18, 2020
ceb5309
Merge branch 'better-errors' into master
srijan-paul Sep 18, 2020
9189ba0
updated the test cases to be more precise with logs
srijan-paul Sep 18, 2020
170094a
Merge branch 'better-errors' into master
srijan-paul Sep 18, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pallene/ast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function ast.toplevel_names(tl_node)
elseif tag == "ast.Toplevel.Builtin" then
table.insert(names, tl_node.name)
else
error("impossible")
typedecl.tag_error(tag)
end
return names
end
Expand Down
28 changes: 15 additions & 13 deletions pallene/checker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ declare_type("Name", {
})

function Checker:add_type(name, typ)
assert(string.match(typ._tag, "^types%.T%."))
assert(typedecl.match_tag(typ._tag, "types.T"))
self.symbol_table:add_symbol(name, checker.Name.Type(typ))
end

Expand Down Expand Up @@ -218,7 +218,7 @@ function Checker:from_ast_type(ast_typ)
return types.T.Function(p_types, ret_types)

else
error("impossible")
typedecl.tag_error(tag)
end
end

Expand Down Expand Up @@ -360,7 +360,7 @@ function Checker:check_program(prog_ast)
self:add_type(tl_node.name, typ)

else
error("impossible")
typedecl.tag_error(tag)
end
end

Expand Down Expand Up @@ -455,7 +455,7 @@ function Checker:check_stat(stat)
elseif loop_type._tag == "types.T.Float" then
stat.step = ast.Exp.Float(stat.limit.loc, 1.0)
else
error("impossible")
typedecl.tag_error(loop_type._tag, "loop type is not a number.")
end
end

Expand Down Expand Up @@ -515,7 +515,7 @@ function Checker:check_stat(stat)
-- ok

else
error("impossible")
typedecl.tag_error(tag)
end

return stat
Expand Down Expand Up @@ -547,7 +547,7 @@ function Checker:check_var(var)
"cannot reference module name '%s' without dot notation",
var.name)
else
error("impossible")
typedecl.tag_error(cname._tag)
end

elseif tag == "ast.Var.Dot" then
Expand Down Expand Up @@ -603,7 +603,7 @@ function Checker:check_var(var)
var._type = arr_type.elem

else
error("impossible")
typedecl.tag_error(tag)
end
return var
end
Expand All @@ -618,8 +618,10 @@ function Checker:coerce_numeric_exp_to_float(exp)
return exp
elseif tag == "types.T.Integer" then
return self:check_exp_synthesize(ast.Exp.ToFloat(exp.loc, exp))
elseif typedecl.match_tag(tag, "types.T") then
typedecl.tag_error(tag, "this type cannot be coerced to float.")
else
error("impossible")
typedecl.tag_error(tag)
end
end

Expand Down Expand Up @@ -687,7 +689,7 @@ function Checker:check_exp_synthesize(exp)
check_type_is_condition(exp.exp, "'not' operator")
exp._type = types.T.Boolean()
else
error("impossible")
typedecl.tag_error(op)
end

elseif tag == "ast.Exp.Binop" then
Expand Down Expand Up @@ -797,7 +799,7 @@ function Checker:check_exp_synthesize(exp)
exp._type = types.T.Integer()

else
error("impossible")
typedecl.tag_error(op)
end

elseif tag == "ast.Exp.CallFunc" then
Expand Down Expand Up @@ -865,7 +867,7 @@ function Checker:check_exp_synthesize(exp)
exp._type = types.T.Float()

else
error("impossible")
typedecl.tag_error(tag)
end

return exp
Expand Down Expand Up @@ -898,7 +900,7 @@ function Checker:check_exp_verify(exp, expected_type, errmsg_fmt, ...)
field.exp, expected_type.elem,
"array initializer")
else
error("impossible")
typedecl.tag_error(ftag)
end
end

Expand Down Expand Up @@ -928,7 +930,7 @@ function Checker:check_exp_verify(exp, expected_type, errmsg_fmt, ...)
field.exp, field_type,
"table initializer")
else
error("impossible")
typedecl.tag_error(ftag)
end
end

Expand Down
21 changes: 12 additions & 9 deletions pallene/coder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ local function ctype(typ)
elseif tag == "types.T.Table" then return "Table *"
elseif tag == "types.T.Record" then return "Udata *"
elseif tag == "types.T.Any" then return "TValue"
else error("impossible")
else typedecl.tag_error(tag)
end
end

Expand Down Expand Up @@ -90,7 +90,7 @@ local function lua_value(typ, src_slot)
elseif tag == "types.T.Table" then tmpl = "hvalue($src)"
elseif tag == "types.T.Record" then tmpl = "uvalue($src)"
elseif tag == "types.T.Any" then tmpl = "*($src)"
else error("impossible")
else typedecl.tag_error(tag)
end
return (util.render(tmpl, {src = src_slot}))
end
Expand All @@ -117,8 +117,9 @@ local function set_stack_slot(typ, dst_slot, value)
elseif tag == "types.T.Table" then tmpl = "sethvalue(L, $dst, $src);"
elseif tag == "types.T.Record" then tmpl = "setuvalue(L, $dst, $src);"
elseif tag == "types.T.Any" then tmpl = "setobj(L, $dst, &$src);"
else error("impossible")
else typedecl.tag_error(tag)
end

return (util.render(tmpl, { dst = dst_slot, src = value }))
end

Expand Down Expand Up @@ -165,8 +166,8 @@ local function pallene_type_tag(typ)
elseif tag == "types.T.Array" then return "LUA_TTABLE"
elseif tag == "types.T.Table" then return "LUA_TTABLE"
elseif tag == "types.T.Record" then return "LUA_TUSERDATA"
elseif tag == "types.T.Any" then error("value is not a tag")
else error("impossible")
elseif tag == "types.T.Any" then typedecl.tag_error(tag, "'Any' is not a Lua type tag.")
else typedecl.tag_error(tag)
end
end

Expand Down Expand Up @@ -324,8 +325,10 @@ function Coder:c_value(value)
local f_id = value.id
local typ = self.module.functions[f_id].typ
return lua_value(typ, self:function_upvalue_slot(f_id))
elseif typedecl.match_tag(tag, "ir.Value") then
typedecl.tag_error(tag, "unable to get C expression for this value type.")
else
error("impossible")
typedecl.tag_error(tag)
end
end

Expand Down Expand Up @@ -1476,7 +1479,7 @@ gen_cmd["For"] = function(self, cmd, func)
elseif typ._tag == "types.T.Float" then
macro = "PALLENE_FLT_FOR_LOOP"
else
error("impossible")
typedecl.tag_error(typ._tag)
end

return (util.render([[
Expand All @@ -1502,7 +1505,7 @@ gen_cmd["CheckGC"] = function(self, cmd, func)
end

function Coder:generate_cmd(func, cmd)
local name = assert(string.match(cmd._tag, "^ir%.Cmd%.(.*)$"))
local name = assert(typedecl.match_tag(cmd._tag, "ir.Cmd"))
local f = assert(gen_cmd[name], "impossible")
local out = f(self, cmd, func)

Expand Down Expand Up @@ -1601,7 +1604,7 @@ function Coder:generate_luaopen_function()
ix = C.integer(self.upvalue_of_function[upv.f_id]),
}))
else
error("impossible")
typedecl.tag_error(tag)
end

table.insert(init_constants, util.render([[
Expand Down
3 changes: 2 additions & 1 deletion pallene/constant_propagation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
-- SPDX-License-Identifier: MIT

local ir = require "pallene.ir"
local typedecl = require "pallene.typedecl"

local constant_propagation = {}

Expand All @@ -17,7 +18,7 @@ local function is_constant_value(v)
elseif tag == "ir.Value.LocalVar" then return false
elseif tag == "ir.Value.Function" then return true
else
error("impossible")
typedecl.tag_error(tag)
end
end

Expand Down
6 changes: 4 additions & 2 deletions pallene/print_ir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
local C = require "pallene.C"
local ir = require "pallene.ir"
local util = require "pallene.util"
local typedecl = require "pallene.typedecl"

--
-- Generates a human-readable representation of the IR.
Expand Down Expand Up @@ -41,7 +42,7 @@ local function Val(val)
elseif tag == "ir.Value.LocalVar" then return Var(val.id)
elseif tag == "ir.Value.Function" then return Fun(val.id)
else
error("impossible")
typedecl.tag_error(tag)
end
end

Expand Down Expand Up @@ -214,7 +215,8 @@ local function Cmd(cmd)
elseif tag == "ir.Cmd.CallStatic" then rhs = Call(Fun(cmd.f_id), Vals(cmd.srcs))
elseif tag == "ir.Cmd.CallDyn" then rhs = Call(Val(cmd.src_f), Vals(cmd.srcs))
else
local tagname = assert(string.match(cmd._tag, "^ir.Cmd.(.*)"))
local tagname = cmd._tag
assert(typedecl.match_tag(tagname, "ir.Cmd"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be local tagname = assert(typedecl.match_tag(cmd._tag, "ir.Cmd"))?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, my bad.

rhs = Call(tagname, Vals(ir.get_srcs(cmd)))
end

Expand Down
34 changes: 21 additions & 13 deletions pallene/to_ir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function ToIR:convert_toplevel(prog_ast)
self.rec_id_of_typ[typ] = ir.add_record_type(self.module, typ)

else
error("impossible")
typedecl.tag_error(tag)
end
end

Expand Down Expand Up @@ -248,7 +248,7 @@ function ToIR:convert_stat(cmds, stat)
local id = self.glb_id_of_decl[cname.decl]
table.insert(lhss, to_ir.LHS.Global(id))
else
error("impossible")
typedecl.tag_error(cname._tag)
end

elseif var._tag == "ast.Var.Bracket" then
Expand All @@ -266,12 +266,14 @@ function ToIR:convert_stat(cmds, stat)
elseif ttag == "types.T.Record" then
local typ = var.exp._type
table.insert(lhss, to_ir.LHS.Record(typ, t, var.name))
elseif typedecl.tag_is_type(ttag) then
typedecl.tag_error(ttag, "type not indexable.")
else
error("impossible")
typedecl.tag_error(ttag)
end

else
error("impossible")
typedecl.tag_error(var._tag)
end
end

Expand Down Expand Up @@ -326,7 +328,7 @@ function ToIR:convert_stat(cmds, stat)
elseif ltag == "to_ir.LHS.Record" then
cmd = ir.Cmd.SetField(loc, lhs.typ, lhs.rec, lhs.field, val)
else
error("impossible")
typedecl.tag_error(ltag)
end
table.insert(cmds, cmd)
end
Expand Down Expand Up @@ -355,7 +357,7 @@ function ToIR:convert_stat(cmds, stat)
table.insert(cmds, ir.Cmd.Break())

else
error("impossible")
typedecl.type_error(tag)
end
end

Expand Down Expand Up @@ -498,7 +500,7 @@ function ToIR:exp_to_value(cmds, exp, _recursive)
error("not implemented")

else
error("impossible")
typedecl.tag_error(cname._tag)
end
else
-- Fallthrough to default
Expand Down Expand Up @@ -577,7 +579,7 @@ function ToIR:exp_to_assignment(cmds, dst, exp)
table.insert(cmds, ir.Cmd.SetField(exp.loc, typ, dv, field_name, vv))
end
else
error("impossible")
typedecl.tag_error(typ._tag)
end

elseif tag == "ast.Exp.Lambda" then
Expand Down Expand Up @@ -649,7 +651,7 @@ function ToIR:exp_to_assignment(cmds, dst, exp)
assert(#xs == 1)
table.insert(cmds, ir.Cmd.BuiltinTostring(loc, dsts, xs))
else
error("impossible")
typedecl.tag_error(bname)
end

elseif cname and cname._tag == "checker.Name.Function" then
Expand Down Expand Up @@ -691,13 +693,16 @@ function ToIR:exp_to_assignment(cmds, dst, exp)
cmd = ir.Cmd.GetTable(loc, dst_typ, dst, rec, key)
elseif typ._tag == "types.T.Record" then
cmd = ir.Cmd.GetField(loc, typ, dst, rec, field)
elseif typedecl.tag_is_type(typ._tag) then
typedecl.tag_error(typ._tag, "cannot index this type.")
else
error("impossible")
typedecl.tag_error(typ._tag)
end

table.insert(cmds, cmd)

else
error("impossible")
typedecl.tag_error(var._tag)
end

elseif tag == "ast.Exp.Unop" then
Expand Down Expand Up @@ -775,7 +780,8 @@ function ToIR:exp_to_assignment(cmds, dst, exp)
elseif src_typ._tag == "types.T.Any" then
table.insert(cmds, ir.Cmd.FromDyn(loc, dst_typ, dst, v))
else
error("impossible")
error(string.format("error casting from type '%s' to '%s'",
types.tostring(src_typ), types.tostring(dst_typ)))
hugomg marked this conversation as resolved.
Show resolved Hide resolved
end
end

Expand Down Expand Up @@ -803,8 +809,10 @@ function ToIR:value_is_truthy(cmds, exp, val)
local b = ir.add_local(self.func, false, types.T.Boolean())
table.insert(cmds, ir.Cmd.IsTruthy(exp.loc, b, val))
return ir.Value.LocalVar(b)
elseif typedecl.tag_is_type(typ) then
typedecl.tag_error(typ._tag, "unable to test this type for truthiness.")
else
error("impossible")
typedecl.tag_error(typ._tag)
end
end

Expand Down