|
|
@ -13,17 +13,33 @@ module Lox
|
|
|
|
def parse!
|
|
|
|
def parse!
|
|
|
|
statements = []
|
|
|
|
statements = []
|
|
|
|
until eot?
|
|
|
|
until eot?
|
|
|
|
statements << statement
|
|
|
|
statements << declaration
|
|
|
|
end
|
|
|
|
end
|
|
|
|
statements
|
|
|
|
statements
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def declaration
|
|
|
|
|
|
|
|
return var_declaration if match?(:VAR)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
statement
|
|
|
|
|
|
|
|
rescue ParseError
|
|
|
|
|
|
|
|
synchronize!
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def var_declaration
|
|
|
|
|
|
|
|
name = consume!(:IDENTIFIER, "Expect variable name.")
|
|
|
|
|
|
|
|
initializer = match?(:EQUAL) ? expression : nil
|
|
|
|
|
|
|
|
consume!(:SEMICOLON, "Expect ';' after variable declaration.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Stmt::Var.new(name, initializer)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def statement
|
|
|
|
def statement
|
|
|
|
return print if match?(:PRINT)
|
|
|
|
return print if match?(:PRINT)
|
|
|
|
|
|
|
|
|
|
|
|
expressionStmt
|
|
|
|
expression_stmt
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def print
|
|
|
|
def print
|
|
|
@ -32,7 +48,7 @@ module Lox
|
|
|
|
Stmt::Print.new(value)
|
|
|
|
Stmt::Print.new(value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def expressionStmt
|
|
|
|
def expression_stmt
|
|
|
|
value = expression
|
|
|
|
value = expression
|
|
|
|
consume!(:SEMICOLON, "Expect ';' after value.")
|
|
|
|
consume!(:SEMICOLON, "Expect ';' after value.")
|
|
|
|
Stmt::Expr.new(value)
|
|
|
|
Stmt::Expr.new(value)
|
|
|
@ -101,6 +117,7 @@ module Lox
|
|
|
|
return Expr::Literal.new(true) if match?(:TRUE)
|
|
|
|
return Expr::Literal.new(true) if match?(:TRUE)
|
|
|
|
return Expr::Literal.new(nil) if match?(:NIL)
|
|
|
|
return Expr::Literal.new(nil) if match?(:NIL)
|
|
|
|
return Expr::Literal.new(prev.literal) if match?(:NUMBER, :STRING)
|
|
|
|
return Expr::Literal.new(prev.literal) if match?(:NUMBER, :STRING)
|
|
|
|
|
|
|
|
return Expr::Variable.new(prev) if match?(:IDENTIFIER)
|
|
|
|
|
|
|
|
|
|
|
|
if match?(:LEFT_PAREN)
|
|
|
|
if match?(:LEFT_PAREN)
|
|
|
|
expr = expression
|
|
|
|
expr = expression
|
|
|
@ -151,6 +168,6 @@ module Lox
|
|
|
|
advance!
|
|
|
|
advance!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|