Skip to content

Commit

Permalink
Bind the js u64 arithmetic and bitwise operations (#910)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfellis authored Sep 21, 2024
1 parent c0ca505 commit 9e111b6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 29 deletions.
18 changes: 9 additions & 9 deletions alan/src/compile/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,35 +644,35 @@ test_full!(u32_max => r#"
stdout "5\n";
);

test!(u64_add => r#"
test_full!(u64_add => r#"
export fn main = print(1.u64 + 2.u64);"#;
stdout "3\n";
);
test!(u64_sub => r#"
test_full!(u64_sub => r#"
export fn main = print(2.u64 - 1.u64);"#;
stdout "1\n";
);
test!(u64_mul => r#"
test_full!(u64_mul => r#"
export fn main = print(2.u64 * 1.u64);"#;
stdout "2\n";
);
test!(u64_div => r#"
test_full!(u64_div => r#"
export fn main = print(6.u64 / 2.u64);"#;
stdout "3\n";
);
test!(u64_mod => r#"
test_full!(u64_mod => r#"
export fn main = print(6.u64 % 4.u64);"#;
stdout "2\n";
);
test!(u64_pow => r#"
test_full!(u64_pow => r#"
export fn main = print(6.u64 ** 2.u64);"#;
stdout "36\n";
);
test!(u64_min => r#"
test_full!(u64_min => r#"
export fn main = min(3.u64, 5.u64).print;"#;
stdout "3\n";
);
test!(u64_max => r#"
test_full!(u64_max => r#"
export fn main = max(3.u64, 5.u64).print;"#;
stdout "5\n";
);
Expand Down Expand Up @@ -980,7 +980,7 @@ test_full!(u32_bitwise => r#"
}"#;
stdout "0\n3\n6\n4294967295\n4294967295\n4294967292\n4294967289\n";
);
test!(u64_bitwise => r#"
test_full!(u64_bitwise => r#"
prefix u64 as ~ precedence 10
export fn main {
Expand Down
60 changes: 40 additions & 20 deletions alan/src/std/root.ln
Original file line number Diff line number Diff line change
Expand Up @@ -837,31 +837,51 @@ export fn{Js} wrl "alan_std.rotateLeftU32" <- RootBacking :: (u32, u32) -> u32;
export fn{Rs} wrr (a: u32, b: u32) = {Method{"rotate_right"} :: (u32, Deref{u32}) -> u32}(a, b.u32);
export fn{Js} wrr "alan_std::rotateRightU32" <- RootBacking :: (u32, u32) -> u32;

export fn add Method{"wrapping_add"} :: (u64, Deref{u64}) -> u64;
export fn sub Method{"wrapping_sub"} :: (u64, Deref{u64}) -> u64;
export fn mul Method{"wrapping_mul"} :: (u64, Deref{u64}) -> u64;
export fn div Method{"wrapping_div"} :: (u64, Deref{u64}) -> u64;
export fn mod Method{"wrapping_rem"} :: (u64, Deref{u64}) -> u64;
export fn pow (a: u64, b: u64) = {Method{"wrapping_pow"} :: (u64, Deref{u32}) -> u64}(a, b.u32);
export fn and Infix{"&"} :: (Deref{u64}, Deref{u64}) -> u64;
export fn or Infix{"|"} :: (Deref{u64}, Deref{u64}) -> u64;
export fn xor Infix{"^"} :: (Deref{u64}, Deref{u64}) -> u64;
export fn not Prefix{"!"} :: Deref{u64} -> u64;
export fn{Rs} add Method{"wrapping_add"} :: (u64, Deref{u64}) -> u64;
export fn{Js} add "alan_std.wrappingAddU64" <- RootBacking :: (u64, u64) -> u64;
export fn{Rs} sub Method{"wrapping_sub"} :: (u64, Deref{u64}) -> u64;
export fn{Js} sub "alan_std.wrappingSubU64" <- RootBacking :: (u64, u64) -> u64;
export fn{Rs} mul Method{"wrapping_mul"} :: (u64, Deref{u64}) -> u64;
export fn{Js} mul "alan_std.wrappingMulU64" <- RootBacking :: (u64, u64) -> u64;
export fn{Rs} div Method{"wrapping_div"} :: (u64, Deref{u64}) -> u64;
export fn{Js} div "alan_std.wrappingDivU64" <- RootBacking :: (u64, u64) -> u64;
export fn{Rs} mod Method{"wrapping_rem"} :: (u64, Deref{u64}) -> u64;
export fn{Js} mod "alan_std.wrappingModU64" <- RootBacking :: (u64, u64) -> u64;
export fn{Rs} pow (a: u64, b: u64) = {Method{"wrapping_pow"} :: (u64, Deref{u32}) -> u64}(a, b.u32);
export fn{Js} pow "alan_std.wrappingPowU64" <- RootBacking :: (u64, u64) -> u64;
export fn{Rs} and Infix{"&"} :: (Deref{u64}, Deref{u64}) -> u64;
export fn{Js} and Infix{"&"} :: (u64, u64) -> u64;
export fn{Rs} or Infix{"|"} :: (Deref{u64}, Deref{u64}) -> u64;
export fn{Js} or Infix{"|"} :: (u64, u64) -> u64;
export fn{Rs} xor Infix{"^"} :: (Deref{u64}, Deref{u64}) -> u64;
export fn{Js} xor Infix{"^"} :: (u64, u64) -> u64;
export fn{Rs} not Prefix{"!"} :: Deref{u64} -> u64;
export fn{Js} not "alan_std.notU64" <- RootBacking :: u64 -> u64;
export fn nand (a: u64, b: u64) = a.and(b).not;
export fn nor (a: u64, b: u64) = a.or(b).not;
export fn xnor (a: u64, b: u64) = a.xor(b).not;
export fn eq Infix{"=="} :: (Deref{u64}, Deref{u64}) -> bool;
export fn neq Infix{"!="} :: (Deref{u64}, Deref{u64}) -> bool;
export fn lt Infix{"<"} :: (Deref{u64}, Deref{u64}) -> bool;
export fn lte Infix{"<="} :: (Deref{u64}, Deref{u64}) -> bool;
export fn gt Infix{">"} :: (Deref{u64}, Deref{u64}) -> bool;
export fn gte Infix{">="} :: (Deref{u64}, Deref{u64}) -> bool;
export fn{Rs} eq Infix{"=="} :: (Deref{u64}, Deref{u64}) -> bool;
export fn{Js} eq Infix{"=="} :: (u64, u64) -> bool;
export fn{Rs} neq Infix{"!="} :: (Deref{u64}, Deref{u64}) -> bool;
export fn{Js} neq Infix{"!="} :: (u64, u64) -> bool;
export fn{Rs} lt Infix{"<"} :: (Deref{u64}, Deref{u64}) -> bool;
export fn{Js} lt Infix{"<"} :: (u64, u64) -> bool;
export fn{Rs} lte Infix{"<="} :: (Deref{u64}, Deref{u64}) -> bool;
export fn{Js} lte Infix{"<="} :: (u64, u64) -> bool;
export fn{Rs} gt Infix{">"} :: (Deref{u64}, Deref{u64}) -> bool;
export fn{Js} gt Infix{">"} :: (u64, u64) -> bool;
export fn{Rs} gte Infix{">="} :: (Deref{u64}, Deref{u64}) -> bool;
export fn{Js} gte Infix{">="} :: (u64, u64) -> bool;
export fn min (a: u64, b: u64) = if(a.lte(b), a, b);
export fn max (a: u64, b: u64) = if(a.gte(b), a, b);
export fn shl (a: u64, b: u64) = {Method{"wrapping_shl"} :: (u64, Deref{u32}) -> u64}(a, b.u32);
export fn shr (a: u64, b: u64) = {Method{"wrapping_shr"} :: (u64, Deref{u32}) -> u64}(a, b.u32);
export fn wrl (a: u64, b: u64) = {Method{"rotate_left"} :: (u64, Deref{u32}) -> u64}(a, b.u32);
export fn wrr (a: u64, b: u64) = {Method{"rotate_right"} :: (u64, Deref{u32}) -> u64}(a, b.u32);
export fn{Rs} shl (a: u64, b: u64) = {Method{"wrapping_shl"} :: (u64, Deref{u32}) -> u64}(a, b.u32);
export fn{Js} shl "alan_std.wrappingShlU64" <- RootBacking :: (u64, u64) -> u64;
export fn{Rs} shr (a: u64, b: u64) = {Method{"wrapping_shr"} :: (u64, Deref{u32}) -> u64}(a, b.u32);
export fn{Js} shr "alan_std.wrappingShrU64" <- RootBacking :: (u64, u64) -> u64;
export fn{Rs} wrl (a: u64, b: u64) = {Method{"rotate_left"} :: (u64, Deref{u32}) -> u64}(a, b.u32);
export fn{Js} wrl "alan_std.rotateLeftU64" <- RootBacking :: (u64, u64) -> u64;
export fn{Rs} wrr (a: u64, b: u64) = {Method{"rotate_right"} :: (u64, Deref{u32}) -> u64}(a, b.u32);
export fn{Js} wrr "alan_std::rotateRightU64" <- RootBacking :: (u64, u64) -> u64;

/// Float-related functions and function bindings
export fn add Infix{"+"} :: (f32, f32) -> f32;
Expand Down

0 comments on commit 9e111b6

Please sign in to comment.