2017-09-04 09:19:29 +00:00
|
|
|
module Mm2ep
|
|
|
|
module Depend
|
2017-09-12 14:05:50 +00:00
|
|
|
class TreeExpr
|
2017-09-12 13:58:15 +00:00
|
|
|
def compute
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-12 14:05:50 +00:00
|
|
|
class TreeValue
|
2017-09-12 14:19:51 +00:00
|
|
|
def value
|
|
|
|
raise "No value for #{@name}" if @value.nil?
|
2017-09-12 14:32:03 +00:00
|
|
|
@value
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
raise NotImplementedError
|
2017-09-12 14:19:51 +00:00
|
|
|
end
|
2017-09-12 14:05:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
class VarValue < TreeValue
|
2017-09-12 13:58:15 +00:00
|
|
|
def initialize str
|
|
|
|
@name = str
|
|
|
|
@value = nil
|
|
|
|
end
|
2017-09-12 14:32:03 +00:00
|
|
|
|
|
|
|
def to_s
|
|
|
|
"var:#{@name}<-(#{@value})"
|
|
|
|
end
|
2017-09-12 13:58:15 +00:00
|
|
|
end
|
|
|
|
|
2017-09-12 14:05:50 +00:00
|
|
|
class NumberValue < TreeValue
|
2017-09-12 14:32:03 +00:00
|
|
|
def initialize str
|
|
|
|
@value = str.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
"number:#{@value}"
|
|
|
|
end
|
2017-09-12 14:05:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
class StringValue < TreeValue
|
2017-09-12 14:32:03 +00:00
|
|
|
def initialize str
|
|
|
|
@value = str
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
"string:\"#{@value}\""
|
|
|
|
end
|
2017-09-12 14:05:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
class BoolValue < TreeValue
|
2017-09-12 13:58:15 +00:00
|
|
|
def initialize str
|
|
|
|
@value = case str
|
|
|
|
when /true/i then true
|
|
|
|
when /false/i then false
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-09-12 14:32:03 +00:00
|
|
|
def to_s
|
|
|
|
"bool:#{@value}"
|
|
|
|
end
|
2017-09-12 13:58:15 +00:00
|
|
|
end
|
|
|
|
|
2017-09-12 14:05:50 +00:00
|
|
|
class AndOp < TreeExpr
|
2017-09-11 12:33:29 +00:00
|
|
|
def initialize expr1, expr2
|
2017-09-12 13:58:15 +00:00
|
|
|
@expr1 = expr1
|
|
|
|
@expr2 = expr2
|
2017-09-11 12:33:29 +00:00
|
|
|
end
|
|
|
|
|
2017-09-12 13:58:15 +00:00
|
|
|
def compute
|
2017-09-12 13:44:10 +00:00
|
|
|
# binding.pry
|
2017-09-12 13:58:15 +00:00
|
|
|
return @expr1.compute && @expr2.compute
|
2017-09-11 12:33:29 +00:00
|
|
|
end
|
2017-09-12 14:32:03 +00:00
|
|
|
|
|
|
|
def to_s
|
2017-09-12 15:09:53 +00:00
|
|
|
"( #{@expr1.to_s}) AND (#{@expr2.to_s} )"
|
2017-09-12 14:32:03 +00:00
|
|
|
end
|
2017-09-11 12:33:29 +00:00
|
|
|
end
|
|
|
|
|
2017-09-12 13:58:15 +00:00
|
|
|
class OrOp
|
2017-09-11 12:33:29 +00:00
|
|
|
def initialize expr1, expr2
|
2017-09-12 14:23:44 +00:00
|
|
|
@expr1 = expr1
|
|
|
|
@expr2 = expr2
|
2017-09-11 12:33:29 +00:00
|
|
|
end
|
|
|
|
|
2017-09-12 13:58:15 +00:00
|
|
|
def compute
|
2017-09-12 14:23:44 +00:00
|
|
|
return @expr1.compute || @expr2.compute
|
2017-09-11 12:33:29 +00:00
|
|
|
end
|
2017-09-12 14:32:03 +00:00
|
|
|
|
|
|
|
def to_s
|
2017-09-12 15:09:53 +00:00
|
|
|
"( #{@expr1.to_s}) OR (#{@expr2.to_s} )"
|
2017-09-12 14:32:03 +00:00
|
|
|
end
|
2017-09-11 12:33:29 +00:00
|
|
|
end
|
|
|
|
|
2017-09-12 14:05:50 +00:00
|
|
|
class NotOp
|
2017-09-11 12:33:29 +00:00
|
|
|
def initialize expr
|
|
|
|
@expr = expr
|
|
|
|
end
|
|
|
|
|
2017-09-12 13:58:15 +00:00
|
|
|
def compute
|
2017-09-12 14:23:44 +00:00
|
|
|
return ! @expr.compute
|
2017-09-11 12:33:29 +00:00
|
|
|
end
|
2017-09-12 14:32:03 +00:00
|
|
|
|
|
|
|
def to_s
|
2017-09-12 15:09:53 +00:00
|
|
|
"NOT ( #{@expr.to_s} )"
|
2017-09-12 14:32:03 +00:00
|
|
|
end
|
2017-09-11 12:33:29 +00:00
|
|
|
end
|
|
|
|
|
2017-09-12 13:58:15 +00:00
|
|
|
class EqOp
|
2017-09-12 14:23:44 +00:00
|
|
|
def initialize lval, rval
|
|
|
|
@lval = lval
|
|
|
|
@rval = rval
|
2017-09-11 12:33:29 +00:00
|
|
|
end
|
|
|
|
|
2017-09-12 13:58:15 +00:00
|
|
|
def compute
|
2017-09-12 14:23:44 +00:00
|
|
|
return if @lval.value == @rval.value
|
2017-09-11 12:33:29 +00:00
|
|
|
end
|
2017-09-12 14:32:03 +00:00
|
|
|
|
|
|
|
def to_s
|
2017-09-12 14:33:18 +00:00
|
|
|
"#{@lval.to_s} = #{@rval.to_s}"
|
2017-09-12 14:32:03 +00:00
|
|
|
end
|
2017-09-11 12:33:29 +00:00
|
|
|
end
|
|
|
|
|
2017-09-07 15:05:06 +00:00
|
|
|
class Parser < Rly::Yacc
|
|
|
|
|
2017-09-12 15:09:53 +00:00
|
|
|
precedence :left, 'OR_OP'
|
|
|
|
precedence :left, 'AND_OP'
|
|
|
|
precedence :left, 'EQ_OP'
|
|
|
|
precedence :right, :UMINUS
|
2017-09-11 08:13:30 +00:00
|
|
|
|
2017-09-07 15:05:06 +00:00
|
|
|
rule 'statement : expr' do |st, e|
|
|
|
|
st.value = e.value
|
|
|
|
end
|
|
|
|
|
2017-09-11 08:13:30 +00:00
|
|
|
rule 'expr : VAR' do |ex, l|
|
2017-09-12 14:05:50 +00:00
|
|
|
ex.value = EqOp.new(
|
|
|
|
VarExpr.new(l.value),
|
|
|
|
BoolValue.new('true')
|
|
|
|
)
|
2017-09-11 08:13:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
rule 'expr : T_BOOL' do |ex, l|
|
2017-09-12 14:16:57 +00:00
|
|
|
ex.value = BoolValue.new(l.value.to_s)
|
2017-09-11 08:13:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
rule 'expr : F_BOOL' do |ex, l|
|
2017-09-12 14:16:57 +00:00
|
|
|
ex.value = BoolValue.new(l.value.to_s)
|
2017-09-11 08:13:30 +00:00
|
|
|
end
|
|
|
|
|
2017-09-12 15:09:53 +00:00
|
|
|
rule 'expr : NOT_OP SPACE expr %prec UMINUS' do |ex, l, s, e|
|
2017-09-12 14:05:50 +00:00
|
|
|
ex.value = NotOp.new(e.value)
|
2017-09-07 15:05:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
rule 'expr : expr SPACE AND_OP SPACE expr' do |ex, l, s, e, sp, r|
|
2017-09-12 13:58:15 +00:00
|
|
|
ex.value = AndOp.new(l.value, r.value)
|
2017-09-07 15:05:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
rule 'expr : expr SPACE OR_OP SPACE expr' do |ex, l, s, e, sp, r|
|
2017-09-12 13:58:15 +00:00
|
|
|
ex.value = OrOp.new(l.value, r.value)
|
2017-09-07 15:05:06 +00:00
|
|
|
end
|
|
|
|
rule 'expr : L_PAR SPACE expr SPACE R_PAR' do |ex, l, s, e, sp, r|
|
2017-09-11 12:33:29 +00:00
|
|
|
ex.value = e.value
|
2017-09-07 15:05:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
rule 'expr : VAR SPACE EQ_OP SPACE F_BOOL' do |ex, v, s, eq, _, n|
|
2017-09-12 14:16:57 +00:00
|
|
|
ex.value = EqOp.new(
|
|
|
|
VarValue.new(v.value.to_s),
|
|
|
|
BoolValue.new(n.value)
|
|
|
|
)
|
2017-09-07 15:05:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
rule 'expr : VAR SPACE EQ_OP SPACE T_BOOL' do |ex, v, s, eq, _, n|
|
2017-09-12 14:16:57 +00:00
|
|
|
ex.value = EqOp.new(
|
|
|
|
VarValue.new(v.value.to_s),
|
|
|
|
BoolValue.new(n.value)
|
|
|
|
)
|
2017-09-07 15:05:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
rule 'expr : VAR SPACE EQ_OP SPACE STRING' do |ex, v, s, eq, _, n|
|
2017-09-12 14:16:57 +00:00
|
|
|
ex.value = EqOp.new(
|
|
|
|
VarValue.new(v.value.to_s),
|
|
|
|
StringValue.new(n.value)
|
|
|
|
)
|
2017-09-07 15:05:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
rule 'expr : VAR SPACE EQ_OP SPACE NUMBER' do |ex, v, s, eq, _, n|
|
2017-09-12 14:16:57 +00:00
|
|
|
ex.value = EqOp.new(
|
|
|
|
VarValue.new(v.value.to_s),
|
|
|
|
NumberValue.new(n.value)
|
|
|
|
)
|
2017-09-07 15:05:06 +00:00
|
|
|
end
|
|
|
|
|
2017-09-04 09:19:29 +00:00
|
|
|
end # class
|
|
|
|
end # module
|
|
|
|
end # module
|