Skip to content

Commit

Permalink
Properly report syntax errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pavpanchekha committed May 9, 2010
1 parent f087439 commit 19b4467
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
7 changes: 6 additions & 1 deletion pylisp/lisp.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ def run(self, s, mult=True):
Lisp.eval = self.eval._orig
Lisp.quasieval = self.quasieval._orig
debug = -2
if isinstance(s, str): s = parser.parse(s)

try:
if isinstance(s, str): s = parser.parse(s)
except SyntaxError as e:
self.vars["signal"](self, ["error", "standard", "syntax"], *e.args)

if not mult: s = [s]
return map(lambda x: self.eval(self.preprocess(x)), s)[-1] if s else None

Expand Down
2 changes: 2 additions & 0 deletions pylisp/sexp.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ def eat_function(s):
if sexp:
s = s.strip()
body.append(sexp)
else:
raise SyntaxError("Unbalanced {:} notation")

return ["fn", vars] + body, s[1:]

Expand Down
12 changes: 8 additions & 4 deletions pylisp/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
l = lisp.Lisp()

def handle_error(err):
print "Call stack:"
for i in l.call_stack:
print "\t%s" % builtin.str_(i)
print "Error:", err
if len(l.call_stack) > 1:
print "Call stack:"
for i in l.call_stack:
print "\t%s" % builtin.str_(i)
print err
return ["bubble"]
l.call_stack[0]._catches[("error",)] = handle_error

Expand Down Expand Up @@ -67,6 +68,9 @@ def input():
sexps = list(lisp.parser.parse(s))
except IndexError:
s += raw_input("... > ") + "\n"
except SyntaxError as e:
print "(error standard syntax): " + str(e)
return input()
else:
break
return s
Expand Down

0 comments on commit 19b4467

Please sign in to comment.