Add to_s method

This commit is contained in:
Glenn Y. Rolland 2017-09-12 16:32:03 +02:00
parent 4d1cadd4f6
commit 3589d36390
2 changed files with 43 additions and 3 deletions

View file

@ -19,6 +19,7 @@ module Mm2ep
puts line.inspect puts line.inspect
token = parser.parse(line.chomp, true) token = parser.parse(line.chomp, true)
pp token pp token
puts token.to_s
# puts false && true # puts false && true
# puts false || false # puts false || false
# binding.pry # binding.pry

View file

@ -9,6 +9,11 @@ module Mm2ep
class TreeValue class TreeValue
def value def value
raise "No value for #{@name}" if @value.nil? raise "No value for #{@name}" if @value.nil?
@value
end
def to_s
raise NotImplementedError
end end
end end
@ -17,12 +22,30 @@ module Mm2ep
@name = str @name = str
@value = nil @value = nil
end end
def to_s
"var:#{@name}<-(#{@value})"
end
end end
class NumberValue < TreeValue class NumberValue < TreeValue
def initialize str
@value = str.to_i
end
def to_s
"number:#{@value}"
end
end end
class StringValue < TreeValue class StringValue < TreeValue
def initialize str
@value = str
end
def to_s
"string:\"#{@value}\""
end
end end
class BoolValue < TreeValue class BoolValue < TreeValue
@ -34,8 +57,8 @@ module Mm2ep
end end
def compute def to_s
@value "bool:#{@value}"
end end
end end
@ -49,6 +72,10 @@ module Mm2ep
# binding.pry # binding.pry
return @expr1.compute && @expr2.compute return @expr1.compute && @expr2.compute
end end
def to_s
"(#{@expr1.to_s}) AND (#{@expr2.to_s})"
end
end end
class OrOp class OrOp
@ -60,6 +87,10 @@ module Mm2ep
def compute def compute
return @expr1.compute || @expr2.compute return @expr1.compute || @expr2.compute
end end
def to_s
"(#{@expr1.to_s}) OR (#{@expr2.to_s})"
end
end end
class NotOp class NotOp
@ -70,6 +101,10 @@ module Mm2ep
def compute def compute
return ! @expr.compute return ! @expr.compute
end end
def to_s
"NOT (#{@expr.to_s})"
end
end end
class EqOp class EqOp
@ -81,6 +116,10 @@ module Mm2ep
def compute def compute
return if @lval.value == @rval.value return if @lval.value == @rval.value
end end
def to_s
"(#{@lval.to_s} = #{@rval.to_s})"
end
end end
class Parser < Rly::Yacc class Parser < Rly::Yacc