feat(interpreter): add method to stringify values
This commit is contained in:
@@ -169,7 +169,7 @@ class Interpreter(Expr.Visitor[Any], Stmt.Visitor[None]):
|
|||||||
|
|
||||||
def visit_print_stmt(self, stmt: PrintStmt) -> None:
|
def visit_print_stmt(self, stmt: PrintStmt) -> None:
|
||||||
value: Any = self.evaluate(stmt.expression)
|
value: Any = self.evaluate(stmt.expression)
|
||||||
print(value)
|
print(self.stringify(value))
|
||||||
|
|
||||||
def visit_return_stmt(self, stmt: ReturnStmt) -> None:
|
def visit_return_stmt(self, stmt: ReturnStmt) -> None:
|
||||||
value: Any = None
|
value: Any = None
|
||||||
@@ -280,3 +280,17 @@ class Interpreter(Expr.Visitor[Any], Stmt.Visitor[None]):
|
|||||||
if isinstance(value, (int, float)):
|
if isinstance(value, (int, float)):
|
||||||
return
|
return
|
||||||
raise PebbleRuntimeError(clause, "For loop clauses must be numbers.")
|
raise PebbleRuntimeError(clause, "For loop clauses must be numbers.")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def stringify(obj: Any) -> str:
|
||||||
|
if obj is None:
|
||||||
|
return "null"
|
||||||
|
if obj is True:
|
||||||
|
return "true"
|
||||||
|
if obj is False:
|
||||||
|
return "false"
|
||||||
|
if isinstance(obj, (int, float)):
|
||||||
|
if obj.is_integer():
|
||||||
|
obj = int(obj)
|
||||||
|
return str(obj)
|
||||||
|
return obj
|
||||||
|
|||||||
Reference in New Issue
Block a user