Add exit before evaluate grammar if there are errors

This commit is contained in:
Roguelearg 2017-09-14 16:52:54 +02:00
parent 0b2c565cff
commit cc435be659
2 changed files with 53 additions and 12 deletions

View file

@ -20,7 +20,7 @@ module Mm2ep
vars.each do |var| vars.each do |var|
tab_vars[var.split('=')[0]] = var.split('=')[1] tab_vars[var.split('=')[0]] = var.split('=')[1]
end end
binding.pry
# Give vars name and value from shell command to parser # Give vars name and value from shell command to parser
parser.names=tab_vars parser.names=tab_vars
@ -28,6 +28,7 @@ module Mm2ep
pp token pp token
puts "RAW : #{line}" puts "RAW : #{line}"
puts "EVAL: #{token.to_s}" puts "EVAL: #{token.to_s}"
exit 1 unless token.errors.empty?
puts "RESULT: #{token.compute}" puts "RESULT: #{token.compute}"
end end
end end

View file

@ -1,14 +1,15 @@
module Mm2ep module Mm2ep
module Depend module Depend
class TreeExpr class TreeExpr
def compute def compute
raise NotImplementedError raise NotImplementedError
end end
end end
class TreeValue class TreeValue
def value def value
raise "No value for #{@name}" if @value.nil?
@value @value
end end
@ -18,13 +19,22 @@ module Mm2ep
end end
class VarValue < TreeValue class VarValue < TreeValue
attr_reader :errors
def initialize(str, value) def initialize(str, value)
@errors = []
@name = str @name = str
@value = case value @value = case value
when /true/i then true when /true/i then true
when /false/i then false when /false/i then false
else value else value
end end
if @value.nil?
@errors << VarNotDefined.new(
message:"No value for #{@name}",
var: @name
)
end
end end
def compute def compute
@ -37,7 +47,10 @@ module Mm2ep
end end
class NumberValue < TreeValue class NumberValue < TreeValue
attr_reader :errors
def initialize(str) def initialize(str)
@errors = []
@value = str @value = str
end end
@ -51,7 +64,10 @@ module Mm2ep
end end
class StringValue < TreeValue class StringValue < TreeValue
attr_reader :errors
def initialize(str) def initialize(str)
@errors = []
@value = str @value = str
end end
@ -65,7 +81,10 @@ module Mm2ep
end end
class BoolValue < TreeValue class BoolValue < TreeValue
attr_reader :errors
def initialize(str) def initialize(str)
@errors = []
@value = case str @value = case str
when /true/i then true when /true/i then true
when /false/i then false when /false/i then false
@ -82,37 +101,51 @@ module Mm2ep
end end
class AndOp < TreeExpr class AndOp < TreeExpr
def initialize(expr1, expr2) attr_reader :errors
@expr1 = expr1
@expr2 = expr2 def initialize(lval, rval)
@errors = []
@errors.concat lval.errors
@errors.concat rval.errors
@lval = lval
@rval = rval
end end
def compute def compute
@expr1.compute && @expr2.compute @lval.compute && @rval.compute
end end
def to_s def to_s
"( #{@expr1} ) AND ( #{@expr2} )" "( #{@lval} ) AND ( #{@rval} )"
end end
end end
class OrOp class OrOp
def initialize(expr1, expr2) attr_reader :errors
@expr1 = expr1
@expr2 = expr2 def initialize(lval, rval)
@errors = []
@errors.concat lval.errors
@errors.concat rval.errors
@lval = lval
@rval = rval
end end
def compute def compute
@expr1.compute || @expr2.compute @lval.compute || @rval.compute
end end
def to_s def to_s
"( #{@expr1} ) OR ( #{@expr2} )" "( #{@lval} ) OR ( #{@rval} )"
end end
end end
class NotOp class NotOp
attr_reader :errors
def initialize(expr) def initialize(expr)
@errors = []
@errors.concat expr.errors
@expr = expr @expr = expr
end end
@ -126,9 +159,15 @@ module Mm2ep
end end
class EqOp class EqOp
attr_reader :errors
def initialize(lval, rval) def initialize(lval, rval)
@errors = []
@errors.concat lval.errors
@errors.concat rval.errors
@lval = lval @lval = lval
@rval = rval @rval = rval
end end
def compute def compute
@ -142,6 +181,7 @@ module Mm2ep
class Parser < Rly::Yacc class Parser < Rly::Yacc
attr_writer :names attr_writer :names
attr_reader :errors
# def names(tab) # def names(tab)
# @names = tab # @names = tab
# end # end