Skip to content

Commit

Permalink
Bind the JS i64 arithmetic and bitwise operations (#902)
Browse files Browse the repository at this point in the history
* Bind the JS i64 arithmetic and bitwise operations

* Just blow away node_modules too
  • Loading branch information
dfellis authored Sep 20, 2024
1 parent 4b36a2f commit 15cea0c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 31 deletions.
20 changes: 10 additions & 10 deletions alan/src/compile/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,39 +472,39 @@ test_full!(i32_neg => r#"
stdout "-3\n";
);

test!(i64_add => r#"
test_full!(i64_add => r#"
export fn main = print(1 + 2);"#;
stdout "3\n";
);
test!(i64_sub => r#"
test_full!(i64_sub => r#"
export fn main = print(2 - 1);"#;
stdout "1\n";
);
test!(i64_mul => r#"
test_full!(i64_mul => r#"
export fn main = print(2 * 1);"#;
stdout "2\n";
);
test!(i64_div => r#"
test_full!(i64_div => r#"
export fn main = print(6 / 2);"#;
stdout "3\n";
);
test!(i64_mod => r#"
test_full!(i64_mod => r#"
export fn main = print(6 % 4);"#;
stdout "2\n";
);
test!(i64_pow => r#"
test_full!(i64_pow => r#"
export fn main = print(6 ** 2);"#;
stdout "36\n";
);
test!(i64_min => r#"
test_full!(i64_min => r#"
export fn main = min(3, 5).print;"#;
stdout "3\n";
);
test!(i64_max => r#"
test_full!(i64_max => r#"
export fn main = max(3.i64, 5.i64).print;"#;
stdout "5\n";
);
test!(i64_neg => r#"
test_full!(i64_neg => r#"
export fn main = print(- 3);"#; // You wouldn't naturally write this, but should still work
stdout "-3\n";
);
Expand Down Expand Up @@ -925,7 +925,7 @@ test_full!(i32_bitwise => r#"
}"#;
stdout "0\n3\n6\n-1\n-1\n-4\n-7\n";
);
test!(i64_bitwise => r#"
test_full!(i64_bitwise => r#"
export fn main {
print(1 & 2);
print(1 | 3);
Expand Down
2 changes: 2 additions & 0 deletions alan/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,8 @@ pub fn web(source_file: String) -> Result<String, Box<dyn std::error::Error>> {
// Update the npm lockfile, if necessary
match Command::new("rm")
.current_dir(project_dir.clone())
.arg("-r")
.arg("node_modules/")
.arg("package-lock.json")
.stdout(Stdio::null())
.stderr(Stdio::null())
Expand Down
63 changes: 42 additions & 21 deletions alan/src/std/root.ln
Original file line number Diff line number Diff line change
Expand Up @@ -650,32 +650,53 @@ export fn{Js} wrl "alan_std.rotateLeftI32" <- RootBacking :: (i32, i32) -> i32;
export fn{Rs} wrr (a: i32, b: i32) = {Method{"rotate_right"} :: (i32, Deref{u32}) -> i32}(a, b.u32);
export fn{Js} wrr "alan_std::rotateRightI32" <- RootBacking :: (i32, i32) -> i32;

export fn add Method{"wrapping_add"} :: (i64, Deref{i64}) -> i64;
export fn sub Method{"wrapping_sub"} :: (i64, Deref{i64}) -> i64;
export fn mul Method{"wrapping_mul"} :: (i64, Deref{i64}) -> i64;
export fn div Method{"wrapping_div"} :: (i64, Deref{i64}) -> i64;
export fn mod Method{"wrapping_rem"} :: (i64, Deref{i64}) -> i64;
export fn pow (a: i64, b: i64) = {Method{"wrapping_pow"} :: (i64, Deref{u32}) -> i64}(a, b.u32);
export fn neg Prefix{"-"} :: Deref{i64} -> i64;
export fn and Infix{"&"} :: (Deref{i64}, Deref{i64}) -> i64;
export fn or Infix{"|"} :: (Deref{i64}, Deref{i64}) -> i64;
export fn xor Infix{"^"} :: (Deref{i64}, Deref{i64}) -> i64;
export fn not Prefix{"!"} :: i64 -> i64;
export fn{Rs} add Method{"wrapping_add"} :: (i64, Deref{i64}) -> i64;
export fn{Js} add "alan_std.wrappingAddI64" <- RootBacking :: (i64, i64) -> i64;
export fn{Rs} sub Method{"wrapping_sub"} :: (i64, Deref{i64}) -> i64;
export fn{Js} sub "alan_std.wrappingSubI64" <- RootBacking :: (i64, i64) -> i64;
export fn{Rs} mul Method{"wrapping_mul"} :: (i64, Deref{i64}) -> i64;
export fn{Js} mul "alan_std.wrappingMulI64" <- RootBacking :: (i64, i64) -> i64;
export fn{Rs} div Method{"wrapping_div"} :: (i64, Deref{i64}) -> i64;
export fn{Js} div "alan_std.wrappingDivI64" <- RootBacking :: (i64, i64) -> i64;
export fn{Rs} mod Method{"wrapping_rem"} :: (i64, Deref{i64}) -> i64;
export fn{Js} mod "alan_std.wrappingModI64" <- RootBacking :: (i64, i64) -> i64;
export fn{Rs} pow (a: i64, b: i64) = {Method{"wrapping_pow"} :: (i64, Deref{u32}) -> i64}(a, b.u32);
export fn{Js} pow "alan_std.wrappingPowI64" <- RootBacking :: (i64, i64) -> i64;
export fn{Rs} neg Prefix{"-"} :: Deref{i64} -> i64;
export fn{Js} neg Prefix{"-"} :: i64 -> i64;
export fn{Rs} and Infix{"&"} :: (Deref{i64}, Deref{i64}) -> i64;
export fn{Js} and Infix{"&"} :: (i64, i64) -> i64;
export fn{Rs} or Infix{"|"} :: (Deref{i64}, Deref{i64}) -> i64;
export fn{Js} or Infix{"|"} :: (i64, i64) -> i64;
export fn{Rs} xor Infix{"^"} :: (Deref{i64}, Deref{i64}) -> i64;
export fn{Js} xor Infix{"^"} :: (i64, i64) -> i64;
export fn{Rs} not Prefix{"!"} :: Deref{i64} -> i64;
export fn{Js} not Prefix{"~"} :: i64 -> i64;
export fn nand (a: i64, b: i64) = a.and(b).not;
export fn nor (a: i64, b: i64) = a.or(b).not;
export fn xnor (a: i64, b: i64) = a.xor(b).not;
export fn eq Infix{"=="} :: (Deref{i64}, Deref{i64}) -> bool;
export fn neq Infix{"!="} :: (Deref{i64}, Deref{i64}) -> bool;
export fn lt Infix{"<"} :: (Deref{i64}, Deref{i64}) -> bool;
export fn lte Infix{"<="} :: (Deref{i64}, Deref{i64}) -> bool;
export fn gt Infix{">"} :: (Deref{i64}, Deref{i64}) -> bool;
export fn gte Infix{">="} :: (Deref{i64}, Deref{i64}) -> bool;
export fn{Rs} eq Infix{"=="} :: (Deref{i64}, Deref{i64}) -> bool;
export fn{Js} eq Infix{"=="} :: (i64, i64) -> bool;
export fn{Rs} neq Infix{"!="} :: (Deref{i64}, Deref{i64}) -> bool;
export fn{Js} neq Infix{"!="} :: (i64, i64) -> bool;
export fn{Rs} lt Infix{"<"} :: (Deref{i64}, Deref{i64}) -> bool;
export fn{Js} lt Infix{"<"} :: (i64, i64) -> bool;
export fn{Rs} lte Infix{"<="} :: (Deref{i64}, Deref{i64}) -> bool;
export fn{Js} lte Infix{"<="} :: (i64, i64) -> bool;
export fn{Rs} gt Infix{">"} :: (Deref{i64}, Deref{i64}) -> bool;
export fn{Js} gt Infix{">"} :: (i64, i64) -> bool;
export fn{Rs} gte Infix{">="} :: (Deref{i64}, Deref{i64}) -> bool;
export fn{Js} gte Infix{">="} :: (i64, i64) -> bool;
export fn min (a: i64, b: i64) = if(a.lte(b), a, b);
export fn max (a: i64, b: i64) = if(a.gte(b), a, b);
export fn shl (a: i64, b: i64) = {Method{"wrapping_shl"} :: (i64, Deref{u32}) -> i64}(a, b.u32);
export fn shr (a: i64, b: i64) = {Method{"wrapping_shr"} :: (i64, Deref{u32}) -> i64}(a, b.u32);
export fn wrl (a: i64, b: i64) = {Method{"rotate_left"} :: (i64, Deref{u32}) -> i64}(a, b.u32);
export fn wrr (a: i64, b: i64) = {Method{"rotate_right"} :: (i64, Deref{u32}) -> i64}(a, b.u32);
export fn{Rs} shl (a: i64, b: i64) = {Method{"wrapping_shl"} :: (i64, Deref{u32}) -> i64}(a, b.u32);
export fn{Js} shl "alan_std.wrappingShlI64" <- RootBacking :: (i64, i64) -> i64;
export fn{Rs} shr (a: i64, b: i64) = {Method{"wrapping_shr"} :: (i64, Deref{u32}) -> i64}(a, b.u32);
export fn{Js} shr "alan_std.wrappingShrI64" <- RootBacking :: (i64, i64) -> i64;
export fn{Rs} wrl (a: i64, b: i64) = {Method{"rotate_left"} :: (i64, Deref{u32}) -> i64}(a, b.u32);
export fn{Js} wrl "alan_std.rotateLeftI64" <- RootBacking :: (i64, i64) -> i64;
export fn{Rs} wrr (a: i64, b: i64) = {Method{"rotate_right"} :: (i64, Deref{u32}) -> i64}(a, b.u32);
export fn{Js} wrr "alan_std::rotateRightI64" <- RootBacking :: (i64, i64) -> i64;

/// Unsigned Integer-related functions and function bindings
export fn add Method{"wrapping_add"} :: (u8, Deref{u8}) -> u8;
Expand Down

0 comments on commit 15cea0c

Please sign in to comment.