Alpha Chen 2 years ago
parent 23c9bd5174
commit b23936ade6

@ -23,6 +23,7 @@ module Lox
private
def declaration
return function("function") if match?(:FUN)
return var_declaration if match?(:VAR)
statement
@ -116,6 +117,26 @@ module Lox
Stmt::Expr.new(value)
end
def function(kind)
name = consume!(:IDENTIFIER, "Expect #{kind} name.")
consume!(:LEFT_PAREN, "Expect '(' after #{kind} name.")
params = []
unless check?(:RIGHT_PAREN)
loop do
raise ParseError.new(peek, "Can't have more than 255 parameters.") if params.size >= 255
params << consume!(:IDENTIFIER, "Expect parameter name.")
break unless match?(:COMMA)
end
end
consume!(:RIGHT_PAREN, "Expect ')' after parameters.")
consume!(:LEFT_BRACE, "Expect '{' before #{kind} body.")
body = block
Stmt::Function.new(name, params, body)
end
def block
statements = []
until check?(:RIGHT_BRACE) || eot?

@ -13,6 +13,7 @@ module Lox
end
stmt :Block, :stmts
stmt :Fun, :name, :params, :body
stmt :Expr, :expr
stmt :If, :cond, :then, :else
stmt :Print, :expr

Loading…
Cancel
Save