Skip to content

Commit

Permalink
Enable op overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
voltrevo committed Jul 15, 2024
1 parent ea9e16b commit 9a105c4
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 38 deletions.
15 changes: 12 additions & 3 deletions valuescript_compiler/src/optimization/kal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,12 @@ impl FnState {
}
}

fn apply_unary_op(&mut self, arg: &mut Value, dst: &Register, op: fn(input: &Val) -> Val) {
fn apply_unary_op(
&mut self,
arg: &mut Value,
dst: &Register,
op: fn(input: &Val) -> Result<Val, Val>,
) {
match self.apply_unary_op_impl(arg, dst, op) {
Some(_) => {}
None => {
Expand All @@ -848,10 +853,14 @@ impl FnState {
&mut self,
arg: &mut Value,
dst: &Register,
op: fn(input: &Val) -> Val,
op: fn(input: &Val) -> Result<Val, Val>,
) -> Option<()> {
let arg = self.eval_arg(arg).try_to_val()?;
let kal = op(&arg).try_to_kal()?;

let kal = match op(&arg) {
Ok(res) => res.try_to_kal()?,
Err(_) => return None,
};

self.set(dst.name.clone(), kal);

Expand Down
24 changes: 24 additions & 0 deletions valuescript_vm/src/binary_op.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
pub enum BinaryOp {
Plus,
Minus,
Mul,
Div,
Mod,
Exp,
LooseEq,
LooseNe,
Eq,
Ne,
And,
Or,
Less,
LessEq,
Greater,
GreaterEq,
BitAnd,
BitOr,
BitXor,
LeftShift,
RightShift,
RightShiftUnsigned,
}
16 changes: 9 additions & 7 deletions valuescript_vm/src/bytecode_stack_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ pub struct CatchSetting {
}

impl BytecodeStackFrame {
pub fn apply_unary_op(&mut self, op: fn(input: &Val) -> Val) {
pub fn apply_unary_op(&mut self, op: fn(input: &Val) -> Result<Val, Val>) -> Result<(), Val> {
let input = self.decoder.decode_val(&mut self.registers);

if let Some(register_index) = self.decoder.decode_register_index() {
self.registers[register_index] = op(&input);
self.registers[register_index] = op(&input)?;
}

Ok(())
}

pub fn apply_binary_op(
Expand Down Expand Up @@ -184,7 +186,7 @@ impl StackFrameTrait for BytecodeStackFrame {
OpAnd => self.apply_binary_op(operations::op_and)?,
OpOr => self.apply_binary_op(operations::op_or)?,

OpNot => self.apply_unary_op(operations::op_not),
OpNot => self.apply_unary_op(operations::op_not)?,

OpLess => self.apply_binary_op(operations::op_less)?,
OpLessEq => self.apply_binary_op(operations::op_less_eq)?,
Expand All @@ -202,14 +204,14 @@ impl StackFrameTrait for BytecodeStackFrame {
OpBitAnd => self.apply_binary_op(operations::op_bit_and)?,
OpBitOr => self.apply_binary_op(operations::op_bit_or)?,

OpBitNot => self.apply_unary_op(operations::op_bit_not),
OpBitNot => self.apply_unary_op(operations::op_bit_not)?,

OpBitXor => self.apply_binary_op(operations::op_bit_xor)?,
OpLeftShift => self.apply_binary_op(operations::op_left_shift)?,
OpRightShift => self.apply_binary_op(operations::op_right_shift)?,
OpRightShiftUnsigned => self.apply_binary_op(operations::op_right_shift_unsigned)?,

TypeOf => self.apply_unary_op(operations::op_typeof),
TypeOf => self.apply_unary_op(operations::op_typeof)?,

InstanceOf => self.apply_binary_op(operations::op_instance_of)?,
In => self.apply_binary_op(operations::op_in)?,
Expand Down Expand Up @@ -415,8 +417,8 @@ impl StackFrameTrait for BytecodeStackFrame {
}
}

UnaryPlus => self.apply_unary_op(operations::op_unary_plus),
UnaryMinus => self.apply_unary_op(operations::op_unary_minus),
UnaryPlus => self.apply_unary_op(operations::op_unary_plus)?,
UnaryMinus => self.apply_unary_op(operations::op_unary_minus)?,

New => {
// TODO: new Array
Expand Down
2 changes: 2 additions & 0 deletions valuescript_vm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod array_higher_functions;
mod array_methods;
mod bigint_methods;
mod binary_op;
mod builtins;
mod bytecode;
mod bytecode_decoder;
Expand All @@ -20,6 +21,7 @@ pub mod operations;
mod stack_frame;
mod string_methods;
mod todo_fn;
mod unary_op;
mod val_storage;
mod virtual_machine;
pub mod vs_array;
Expand Down
Loading

0 comments on commit 9a105c4

Please sign in to comment.