Skip to content

Commit 670b940

Browse files
committed
feat: add infix operators and warnings
1 parent ec02ac8 commit 670b940

File tree

14 files changed

+2196
-243
lines changed

14 files changed

+2196
-243
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ node_modules/
2727

2828
# macOS
2929
.DS_Store
30+
31+
.claude

examples/array_fold_2n.simf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// From https://github.com/BlockstreamResearch/SimplicityHL/issues/153
22

33
fn sum(elt: u32, acc: u32) -> u32 {
4-
let (_, acc): (bool, u32) = jet::add_32(elt, acc);
5-
acc
4+
// let (_, acc): (bool, u32) = jet::add_32(elt, acc);
5+
elt + acc
6+
// acc
67
}
78

89
fn main() {

examples/infix_operators.simf

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* INFIX OPERATORS
3+
*
4+
* Demonstrates all infix operators: +, -, *, /, %, &, |, ^, &&, ||, ==, !=, <, <=, >, >=
5+
*
6+
* Addition and subtraction panic at runtime on overflow/underflow.
7+
* Multiplication returns a type twice the width of the operands (no overflow).
8+
* Division and modulo return the same type as the operands.
9+
* Bitwise operators return the same type as the operands.
10+
* Logical operators short-circuit and return bool.
11+
* Comparison operators return bool.
12+
*
13+
* Arithmetic operators require: simc -Z infix_arithmetic_operators
14+
*/
15+
fn main() {
16+
let a: u8 = 20;
17+
let b: u8 = 6;
18+
19+
// run `simc` with `-Z infix_arithmetic_operators` to allow these.
20+
// Addition: u8 + u8 → u8, panics on overflow
21+
// let sum: u8 = a + b;
22+
// assert!(jet::eq_8(sum, 26));
23+
24+
// Subtraction: u8 - u8 → u8, panics on underflow
25+
// let diff: u8 = a - b;
26+
// assert!(jet::eq_8(diff, 14));
27+
28+
// Multiplication: u8 * u8 → u16 (full precision, no overflow possible)
29+
// let product: u16 = a * b;
30+
// assert!(jet::eq_16(product, 120));
31+
32+
// Division: u8 / u8 → u8, panics if divisor is zero
33+
// let quotient: u8 = a / b;
34+
// assert!(jet::eq_8(quotient, 3));
35+
36+
// Modulo: u8 % u8 → u8, panics if divisor is zero
37+
// let remainder: u8 = a % b;
38+
// assert!(jet::eq_8(remainder, 2));
39+
40+
// Bitwise AND: u8 & u8 → u8
41+
let and: u8 = a & b;
42+
assert!(jet::eq_8(and, 4)); // 0b00010100 & 0b00000110 = 0b00000100
43+
44+
// Bitwise OR: u8 | u8 → u8
45+
let or: u8 = a | b;
46+
assert!(jet::eq_8(or, 22)); // 0b00010100 | 0b00000110 = 0b00010110
47+
48+
// Bitwise XOR: u8 ^ u8 → u8
49+
let xor: u8 = a ^ b;
50+
assert!(jet::eq_8(xor, 18)); // 0b00010100 ^ 0b00000110 = 0b00010010
51+
52+
// Logical AND: bool && bool → bool, short-circuits (rhs not evaluated if lhs is false)
53+
let a_eq_a: bool = a == a; // true
54+
let b_eq_b: bool = b == b; // true
55+
let a_eq_b: bool = a == b; // false
56+
assert!(a_eq_a && b_eq_b); // true && true = true
57+
assert!(true && true);
58+
// false && _ short-circuits to false:
59+
let and_false: bool = a_eq_b && b_eq_b;
60+
assert!(match and_false { false => true, true => false, });
61+
62+
// Logical OR: bool || bool → bool, short-circuits (rhs not evaluated if lhs is true)
63+
assert!(a_eq_a || a_eq_b); // true || _ = true (rhs not evaluated)
64+
assert!(false || true);
65+
// false || false = false:
66+
let or_false: bool = a_eq_b || a_eq_b;
67+
assert!(match or_false { false => true, true => false, });
68+
69+
// Equality: u8 == u8 → bool
70+
assert!(a == a); // 20 == 20 is true
71+
assert!(a != b); // 20 != 6 is true
72+
73+
// Less than: u8 < u8 → bool
74+
assert!(b < a); // 6 < 20
75+
76+
// Greater than: u8 > u8 → bool
77+
assert!(a > b); // 20 > 6
78+
79+
// Less or equal: u8 <= u8 → bool
80+
assert!(b <= a); // 6 <= 20
81+
assert!(b <= b); // 6 <= 6
82+
83+
// Greater or equal: u8 >= u8 → bool
84+
assert!(a >= b); // 20 >= 6
85+
assert!(a >= a); // 20 >= 20
86+
}

0 commit comments

Comments
 (0)