parent
cf72deef8b
commit
22a126dd0b
@ -1,135 +1,156 @@
|
|||||||
require_relative "error"
|
require_relative "error"
|
||||||
require_relative "expr"
|
require_relative "expr"
|
||||||
|
require_relative "stmt"
|
||||||
|
|
||||||
module Lox
|
module Lox
|
||||||
class Parser
|
class Parser
|
||||||
class State < Struct.new(:tokens, :current)
|
|
||||||
def initialize(tokens)
|
def initialize(tokens)
|
||||||
super(tokens, 0)
|
@tokens = tokens
|
||||||
|
@current = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse!
|
||||||
|
statements = []
|
||||||
|
until eot?
|
||||||
|
statements << statement
|
||||||
end
|
end
|
||||||
|
statements
|
||||||
|
end
|
||||||
|
|
||||||
def expression = equality
|
private
|
||||||
|
|
||||||
def equality
|
def statement
|
||||||
expr = comparison
|
return print if match?(:PRINT)
|
||||||
|
|
||||||
while match?(:BANG_EQUAL, :EQUAL_EQUAL)
|
expressionStmt
|
||||||
op = prev
|
end
|
||||||
right = comparison
|
|
||||||
expr = Expr::Binary.new(expr, op, right)
|
|
||||||
end
|
|
||||||
|
|
||||||
expr
|
def print
|
||||||
end
|
value = expression
|
||||||
|
consume!(:SEMICOLON, "Expect ';' after value.")
|
||||||
|
Stmt::Print.new(value)
|
||||||
|
end
|
||||||
|
|
||||||
def comparison
|
def expressionStmt
|
||||||
expr = term
|
value = expression
|
||||||
|
consume!(:SEMICOLON, "Expect ';' after value.")
|
||||||
|
Stmt::Expr.new(value)
|
||||||
|
end
|
||||||
|
|
||||||
while match?(:GREATER, :GREATER_EQUAL, :LESS, :LESS_EQUAL)
|
def expression = equality
|
||||||
op = prev
|
|
||||||
right = term
|
|
||||||
expr = Expr::Binary.new(expr, op, right)
|
|
||||||
end
|
|
||||||
|
|
||||||
expr
|
def equality
|
||||||
end
|
expr = comparison
|
||||||
|
|
||||||
|
while match?(:BANG_EQUAL, :EQUAL_EQUAL)
|
||||||
|
op = prev
|
||||||
|
right = comparison
|
||||||
|
expr = Expr::Binary.new(expr, op, right)
|
||||||
|
end
|
||||||
|
|
||||||
def term
|
expr
|
||||||
expr = factor
|
end
|
||||||
|
|
||||||
while match?(:MINUS, :PLUS)
|
def comparison
|
||||||
op = prev
|
expr = term
|
||||||
right = factor
|
|
||||||
expr = Expr::Binary.new(expr, op, right)
|
|
||||||
end
|
|
||||||
|
|
||||||
expr
|
while match?(:GREATER, :GREATER_EQUAL, :LESS, :LESS_EQUAL)
|
||||||
|
op = prev
|
||||||
|
right = term
|
||||||
|
expr = Expr::Binary.new(expr, op, right)
|
||||||
end
|
end
|
||||||
|
|
||||||
def factor
|
expr
|
||||||
expr = unary
|
end
|
||||||
|
|
||||||
while match?(:SLASH, :STAR)
|
def term
|
||||||
op = prev
|
expr = factor
|
||||||
right = unary
|
|
||||||
expr = Expr::Binary.new(expr, op, right)
|
|
||||||
end
|
|
||||||
|
|
||||||
expr
|
while match?(:MINUS, :PLUS)
|
||||||
|
op = prev
|
||||||
|
right = factor
|
||||||
|
expr = Expr::Binary.new(expr, op, right)
|
||||||
end
|
end
|
||||||
|
|
||||||
def unary
|
expr
|
||||||
return primary unless match?(:BANG, :MINUS)
|
end
|
||||||
|
|
||||||
|
def factor
|
||||||
|
expr = unary
|
||||||
|
|
||||||
|
while match?(:SLASH, :STAR)
|
||||||
op = prev
|
op = prev
|
||||||
right = unary
|
right = unary
|
||||||
Expr::Unary.new(op, right)
|
expr = Expr::Binary.new(expr, op, right)
|
||||||
end
|
end
|
||||||
|
|
||||||
def primary
|
expr
|
||||||
return Expr::Literal.new(false) if match?(:FALSE)
|
end
|
||||||
return Expr::Literal.new(true) if match?(:TRUE)
|
|
||||||
return Expr::Literal.new(nil) if match?(:NIL)
|
|
||||||
return Expr::Literal.new(prev.literal) if match?(:NUMBER, :STRING)
|
|
||||||
|
|
||||||
if match?(:LEFT_PAREN)
|
|
||||||
expr = expression
|
|
||||||
consume!(:RIGHT_PAREN, "Expect ')' after expression.")
|
|
||||||
return Expr::Grouping.new(expr)
|
|
||||||
end
|
|
||||||
|
|
||||||
raise ParseError.new(peek, "Expect expression.")
|
def unary
|
||||||
end
|
return primary unless match?(:BANG, :MINUS)
|
||||||
|
|
||||||
private
|
op = prev
|
||||||
|
right = unary
|
||||||
|
Expr::Unary.new(op, right)
|
||||||
|
end
|
||||||
|
|
||||||
def match?(*types)
|
def primary
|
||||||
return false unless check?(*types)
|
return Expr::Literal.new(false) if match?(:FALSE)
|
||||||
|
return Expr::Literal.new(true) if match?(:TRUE)
|
||||||
|
return Expr::Literal.new(nil) if match?(:NIL)
|
||||||
|
return Expr::Literal.new(prev.literal) if match?(:NUMBER, :STRING)
|
||||||
|
|
||||||
advance!
|
if match?(:LEFT_PAREN)
|
||||||
return true
|
expr = expression
|
||||||
|
consume!(:RIGHT_PAREN, "Expect ')' after expression.")
|
||||||
|
return Expr::Grouping.new(expr)
|
||||||
end
|
end
|
||||||
|
|
||||||
def consume!(type, message)
|
raise ParseError.new(peek, "Expect expression.")
|
||||||
raise ParseError.new(peek, message) unless check?(type)
|
end
|
||||||
|
|
||||||
advance!
|
private
|
||||||
end
|
|
||||||
|
|
||||||
def check?(*types)
|
def match?(*types)
|
||||||
return false if eot?
|
return false unless check?(*types)
|
||||||
|
|
||||||
types.include?(peek.type)
|
advance!
|
||||||
end
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
def advance!
|
def consume!(type, message)
|
||||||
self.current += 1 unless eot?
|
raise ParseError.new(peek, message) unless check?(type)
|
||||||
prev
|
|
||||||
end
|
|
||||||
|
|
||||||
def eot? = peek.type === :EOF
|
advance!
|
||||||
def peek = tokens.fetch(current)
|
end
|
||||||
def prev = tokens.fetch(current - 1)
|
|
||||||
|
|
||||||
def synchronize!
|
def check?(*types)
|
||||||
advance!
|
return false if eot?
|
||||||
|
|
||||||
until eot?
|
types.include?(peek.type)
|
||||||
return if prev.type == :SEMICOLON
|
end
|
||||||
return if %i[CLASS FUN VAR FOR IF WHILE PRINT RETURN].include?(peek.type)
|
|
||||||
|
|
||||||
advance!
|
def advance!
|
||||||
end
|
@current += 1 unless eot?
|
||||||
end
|
prev
|
||||||
end
|
end
|
||||||
|
|
||||||
# In the book, this returns nil when there's an error, but
|
def eot? = peek.type === :EOF
|
||||||
# that feels weird so let's move that error handling up the
|
def peek = @tokens.fetch(@current)
|
||||||
# stack for now.
|
def prev = @tokens.fetch(@current - 1)
|
||||||
def parse(tokens)
|
|
||||||
state = State.new(tokens)
|
def synchronize!
|
||||||
state.expression
|
advance!
|
||||||
|
|
||||||
|
until eot?
|
||||||
|
return if prev.type == :SEMICOLON
|
||||||
|
return if %i[CLASS FUN VAR FOR IF WHILE PRINT RETURN].include?(peek.type)
|
||||||
|
|
||||||
|
advance!
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -0,0 +1,13 @@
|
|||||||
|
require_relative "visitable"
|
||||||
|
|
||||||
|
module Lox
|
||||||
|
module Stmt
|
||||||
|
Expr = Struct.new(:expr) do
|
||||||
|
include Visitable
|
||||||
|
end
|
||||||
|
|
||||||
|
Print = Struct.new(:expr) do
|
||||||
|
include Visitable
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,8 @@
|
|||||||
|
module Lox
|
||||||
|
module Visitable
|
||||||
|
def accept(visitor)
|
||||||
|
klass = self.class.to_s.split("::").last.downcase
|
||||||
|
visitor.send("visit_#{klass}", self)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in new issue