Skip to content

Commit cf3095c

Browse files
authored
Improve error codes and ICEs (#72)
1 parent 1730064 commit cf3095c

File tree

3 files changed

+27
-31
lines changed

3 files changed

+27
-31
lines changed

pyndustric/compiler.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ def __init__(self):
3232

3333
def __str__(self):
3434
if self._lineno is None:
35-
raise InternalCompilerError(
36-
"lineno should be set. some instruction likely referenced this unstored label", None
35+
raise CompilerError(
36+
INTERNAL_COMPILER_ERR,
37+
None,
38+
"lineno should be set. some instruction likely referenced this unstored label",
3739
)
3840

3941
return str(self._lineno)
@@ -63,7 +65,7 @@ class Function:
6365

6466

6567
class CompilerError(ValueError):
66-
def __init__(self, code, node: ast.AST, **context):
68+
def __init__(self, code, node: ast.AST, desc="", **context):
6769
if node is None:
6870
node = ast.Module(lineno=0, col_offset=0) # dummy value
6971

@@ -76,18 +78,10 @@ def __init__(self, code, node: ast.AST, **context):
7678
]:
7779
context["unparsed"] = ast.unparse(node)
7880
super().__init__(
79-
f"[{code}/{node.lineno}:{node.col_offset}] {ERROR_DESCRIPTIONS[code].format(**context)}"
81+
f" {code}: {ERROR_DESCRIPTIONS[code].format(**context)}, line {node.lineno}, column {node.col_offset}\n{desc}"
8082
)
8183

8284

83-
class InternalCompilerError(CompilerError):
84-
def __init__(self, code, node: ast.AST):
85-
if node is None:
86-
node = ast.Module(lineno=0, col_offset=0) # dummy value
87-
88-
ValueError.__init__(self, f"[ICE/{node.lineno}:{node.col_offset}] {code}")
89-
90-
9185
class CompatTransformer(ast.NodeTransformer):
9286
if sys.version_info < (3, 9):
9387

@@ -492,7 +486,7 @@ def visit_FunctionDef(self, node):
492486

493487
def visit_Return(self, node):
494488
if not self._epilogue:
495-
raise InternalCompilerError("return encountered with epilogue being unset", node)
489+
raise CompilerError(INTERNAL_COMPILER_ERR, node, "return encountered with epilogue being unset")
496490

497491
val = self.as_value(node.value)
498492
self.ins_append(f"set {REG_RET} {val}")

pyndustric/constants.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import ast
22

3-
ERR_COMPLEX_ASSIGN = "E002"
4-
ERR_COMPLEX_VALUE = "E003"
5-
ERR_UNSUPPORTED_OP = "E004"
6-
ERR_UNSUPPORTED_ITER = "E005"
7-
ERR_BAD_ITER_ARGS = "E006"
8-
ERR_UNSUPPORTED_IMPORT = "E007"
9-
ERR_UNSUPPORTED_EXPR = "E008"
10-
ERR_UNSUPPORTED_SYSCALL = "E009"
11-
ERR_BAD_SYSCALL_ARGS = "E010"
12-
ERR_NESTED_DEF = "E011"
13-
ERR_INVALID_DEF = "E012"
14-
ERR_REDEF = "E013"
15-
ERR_NO_DEF = "E014"
16-
ERR_ARGC_MISMATCH = "E015"
17-
ERR_TOO_LONG = "E016"
18-
ERR_INVALID_SOURCE = "E017"
19-
ERR_BAD_TUPLE_ASSIGN = "E018"
3+
ERR_COMPLEX_ASSIGN = "NoComplexError"
4+
ERR_COMPLEX_VALUE = "NoComplexMathError"
5+
ERR_UNSUPPORTED_OP = "UnsupportedOperationError"
6+
ERR_UNSUPPORTED_ITER = "UnsupportedIteratorError"
7+
ERR_BAD_ITER_ARGS = "InvalidRangeError"
8+
ERR_UNSUPPORTED_IMPORT = "InvalidImportError"
9+
ERR_UNSUPPORTED_EXPR = "StandaloneError"
10+
ERR_UNSUPPORTED_SYSCALL = "InvalidSysCallError"
11+
ERR_BAD_SYSCALL_ARGS = "InvalidSysCallArgsError"
12+
ERR_NESTED_DEF = "NoRecursionError"
13+
ERR_INVALID_DEF = "FuncDefError"
14+
ERR_REDEF = "RedefinitionError"
15+
ERR_NO_DEF = "UndefinedFuncError"
16+
ERR_ARGC_MISMATCH = "ArgumentsError"
17+
ERR_TOO_LONG = "OverflowError"
18+
ERR_INVALID_SOURCE = "CompilerError"
19+
ERR_BAD_TUPLE_ASSIGN = "BadTupleError"
20+
INTERNAL_COMPILER_ERR = "InternalCompilerError"
2021

2122
ERROR_DESCRIPTIONS = {
2223
ERR_COMPLEX_ASSIGN: "cannot perform complex assignment `{unparsed}`",
@@ -36,6 +37,7 @@
3637
ERR_TOO_LONG: "the program is too long to fit in a logic processor",
3738
ERR_INVALID_SOURCE: "the provided source type to compile is not supported",
3839
ERR_BAD_TUPLE_ASSIGN: "can only assign to a tuple if the right-hand side is a tuple of the same length",
40+
INTERNAL_COMPILER_ERR: "internal compiler error",
3941
}
4042

4143
BIN_CMP = {

test_all.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def test_all_err_have_desc_and_tests():
6262
error_values = {getattr(pyndustric, name) for name in error_names}
6363

6464
assert len(error_values) == len(error_names), "some error constants have duplicate values"
65-
assert len(error_values) == len(pyndustric.ERROR_DESCRIPTIONS), "not all errors are documented"
65+
assert len(error_values) + 1 == len(pyndustric.ERROR_DESCRIPTIONS), "not all errors are documented"
6666

6767
with open(__file__, encoding="utf-8") as fd:
6868
source = fd.read()

0 commit comments

Comments
 (0)