fix operators, only binary
This commit is contained in:
parent
ba7ecbfa7b
commit
af8febe456
@ -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
|
||||
|
9
test/cases/operator_value/field_var.tst
Normal file
9
test/cases/operator_value/field_var.tst
Normal 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))
|
9
test/cases/operator_value/int_field.tst
Normal file
9
test/cases/operator_value/int_field.tst
Normal 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))))
|
5
test/cases/operator_value/int_var.tst
Normal file
5
test/cases/operator_value/int_var.tst
Normal file
@ -0,0 +1,5 @@
|
||||
3 > var
|
||||
-- -- --
|
||||
s(:operator_value, :>,
|
||||
s(:int, 3),
|
||||
s(:name, :var))
|
5
test/cases/operator_value/ints.tst
Normal file
5
test/cases/operator_value/ints.tst
Normal file
@ -0,0 +1,5 @@
|
||||
5 + 7
|
||||
-- -- --
|
||||
s(:operator_value, :+,
|
||||
s(:int, 5),
|
||||
s(:int, 7))
|
@ -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))
|
||||
|
@ -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))
|
||||
|
@ -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"))
|
||||
|
@ -1,6 +1,5 @@
|
||||
a == true
|
||||
-- -- --
|
||||
s(:expressions,
|
||||
s(:operator, "==",
|
||||
s(:name, :a),
|
||||
s(:true)))
|
||||
s(:operator_value, :==,
|
||||
s(:name, :a),
|
||||
s(:true))
|
||||
|
@ -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))
|
||||
|
@ -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))
|
||||
|
@ -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))
|
||||
|
@ -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))
|
||||
|
@ -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))
|
||||
|
@ -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))
|
||||
|
9
test/cases/operator_value/var_field.tst
Normal file
9
test/cases/operator_value/var_field.tst
Normal 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))))
|
5
test/cases/operator_value/var_var.tst
Normal file
5
test/cases/operator_value/var_var.tst
Normal file
@ -0,0 +1,5 @@
|
||||
bar - gumbar
|
||||
-- -- --
|
||||
s(:operator_value, :-,
|
||||
s(:name, :bar),
|
||||
s(:name, :gumbar))
|
Loading…
Reference in New Issue
Block a user