Fix #284 #316

Merged
sb10q merged 7 commits from issue-284 into master 2023-09-11 11:27:35 +08:00
Collaborator

Fixes the issue where, if the loop variable is used after a range loop, the value would be off by one.

This MR fixes the problem by rewriting range loops into a do-while loop similar to

bool inc = step > 0;
if (inc ? value < end : value > end) {
  int next_value = start;
  do {
    int value = next_value;
    body(value);

    next_value = value + step;
    } while (inc ? next_value < end : next_value > end);
}

This MR also implements the behavior where when 0 is provided to the step argument of range, a ValueError will be raised.

In addition, some minor code cleanup has also been performed, namely

  • Add statement type to basic block label names

  • Add variable name to IR variables if able

  • Refactored the loop logic for iterables case to align with the standard C-style loop

    for (size_t i = 0; i < len; ++i) {
      body(iterable[i]);
    }
    
Fixes the issue where, if the loop variable is used after a `range` loop, the value would be off by one. This MR fixes the problem by rewriting `range` loops into a do-while loop similar to ```c bool inc = step > 0; if (inc ? value < end : value > end) { int next_value = start; do { int value = next_value; body(value); next_value = value + step; } while (inc ? next_value < end : next_value > end); } ``` This MR also implements the behavior where when 0 is provided to the `step` argument of `range`, a `ValueError` will be raised. In addition, some minor code cleanup has also been performed, namely - Add statement type to basic block label names - Add variable name to IR variables if able - Refactored the loop logic for iterables case to align with the standard C-style loop ```c for (size_t i = 0; i < len; ++i) { body(iterable[i]); } ```
sb10q was assigned by derppening 2023-09-05 18:47:21 +08:00
derppening added 8 commits 2023-09-05 18:47:22 +08:00
aaf7ea74b8 core: Add gen_{var_alloc,store_target}_named
For assigning custom names to stack-alloc variables and pointers.
7b9f8e8aaa core: Preserve value of variable shadowed by for loop
Previously, the final value of the target expression would be one after
the last element of the loop, which does not match Python's behavior.

This commit fixes this problem while also preserving the last assigned
value of the loop beyond the loop, matching Python's behavior.
18a3263af1 core: Use C-style for loop logic for iterables
Index increment is performed at the end of the loop body.
sb10q reviewed 2023-09-05 18:57:38 +08:00
@ -83,24 +83,49 @@ pub trait CodeGenerator {
/// Allocate memory for a variable and return a pointer pointing to it.
/// The default implementation places the allocations at the start of the function.
fn gen_var_alloc_named<'ctx, 'a>(

Instead of two functions, just add the name parameter to gen_var and refactor the code to pass None. Would be another PR.

Instead of two functions, just add the name parameter to gen_var and refactor the code to pass None. Would be another PR.
Poster
Collaborator

Created #317 to address this issue.

Created #317 to address this issue.
derppening force-pushed issue-284 from 18a3263af1 to 4481d48709 2023-09-06 20:09:52 +08:00 Compare
Poster
Collaborator

Rebased against master. Ready for re-review.

Rebased against `master`. Ready for re-review.
sb10q merged commit 4481d48709 into master 2023-09-11 11:27:35 +08:00
Sign in to join this conversation.
No reviewers
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: M-Labs/nac3#316
There is no content yet.