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

Cascading returns are not collapsed #10

Open
dwayne opened this issue Mar 29, 2022 · 1 comment
Open

Cascading returns are not collapsed #10

dwayne opened this issue Mar 29, 2022 · 1 comment

Comments

@dwayne
Copy link

dwayne commented Mar 29, 2022

Add the following test cases

("return if (true) { return 10; };", Some(Object::Int(10))),
("return if (true) { return if (true) { return 10; }; };", Some(Object::Int(10))),
("return if (true) { return if (false) { return 10; } else { return 5; }; };", Some(Object::Int(5))),

to the test_return_stmt function in src/evaluator/mod.rs to see what I mean.

For e.g. the tests fail as follows:

left: `Some(Int(10))`,
right: `Some(ReturnValue(Int(10)))`

left: `Some(Int(10))`,
right: `Some(ReturnValue(ReturnValue(Int(10))))`

left: `Some(Int(5))`,
right: `Some(ReturnValue(ReturnValue(Int(5))))`

Three possible solutions:

  1. Implement Eq for Object such that ReturnValue(ReturnValue(Int(10)) would be equal to Int(10) etc. For e.g. that approach taken here.
  2. Add a smart constructor specifically for ReturnValue, say makeReturnValue v, such that if v is already a ReturnValue we simply return v otherwise we return ReturnValue v.
  3. Collapse the ReturnValue when the final output from the evaluator is a ReturnValue.

Note: If these examples are tried in the online interpreter they will display the correct result because of how Display is implemented for Object. Hence, it hides the issue.

@dwayne
Copy link
Author

dwayne commented Apr 9, 2022

A related problem. Given,

[fn (x) { return [x]; }][0](4)[0]

it fails with:

uknown operator: [4] 0

But,

[fn (x) { [x] }][0](4)[0]

without the return statement gives the correct answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant