Skip to content

Commit

Permalink
ternary expressions added. seem ok
Browse files Browse the repository at this point in the history
  • Loading branch information
Baltasar Sánchez E committed May 7, 2023
1 parent 476c760 commit 89bb18a
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 10 deletions.
4 changes: 4 additions & 0 deletions exp.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(13 > (5 * 3 + 9));
123 != 12312 + (2.3 == 123) * 9.12
// asf asf
1+2-3*4/5 // saywha
8 changes: 8 additions & 0 deletions expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ class Unary(Expr):
class Variable(Expr):
name: Token #identifier

@dataclass
class Ternary(Expr):
comparison: Expr
left: Expr # true evaluation, comparison must be boolean
right: Expr


class AstPrinter(Visitor):
"""only good at printing out (and making strings) from binary expressions"""
Expand Down Expand Up @@ -65,6 +71,8 @@ def visitUnary(self, expr):
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}]'
"""
def visitExpr(self, expr):
return self.parensiffy(expr.expr)
Expand Down
7 changes: 7 additions & 0 deletions interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ def visitStmtPrint(self, stmt):
print(val)
return val

def visitStmtVar(self, stmt):
"""
stmt.name
stmt.initializer
"""
...

def visitLiteral(self, expr):
return expr.value

Expand Down
27 changes: 23 additions & 4 deletions parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ class Parser(ParserNav):
exprStmt → expression ";" ;
printStmt → "print" expression ";" ;
expression → equality ;
expression → equality |
ternary ;
ternary → equality "?" expression ":" expression ;
equality → comparison ( ( "!=" | "==" ) comparison )* ;
comparison → term ( ( ">" | ">=" | "<" | "<=" ) term )* ;
term → factor ( ( "-" | "+" ) factor )* ;
Expand All @@ -97,7 +99,7 @@ class Parser(ParserNav):
primary → NUMBER | STRING | "true" | "false" | "nil"
| "(" expression ")"
| IDENTIFIER ;
"""
"""

def __init__(self, tokens: list[Token]):
self.tokens = tokens
Expand Down Expand Up @@ -147,12 +149,29 @@ def printStmt(self):

def expressionStmt(self):
val = self.expression()
assert isinstance(val, Expr)

# if val is None:
# val = Expr()
# next assert is now guaranteed... lame
assert isinstance(val, (Stmt, Expr)), f"{val}. repr: `"+repr(val)+"`"

self.consume(TokenType.SEMICOLON, "expected a ';' after expressionStmt. where is my ; !?")
return StmtExpr(val)

def expression(self):
return self.equality()
"""
expression → equality |
ternary ;
"""
eq = self.equality()

if self.match(TokenType.QUESTION):
left = self.expression()
self.consume(TokenType.COLON, "Expected ':' for a ternary expr.")
right = self.expression()
return Ternary(eq, left, right)
else:
return eq

def equality(self):
expr = self.comparison()
Expand Down
3 changes: 3 additions & 0 deletions scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class TokenType(Enum):
LESS = '<'
LESS_EQUAL = '<='

QUESTION = '?'
COLON = ':'

# // Literals.
IDENTIFIER = auto()
STRING = auto()
Expand Down
12 changes: 7 additions & 5 deletions src.lox
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// sample, should write a real program.
() [] [ ] ([]) (])
"strings"
123
123.1233
var language = "lox";
var test =
1 == 2 ? "help" : "we cool, no help";


"strings";
123;
123.1233; // sample, should write a real program.
2 changes: 1 addition & 1 deletion statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(self):

def print(self, stmt):
# Visitor.start_walk
print(stmt.accept(visitor=self))
print(f'{stmt.__class__.__name__}({stmt.accept(visitor=self)})')

def _exprVisit(self, expr):
return expr.accept(visitor=self.exprPrinter)
Expand Down

0 comments on commit 89bb18a

Please sign in to comment.