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

Fix operator precedence not applying correctly #12

Merged
merged 11 commits into from
Mar 25, 2024

Conversation

AjaniBilby
Copy link
Member

Operator precedence is being incorrectly applied within this file, rewrite to fix the issue.
/compiler/codegen/expression/precedence.ts

This function should return true, but is instead returning false:

fn main(): bool {
	// (-2.5 % 2.0) * -3.0 == 10.0 - ((1.0 / 2.0) % 10.0) - 8.0;
	// 1.5 == 1.5
	// true == 1
	return -2.5 % 2.0 * -3.0 == 10.0 - 1.0 / 2.0 % 10.0 - 8.0;
}

@AjaniBilby AjaniBilby added the bug Something isn't working label Mar 24, 2024
@AjaniBilby
Copy link
Member Author

AjaniBilby commented Mar 24, 2024

Based on the fact that this test case fails on return 41 it implies the precedence of - is causing an issue.

fn main(): i32 {
  // (-2.5 % 2.0) * -3.0 == 10.0 - ((1.0 / 2.0) % 10.0) - 8.0;
  // 1.5 == 1.5
  // true == 1

  // doing this in a single expression to also ensure == is applied correctly
  if ( (-2.5 % 2.0) * -3.0 ) != ( 10.0 - ( (1.0 / 2.0) % 10.0 ) - 8.0 ) {
    return 25;
  };

  if ( (-2.5 % 2.0) * -3.0 != 10.0 - ( (1.0 / 2.0) % 10.0 ) - 8.0 ) {
    return 29;
  };

  if ( (-2.5 % 2.0) * -3.0 != 10.0 - ( 1.0 / 2.0 % 10.0 ) - 8.0 ) {
    return 33;
  };

  if ( -2.5 % 2.0 * -3.0 != 10.0 - ( 1.0 / 2.0 % 10.0 ) - 8.0 ) {
    return 37;
  };

  if ( -2.5 % 2.0 * -3.0 != 10.0 - 1.0 / 2.0 % 10.0 - 8.0 ) {
    return 41;
  };

  return 36;
}

@AjaniBilby
Copy link
Member Author

fn left(): f32 {
  return 10.0 - ( 3.0 / 2.0 ) - 8.0;
}

fn right(): f32 {
  return 10.0 - 3.0 / 2.0 - 8.0;
}

fn main(): bool {
  left();
  right();

  return 10.0 - ( 3.0 / 2.0 ) - 8.0 == 10.0 - 3.0 / 2.0 - 8.0;
}
(module
  (type (;0;) (func (result i32)))
  (type (;1;) (func (result f32)))
  (func (;0;) (type 0) (result i32)
    call 1
    drop
    call 2
    drop

    f32.const 0x1.4p+3 (;=10;)
    f32.const 0x1.8p+1 (;=3;)
    f32.const 0x1p+1 (;=2;)
    f32.div
    f32.sub
    f32.const 0x1p+3 (;=8;)
    f32.sub

    f32.const 0x1.4p+3 (;=10;)
    f32.const 0x1.8p+1 (;=3;)
    f32.sub                    ;; FLIPPED
    f32.const 0x1p+1 (;=2;)
    f32.div                    ;; FLIPPED
    f32.const 0x1p+3 (;=8;)
    f32.sub
    f32.eq
    return
    unreachable)
  (func (;1;) (type 1) (result f32)
    f32.const 0x1.4p+3 (;=10;)
    f32.const 0x1.8p+1 (;=3;)
    f32.const 0x1p+1 (;=2;)
    f32.div
    f32.sub
    f32.const 0x1p+3 (;=8;)
    f32.sub
    return
    unreachable)
  (func (;2;) (type 1) (result f32)
    f32.const 0x1.4p+3 (;=10;)
    f32.const 0x1.8p+1 (;=3;)
    f32.const 0x1p+1 (;=2;)
    f32.div
    f32.sub
    f32.const 0x1p+3 (;=8;)
    f32.sub
    return
    unreachable)
  (memory (;0;) 1 1)
  (global (;0;) (mut i32) (i32.const 0))
  (export "_start" (func 0))
  (export "main" (func 0)))

@AjaniBilby AjaniBilby marked this pull request as ready for review March 25, 2024 00:36
@AjaniBilby AjaniBilby self-assigned this Mar 25, 2024
@AjaniBilby AjaniBilby merged commit 0ee3d57 into main Mar 25, 2024
1 check passed
@AjaniBilby AjaniBilby deleted the fix-operator-precedence branch March 25, 2024 00:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant