Skip to content

Commit 7cfa897

Browse files
gserena01gserenagithub-actions[bot]
authored
add modulo and power operators (#1341)
* add modulo and power operators * format * point to main instead of serena branch * reset cargo lock * A snapshot a day keeps the bugs away! πŸ“·πŸ› (OS: ubuntu) --------- Co-authored-by: gserena <serena@zoo.dev> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 0d88040 commit 7cfa897

File tree

4 files changed

+58
-10
lines changed

4 files changed

+58
-10
lines changed

β€Žsrc/wasm-lib/Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žsrc/wasm-lib/grackle/src/lib.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,8 @@ impl Planner {
202202
ast::types::BinaryOperator::Sub => ep::BinaryOperation::Sub,
203203
ast::types::BinaryOperator::Mul => ep::BinaryOperation::Mul,
204204
ast::types::BinaryOperator::Div => ep::BinaryOperation::Div,
205-
ast::types::BinaryOperator::Mod => {
206-
todo!("execution plan instruction set doesn't support Mod yet")
207-
}
208-
ast::types::BinaryOperator::Pow => {
209-
todo!("execution plan instruction set doesn't support Pow yet")
210-
}
205+
ast::types::BinaryOperator::Mod => ep::BinaryOperation::Mod,
206+
ast::types::BinaryOperator::Pow => ep::BinaryOperation::Pow,
211207
},
212208
operand0: ep::Operand::Reference(l_binding),
213209
operand1: ep::Operand::Reference(r_binding),

β€Žsrc/wasm-lib/grackle/src/tests.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,3 +1145,54 @@ fn arrays_as_parameters() {
11451145
}
11461146
)
11471147
}
1148+
1149+
#[test]
1150+
fn mod_and_pow() {
1151+
let program = "
1152+
let x = 2
1153+
let y = x^3
1154+
let z = y % 5
1155+
";
1156+
let (plan, _bindings) = must_plan(program);
1157+
let addr0 = Address::ZERO;
1158+
let addr1 = Address::ZERO.offset(1);
1159+
let addr2 = Address::ZERO.offset(2);
1160+
let addr3 = Address::ZERO.offset(3);
1161+
let addr4 = Address::ZERO.offset(4);
1162+
print!("{:?}", plan);
1163+
assert_eq!(
1164+
plan,
1165+
vec![
1166+
Instruction::SetPrimitive {
1167+
address: addr0,
1168+
value: 2i64.into(),
1169+
},
1170+
Instruction::SetPrimitive {
1171+
address: addr1,
1172+
value: 3i64.into(),
1173+
},
1174+
// x ^ 3, where x = 2
1175+
Instruction::BinaryArithmetic {
1176+
arithmetic: ep::BinaryArithmetic {
1177+
operation: ep::BinaryOperation::Pow,
1178+
operand0: ep::Operand::Reference(addr0),
1179+
operand1: ep::Operand::Reference(addr1),
1180+
},
1181+
destination: Destination::Address(addr2),
1182+
},
1183+
Instruction::SetPrimitive {
1184+
address: addr3,
1185+
value: 5i64.into(),
1186+
},
1187+
// y % 5, where y is 2^3
1188+
Instruction::BinaryArithmetic {
1189+
arithmetic: ep::BinaryArithmetic {
1190+
operation: ep::BinaryOperation::Mod,
1191+
operand0: ep::Operand::Reference(addr2),
1192+
operand1: ep::Operand::Reference(addr3),
1193+
},
1194+
destination: Destination::Address(addr4),
1195+
}
1196+
]
1197+
);
1198+
}

β€Žsrc/wasm-lib/kcl/src/parser/parser_impl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ fn binary_operator(i: TokenSlice) -> PResult<BinaryOperator> {
267267
"/" => BinaryOperator::Div,
268268
"*" => BinaryOperator::Mul,
269269
"%" => BinaryOperator::Mod,
270+
"^" => BinaryOperator::Pow,
270271
_ => {
271272
return Err(KclError::Syntax(KclErrorDetails {
272273
source_ranges: token.as_source_ranges(),

0 commit comments

Comments
Β (0)