Skip to content

Commit

Permalink
gh issues #1 and #2 are solved:
Browse files Browse the repository at this point in the history
autoappend ";" if not there in the prompt.
implement `_generate_next_value_` for unique Enum with mixed int and str values.

clip the number of reprinted tokens.
  • Loading branch information
Baltasar Sánchez E committed May 7, 2023
1 parent bf4507b commit e1d7426
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 8 deletions.
15 changes: 15 additions & 0 deletions exp.lox
Original file line number Diff line number Diff line change
Expand Up @@ -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";
13 changes: 10 additions & 3 deletions plox/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -55,15 +60,15 @@ 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

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)
Expand All @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions plox/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand Down
7 changes: 6 additions & 1 deletion plox/plox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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')
Expand Down
4 changes: 4 additions & 0 deletions plox/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ')'
Expand Down
3 changes: 1 addition & 2 deletions plox/statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src.lox
Original file line number Diff line number Diff line change
Expand Up @@ -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";

0 comments on commit e1d7426

Please sign in to comment.