diff --git a/exp.lox b/exp.lox index 6b0bb8c..de1c9d1 100644 --- a/exp.lox +++ b/exp.lox @@ -4,3 +4,18 @@ print 1+2-3*4/5 ;// saywha var X = 1 == 2 ? "help" : "we cool, no help"; print (10 + (true ? 5 : - 5)); + +print "GO ON END OF PROG, next is the end?"; +1 ? 2 : 3 ? 4 : 5; +1 ? 2 : (3 ? 4 : 5); +(1 ? 2 : 3) ? 4 : 5; + +// SOMETHING IS WRONG WITH PRECEDENCE +// well, the ? has higher predecedence than "?" +print 1 ? 2 : 3 ? 4 : 5 == 1 ? 2 : (3 ? 4 : 5); +//print +(1 ? 2 : 3 ? 4 : 5) == 1 ? 2 : (3 ? 4 : 5); + +print (1 ? 2 : 3 ? 4 : 5) == (1 ? 2 : (3 ? 4 : 5)); +print (1 ? 2 : 3 ? 4 : 5) == ((1 ? 2 : 3) ? 4 : 5); +print (1 ? 2 : 3 ? 4 : 5) == ((1 ? 2 : 3) ? 4 : 5) ? "False" : "True"; diff --git a/plox/expressions.py b/plox/expressions.py index 221c43d..68b5365 100644 --- a/plox/expressions.py +++ b/plox/expressions.py @@ -7,6 +7,11 @@ @dataclass class Expr(Visitable): + # what if I put a Token here, the token that started this Expr for any Expr. + # in some cases it'll be a TokenType like VAR + # in others, it'll be an IDENTIFIER or LITERAL. just the first token in thing. + # an empty file, with only an EOF would be that StmtExpr.expr = Expr().token=that EOF/ + # this would conttain file position data. pass @dataclass @@ -55,7 +60,7 @@ def parensiffy(self, name, *exprs): s += ' '.join((exp.accept(visitor=self) for exp in exprs)) except AttributeError as e: print('\n', exprs, '\n') - # raise e + raise e #why this? how to avoid this? s += ')' return s @@ -63,7 +68,7 @@ def visitGrouping(self, expr): return self.parensiffy('Group', expr.expr) def visitLiteral(self, expr): - return "nil" if expr.value is None else str(expr.value) + return "nil" if expr.value is None else f'"{expr.value}"' def visitUnary(self, expr): return self.parensiffy(expr.op.lexeme, expr.right) @@ -72,7 +77,9 @@ def visitBinary(self, expr): return self.parensiffy(expr.op.lexeme, expr.left, expr.right) def visitTernary(self, expr): - return f'if [{expr.comparison}]; then [{expr.left}]; else [{expr.right}]' + return (f'if [{expr.comparison.accept(visitor=self)}]; ' + f'then [{expr.left.accept(visitor=self)}]; ' + f'else [{expr.right.accept(visitor=self)}]') """ def visitExpr(self, expr): return self.parensiffy(expr.expr) diff --git a/plox/interpreter.py b/plox/interpreter.py index 4a00aaf..c6f2e9b 100644 --- a/plox/interpreter.py +++ b/plox/interpreter.py @@ -2,11 +2,13 @@ from scanner import Token, TokenType from expressions import * +from statements import StmtVisitor from util import Visitor, Visitable - class Interpreter(Visitor): + str_visitor = StmtVisitor() + def interpret(self, expr: Visitable): assert isinstance(expr, Visitable), f'expr is {expr.__class__}' val = self.evaluate(expr) @@ -17,7 +19,8 @@ def evaluate(self, expr): def visitStmtExpr(self, stmt): val = self.evaluate(stmt.expr) - print("# ", val) # print anyways, with a '# ' + print("# ", stmt.accept(visitor=self.str_visitor)) # print anyways, with a '# ' + print("#->", val) # print anyways, with a '# ' return val def visitStmtPrint(self, stmt): diff --git a/plox/plox.py b/plox/plox.py index c36303b..5a89c8c 100644 --- a/plox/plox.py +++ b/plox/plox.py @@ -18,6 +18,9 @@ def runPrompt(): break else: # Interpreter().interpret(Parser(Scanner(line).scanTokens()).parse()) + if not line.rstrip().endswith(';'): + print("WARNING, statements must end with a ';'.") + line += ';' run(line) hadError = False @@ -29,7 +32,9 @@ def runFile(filepath): def run(source): print('TOKENS') tokens = Scanner(source).scanTokens() - for t in tokens: + if (L := len(tokens)) > 12: + print(f'...{L} more tokens before.') + for t in tokens[-12:]: print(t) print('\nSTATEMENTS') diff --git a/plox/scanner.py b/plox/scanner.py index 425a490..7cfc6ff 100644 --- a/plox/scanner.py +++ b/plox/scanner.py @@ -11,6 +11,10 @@ def scan_error(line: int, msg: str): @unique class TokenType(Enum): + def _generate_next_value_(name, start, count, last_values): + # implements "auto".... + return count + # // Single-character tokens. LEFT_PAREN = '(' RIGHT_PAREN = ')' diff --git a/plox/statements.py b/plox/statements.py index eb37028..a23a303 100644 --- a/plox/statements.py +++ b/plox/statements.py @@ -25,8 +25,7 @@ class StmtVar(Stmt): class StmtVisitor(Visitor): - def __init__(self): - self.exprPrinter = AstPrinter() + exprPrinter = AstPrinter() def print(self, stmt): # Visitor.start_walk diff --git a/src.lox b/src.lox index d43d20d..387660e 100644 --- a/src.lox +++ b/src.lox @@ -7,3 +7,8 @@ var test = print 123; 123.1233; // sample, should write a real program. print 123+123; + +print ""; +print "These should be more different somehow... the # version should tell it lies."; +1 == 1 ? "False" : "True"; +print 1 == 1 ? "False" : "True";