Ignore space

This commit is contained in:
Roguelearg 2017-09-12 17:25:16 +02:00
parent b89add6adf
commit 9fac1514c9
3 changed files with 62 additions and 59 deletions

View file

@ -2,7 +2,9 @@ module Mm2ep
module Depend module Depend
class Lexer < Rly::Lex class Lexer < Rly::Lex
token :SPACE, /\s+/ ignore "\t\n "
# token :SPACE, /\s+/
token :L_PAR, /\(/ token :L_PAR, /\(/
token :R_PAR, /\)/ token :R_PAR, /\)/
token :NUMBER, /[0-9]+(\.[0-9]+)?/ token :NUMBER, /[0-9]+(\.[0-9]+)?/

View file

@ -1,70 +1,70 @@
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? raise "No value for #{@name}" if @value.nil?
@value @value
end end
def to_s def to_s
raise NotImplementedError raise NotImplementedError
end end
end end
class VarValue < TreeValue class VarValue < TreeValue
def initialize str def initialize str
@name = str @name = str
@value = nil @value = nil
end end
def to_s def to_s
"var:#{@name}<-(#{@value})" "var:#{@name}<-(#{@value})"
end end
end end
class NumberValue < TreeValue class NumberValue < TreeValue
def initialize str def initialize str
@value = str.to_i @value = str.to_i
end end
def to_s def to_s
"number:#{@value}" "number:#{@value}"
end end
end end
class StringValue < TreeValue class StringValue < TreeValue
def initialize str def initialize str
@value = str @value = str
end end
def to_s def to_s
"string:\"#{@value}\"" "string:\"#{@value}\""
end end
end end
class BoolValue < TreeValue class BoolValue < TreeValue
def initialize str def initialize str
@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
end end
end end
def to_s def to_s
"bool:#{@value}" "bool:#{@value}"
end end
end end
class AndOp < TreeExpr class AndOp < TreeExpr
def initialize expr1, expr2 def initialize expr1, expr2
@expr1 = expr1 @expr1 = expr1
@expr2 = expr2 @expr2 = expr2
end end
@ -73,14 +73,14 @@ module Mm2ep
return @expr1.compute && @expr2.compute return @expr1.compute && @expr2.compute
end end
def to_s def to_s
"( #{@expr1.to_s}) AND (#{@expr2.to_s} )" "( #{@expr1.to_s}) AND (#{@expr2.to_s} )"
end end
end end
class OrOp class OrOp
def initialize expr1, expr2 def initialize expr1, expr2
@expr1 = expr1 @expr1 = expr1
@expr2 = expr2 @expr2 = expr2
end end
@ -88,9 +88,9 @@ module Mm2ep
return @expr1.compute || @expr2.compute return @expr1.compute || @expr2.compute
end end
def to_s def to_s
"( #{@expr1.to_s}) OR (#{@expr2.to_s} )" "( #{@expr1.to_s}) OR (#{@expr2.to_s} )"
end end
end end
class NotOp class NotOp
@ -99,17 +99,17 @@ module Mm2ep
end end
def compute def compute
return ! @expr.compute return ! @expr.compute
end end
def to_s def to_s
"NOT ( #{@expr.to_s} )" "NOT ( #{@expr.to_s} )"
end end
end end
class EqOp class EqOp
def initialize lval, rval def initialize lval, rval
@lval = lval @lval = lval
@rval = rval @rval = rval
end end
@ -117,16 +117,16 @@ module Mm2ep
return if @lval.value == @rval.value return if @lval.value == @rval.value
end end
def to_s def to_s
"#{@lval.to_s} = #{@rval.to_s}" "#{@lval.to_s} = #{@rval.to_s}"
end end
end end
class Parser < Rly::Yacc class Parser < Rly::Yacc
precedence :left, 'OR_OP' precedence :left, 'OR_OP'
precedence :left, 'AND_OP' precedence :left, 'AND_OP'
precedence :left, 'EQ_OP' precedence :left, 'EQ_OP'
precedence :right, :UMINUS precedence :right, :UMINUS
rule 'statement : expr' do |st, e| rule 'statement : expr' do |st, e|
@ -135,7 +135,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), VarExpr.new(l.value),
BoolValue.new('true') BoolValue.new('true')
) )
end end
@ -148,45 +148,45 @@ module Mm2ep
ex.value = BoolValue.new(l.value.to_s) ex.value = BoolValue.new(l.value.to_s)
end end
rule 'expr : NOT_OP SPACE expr %prec UMINUS' do |ex, l, s, e| rule 'expr : NOT_OP expr %prec UMINUS' do |ex, l, e|
ex.value = NotOp.new(e.value) ex.value = NotOp.new(e.value)
end end
rule 'expr : expr SPACE AND_OP SPACE expr' do |ex, l, s, e, sp, r| rule 'expr : expr AND_OP expr' do |ex, l, e, r|
ex.value = AndOp.new(l.value, r.value) ex.value = AndOp.new(l.value, r.value)
end end
rule 'expr : expr SPACE OR_OP SPACE expr' do |ex, l, s, e, sp, r| rule 'expr : expr OR_OP expr' do |ex, l, e, r|
ex.value = OrOp.new(l.value, r.value) ex.value = OrOp.new(l.value, r.value)
end end
rule 'expr : L_PAR SPACE expr SPACE R_PAR' do |ex, l, s, e, sp, r| rule 'expr : L_PAR expr R_PAR' do |ex, l, e, r|
ex.value = e.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 EQ_OP F_BOOL' 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),
BoolValue.new(n.value) BoolValue.new(n.value)
) )
end end
rule 'expr : VAR SPACE EQ_OP SPACE T_BOOL' do |ex, v, s, eq, _, n| rule 'expr : VAR EQ_OP T_BOOL' 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),
BoolValue.new(n.value) BoolValue.new(n.value)
) )
end end
rule 'expr : VAR SPACE EQ_OP SPACE STRING' do |ex, v, s, 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),
StringValue.new(n.value) StringValue.new(n.value)
) )
end end
rule 'expr : VAR SPACE EQ_OP SPACE NUMBER' do |ex, v, s, 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),
NumberValue.new(n.value) NumberValue.new(n.value)
) )
end end

1
spec/files/test5.txt Normal file
View file

@ -0,0 +1 @@
truc bidule