fix operators, only binary

This commit is contained in:
Torsten Ruger 2015-10-09 17:25:09 +03:00
parent ba7ecbfa7b
commit af8febe456
17 changed files with 81 additions and 82 deletions

View File

@ -1,48 +1,15 @@
module Parser
module Operators
include Parslet
rule(:exponent) { str('**') >> space?}
rule(:multiply) { match['*/%'] >> space? }
rule(:plus) { match['+-'] >> space? }
rule(:shift) { str(">>") | str("<<") >> space?}
rule(:bit_and) { str('&') >> space?}
rule(:bit_or) { str('|') >> space?}
rule(:greater_equal) { str('>=') >> space?}
rule(:less_or_equal) { str('<=') >> space?}
rule(:larger) { str('>') >> space?}
rule(:smaller) { str('<') >> space?}
rule(:identity) { str('===') >> space?}
rule(:equal) { str('==') >> space?}
rule(:not_equal) { str('!=') >> space?}
rule(:boolean_and) { str('&&') | str("and") >> space?}
rule(:boolean_or) { str('||') | str("or") >> space?}
rule(:assign) { str('=') >> space?}
rule(:op_assign) { str('+=')|str('-=')|str('*=')|str('/=')|str('%=') >> space?}
rule(:eclipse) { str('..') |str("...") >> space?}
rule(:assign) { str('=') >> space?}
#infix doing the heavy lifting here,
# is defined as an expressions and array of [atoms,priority,binding] triples
rule(:operator_expression) do infix_expression(r_value,
[exponent, 120, :left] ,
[multiply, 120, :left] ,
[plus, 110, :left],
[shift, 100, :left],
[bit_and, 90, :left],
[bit_or, 90, :right],
[greater_equal, 80, :left],
[less_or_equal, 80, :left],
[larger, 80, :left],
[smaller, 80, :left],
[identity, 70, :right],
[equal, 70, :right],
[not_equal, 70, :right],
[boolean_and, 60, :left],
[boolean_or, 50, :right],
[eclipse, 40, :right],
[keyword_rescue, 30, :right],
[assign, 20, :right],
[op_assign, 20, :right] ) >> space?
end
rule(:operator_sym) {
str('**') | str('*') | str('/') | str('/') | str('%') |
str('+') | str('-') | str('<<')| str('>>') |
str('|') | str('&') |
str('>=') | str('<=') | str('>') | str('<') |
str('==') | str('!=') }
rule(:operator_value) { (field_access|basic_type).as(:left) >>
operator_sym.as(:operator) >> space? >> (field_access|basic_type).as(:right) }
end
end

View File

@ -0,0 +1,9 @@
foo.bar - gumbar
-- -- --
s(:operator_value, :-,
s(:field_access,
s(:receiver,
s(:name, :foo)),
s(:field,
s(:name, :bar))),
s(:name, :gumbar))

View File

@ -0,0 +1,9 @@
5 % foo.bar
-- -- --
s(:operator_value, :%,
s(:int, 5),
s(:field_access,
s(:receiver,
s(:name, :foo)),
s(:field,
s(:name, :bar))))

View File

@ -0,0 +1,5 @@
3 > var
-- -- --
s(:operator_value, :>,
s(:int, 3),
s(:name, :var))

View File

@ -0,0 +1,5 @@
5 + 7
-- -- --
s(:operator_value, :+,
s(:int, 5),
s(:int, 7))

View File

@ -1,6 +1,5 @@
a - b
-- -- --
s(:expressions,
s(:operator, "-",
s(:name, :a),
s(:name, :b)))
s(:operator_value, :-,
s(:name, :a),
s(:name, :b))

View File

@ -1,6 +1,5 @@
a - 5
-- -- --
s(:expressions,
s(:operator, "-",
s(:name, :a),
s(:int, 5)))
s(:operator_value, :-,
s(:name, :a),
s(:int, 5))

View File

@ -1,6 +1,5 @@
a - "st"
-- -- --
s(:expressions,
s(:operator, "-",
s(:name, :a),
s(:string, "st")))
s(:operator_value, :-,
s(:name, :a),
s(:string, "st"))

View File

@ -1,6 +1,5 @@
a == true
-- -- --
s(:expressions,
s(:operator, "==",
s(:name, :a),
s(:true)))
s(:operator_value, :==,
s(:name, :a),
s(:true))

View File

@ -1,6 +1,5 @@
5 / 3
-- -- --
s(:expressions,
s(:operator, "/",
s(:int, 5),
s(:int, 3)))
s(:operator_value, :/,
s(:int, 5),
s(:int, 3))

View File

@ -1,6 +1,5 @@
5 > 3
-- -- --
s(:expressions,
s(:operator, ">",
s(:int, 5),
s(:int, 3)))
s(:operator_value, :>,
s(:int, 5),
s(:int, 3))

View File

@ -1,6 +1,5 @@
5 - 3
-- -- --
s(:expressions,
s(:operator, "-",
s(:int, 5),
s(:int, 3)))
s(:operator_value, :-,
s(:int, 5),
s(:int, 3))

View File

@ -1,6 +1,5 @@
5 * 3
-- -- --
s(:expressions,
s(:operator, "*",
s(:int, 5),
s(:int, 3)))
s(:operator_value, :*,
s(:int, 5),
s(:int, 3))

View File

@ -1,6 +1,5 @@
5 + 3
-- -- --
s(:expressions,
s(:operator, "+",
s(:int, 5),
s(:int, 3)))
s(:operator_value, :+,
s(:int, 5),
s(:int, 3))

View File

@ -1,6 +1,5 @@
5 < 3
-- -- --
s(:expressions,
s(:operator, "<",
s(:int, 5),
s(:int, 3)))
s(:operator_value, :<,
s(:int, 5),
s(:int, 3))

View File

@ -0,0 +1,9 @@
gumbar & foo.bar
-- -- --
s(:operator_value, :&,
s(:name, :gumbar),
s(:field_access,
s(:receiver,
s(:name, :foo)),
s(:field,
s(:name, :bar))))

View File

@ -0,0 +1,5 @@
bar - gumbar
-- -- --
s(:operator_value, :-,
s(:name, :bar),
s(:name, :gumbar))