feat(repl): evaluate expression statements and print value
This commit is contained in:
13
src/repl.py
13
src/repl.py
@@ -2,8 +2,9 @@ import atexit
|
||||
import readline
|
||||
from pathlib import Path
|
||||
|
||||
from src.ast.stmt import Stmt
|
||||
from src.ast.stmt import Stmt, ExpressionStmt
|
||||
from src.completer.completer import Completer
|
||||
from src.interpreter.error import PebbleRuntimeError
|
||||
from src.interpreter.interpreter import Interpreter
|
||||
from src.interpreter.resolver import Resolver
|
||||
from src.parser.parser import Parser
|
||||
@@ -91,7 +92,15 @@ class REPL:
|
||||
continue
|
||||
|
||||
try:
|
||||
self.interpreter.interpret(program)
|
||||
if len(program) == 1 and isinstance(program[0], ExpressionStmt):
|
||||
stmt: ExpressionStmt = program[0] # type: ignore
|
||||
try:
|
||||
value = self.interpreter.evaluate(stmt.expression)
|
||||
print(self.interpreter.stringify(value))
|
||||
except PebbleRuntimeError as e:
|
||||
Pebble.runtime_error(e)
|
||||
else:
|
||||
self.interpreter.interpret(program)
|
||||
except KeyboardInterrupt:
|
||||
print()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user