Permalink
Browse files

Brace activation produces cell nodes as receivers. Operator shuffling…

… produces activation on cells.
  • Loading branch information...
1 parent ddee133 commit eb0bb7ac884eefc0b298af0c786cb1606fcd3cab @vic committed Sep 21, 2011
Showing with 79 additions and 60 deletions.
  1. +4 −1 lib/akin/grammar.kpeg
  2. +19 −3 lib/akin/grammar.rb
  3. +2 −2 lib/akin/shuffle.rb
  4. +3 −3 spec/grammar_spec.rb
  5. +51 −51 spec/shuffle_spec.rb
@@ -46,7 +46,10 @@ chain_(x) = chain_(x):c - "." &{x.kmsg?} - chain_(x):v {c + v}
value(x) = value_(x):v {Array(v)}:a &{a.first.pos.column > x.pos.column} ~a
value_(x) = &{x.kmsg?} kmsg(x)
- | value_(x):v p:p braced:b !&":" {Array(v) + [n(p, :send, *b)]}
+ | value_(x):v p:p braced:b !&":" {Array(v)}:a {a.first}:f
+ { a = [f.with(:cell, *f.args)] if a.size == 1 &&
+ f.name == :name || f.name == :oper
+ a + [n(p, :send, *b)] }
| empty(x) | space
| literal(x):a (&{x.kmsg?} | !&":") ~a
View
@@ -1228,7 +1228,7 @@ def _value(x)
set_failed_rule :_value unless _tmp
return _tmp
end
-
+
# value_ = (&{x.kmsg?} kmsg(x) | value_(x):v p:p braced:b !(&":") {Array(v)}:a {a.first}:f { a = [f.with(:cell, *f.args)] if a.size == 1 && f.name == :name || f.name == :oper a + [n(p, :send, *b)] } | empty(x) | space | literal(x):a (&{x.kmsg?} | !(&":")) {a})
def _value_(x)
@@ -1283,7 +1283,23 @@ def _value_(x)
unless _tmp
self.pos = _save3
break
- end
+ end
+ @result = begin; Array(v); end
+ _tmp = true
+ a = @result
+ unless _tmp
+ self.pos = _save3
+ break
+ end
+ @result = begin; a.first; end
+ _tmp = true
+ f = @result
+ unless _tmp
+ self.pos = _save3
+ break
+ end
+ @result = begin; a = [f.with(:cell, *f.args)] if a.size == 1 &&
+ f.name == :name || f.name == :oper
a + [n(p, :send, *b)] ; end
_tmp = true
unless _tmp
@@ -4777,7 +4793,7 @@ def _number_escapes
Rules[:_block] = rule_info("block", "(block(x):a ws* nl - block(x):b &{a.pos.column < b.pos.column} { a.name == :chain && (a.args.push(b);a) || a.with(:chain, a, b) } | block_(x):b {b.size > 1 && n(b.first.pos, :block, *b) || b.first})")
Rules[:_block_] = rule_info("block_", "(block_(x):b - \";\" s chain(x):c {Array(c)}:a {b + a} | block_(x):b s chain(x):c {Array(c)}:a &{b.first.pos.column == a.first.pos.column} {b + a} | chain(x):c {Array(c)})")
Rules[:_chain] = rule_info("chain", "chain_(x):c {c.size > 1 && n(c.first.pos, :chain, *c) || c.first}")
- Rules[:_chain_] = rule_info("chain_", "(chain_(x):c - \".\" &{x.kmsg?} - chain_(x):v {c + v} | chain_(x):c &{c.last.name == :oper} (ws* nl -)? value(x.at(c.first.pos)):v {c + v} | chain_(x):c oper:o {c + [o]} | chain_(x):c ws+ value(x.at(c.first.pos)):v {c + v} | value(x))")
+ Rules[:_chain_] = rule_info("chain_", "(chain_(x):c - \".\" &{x.kmsg?} - chain_(x):v {c + v} | chain_(x):c &{c.last.name == :oper} (ws* nl -)? value(x.at(c.first.pos)):v {c + v} | chain_(x):c oper:o {c + [o]} | chain_(x):c ws+ value(x.at(c.first.pos)):v {c + v} | value(x))")
Rules[:_value] = rule_info("value", "value_(x):v {Array(v)}:a &{a.first.pos.column > x.pos.column} {a}")
Rules[:_value_] = rule_info("value_", "(&{x.kmsg?} kmsg(x) | value_(x):v p:p braced:b !(&\":\") {Array(v)}:a {a.first}:f { a = [f.with(:cell, *f.args)] if a.size == 1 && f.name == :name || f.name == :oper a + [n(p, :send, *b)] } | empty(x) | space | literal(x):a (&{x.kmsg?} | !(&\":\")) {a})")
Rules[:_space] = rule_info("space", "p:p braced:a {n(p, :space, *a)}")
View
@@ -64,7 +64,7 @@ def shuffle_part(node)
end
def shuffle_infix(node)
- node.with(:oper, node.args.first)
+ node.with(:name, node.args.first)
end
def shuffle_chain(node)
@@ -120,7 +120,7 @@ def shuffle_op(op, chain, ops)
if lhs || rhs
lhs = lhs.size == 1 && lhs.first || lhs.first.with(:chain, *lhs) if lhs
rhs = rhs.size == 1 && rhs.first || rhs.first.with(:chain, *rhs) if rhs
- now = [op.node.with(:oper, op.node.args.first),
+ now = [op.node.with(:cell, op.node.args.first),
op.node.with(:send, nil, *(Array(lhs) + Array(rhs)))]
end
@@ -36,19 +36,19 @@
it 'allows operator to have arguments' do
s("a +(b, c)").should == [:chain,
- [:name, "a"], [:oper, "+"], [:send, ["(", ")"], [:name, "b"], [:name, "c"]]]
+ [:name, "a"], [:cell, "+"], [:send, ["(", ")"], [:name, "b"], [:name, "c"]]]
end
it 'parses args' do
s('a(b, c)').should ==
- [:chain, [:name, "a"],
+ [:chain, [:cell, "a"],
[:send, ["(", ")"],
[:name, "b"], [:name, "c"]]]
end
it 'parses chained args' do
s('a(b, c)[d]').should ==
- [:chain, [:name, "a"],
+ [:chain, [:cell, "a"],
[:send, ["(", ")"],
[:name, "b"], [:name, "c"]],
[:send, ["[", "]"],
View
@@ -9,20 +9,20 @@
n('a + b * c - d').should ==
[:chain,
[:name, "a"],
- [:oper, "+"],
+ [:cell, "+"],
[:send, nil,
- [:chain, [:name, "b"], [:oper, "*"], [:send, nil, [:name, "c"]]]],
- [:oper, "-"],
+ [:chain, [:name, "b"], [:cell, "*"], [:send, nil, [:name, "c"]]]],
+ [:cell, "-"],
[:send, nil, [:name, "d"]]]
end
it 'should not shuffle already shuffled operators' do
sexp = [:chain,
[:name, "a"],
- [:oper, "+"],
+ [:cell, "+"],
[:send, nil,
- [:chain, [:name, "b"], [:oper, "*"], [:send, nil, [:name, "c"]]]],
- [:oper, "-"],
+ [:chain, [:name, "b"], [:cell, "*"], [:send, nil, [:name, "c"]]]],
+ [:cell, "-"],
[:send, nil, [:name, "d"]]]
n = c('a + b * c - d')
s = shuffler.shuffle(n)
@@ -37,97 +37,97 @@
describe 'assignment' do
it 'takes lhs and rhs' do
n('a = b').should ==
- [:chain, [:oper, "="],
+ [:chain, [:cell, "="],
[:send, nil, [:name, "a"], [:name, "b"]]]
end
it 'has higher precedence than +' do
- [:chain, [:oper, "="],
+ [:chain, [:cell, "="],
[:send, nil, [:name, "a"],
- [:chain, [:name, "b"], [:oper, "+"],
+ [:chain, [:name, "b"], [:cell, "+"],
[:send, nil, [:name, "c"]]]]]
end
it 'has higher precedence than both + and *' do
n('a * d = b + c').should ==
- [:chain, [:oper, "="],
+ [:chain, [:cell, "="],
[:send, nil,
- [:chain, [:name, "a"], [:oper, "*"], [:send, nil, [:name, "d"]]],
- [:chain, [:name, "b"], [:oper, "+"], [:send, nil, [:name, "c"]]]]]
+ [:chain, [:name, "a"], [:cell, "*"], [:send, nil, [:name, "d"]]],
+ [:chain, [:name, "b"], [:cell, "+"], [:send, nil, [:name, "c"]]]]]
end
it 'is right associative' do
n('a = b = c').should ==
- [:chain, [:oper, "="],
+ [:chain, [:cell, "="],
[:send, nil,
- [:name, "a"], [:chain, [:oper, "="],
+ [:name, "a"], [:chain, [:cell, "="],
[:send, nil, [:name, "b"], [:name, "c"]]]]]
end
it 'is parsed correctly with &&' do
n('a && b = c').should ==
[:chain, [:name, "a"],
- [:oper, "&&"],
+ [:cell, "&&"],
[:send, nil,
- [:chain, [:oper, "="], [:send, nil, [:name, "b"], [:name, "c"]]]]]
+ [:chain, [:cell, "="], [:send, nil, [:name, "b"], [:name, "c"]]]]]
end
it 'is parsed correctly with and' do
n('a and b = c').should ==
[:chain, [:name, "a"],
- [:oper, "and"],
+ [:cell, "and"],
[:send, nil,
- [:chain, [:oper, "="], [:send, nil, [:name, "b"], [:name, "c"]]]]]
+ [:chain, [:cell, "="], [:send, nil, [:name, "b"], [:name, "c"]]]]]
end
it 'shuffles correctly with or' do
n('a = b = c or d').should ==
[:chain,
- [:oper, "="],
+ [:cell, "="],
[:send, nil, [:name, "a"],
- [:chain, [:oper, "="],
+ [:chain, [:cell, "="],
[:send, nil, [:name, "b"], [:name, "c"]]]],
- [:oper, "or"], [:send, nil, [:name, "d"]]]
+ [:cell, "or"], [:send, nil, [:name, "d"]]]
end
end
describe 'unary negation' do
it 'binds to right' do
- n('!b').should == [:chain, [:oper, "!"], [:send, nil, [:name, "b"]]]
+ n('!b').should == [:chain, [:cell, "!"], [:send, nil, [:name, "b"]]]
end
it 'binds chain to right' do
n('!b c d').should ==
- [:chain, [:oper, "!"], [:send, nil,
+ [:chain, [:cell, "!"], [:send, nil,
[:chain, [:name, "b"], [:name, "c"], [:name, "d"]]]]
end
it 'binds chain to right till oper' do
n('!b c + d').should ==
[:chain,
- [:oper, "!"],
+ [:cell, "!"],
[:send, nil, [:chain, [:name, "b"], [:name, "c"]]],
- [:oper, "+"],
+ [:cell, "+"],
[:send, nil, [:name, "d"]]]
end
it 'binds chain to right till end' do
n('b + ! c d').should ==
- [:chain, [:name, "b"], [:oper, "+"],
- [:send, nil, [:chain, [:oper, "!"],
+ [:chain, [:name, "b"], [:cell, "+"],
+ [:send, nil, [:chain, [:cell, "!"],
[:send, nil, [:chain, [:name, "c"], [:name, "d"]]]]]]
end
it 'shuffles correctly with logical opers' do
n('a = !c || b && d').should ==
- [:chain, [:oper, "="],
+ [:chain, [:cell, "="],
[:send, nil,
[:name, "a"],
- [:chain, [:oper, "!"],
+ [:chain, [:cell, "!"],
[:send, nil, [:name, "c"]]]],
- [:oper, "||"],
+ [:cell, "||"],
[:send, nil,
- [:chain, [:name, "b"], [:oper, "&&"], [:send, nil, [:name, "d"]]]]]
+ [:chain, [:name, "b"], [:cell, "&&"], [:send, nil, [:name, "d"]]]]]
end
end
@@ -136,32 +136,32 @@
it 'associates correctly with logical opers' do
n('a = b && c || d').should ==
[:chain,
- [:oper, "="],
+ [:cell, "="],
[:send, nil, [:name, "a"], [:name, "b"]],
- [:oper, "&&"], [:send, nil, [:name, "c"]],
- [:oper, "||"], [:send, nil, [:name, "d"]]]
+ [:cell, "&&"], [:send, nil, [:name, "c"]],
+ [:cell, "||"], [:send, nil, [:name, "d"]]]
end
it 'can be chained' do
n('a && b && c').should ==
[:chain,
[:name, "a"],
- [:oper, "&&"],
+ [:cell, "&&"],
[:send, nil, [:name, "b"]],
- [:oper, "&&"],
+ [:cell, "&&"],
[:send, nil, [:name, "c"]]]
end
end
describe 'decrement operator --' do
it 'binds left expression' do
n('a --').should ==
- [:chain, [:oper, "--"], [:send, nil, [:name, "a"]]]
+ [:chain, [:cell, "--"], [:send, nil, [:name, "a"]]]
end
it 'doesnt takes right chain' do
n('a -- b').should ==
- [:chain, [:oper, "--"], [:send, nil, [:name, "a"]], [:name, "b"]]
+ [:chain, [:cell, "--"], [:send, nil, [:name, "a"]], [:name, "b"]]
end
end
@@ -170,17 +170,17 @@
n('a ∈ b').should ==
[:chain,
[:name, "b"],
- [:oper, ""], [:send, nil, [:name, "a"]]]
+ [:cell, ""], [:send, nil, [:name, "a"]]]
end
it 'can be chained with other inverted operator and is right associative' do
n('a ∈ b ∉ c').should ==
[:chain,
[:name, "c"],
- [:oper, ""],
+ [:cell, ""],
[:send, nil,
[:chain, [:name, "b"],
- [:oper, ""],
+ [:cell, ""],
[:send, nil, [:name, "a"]]]]]
end
end
@@ -190,27 +190,27 @@
n('a b ? c + d').should ==
[:chain,
[:name, "a"],
- [:oper, "?"],
+ [:cell, "?"],
[:send, nil,
[:name, "b"],
- [:chain, [:name, "c"], [:oper, "+"], [:send, nil, [:name, "d"]]]]]
+ [:chain, [:name, "c"], [:cell, "+"], [:send, nil, [:name, "d"]]]]]
end
end
describe 'on message sends' do
it 'shuffles chain inside activation' do
n('a(b + c)').should ==
[:chain,
- [:name, "a"],
+ [:cell, "a"],
[:send, ["(", ")"],
- [:chain, [:name, "b"], [:oper, "+"], [:send, nil, [:name, "c"]]]]]
+ [:chain, [:name, "b"], [:cell, "+"], [:send, nil, [:name, "c"]]]]]
end
it 'shuffles chain inside args' do
n('(b + c)').should ==
[:space,
["(", ")"],
- [:chain, [:name, "b"], [:oper, "+"], [:send, nil, [:name, "c"]]]]
+ [:chain, [:name, "b"], [:cell, "+"], [:send, nil, [:name, "c"]]]]
end
end
@@ -220,32 +220,32 @@
n('a(b + c):').should ==
[:kmsg,
[:part, "a", ["(", ")"],
- [:chain, [:name, "b"], [:oper, "+"], [:send, nil, [:name, "c"]]]]]
+ [:chain, [:name, "b"], [:cell, "+"], [:send, nil, [:name, "c"]]]]]
end
it 'shuffles chain inside keyword arg' do
n('a: b + c').should ==
[:kmsg,
[:part, "a", nil,
- [:chain, [:name, "b"], [:oper, "+"], [:send, nil, [:name, "c"]]]]]
+ [:chain, [:name, "b"], [:cell, "+"], [:send, nil, [:name, "c"]]]]]
end
end
describe 'named infix operator' do
it 'shuffles chain with binary infix operator' do
n('a #b# c').should ==
- [:chain, [:oper, "b"], [:send, nil, [:name, "a"], [:name, "c"]]]
+ [:chain, [:cell, "b"], [:send, nil, [:name, "a"], [:name, "c"]]]
end
it 'shuffles chain with left unary infix operator' do
n('a #b c').should ==
- [:chain, [:oper, "b"], [:send, nil, [:name, "a"]], [:name, "c"]]
+ [:chain, [:cell, "b"], [:send, nil, [:name, "a"]], [:name, "c"]]
end
it 'shuffles chain with right unary infix operator' do
n('a b# c').should ==
- [:chain, [:name, "a"], [:oper, "b"], [:send, nil, [:name, "c"]]]
+ [:chain, [:name, "a"], [:cell, "b"], [:send, nil, [:name, "c"]]]
end
end

0 comments on commit eb0bb7a

Please sign in to comment.