adds basic math operators, thanks to kasper associative and prioritised

This commit is contained in:
Torsten Ruger
2014-05-10 21:41:46 +03:00
parent 6378209f33
commit 2d389d2e00
7 changed files with 108 additions and 5 deletions

View File

@ -14,5 +14,9 @@ module Parser
rule(:expressions_else) { delimited_expressions(keyword_else) }
rule(:expressions_end) { delimited_expressions(keyword_end) }
rule(:operator_expression) do
infix_expression(expression, [multiply, 2, :left], [plus, 1, :right])
end
end
end

View File

@ -12,7 +12,10 @@ module Parser
rule(:comma) { str(',') >> space? }
rule(:colon) { str(':') >> space? }
rule(:semicolon) { str(';') >> space? }
rule(:question_mark) { str('?') >> space? }
rule(:excamation_mark) { str('!') >> space? }
rule(:question_mark) { str('?') >> space? }
rule(:excamation_mark) { str('!') >> space? }
rule(:multiply) { match['*/'] >> space? }
rule(:plus) { match['+-'] >> space? }
end
end

View File

@ -41,6 +41,11 @@ module Parser
rule(:asignee => simple(:left) , :asigned => simple(:right) ) do
Ast::AssignmentExpression.new(left , right )
end
rule(l: simple(:l), o: simple(:o) , r: simple(:r)) do
Ast::OperatorExpression.new( o.to_s.strip , l ,r)
end
#shortcut to get the ast tree for a given string
# optional second arguement specifies a rule that will be parsed (mainly for testing)
def self.ast string , rule = :root