chapter 5

This commit is contained in:
2018-07-24 15:30:43 +02:00
parent 1afbd21973
commit 6ac116c169
2 changed files with 33 additions and 0 deletions

View File

@@ -2,6 +2,7 @@
require './rlox/prompt'
require './rlox/file'
require './rlox/expression'
case ARGV.length
when 0 then prompt

32
rlox/expression.rb Normal file
View File

@@ -0,0 +1,32 @@
def parenthesize(name, *exprs)
res = "(#{name}"
exprs.each { | expr |
res << " "
res << expr.dbg
}
res << ")"
res
end
Binary = Struct.new(:left, :operator, :right) do
def dbg()
parenthesize(operator.lexeme, left, right)
end
end
Grouping = Struct.new(:expression) do
def dbg()
parenthesize("group", expression)
end
end
Literal = Struct.new(:value) do
def dbg()
value.to_s
end
end
Unary = Struct.new(:operator, :right) do
def dbg()
parenthesize(operator.lexeme, right)
end
end