diff --git a/exe/mm2ep-depend b/exe/mm2ep-depend index 6c40978..a3fcfe5 100755 --- a/exe/mm2ep-depend +++ b/exe/mm2ep-depend @@ -12,21 +12,21 @@ module Mm2ep module Depend class ParseCli < Thor - desc 'parse INFILE', 'Parse INFILE into tokens' - def parse(infile) + desc 'parse INFILE VARS', 'Parse INFILE into tokens and evaluate vars' + def parse(infile, *vars) line = File.read(infile).gsub(/\n/,'') parser = Parser.new(Lexer.new) - puts line.inspect + vartruc = {} + vars.each do |var| + vartruc[var.split('=')[0]] = var.split('=')[1] + end + puts vartruc + parser.names(vartruc) token = parser.parse(line.chomp, true) pp token puts "RAW : #{line}" puts "EVAL: #{token.to_s}" - # puts false && true - # puts false || false - # binding.pry - # parser = Parser2.new - # lexer = TestLexer.new(parser.parse(line)) - # puts lexer.testlexe + puts "RESULT: #{token.compute}" end end end diff --git a/lib/mm2ep_depend/parser.rb b/lib/mm2ep_depend/parser.rb index effad62..b0bbebe 100755 --- a/lib/mm2ep_depend/parser.rb +++ b/lib/mm2ep_depend/parser.rb @@ -18,9 +18,16 @@ module Mm2ep end class VarValue < TreeValue - def initialize str + def initialize str, value @name = str - @value = nil + @value = case value + when /true/i then true + when /false/i then false + end + end + + def compute + @value end def to_s @@ -33,6 +40,10 @@ module Mm2ep @value = str.to_i end + def compute + @value + end + def to_s "number:#{@value}" end @@ -43,6 +54,10 @@ module Mm2ep @value = str end + def compute + @value + end + def to_s "string:\"#{@value}\"" end @@ -57,6 +72,10 @@ module Mm2ep end + def compute + @value + end + def to_s "bool:#{@value}" end @@ -69,7 +88,6 @@ module Mm2ep end def compute - # binding.pry return @expr1.compute && @expr2.compute end @@ -114,7 +132,7 @@ module Mm2ep end def compute - return if @lval.value == @rval.value + return @lval.value == @rval.value end def to_s @@ -124,6 +142,10 @@ module Mm2ep class Parser < Rly::Yacc + def names tab + @names = tab + end + precedence :left, :OR_OP precedence :left, :AND_OP precedence :left, :EQ_OP @@ -136,7 +158,7 @@ module Mm2ep rule 'expr : VAR' do |ex, l| ex.value = EqOp.new( - VarExpr.new(l.value), + VarValue.new(l.value, @names[l.value]), BoolValue.new('true') ) end @@ -170,22 +192,23 @@ module Mm2ep end rule 'expr : VAR EQ_OP bool_expr' do |ex, v, eq, n| + # binding.pry ex.value = EqOp.new( - VarValue.new(v.value.to_s), + VarValue.new(v.value.to_s, @names[v.value]), BoolValue.new(n.value) ) end rule 'expr : VAR EQ_OP STRING' do |ex, v, eq, n| ex.value = EqOp.new( - VarValue.new(v.value.to_s), + VarValue.new(v.value.to_s, @names[v.value]), StringValue.new(n.value) ) end rule 'expr : VAR EQ_OP NUMBER' do |ex, v, eq, n| ex.value = EqOp.new( - VarValue.new(v.value.to_s), + VarValue.new(v.value.to_s, @names[v.value]), NumberValue.new(n.value) ) end diff --git a/spec/files/test2.txt b/spec/files/test2.txt index 6ac6b5e..eed18cf 100644 --- a/spec/files/test2.txt +++ b/spec/files/test2.txt @@ -1 +1 @@ -NOT ( truc_bidule = true ) OR ( false ) AND machin = false +NOT ( truc_bidule = true ) OR ( truc ) AND machin = false