Clean up some code

This commit is contained in:
Sofia 2026-03-20 20:12:18 +02:00
parent 8fe3ffc8e0
commit 4ac41e7df1

View File

@ -739,204 +739,78 @@ impl ClosureRunner {
}
Instruction::Concat(res, lhs, rhs) => {
let lhs = self
.stack
.get(lhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let rhs = self
.stack
.get(rhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let (lhs, rhs) = self.lhs_and_rhs(lhs, rhs);
self.set_stack(*res, StackValue::Value(lhs.concat(&rhs)?));
}
Instruction::Add(res, lhs, rhs) => {
let lhs = self
.stack
.get(lhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let rhs = self
.stack
.get(rhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let (lhs, rhs) = self.lhs_and_rhs(lhs, rhs);
self.set_stack(*res, StackValue::Value(lhs.add(&rhs)?));
}
Instruction::Mult(res, lhs, rhs) => {
let lhs = self
.stack
.get(lhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let rhs = self
.stack
.get(rhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let (lhs, rhs) = self.lhs_and_rhs(lhs, rhs);
self.set_stack(*res, StackValue::Value(lhs.mult(&rhs)?));
}
Instruction::Div(res, lhs, rhs) => {
let lhs = self
.stack
.get(lhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let rhs = self
.stack
.get(rhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let (lhs, rhs) = self.lhs_and_rhs(lhs, rhs);
self.set_stack(*res, StackValue::Value(lhs.div(&rhs)?));
}
Instruction::IDiv(res, lhs, rhs) => {
let lhs = self
.stack
.get(lhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let rhs = self
.stack
.get(rhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let (lhs, rhs) = self.lhs_and_rhs(lhs, rhs);
self.set_stack(*res, StackValue::Value(lhs.idiv(&rhs)?));
}
Instruction::Mod(res, lhs, rhs) => {
let lhs = self
.stack
.get(lhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let rhs = self
.stack
.get(rhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let (lhs, rhs) = self.lhs_and_rhs(lhs, rhs);
self.set_stack(*res, StackValue::Value(lhs.r#mod(&rhs)?));
}
Instruction::Exp(res, lhs, rhs) => {
let lhs = self
.stack
.get(lhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let rhs = self
.stack
.get(rhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let (lhs, rhs) = self.lhs_and_rhs(lhs, rhs);
self.set_stack(*res, StackValue::Value(lhs.exp(&rhs)?));
}
Instruction::BitAnd(res, lhs, rhs) => {
let lhs = self
.stack
.get(lhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let rhs = self
.stack
.get(rhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let (lhs, rhs) = self.lhs_and_rhs(lhs, rhs);
self.set_stack(*res, StackValue::Value(lhs.band(&rhs)?));
}
Instruction::BitOr(res, lhs, rhs) => {
let lhs = self
.stack
.get(lhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let rhs = self
.stack
.get(rhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let (lhs, rhs) = self.lhs_and_rhs(lhs, rhs);
self.set_stack(*res, StackValue::Value(lhs.bor(&rhs)?));
}
Instruction::BitXOr(res, lhs, rhs) => {
let lhs = self
.stack
.get(lhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let rhs = self
.stack
.get(rhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let (lhs, rhs) = self.lhs_and_rhs(lhs, rhs);
self.set_stack(*res, StackValue::Value(lhs.bxor(&rhs)?));
}
Instruction::BitSRight(res, lhs, rhs) => {
let lhs = self
.stack
.get(lhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let rhs = self
.stack
.get(rhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let (lhs, rhs) = self.lhs_and_rhs(lhs, rhs);
self.set_stack(*res, StackValue::Value(lhs.bsright(&rhs)?));
}
Instruction::BitSLeft(res, lhs, rhs) => {
let lhs = self
.stack
.get(lhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let rhs = self
.stack
.get(rhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let (lhs, rhs) = self.lhs_and_rhs(lhs, rhs);
self.set_stack(*res, StackValue::Value(lhs.bsleft(&rhs)?));
}
Instruction::Equal(res, lhs, rhs) => {
let lhs = self
.stack
.get(lhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let rhs = self
.stack
.get(rhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let (lhs, rhs) = self.lhs_and_rhs(lhs, rhs);
self.set_stack(*res, StackValue::Value(lhs.eq(&rhs)?));
}
Instruction::LessThan(res, lhs, rhs) => {
let lhs = self
.stack
.get(lhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let rhs = self
.stack
.get(rhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let (lhs, rhs) = self.lhs_and_rhs(lhs, rhs);
self.set_stack(*res, StackValue::Value(lhs.lt(&rhs)?));
}
Instruction::LessThanOrEqual(res, lhs, rhs) => {
let lhs = self
.stack
.get(lhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let rhs = self
.stack
.get(rhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let (lhs, rhs) = self.lhs_and_rhs(lhs, rhs);
self.set_stack(*res, StackValue::Value(lhs.lte(&rhs)?));
}
Instruction::Or(res, lhs, rhs) => {
let (lhs, rhs) = self.lhs_and_rhs(lhs, rhs);
self.set_stack(*res, StackValue::Value(lhs.or(&rhs)?));
}
Instruction::And(res, lhs, rhs) => {
let (lhs, rhs) = self.lhs_and_rhs(lhs, rhs);
self.set_stack(*res, StackValue::Value(lhs.and(&rhs)?));
}
Instruction::Unm(res, reg) => {
self.set_stack(
*res,
@ -986,32 +860,6 @@ impl ClosureRunner {
);
}
Instruction::Or(res, lhs, rhs) => {
let lhs = self
.stack
.get(lhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let rhs = self
.stack
.get(rhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
self.set_stack(*res, StackValue::Value(lhs.or(&rhs)?));
}
Instruction::And(res, lhs, rhs) => {
let lhs = self
.stack
.get(lhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
let rhs = self
.stack
.get(rhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil);
self.set_stack(*res, StackValue::Value(lhs.and(&rhs)?));
}
Instruction::Vararg(reg, len) => {
self.function_register = reg - 1;
if *len == 0 {
@ -1054,4 +902,17 @@ impl ClosureRunner {
Ok(Some(Vec::new()))
}
}
fn lhs_and_rhs(&self, lhs: &u16, rhs: &u16) -> (Value, Value) {
(
self.stack
.get(lhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil),
self.stack
.get(rhs)
.map(|v| v.borrow().clone())
.unwrap_or(Value::Nil),
)
}
}