|
|
@ -37,12 +37,24 @@ module Lox
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def statement
|
|
|
|
def statement
|
|
|
|
|
|
|
|
return if_stmt if match?(:IF)
|
|
|
|
return print if match?(:PRINT)
|
|
|
|
return print if match?(:PRINT)
|
|
|
|
return Stmt::Block.new(block) if match?(:LEFT_BRACE)
|
|
|
|
return Stmt::Block.new(block) if match?(:LEFT_BRACE)
|
|
|
|
|
|
|
|
|
|
|
|
expression_stmt
|
|
|
|
expression_stmt
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def if_stmt
|
|
|
|
|
|
|
|
consume!(:LEFT_PAREN, "Expect '(' after 'if'.")
|
|
|
|
|
|
|
|
cond = expression
|
|
|
|
|
|
|
|
consume!(:RIGHT_PAREN, "Expect ')' after 'if'.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
then_stmt = statement
|
|
|
|
|
|
|
|
else_stmt = match?(:ELSE) ? statement : nil
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Stmt::If.new(cond, then_stmt, else_stmt)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def print
|
|
|
|
def print
|
|
|
|
value = expression
|
|
|
|
value = expression
|
|
|
|
consume!(:SEMICOLON, "Expect ';' after value.")
|
|
|
|
consume!(:SEMICOLON, "Expect ';' after value.")
|
|
|
|