Permalink
Browse files

Specs for unary negation operator (!)

  • Loading branch information...
1 parent de411e6 commit 28ad47831f69692b66f14b24199705fa47263efe @vic committed Aug 24, 2011
Showing with 40 additions and 9 deletions.
  1. +7 −8 lib/akin/shuffle.rb
  2. +33 −1 spec/shuffle_spec.rb
View
@@ -87,21 +87,19 @@ def shuffle(ary)
lhs = pre.reverse.take_while { |i| !oper?(i) }.reverse
unless lhs.empty?
pre = ary[0...ary.index(lhs.first)]
- if lhs.size == 1
- act.args.push lhs.first
- else
- act.args.push lhs.first.with(:chain, *lhs)
- end
+ lhs = lhs.size == 1 && lhs.first || lhs.first.with(:chain, *lhs)
+ act.args.push lhs
end
end
if rhs? && post
rhs = post.take_while { |i| !oper?(i) }
unless rhs.empty?
post = ary[ary.index(rhs.last)+1..-1]
- if rhs.size == 1
- act.args.push rhs.first
+ if info.assoc > 1
+ act = rhs + [node.with(:act, oper, nil)]
else
- act.args.push rhs.first.with(:chain, *rhs)
+ rhs = rhs.size == 1 && rhs.first || rhs.first.with(:chain, *rhs)
+ act.args.push rhs
end
end
end
@@ -126,6 +124,7 @@ def <=>(other)
end
OPERATORS = {
+ "!" => Oper::Info.new(3, 2),
"*" => Oper::Info.new(5),
"+" => Oper::Info.new(6),
"-" => Oper::Info.new(6),
View
@@ -47,9 +47,41 @@
[:chain, [:name, "b"],
[:act, "+", nil, [:name, "c"]]]]
end
-end
+ end
+ describe 'unary negation' do
+ it 'binds to right' do
+ n = c('!b')
+ n.shuffle.sexp.should ==
+ [:chain, [:name, "b"], [:act, "!", nil]]
+ end
+ it 'binds chain to right' do
+ n = c('!b c d')
+ n.shuffle.sexp.should ==
+ [:chain,
+ [:name, "b"], [:name, "c"], [:name, "d"],
+ [:act, "!", nil]]
+ end
+
+ it 'binds chain to right till oper' do
+ n = c('!b c + d')
+ n.shuffle.sexp.should ==
+ [:chain,
+ [:name, "b"], [:name, "c"],
+ [:act, "!", nil],
+ [:act, "+", nil, [:name, "d"]]]
+ end
+
+ it 'binds chain to right till oper' do
+ n = c('b + ! c d')
+ n.shuffle.sexp.should ==
+ [:chain,
+ [:name, "b"],
+ [:act, "+", nil,
+ [:chain, [:name, "c"], [:name, "d"], [:act, "!", nil]]]]
+ end
+ end
end

0 comments on commit 28ad478

Please sign in to comment.