Skip to content

Commit

Permalink
Temporary fix divide by zero crash.
Browse files Browse the repository at this point in the history
Exposed by a case in test/runtime-errors.sh

Add TODOs for a better fix.
  • Loading branch information
Andy Chu committed Aug 22, 2018
1 parent 0381507 commit e19cee4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
17 changes: 14 additions & 3 deletions core/expr_eval.py
Expand Up @@ -125,6 +125,8 @@ def _StringToIntegerOrError(self, s):
raise
else:
i = 0
# TODO: Need the arena for printing this error?
#ui.PrettyPrintError(e)
warn(e.UserErrorString())
return i

Expand Down Expand Up @@ -394,10 +396,19 @@ def Eval(self, node):
try:
return lhs / rhs
except ZeroDivisionError:
# TODO: Instead of op_id, I should have the token
# node.right.w crashes if it's not a constant!
e_die('Divide by zero', word=node.right.w)
# TODO: _ErrorWithLocation should also accept arith_expr ? I
# think I needed that for other stuff.
# Or I could blame the '/' token, instead of op_id.
error_expr = node.right # node is ArithBinary
if error_expr.tag == arith_expr_e.ArithVarRef:
# TODO: ArithVarRef should store a token instead of a string!
e_die('Divide by zero (name)')
elif error_expr.tag == arith_expr_e.ArithWord:
e_die('Divide by zero', word=node.right.w)
else:
e_die('Divide by zero')
if op_id == Id.Arith_Percent:

return lhs % rhs
if op_id == Id.Arith_DStar:
return lhs ** rhs
Expand Down
1 change: 1 addition & 0 deletions core/tdop.py
Expand Up @@ -125,6 +125,7 @@ def LeftError(p, t, left, rbp):

def LeftBinaryOp(p, w, left, rbp):
""" Normal binary operator like 1+2 or 2*3, etc. """
# TODO: w shoudl be a TokenWord, and we should extract the token from it.
return ast.ArithBinary(word.ArithId(w), left, p.ParseUntil(rbp))


Expand Down

0 comments on commit e19cee4

Please sign in to comment.