Permalink
Browse files

Fixed more operator shuffling specs.

  • Loading branch information...
1 parent 92c8043 commit 579e84593da2ef3378f2668dcf8d705616bd20e6 @vic committed Sep 20, 2011
Showing with 85 additions and 68 deletions.
  1. +17 −3 lib/akin/shuffle.rb
  2. +68 −65 spec/shuffle_spec.rb
View
@@ -43,9 +43,23 @@ def shuffle_send(node)
node.with(:send, a[0], *shuffle(a[1..-1]))
end
- def shuffle_msg(node)
- a = node.args.map { |n| n.with(n.name, n.args.first, *shuffle(n.args[1..-1])) }
- node.with(:msg, *a)
+ def shuffle_space(node)
+ a = node.args
+ node.with(:space, a[0], *shuffle(a[1..-1]))
+ end
+
+ def shuffle_empty(node)
+ a = node.args
+ node.with(:empty, a[0], *shuffle(a[1..-1]))
+ end
+
+ def shuffle_kmsg(node)
+ node.with(:kmsg, *shuffle(node.args))
+ end
+
+ def shuffle_part(node)
+ a = node.args
+ node.with(:part, a[0], a[1], *shuffle(a[2..-1]))
end
def shuffle_chain(node)
View
@@ -6,21 +6,21 @@
describe 'basic math opers' do
it 'associates correctly + and *' do
- n('a + b * c - d').should ==
- [:chain,
- [:name, "a"],
+ n('a + b * c - d').should ==
+ [:chain,
+ [:name, "a"],
[:oper, "+"],
- [:send, nil,
+ [:send, nil,
[:chain, [:name, "b"], [:oper, "*"], [:send, nil, [:name, "c"]]]],
[:oper, "-"],
[:send, nil, [:name, "d"]]]
end
it 'should not shuffle already shuffled operators' do
- sexp = [:chain,
- [:name, "a"],
+ sexp = [:chain,
+ [:name, "a"],
[:oper, "+"],
- [:send, nil,
+ [:send, nil,
[:chain, [:name, "b"], [:oper, "*"], [:send, nil, [:name, "c"]]]],
[:oper, "-"],
[:send, nil, [:name, "d"]]]
@@ -37,7 +37,7 @@
describe 'assignment' do
it 'takes lhs and rhs' do
n('a = b').should ==
- [:chain, [:oper, "="],
+ [:chain, [:oper, "="],
[:send, nil, [:name, "a"], [:name, "b"]]]
end
@@ -51,7 +51,7 @@
it 'has higher precedence than both + and *' do
n('a * d = b + c').should ==
[:chain, [:oper, "="],
- [:send, nil,
+ [:send, nil,
[:chain, [:name, "a"], [:oper, "*"], [:send, nil, [:name, "d"]]],
[:chain, [:name, "b"], [:oper, "+"], [:send, nil, [:name, "c"]]]]]
end
@@ -60,33 +60,33 @@
n('a = b = c').should ==
[:chain, [:oper, "="],
[:send, nil,
- [:name, "a"], [:chain, [:oper, "="],
+ [:name, "a"], [:chain, [:oper, "="],
[:send, nil, [:name, "b"], [:name, "c"]]]]]
end
it 'is parsed correctly with &&' do
n('a && b = c').should ==
[:chain, [:name, "a"],
[:oper, "&&"],
- [:send, nil,
+ [:send, nil,
[:chain, [:oper, "="], [: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"],
- [:send, nil,
+ [:send, nil,
[:chain, [:oper, "="], [:send, nil, [:name, "b"], [:name, "c"]]]]]
end
it 'shuffles correctly with or' do
n('a = b = c or d').should ==
- [:chain,
- [:oper, "="],
- [:send, nil, [:name, "a"],
- [:chain, [:oper, "="],
- [:send, nil, [:name, "b"], [:name, "c"]]]],
+ [:chain,
+ [:oper, "="],
+ [:send, nil, [:name, "a"],
+ [:chain, [:oper, "="],
+ [:send, nil, [:name, "b"], [:name, "c"]]]],
[:oper, "or"], [:send, nil, [:name, "d"]]]
end
end
@@ -104,64 +104,64 @@
it 'binds chain to right till oper' do
n('!b c + d').should ==
- [:chain,
+ [:chain,
[:oper, "!"],
[:send, nil, [:chain, [:name, "b"], [:name, "c"]]],
[:oper, "+"],
- [:send, nil, [:chain, [:name, "d"]]]]
+ [:send, nil, [:name, "d"]]]
end
it 'binds chain to right till end' do
n('b + ! c d').should ==
- [:chain,
- [:name, "b"],
- [:act, "+", nil,
- [:act, "!", nil,
- [:chain, [:name, "c"], [:name, "d"]]]]]
+ [:chain, [:name, "b"], [:oper, "+"],
+ [:send, nil, [:chain, [:oper, "!"],
+ [:send, nil, [:chain, [:name, "c"], [:name, "d"]]]]]]
end
it 'shuffles correctly with logical opers' do
n('a = !c || b && d').should ==
- [:chain,
-
- [:act, "=", nil,
+ [:chain, [:oper, "="],
+ [:send, nil,
[:name, "a"],
- [:act, "!", nil, [:name, "c"]]],
-
- [:act, "||", nil,
- [:chain, [:name, "b"],
- [:act, "&&", nil, [:name, "d"]]]]]
+ [:chain, [:oper, "!"],
+ [:send, nil, [:name, "c"]]]],
+ [:oper, "||"],
+ [:send, nil,
+ [:chain, [:name, "b"], [:oper, "&&"], [:send, nil, [:name, "d"]]]]]
end
end
-=begin
+
describe 'logical operator &&' do
it 'associates correctly with logical opers' do
n('a = b && c || d').should ==
[:chain,
- [:act, "=", nil,
- [:name, "a"],
- [:name, "b"]],
- [:act, "&&", nil, [:name, "c"]],
- [:act, "||", nil, [:name, "d"]]]
+ [:oper, "="],
+ [:send, nil, [:name, "a"], [:name, "b"]],
+ [:oper, "&&"], [:send, nil, [:name, "c"]],
+ [:oper, "||"], [:send, nil, [:name, "d"]]]
end
it 'can be chained' do
n('a && b && c').should ==
- [:chain, [:name, "a"],
- [:act, "&&", nil, [:name, "b"]], [:act, "&&", nil, [:name, "c"]]]
+ [:chain,
+ [:name, "a"],
+ [:oper, "&&"],
+ [:send, nil, [:name, "b"]],
+ [:oper, "&&"],
+ [:send, nil, [:name, "c"]]]
end
end
describe 'decrement operator --' do
it 'binds left expression' do
n('a --').should ==
- [:act, "--", nil, [:name, "a"]]
+ [:chain, [:oper, "--"], [:send, nil, [:name, "a"]]]
end
it 'doesnt takes right chain' do
n('a -- b').should ==
- [:chain, [:act, "--", nil, [:name, "a"]], [:name, "b"]]
+ [:chain, [:oper, "--"], [:send, nil, [:name, "a"]], [:name, "b"]]
end
end
@@ -170,17 +170,18 @@
n('a ∈ b').should ==
[:chain,
[:name, "b"],
- [:act, "∈", nil, [:name, "a"]]]
+ [:oper, ""], [: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, "b"],
- [:act, "∈", nil,
- [:chain,
- [:name, "c"],
- [:act, "∉", nil, [:name, "a"]]]]]
+ [:name, "c"],
+ [:oper, ""],
+ [:send, nil,
+ [:chain, [:name, "b"],
+ [:oper, ""],
+ [:send, nil, [:name, "a"]]]]]
end
end
@@ -189,43 +190,45 @@
n('a b ? c + d').should ==
[:chain,
[:name, "a"],
- [:act, "?", nil, [:name, "b"],
- [:chain,
- [:name, "c"],
- [:act, "+", nil, [:name, "d"]]]]]
+ [:oper, "?"],
+ [:send, nil,
+ [:name, "b"],
+ [:chain, [:name, "c"], [:oper, "+"], [:send, nil, [:name, "d"]]]]]
end
end
describe 'on message sends' do
it 'shuffles chain inside activation' do
n('a(b + c)').should ==
- [:act, [:name, "a"], "()",
- [:chain, [:name, "b"],
- [:act, "+", nil, [:name, "c"]]]]
+ [:chain,
+ [:name, "a"],
+ [:send, ["(", ")"],
+ [:chain, [:name, "b"], [:oper, "+"], [:send, nil, [:name, "c"]]]]]
end
it 'shuffles chain inside args' do
n('(b + c)').should ==
- [:act, nil, "()",
- [:chain, [:name, "b"],
- [:act, "+", nil, [:name, "c"]]]]
+ [:space,
+ ["(", ")"],
+ [:chain, [:name, "b"], [:oper, "+"], [:send, nil, [:name, "c"]]]]
+
end
end
describe 'on keyword messages' do
it 'shuffles chain inside args' do
n('a(b + c):').should ==
- [:msg, ["a", "()",
- [:chain, [:name, "b"],
- [:act, "+", nil, [:name, "c"]]]]]
+ [:kmsg,
+ [:part, "a", ["(", ")"],
+ [:chain, [:name, "b"], [:oper, "+"], [:send, nil, [:name, "c"]]]]]
end
it 'shuffles chain inside keyword arg' do
n('a: b + c').should ==
- [:msg, ["a", nil,
- [:chain, [:name, "b"],
- [:act, "+", nil, [:name, "c"]]]]]
+ [:kmsg,
+ [:part, "a", nil,
+ [:chain, [:name, "b"], [:oper, "+"], [:send, nil, [:name, "c"]]]]]
end
end
-=end
+
end

0 comments on commit 579e845

Please sign in to comment.