feat(repl): evaluate expression statements and print value
This commit is contained in:
11
src/repl.py
11
src/repl.py
@@ -2,8 +2,9 @@ import atexit
|
|||||||
import readline
|
import readline
|
||||||
from pathlib import Path
|
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.completer.completer import Completer
|
||||||
|
from src.interpreter.error import PebbleRuntimeError
|
||||||
from src.interpreter.interpreter import Interpreter
|
from src.interpreter.interpreter import Interpreter
|
||||||
from src.interpreter.resolver import Resolver
|
from src.interpreter.resolver import Resolver
|
||||||
from src.parser.parser import Parser
|
from src.parser.parser import Parser
|
||||||
@@ -91,6 +92,14 @@ class REPL:
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
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)
|
self.interpreter.interpret(program)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print()
|
print()
|
||||||
|
|||||||
Reference in New Issue
Block a user