Skip to content

Implement correct for loop behavior#316

Merged
CohenArthur merged 18 commits into
masterfrom
iter-in-for-loops
Oct 25, 2021
Merged

Implement correct for loop behavior#316
CohenArthur merged 18 commits into
masterfrom
iter-in-for-loops

Conversation

@CohenArthur

@CohenArthur CohenArthur commented Oct 21, 2021

Copy link
Copy Markdown
Member

Closes #132
Closes #322

@CohenArthur

Copy link
Copy Markdown
Member Author

In order to advance the iterator properly, we need variable mutability. We can return a new iterator for now but that's it

@CohenArthur

Copy link
Copy Markdown
Member Author

To progress further, we need comparison operators in order for the Range_int type to act correctly

@CohenArthur CohenArthur force-pushed the iter-in-for-loops branch 2 times, most recently from 107f18a to ba56c46 Compare October 25, 2021 07:42
@CohenArthur CohenArthur marked this pull request as ready for review October 25, 2021 08:08
@CohenArthur CohenArthur requested a review from Skallwar October 25, 2021 08:08
@codecov-commenter

codecov-commenter commented Oct 25, 2021

Copy link
Copy Markdown

Codecov Report

Merging #316 (cf5a706) into master (8d8b772) will increase coverage by 0.72%.
The diff coverage is 86.15%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #316      +/-   ##
==========================================
+ Coverage   92.04%   92.76%   +0.72%     
==========================================
  Files          41       41              
  Lines        4171     4230      +59     
==========================================
+ Hits         3839     3924      +85     
+ Misses        332      306      -26     
Impacted Files Coverage Δ
src/error/mod.rs 78.08% <ø> (ø)
src/instruction/loop_block.rs 85.34% <85.96%> (+32.96%) ⬆️
src/builtins.rs 94.68% <87.50%> (-0.78%) ⬇️
src/parser/constructs.rs 100.00% <0.00%> (+0.12%) ⬆️
src/instruction/operator.rs 90.90% <0.00%> (+3.03%) ⬆️
src/value/jk_constant.rs 91.30% <0.00%> (+3.62%) ⬆️
src/instruction/var.rs 85.36% <0.00%> (+8.53%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8d8b772...cf5a706. Read the comment docs.

@Skallwar Skallwar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wonder if this could be done via a jinko! macro combined with a format!. The code is ugly (and that's not your fault) but maybe a jinko! macro will be more elegant and (maybe) more performant?

There is almost no test for this feature, we should add some

Great works 🚀 🔥

Comment thread src/builtins.rs Outdated
Comment on lines +156 to +157
__builtin_string_display("to display", false);
__builtin_string_display_err("to display on err", true);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not a big fan of this. If the user wants a new line, he just needs to add a '\n' on his string

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Well the issue is that for now these strings aren't parsed correctly... So if we were to do print("hey\n"), then the character \ and the character n would both get printed to the stdout. I think that maybe there should be a FIXME or something in the standard library

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ok let's go with a FIXME and an issue

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done in ba698fd and #323

Comment thread src/instruction/loop_block.rs Outdated
@@ -77,32 +77,133 @@ impl Instruction for Loop {
// that that result, as a boolean, returns true. If it does, execute the
// body. If it does not, break from the for.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess this comment needs to be updated

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is the behavior that I'd still want the loop blocks to have, but yeah it's not implemented right now. I'll add a FIXME

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ooops I read that too fast. You're right it needs updating

// We create new variables in the scope, with the required iterator's
// name from the user. We can create these variables with special names
// since we are only using them from the interpreter. Here, we prefix
// them with a plus sign to make sure they do not interact with the user.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This should be documented. The user needs to be aware that he is not allowed to use identifier starting with a '+' sign

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't know if it explicitly needs to be documented, as if the user tries to do it they will simply encounter a parse error (identifiers can only be comprised of underscores, digits and letters). Here, since we are creating them directly from the interpreter, we're allowed to, but it's an "edge case" and abusing the language since we're not going through the parser

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Documenting it to understand why there is a parser error is useful imao

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Do you mean in the parser or here? And do you mean by emitting an error or adding a comment? Because the user will never see those variables, ever. Think of them as the __<ident> variables we were talking about in gcc

Comment thread src/instruction/loop_block.rs Outdated
Comment on lines +148 to +151
// let maybe_is_some = |ctx| {
// let instance = maybe_is_some.execute(ctx).unwrap();
// JkBool::from_instance(&instance).0.clone()
// };

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

dead code ?

Comment thread src/instruction/loop_block.rs Outdated
counter = counter + 1;
}

// FIXME: Add assertion here

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This needs to be fixed I guess

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think it depends on #187 sadly. I'll fix the FIXME

Comment thread src/instruction/loop_block.rs Outdated
Comment on lines +105 to +108
// // FIXME: This needs to change
// +iterator = next(+iterator);
// +maybe_value = current(+iterator);
// if !+maybe_value.is_some() { break }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Are we not doing this already? iterator.next() is in this PR

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, but ideally we should simply be mutating the +iterator variable instead of creating a new iterator each time. Something like

func next(iter: &mut Iter) {
    ...
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see

Comment thread stdlib/range.jk
Co-authored-by: Skallwar <esteban.blanc@epita.fr>
CohenArthur and others added 4 commits October 25, 2021 11:37
Co-authored-by: Skallwar <esteban.blanc@epita.fr>
Co-authored-by: Skallwar <esteban.blanc@epita.fr>
Co-authored-by: Skallwar <esteban.blanc@epita.fr>
Co-authored-by: Skallwar <esteban.blanc@epita.fr>
@CohenArthur CohenArthur merged commit 1f590f8 into master Oct 25, 2021
@CohenArthur CohenArthur deleted the iter-in-for-loops branch October 25, 2021 09:54
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.

Range_int is not implemented correctly Implement correct for loop behavior

3 participants