Skip to content

Commit

Permalink
functions with blocks : first success
Browse files Browse the repository at this point in the history
  • Loading branch information
jmettraux committed Jul 13, 2011
1 parent 7def6bf commit 6a94506
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/subaltern/evaluator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ def self.eval_tree(context, tree)
p tree
puts nme.backtrace.join("\n")
puts '-' * 80
raise nme
end

%w[ false true nil ].each do |key|
Expand Down Expand Up @@ -408,6 +409,12 @@ def self.eval_return(context, tree)

def self.eval_iter(context, tree)

if tree[1][1] == nil
# method call with a block
con = Context.new(context, 'block' => tree[3])
return eval_call(con, tree[1])
end

# TODO : raise if method not in whitelist of target

target = eval_tree(context, tree[1][1])
Expand All @@ -419,5 +426,10 @@ def self.eval_iter(context, tree)

target.send(method, *method_args, &prok)
end

def self.eval_yield(context, tree)

eval_tree(context, context['block'])
end
end

12 changes: 12 additions & 0 deletions spec/functions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ def hello
hello
}).should == 'nada'
end

it 'accepts functions with blocks' do

Subaltern.eval(%{
def hello
1 + yield
end
hello { 3 }
}).should == 4
end

it 'accepts functions with explicit blocks'
end
end

0 comments on commit 6a94506

Please sign in to comment.