|
|
|
@ -215,13 +215,41 @@ module Lox
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def unary
|
|
|
|
|
return primary unless match?(:BANG, :MINUS)
|
|
|
|
|
return call unless match?(:BANG, :MINUS)
|
|
|
|
|
|
|
|
|
|
op = prev
|
|
|
|
|
right = unary
|
|
|
|
|
Expr::Unary.new(op, right)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def call
|
|
|
|
|
expr = primary
|
|
|
|
|
|
|
|
|
|
loop do
|
|
|
|
|
if match?(:LEFT_PAREN)
|
|
|
|
|
expr = finish_call(expr)
|
|
|
|
|
else
|
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
expr
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def finish_call(callee)
|
|
|
|
|
args = []
|
|
|
|
|
if !check?(:RIGHT_PAREN)
|
|
|
|
|
loop do
|
|
|
|
|
args << expression
|
|
|
|
|
break unless match?(:COMMA)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
paren = consume!(:RIGHT_PAREN, "Expect ')' after arguments.")
|
|
|
|
|
|
|
|
|
|
Expr::Call.new(callee, paren, args)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def primary
|
|
|
|
|
return Expr::Literal.new(false) if match?(:FALSE)
|
|
|
|
|
return Expr::Literal.new(true) if match?(:TRUE)
|
|
|
|
|