Fix comparison between non-booleans

This commit is contained in:
Glenn Y. Rolland 2019-12-21 20:33:01 +01:00
parent 5af8b84770
commit 43989e6e48

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
module Namarara module Namarara
class TreeExpr class TreeExpr
def compute def compute
@ -23,6 +25,7 @@ module Namarara
@value = true if value =~ /^true$/i @value = true if value =~ /^true$/i
@value = false if value =~ /^false$/i @value = false if value =~ /^false$/i
return unless @value.nil? return unless @value.nil?
@errors << Errors::VarNotDefined.new( @errors << Errors::VarNotDefined.new(
message: "No value for #{@name}", message: "No value for #{@name}",
var: @name var: @name
@ -104,7 +107,9 @@ module Namarara
end end
def compute def compute
@lval.compute && @rval.compute # rubocop:disable Style/DoubleNegation
!!@lval.compute && !!@rval.compute
# rubocop:enable Style/DoubleNegation
end end
def to_s def to_s
@ -124,7 +129,9 @@ module Namarara
end end
def compute def compute
@lval.compute || @rval.compute # rubocop:disable Style/DoubleNegation
!!@lval.compute || !!@rval.compute
# rubocop:enable Style/DoubleNegation
end end
def to_s def to_s
@ -182,11 +189,13 @@ module Namarara
expr = line.split(/=|AND|OR/) expr = line.split(/=|AND|OR/)
return if grammar.size == expr.size return if grammar.size == expr.size
return if grammar.empty? return if grammar.empty?
tokens.errors << Errors::InvalidGrammar.new( tokens.errors << Errors::InvalidGrammar.new(
message: 'Invalid Grammar' message: 'Invalid Grammar'
) )
end end
precedence :left, :VAR
precedence :left, :OR_OP precedence :left, :OR_OP
precedence :left, :AND_OP precedence :left, :AND_OP
precedence :left, :EQ_OP precedence :left, :EQ_OP
@ -197,13 +206,6 @@ module Namarara
st.value = e.value st.value = e.value
end end
rule 'expr : VAR' do |ex, l|
ex.value = EqOp.new(
VarValue.new(l.value, @names[l.value]),
BoolValue.new('true')
)
end
rule 'bool_expr : F_BOOL' do |ex, l| rule 'bool_expr : F_BOOL' do |ex, l|
ex.value = l.value ex.value = l.value
end end
@ -212,16 +214,41 @@ module Namarara
ex.value = l.value ex.value = l.value
end end
rule 'expr : VAR' do |ex, l|
ex.value = VarValue.new(l.value.to_s, @names[l.value.to_s])
end
rule 'expr : bool_expr' do |ex, l| rule 'expr : bool_expr' do |ex, l|
ex.value = BoolValue.new(l.value.to_s) ex.value = BoolValue.new(l.value)
end
rule 'expr : STRING' do |ex, l|
ex.value = StringValue.new(l.value)
end
rule 'expr : NUMBER' do |ex, l|
ex.value = NumberValue.new(l.value)
end end
rule 'expr : expr OR_OP expr' do |ex, l, _e, r| rule 'expr : expr OR_OP expr' do |ex, l, _e, r|
ex.value = OrOp.new(l.value, r.value) ex.value = OrOp.new(
l.value,
r.value
)
end end
rule 'expr : expr AND_OP expr' do |ex, l, _e, r| rule 'expr : expr AND_OP expr' do |ex, l, _e, r|
ex.value = AndOp.new(l.value, r.value) ex.value = AndOp.new(
l.value,
r.value
)
end
rule 'expr : expr EQ_OP expr' do |ex, v, _eq, n|
ex.value = EqOp.new(
v.value,
n.value
)
end end
rule 'expr : L_PAR expr R_PAR' do |ex, _l, e, _r| rule 'expr : L_PAR expr R_PAR' do |ex, _l, e, _r|
@ -232,25 +259,25 @@ module Namarara
ex.value = NotOp.new(e.value) ex.value = NotOp.new(e.value)
end end
rule 'expr : VAR EQ_OP bool_expr' do |ex, v, _eq, n| # rule 'expr : VAR EQ_OP bool_expr' do |ex, v, _eq, n|
ex.value = EqOp.new( # ex.value = EqOp.new(
VarValue.new(v.value.to_s, @names[v.value]), # VarValue.new(v.value.to_s, @names[v.value.to_s]),
BoolValue.new(n.value) # BoolValue.new(n.value)
) # )
end # end
rule 'expr : VAR EQ_OP STRING' do |ex, v, _eq, n| # rule 'expr : VAR EQ_OP STRING' do |ex, v, _eq, n|
ex.value = EqOp.new( # ex.value = EqOp.new(
VarValue.new(v.value.to_s, @names[v.value]), # VarValue.new(v.value.to_s, @names[v.value.to_s]),
StringValue.new(n.value) # StringValue.new(n.value)
) # )
end # end
rule 'expr : VAR EQ_OP NUMBER' do |ex, v, _eq, n| # rule 'expr : VAR EQ_OP NUMBER' do |ex, v, _eq, n|
ex.value = EqOp.new( # ex.value = EqOp.new(
VarValue.new(v.value.to_s, @names[v.value]), # VarValue.new(v.value.to_s, @names[v.value.to_s]),
NumberValue.new(n.value) # NumberValue.new(n.value)
) # )
# end
end
end end
end # class
end # module