Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #109. Fix closure handling in various corner cases. #132

Merged
merged 2 commits into from
Jan 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
217 changes: 217 additions & 0 deletions spec/interpreter/misc/closures_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
require "../../spec_helper.cr"
require "../../support/nodes.cr"
require "../../support/interpret.cr"

describe "Interpreter - Closures" do
describe "on blocks" do
it "captures the value of `self` as part of the closure" do
itr = parse_and_interpret %q(
@sum = 0
[1, 2, 3].each{ |e| @sum += e }
@sum
)

itr.stack.last.should eq(val(6))
end

it "can access variables from blocks on static methods (see #109)" do
itr = parse_and_interpret %q(
deftype Test
defstatic hello(&block)
block()
end
end

i = 0
Test.hello do
i += 6
end
i
)

itr.stack.last.should eq(val(6))
end

it "can access variables from within nested blocks" do
itr = parse_and_interpret %q(
defmodule Test
def hello(&block)
block()
end
end

i = 0
Test.hello do
[1, 2, 3].each{ |e| i += e }
end
i
)

itr.stack.last.should eq(val(6))
end

it "can access variables from within nested blocks on static methods (see #109)" do
itr = parse_and_interpret %q(
deftype Test
defstatic hello(&block)
block()
end
end

i = 0
Test.hello do
[1, 2, 3].each{ |e| i += e }
end
i
)

itr.stack.last.should eq(val(6))
end

it "maintains the top level value of `self` from within nested blocks" do
itr = parse_and_interpret %q(
deftype Test
defstatic hello(&block)
block()
end
end

@sum = 0
Test.hello do
[1, 2, 3].each{ |e| @sum += e }
end
@sum
)

itr.stack.last.should eq(val(6))
end

it "can access variables from a block within an anonymous function" do
itr = parse_and_interpret %q(
defmodule Test
def hello(&block)
block()
end
end

i = 0

func = fn
->() { [1, 2, 3].each{ |e| i += e }; i }
end

Test.hello(&func)
)

itr.stack.last.should eq(val(6))
end

it "can access variables from a block within an anonymous function on static methods (see #109)" do
itr = parse_and_interpret %q(
deftype Test
defstatic hello(&block)
block()
end
end

i = 0

func = fn
->() { [1, 2, 3].each{ |e| i += e }; i }
end

Test.hello(&func)
)

itr.stack.last.should eq(val(6))
end
end


describe "on anonymous functions" do
it "captures the value of `self` as part of the closure" do
itr = parse_and_interpret %q(
@sum = 0
func = fn
->(a) { @sum += a }
end

func(6)
@sum
)

itr.stack.last.should eq(val(6))
end

it "can access variables from anonymous functions on static methods (see #109)" do
itr = parse_and_interpret %q(
deftype Test
defstatic hello(&block)
block()
end
end

i = 0
Test.hello(&fn ->() { i += 6 } end)
i
)

itr.stack.last.should eq(val(6))
end

it "can access variables from within a nested block" do
itr = parse_and_interpret %q(
defmodule Test
def hello(&block)
block()
end
end

i = 0
Test.hello do
func = fn ->(e) { i += e } end
[1, 2, 3].each(&func)
end
i
)

itr.stack.last.should eq(val(6))
end

it "can access variables from within a nested block on static methods (see #109)" do
itr = parse_and_interpret %q(
deftype Test
defstatic hello(&block)
block()
end
end

i = 0
Test.hello do
func = fn ->(e) { i += e } end
[1, 2, 3].each(&func)
end
i
)

itr.stack.last.should eq(val(6))
end

it "maintains the top level value of `self` from within a nested block" do
itr = parse_and_interpret %q(
deftype Test
defstatic hello(&block)
block()
end
end

@sum = 0
Test.hello do
func = fn ->(e) { @sum += e } end
[1, 2, 3].each(&func)
end
@sum
)
end
end
end
14 changes: 0 additions & 14 deletions spec/interpreter/nodes/anonymous_function_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,6 @@ describe "Interpreter - AnonymousFunction" do
itr.stack.last.should eq(val(:called_foo))
end

it "captures the value of `self` as part of the closure" do
itr = parse_and_interpret %q(
@sum = 0
func = fn
->(a) { @sum += a }
end

func(6)
@sum
)

itr.stack.last.should eq(val(6))
end

it "allows clauses of various arities" do
itr = parse_and_interpret %q(
fn
Expand Down
10 changes: 0 additions & 10 deletions spec/interpreter/nodes/block_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,4 @@ describe "Interpreter - Block" do

itr.stack.last.should eq(val(6))
end

it "captures the value of `self` as part of the closure" do
itr = parse_and_interpret %q(
@sum = 0
[1, 2, 3].each{ |e| @sum += e }
@sum
)

itr.stack.last.should eq(val(6))
end
end
1 change: 1 addition & 0 deletions spec/myst/spec.mt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "stdlib/spec.mt"

include Spec
include Spec.DSL

# TODO: add Dir globbing to automatically detect and require all `*_spec.mt`
# files under this directory.
Expand Down
14 changes: 5 additions & 9 deletions src/myst/interpreter/invocation.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@ module Myst

def invoke
@selfstack_size_at_entry = @itr.self_stack.size
case
when @receiver
# If the invocation has a receiver, use it as the current value of `self`
# for the duration of the Invocation.
@itr.push_self(@receiver.not_nil!)
when @func.closure?
# If the invoked functor is a closure, use the closed value of `self`.
@itr.push_self(@func.closed_self)
end
# If the invocation has a receiver, use it as the current value of `self`
# for the duration of the Invocation.
@itr.push_self(@receiver.not_nil!) if @receiver
# If the invoked functor is a closure, use the closed value of `self`.
@itr.push_self(@func.closed_self) if @func.closure?

result = @func.clauses.each do |clause|
@itr.push_scope_override(@func.new_scope)
Expand Down
2 changes: 1 addition & 1 deletion src/myst/interpreter/nodes/references.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Myst
end

def visit(node : Const)
if value = current_scope[node.name]? || __typeof(current_self).scope[node.name]
if value = (current_scope[node.name]? || __typeof(current_self).scope[node.name]? || recursive_lookup(current_self, node.name))
stack.push(value)
else
@callstack.push(node)
Expand Down
1 change: 1 addition & 0 deletions stdlib/spec.mt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "./spec/dsl.mt"
require "./spec/errors.mt"
require "./spec/single_spec.mt"

Expand Down
41 changes: 41 additions & 0 deletions stdlib/spec/dsl.mt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
defmodule Spec
defmodule DSL
def assert(assertion)
unless assertion
raise %AssertionFailure{true, assertion}
end
end

def refute(assertion)
when assertion
raise %AssertionFailure{false, assertion}
end
end

# Expect the given block to raise an error matching the given value. If no
# error, or an error with a different value, is raised, the assertion fails.
def expect_raises(expected_error, &block)
block()
raise %AssertionFailure{expected_error, "no error"}
rescue <expected_error>
# If the raised error matches what was expected, the assertion passes.
rescue received_error
# For any other error
raise %AssertionFailure{expected_error, received_error}
end

# Same as `expect_raises(expected, &block)`, but without the expectation of
# a specific error.
def expect_raises(&block)
block()
raise %AssertionFailure{"Any Error", "no error"}
rescue ex : AssertionFailure
# Rescuing an AssertionFailure implies that `block` did
# not raise an exception, so the exception is re-raised.
raise ex
rescue
# Otherwise, the error must have come from `block`, so the
# assertion is successful.
end
end
end
6 changes: 2 additions & 4 deletions stdlib/spec/errors.mt
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@ defmodule Spec
# made within an `it` block fails. The failure contains the Spec object that
# failed, the value that was expected, and the value that was received.
deftype AssertionFailure
def initialize(name : String, expected, got)
@name = name
def initialize(expected, got)
@expected = expected
@got = got
end

def name; @name; end
def expected; @expected; end
def got; @got; end

def to_s
"Assertion failed: <(@name)>.\n" +
"Assertion failed.\n" +
" Expected: <(@expected)>\n" +
" Got: <(@got)>\n"
end
Expand Down
Loading