Add Evaluation grammar

This commit is contained in:
Roguelearg 2017-09-13 14:55:56 +02:00
parent f7cf3da18f
commit d555556e54
3 changed files with 41 additions and 18 deletions

View file

@ -12,21 +12,21 @@ module Mm2ep
module Depend module Depend
class ParseCli < Thor class ParseCli < Thor
desc 'parse INFILE', 'Parse INFILE into tokens' desc 'parse INFILE VARS', 'Parse INFILE into tokens and evaluate vars'
def parse(infile) def parse(infile, *vars)
line = File.read(infile).gsub(/\n/,'') line = File.read(infile).gsub(/\n/,'')
parser = Parser.new(Lexer.new) 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) token = parser.parse(line.chomp, true)
pp token pp token
puts "RAW : #{line}" puts "RAW : #{line}"
puts "EVAL: #{token.to_s}" puts "EVAL: #{token.to_s}"
# puts false && true puts "RESULT: #{token.compute}"
# puts false || false
# binding.pry
# parser = Parser2.new
# lexer = TestLexer.new(parser.parse(line))
# puts lexer.testlexe
end end
end end
end end

View file

@ -18,9 +18,16 @@ module Mm2ep
end end
class VarValue < TreeValue class VarValue < TreeValue
def initialize str def initialize str, value
@name = str @name = str
@value = nil @value = case value
when /true/i then true
when /false/i then false
end
end
def compute
@value
end end
def to_s def to_s
@ -33,6 +40,10 @@ module Mm2ep
@value = str.to_i @value = str.to_i
end end
def compute
@value
end
def to_s def to_s
"number:#{@value}" "number:#{@value}"
end end
@ -43,6 +54,10 @@ module Mm2ep
@value = str @value = str
end end
def compute
@value
end
def to_s def to_s
"string:\"#{@value}\"" "string:\"#{@value}\""
end end
@ -57,6 +72,10 @@ module Mm2ep
end end
def compute
@value
end
def to_s def to_s
"bool:#{@value}" "bool:#{@value}"
end end
@ -69,7 +88,6 @@ module Mm2ep
end end
def compute def compute
# binding.pry
return @expr1.compute && @expr2.compute return @expr1.compute && @expr2.compute
end end
@ -114,7 +132,7 @@ module Mm2ep
end end
def compute def compute
return if @lval.value == @rval.value return @lval.value == @rval.value
end end
def to_s def to_s
@ -124,6 +142,10 @@ module Mm2ep
class Parser < Rly::Yacc class Parser < Rly::Yacc
def names tab
@names = tab
end
precedence :left, :OR_OP precedence :left, :OR_OP
precedence :left, :AND_OP precedence :left, :AND_OP
precedence :left, :EQ_OP precedence :left, :EQ_OP
@ -136,7 +158,7 @@ module Mm2ep
rule 'expr : VAR' do |ex, l| rule 'expr : VAR' do |ex, l|
ex.value = EqOp.new( ex.value = EqOp.new(
VarExpr.new(l.value), VarValue.new(l.value, @names[l.value]),
BoolValue.new('true') BoolValue.new('true')
) )
end end
@ -170,22 +192,23 @@ module Mm2ep
end end
rule 'expr : VAR EQ_OP bool_expr' do |ex, v, eq, n| rule 'expr : VAR EQ_OP bool_expr' do |ex, v, eq, n|
# binding.pry
ex.value = EqOp.new( ex.value = EqOp.new(
VarValue.new(v.value.to_s), VarValue.new(v.value.to_s, @names[v.value]),
BoolValue.new(n.value) BoolValue.new(n.value)
) )
end end
rule 'expr : VAR EQ_OP STRING' do |ex, v, eq, n| rule 'expr : VAR EQ_OP STRING' do |ex, v, eq, n|
ex.value = EqOp.new( ex.value = EqOp.new(
VarValue.new(v.value.to_s), VarValue.new(v.value.to_s, @names[v.value]),
StringValue.new(n.value) StringValue.new(n.value)
) )
end end
rule 'expr : VAR EQ_OP NUMBER' do |ex, v, eq, n| rule 'expr : VAR EQ_OP NUMBER' do |ex, v, eq, n|
ex.value = EqOp.new( ex.value = EqOp.new(
VarValue.new(v.value.to_s), VarValue.new(v.value.to_s, @names[v.value]),
NumberValue.new(n.value) NumberValue.new(n.value)
) )
end end

View file

@ -1 +1 @@
NOT ( truc_bidule = true ) OR ( false ) AND machin = false NOT ( truc_bidule = true ) OR ( truc ) AND machin = false