Fix #284 #316

Merged
sb10q merged 1 commits from issue-284 into master 2024-08-17 17:37:19 +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
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>(
Owner

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.
Author
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
Author
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
No description provided.