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

Add ++i and --i support #118

Merged
merged 5 commits into from
Oct 19, 2023

Conversation

wrbl606
Copy link
Contributor

@wrbl606 wrbl606 commented Oct 16, 2023

No description provided.

@wrbl606 wrbl606 changed the base branch from master to v0.7.0 October 16, 2023 19:33
@wrbl606
Copy link
Contributor Author

wrbl606 commented Oct 16, 2023

Resolves #4

Copy link
Owner

@ethanblake4 ethanblake4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good! Two comments; also, your tests should probably test the return values of the postfix/prefix expressions since that's the main difference between them.

lib/src/eval/compiler/expression/prefix.dart Outdated Show resolved Hide resolved
test/lib/src/eval/compiler/expression/prefix_test.dart Outdated Show resolved Hide resolved
lib/src/eval/compiler/expression/prefix.dart Outdated Show resolved Hide resolved
Reference V,
TypeRef type,
Variable L,
) {
var out = L;

if (L.name != null) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would happen if L.name was null? like if you did:

final list = [1]; 
final result = ++list[0];
print(result);

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm realizing that you just took that from the postfix.dart code so this one's my fault actually, sorry! Still, if you wouldn't mind that check should be removed from both prefix and postfix, it's incorrect

Copy link
Contributor Author

@wrbl606 wrbl606 Oct 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, for ++list[0], this happens: See below

dart_eval runtime exception: type 'int' is not a subtype of type '$Value'
#0      BoxList.run (package:dart_eval/src/eval/runtime/ops/primitives.dart:255:69)
#1      Runtime.execute (package:dart_eval/src/eval/runtime/runtime.dart:787:12)
#2      Runtime.executeLib (package:dart_eval/src/eval/runtime/runtime.dart:775:12)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After removal of if (L.name != null)... plus_plus and minus_minus works for both prefix and postfix, but there's an issue with boxing I think?

As you can see in the failed test, doing

List<int> list = [0];
print(list[0]++);
print(list[0]);

causes

dart_eval runtime exception: type 'int' is not a subtype of type '$Value?' in type cast
#0      InvokeExternal.run (package:dart_eval/src/eval/runtime/ops/bridge.dart:101:34)
#1      Runtime.execute (package:dart_eval/src/eval/runtime/runtime.dart:787:12)
#2      Runtime.executeLib (package:dart_eval/src/eval/runtime/runtime.dart:775:12)

where

List<int> list = [0];
print(list[0]);
print(list[0]++);

works fine.

Not sure how to fix that, also doing any other list reading with eg. print(list)/print(list.first) after plus_plus/minus_minus prefix/postfix also causes similar issues:

print(list):

dart_eval runtime exception: type 'int' is not a subtype of type '$Value'
#0      BoxList.run (package:dart_eval/src/eval/runtime/ops/primitives.dart:255:69)
#1      Runtime.execute (package:dart_eval/src/eval/runtime/runtime.dart:787:12)
#2      Runtime.executeLib (package:dart_eval/src/eval/runtime/runtime.dart:775:12)

print(list.first):

dart_eval runtime exception: type 'int' is not a subtype of type '$Value'
#0      BoxList.run (package:dart_eval/src/eval/runtime/ops/primitives.dart:255:69)
#1      Runtime.execute (package:dart_eval/src/eval/runtime/runtime.dart:787:12)
#2      Runtime.executeLib (package:dart_eval/src/eval/runtime/runtime.dart:775:12)

Copy link
Contributor Author

@wrbl606 wrbl606 Oct 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Above also happens for current master so we can rule out the possibility of introducing this issue with changes in this PR

Copy link
Owner

@ethanblake4 ethanblake4 Oct 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok that turned out to be a bit of a rabbit hole that I wouldn't expect you to dive into at this point, so I went ahead and pushed a fix to reference.dart in the v0.7.0 branch. If you pull it in, you should be able to do return V.setValue(ctx, result); on line 82 of prefix.dart and everything will work correctly.

TLDR: lists in dart_eval are expected to contain only boxed types like $int, but we were passing an unboxed int to setValue and it was just setting that directly into the list. You should be able to pass anything to setValue and let it figure it out, so I made setValue box/unbox the argument you pass it automatically. The caller of setValue needs to be informed about this change though otherwise it will think the variable is still in its previous boxing state, so I also made it return the argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With your changes, list access is now working correctly, thanks!

lib/src/eval/compiler/expression/prefix.dart Outdated Show resolved Hide resolved
@wrbl606 wrbl606 force-pushed the add_++_--_prefix_support branch from 40eccf7 to 22386d8 Compare October 19, 2023 18:18
@ethanblake4 ethanblake4 merged commit 3558bac into ethanblake4:v0.7.0 Oct 19, 2023
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

Successfully merging this pull request may close these issues.

2 participants