diff --git a/exe/mm2ep-depend b/exe/mm2ep-depend index b2a5dab..d743cba 100755 --- a/exe/mm2ep-depend +++ b/exe/mm2ep-depend @@ -16,16 +16,15 @@ module Mm2ep class ParseCli < Thor desc 'parse INFILE', 'Parse INFILE into tokens' def parse(infile) - File.open(infile).each_line do |line| - # parser = Parser.new(Lexer.new) - # puts line.inspect - # token = parser.parse(line.chomp, true) - # puts token + parser = Parser.new(Lexer.new) + puts line.inspect + token = parser.parse(line.chomp, true) + puts token # binding.pry - parser = Parser2.new - lexer = TestLexer.new(parser.parse(line)) - puts lexer.testlexe + # parser = Parser2.new + # lexer = TestLexer.new(parser.parse(line)) + # puts lexer.testlexe end end end diff --git a/lib/lexer2.rb b/lib/lexer2.rb index 502a390..bb71d6a 100644 --- a/lib/lexer2.rb +++ b/lib/lexer2.rb @@ -26,6 +26,7 @@ module Mm2ep @atteint = false end + # def try_expr @truc = Hash[@truc.sort_by { |key, value| value.length }.reverse!] @truc.each do |_, value| @@ -57,13 +58,13 @@ module Mm2ep @fait = false try_expr # binding.pry - if !fait && @i < @tokens.size-1 + if !@fait && @i < @tokens.size-1 if !@atteint || (!@tokens[@i].eql?('OR_OP') && !@tokens[@i].eql?('AND_OP')) move_forward else move_backwards end - elsif !fait + elsif !@fait move_backwards end end diff --git a/lib/parser.rb b/lib/parser.rb index d1bafdd..69ace7e 100755 --- a/lib/parser.rb +++ b/lib/parser.rb @@ -1,5 +1,53 @@ module Mm2ep module Depend + class And_op + attr_reader :expr1, :expr2 + def initialize expr1, expr2 + @expr1 = expr1 + @expr2 = expr2 + end + + def and_op + return expr1 && expr2 + end + end + + class Or_op + attr_reader :expr1, :expr2 + def initialize expr1, expr2 + @expr1 = expr1 + @expr2 = expr2 + end + + def or_op + return expr1 || expr2 + end + end + + class Not_op + attr_reader :expr + def initialize expr + @expr = expr + end + + def not_op + return true unless expr == true + return false + end + end + + class Eq_op + attr_reader :val, :other + def initialize val, other + @val = val + @other = other + end + + def eq_op + return val = other + end + end + class Parser < Rly::Yacc precedence :left, 'OR_OP', 'EQ_OP' @@ -22,34 +70,34 @@ module Mm2ep end rule 'expr : NOT_OP SPACE expr' do |ex, l, s, e| - ex.value = "#{l.value} #{s.value} #{e.value}" + ex.value = Not_op.new(e).not_op end rule 'expr : expr SPACE AND_OP SPACE expr' do |ex, l, s, e, sp, r| - ex.value = "#{l.value} #{s.value} #{e.value} #{sp.value} #{r.value}" + ex.value = And_op.new(l, r).and_op end rule 'expr : expr SPACE OR_OP SPACE expr' do |ex, l, s, e, sp, r| - ex.value = "#{l.value} #{s.value} #{e.value} #{sp.value} #{r.value}" + ex.value = Or_op.new(l, r).or_op end rule 'expr : L_PAR SPACE expr SPACE R_PAR' do |ex, l, s, e, sp, r| - ex.value = "#{l.value} #{s.value} #{e.value} #{sp.value} #{r.value}" + ex.value = e.value end rule 'expr : VAR SPACE EQ_OP SPACE F_BOOL' do |ex, v, s, eq, _, n| - ex.value = "#{v.value} #{s.value} #{eq.value} #{s.value} #{n.value}" + ex.value = Eq_op.new(v, n).eq_op end rule 'expr : VAR SPACE EQ_OP SPACE T_BOOL' do |ex, v, s, eq, _, n| - ex.value = "#{v.value} #{s.value} #{eq.value} #{s.value} #{n.value}" + ex.value = Eq_op.new(v, n).eq_op end rule 'expr : VAR SPACE EQ_OP SPACE STRING' do |ex, v, s, eq, _, n| - ex.value = "#{v.value} #{s.value} #{eq.value} #{s.value} #{n.value}" + ex.value = Eq_op.new(v, n).eq_op end rule 'expr : VAR SPACE EQ_OP SPACE NUMBER' do |ex, v, s, eq, _, n| - ex.value = "#{v.value} #{s.value} #{eq.value} #{s.value} #{n.value}" + ex.value = Eq_op.new(v, n).eq_op end end # class