Add evaluation

This commit is contained in:
Roguelearg 2017-09-11 14:33:29 +02:00
parent 977391f75f
commit 252f8531b9
3 changed files with 66 additions and 18 deletions

View file

@ -16,16 +16,15 @@ module Mm2ep
class ParseCli < Thor class ParseCli < Thor
desc 'parse INFILE', 'Parse INFILE into tokens' desc 'parse INFILE', 'Parse INFILE into tokens'
def parse(infile) def parse(infile)
File.open(infile).each_line do |line| File.open(infile).each_line do |line|
# parser = Parser.new(Lexer.new) parser = Parser.new(Lexer.new)
# puts line.inspect puts line.inspect
# token = parser.parse(line.chomp, true) token = parser.parse(line.chomp, true)
# puts token puts token
# binding.pry # binding.pry
parser = Parser2.new # parser = Parser2.new
lexer = TestLexer.new(parser.parse(line)) # lexer = TestLexer.new(parser.parse(line))
puts lexer.testlexe # puts lexer.testlexe
end end
end end
end end

View file

@ -26,6 +26,7 @@ module Mm2ep
@atteint = false @atteint = false
end end
#
def try_expr def try_expr
@truc = Hash[@truc.sort_by { |key, value| value.length }.reverse!] @truc = Hash[@truc.sort_by { |key, value| value.length }.reverse!]
@truc.each do |_, value| @truc.each do |_, value|
@ -57,13 +58,13 @@ module Mm2ep
@fait = false @fait = false
try_expr try_expr
# binding.pry # 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')) if !@atteint || (!@tokens[@i].eql?('OR_OP') && !@tokens[@i].eql?('AND_OP'))
move_forward move_forward
else else
move_backwards move_backwards
end end
elsif !fait elsif !@fait
move_backwards move_backwards
end end
end end

View file

@ -1,5 +1,53 @@
module Mm2ep module Mm2ep
module Depend 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 class Parser < Rly::Yacc
precedence :left, 'OR_OP', 'EQ_OP' precedence :left, 'OR_OP', 'EQ_OP'
@ -22,34 +70,34 @@ module Mm2ep
end end
rule 'expr : NOT_OP SPACE expr' do |ex, l, s, e| 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 end
rule 'expr : expr SPACE AND_OP SPACE expr' do |ex, l, s, e, sp, r| 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 end
rule 'expr : expr SPACE OR_OP SPACE expr' do |ex, l, s, e, sp, r| 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 end
rule 'expr : L_PAR SPACE expr SPACE R_PAR' do |ex, l, s, e, sp, r| 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 end
rule 'expr : VAR SPACE EQ_OP SPACE F_BOOL' do |ex, v, s, eq, _, n| 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 end
rule 'expr : VAR SPACE EQ_OP SPACE T_BOOL' do |ex, v, s, eq, _, n| 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 end
rule 'expr : VAR SPACE EQ_OP SPACE STRING' do |ex, v, s, eq, _, n| 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 end
rule 'expr : VAR SPACE EQ_OP SPACE NUMBER' do |ex, v, s, eq, _, n| 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
end # class end # class