seq_runtime/arithmetic/
compare.rs1use crate::stack::{Stack, pop, pop_two, push};
5use crate::value::Value;
6
7#[unsafe(no_mangle)]
10pub unsafe extern "C" fn patch_seq_eq(stack: Stack) -> Stack {
11 let (rest, a, b) = unsafe { pop_two(stack, "=") };
12 match (a, b) {
13 (Value::Int(a_val), Value::Int(b_val)) => unsafe {
14 push(rest, Value::Bool(a_val == b_val))
15 },
16 _ => panic!("=: expected two integers on stack"),
17 }
18}
19
20#[unsafe(no_mangle)]
28pub unsafe extern "C" fn patch_seq_lt(stack: Stack) -> Stack {
29 let (rest, a, b) = unsafe { pop_two(stack, "<") };
30 match (a, b) {
31 (Value::Int(a_val), Value::Int(b_val)) => unsafe { push(rest, Value::Bool(a_val < b_val)) },
32 _ => panic!("<: expected two integers on stack"),
33 }
34}
35
36#[unsafe(no_mangle)]
44pub unsafe extern "C" fn patch_seq_gt(stack: Stack) -> Stack {
45 let (rest, a, b) = unsafe { pop_two(stack, ">") };
46 match (a, b) {
47 (Value::Int(a_val), Value::Int(b_val)) => unsafe { push(rest, Value::Bool(a_val > b_val)) },
48 _ => panic!(">: expected two integers on stack"),
49 }
50}
51
52#[unsafe(no_mangle)]
60pub unsafe extern "C" fn patch_seq_lte(stack: Stack) -> Stack {
61 let (rest, a, b) = unsafe { pop_two(stack, "<=") };
62 match (a, b) {
63 (Value::Int(a_val), Value::Int(b_val)) => unsafe {
64 push(rest, Value::Bool(a_val <= b_val))
65 },
66 _ => panic!("<=: expected two integers on stack"),
67 }
68}
69
70#[unsafe(no_mangle)]
78pub unsafe extern "C" fn patch_seq_gte(stack: Stack) -> Stack {
79 let (rest, a, b) = unsafe { pop_two(stack, ">=") };
80 match (a, b) {
81 (Value::Int(a_val), Value::Int(b_val)) => unsafe {
82 push(rest, Value::Bool(a_val >= b_val))
83 },
84 _ => panic!(">=: expected two integers on stack"),
85 }
86}
87
88#[unsafe(no_mangle)]
96pub unsafe extern "C" fn patch_seq_neq(stack: Stack) -> Stack {
97 let (rest, a, b) = unsafe { pop_two(stack, "<>") };
98 match (a, b) {
99 (Value::Int(a_val), Value::Int(b_val)) => unsafe {
100 push(rest, Value::Bool(a_val != b_val))
101 },
102 _ => panic!("<>: expected two integers on stack"),
103 }
104}
105
106#[unsafe(no_mangle)]
115pub unsafe extern "C" fn patch_seq_and(stack: Stack) -> Stack {
116 let (rest, a, b) = unsafe { pop_two(stack, "and") };
117 match (a, b) {
118 (Value::Int(a_val), Value::Int(b_val)) => unsafe {
119 push(
120 rest,
121 Value::Int(if a_val != 0 && b_val != 0 { 1 } else { 0 }),
122 )
123 },
124 _ => panic!("and: expected two integers on stack"),
125 }
126}
127
128#[unsafe(no_mangle)]
137pub unsafe extern "C" fn patch_seq_or(stack: Stack) -> Stack {
138 let (rest, a, b) = unsafe { pop_two(stack, "or") };
139 match (a, b) {
140 (Value::Int(a_val), Value::Int(b_val)) => unsafe {
141 push(
142 rest,
143 Value::Int(if a_val != 0 || b_val != 0 { 1 } else { 0 }),
144 )
145 },
146 _ => panic!("or: expected two integers on stack"),
147 }
148}
149
150#[unsafe(no_mangle)]
159pub unsafe extern "C" fn patch_seq_not(stack: Stack) -> Stack {
160 assert!(!stack.is_null(), "not: stack is empty");
161 let (rest, a) = unsafe { pop(stack) };
162
163 match a {
164 Value::Int(a_val) => unsafe { push(rest, Value::Int(if a_val == 0 { 1 } else { 0 })) },
165 _ => panic!("not: expected integer on stack"),
166 }
167}