Add add, sub, mul, and div functions to integers

Arithmetic among integers is supportd. Yey!

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-10-19 16:41:57 -07:00
parent 57c1aa4702
commit f35d44cf65
6 changed files with 117 additions and 16 deletions

View File

@@ -11,7 +11,6 @@ pub enum Error {
#[snafu(display("{}", error))]
ValueError {
error: String,
value: ObjRef,
},
#[snafu(display("incorrect function arity; expected {} but got {} instead", expected, got))]

View File

@@ -149,7 +149,7 @@ impl<'c> Vm<'c> {
Signal::Call(callee, args) => {
read_obj!(let callee_obj = callee);
let frame = callee_obj.as_fun()
.ok_or_else(|| Error::ValueError { error: "cannot call this object".to_string(), value: callee.clone() })?
.ok_or_else(|| Error::ValueError { error: "cannot call this object".to_string() })?
.create_frame(callee.clone(), self, args)?;
// Jump to the first address of the new function call if it's a user function
if let Frame::User(_) = &frame {
@@ -282,18 +282,18 @@ impl<'c> Vm<'c> {
let lhs = self.pop().unwrap();
let fun = {
read_obj!(let lhs = lhs);
lhs.get_plus().expect("TODO: throw an error for missing __plus__ attr")
lhs.get_plus().expect("TODO: throw an error for missing __add__ attr")
};
signal = Some(Signal::Call(fun, vec![lhs, rhs]));
signal = Some(Signal::Call(fun, vec![rhs]));
}
Inst::BinMinus => {
let rhs = self.pop().unwrap();
let lhs = self.pop().unwrap();
let fun = {
read_obj!(let lhs = lhs);
lhs.get_minus().expect("TODO: throw an error for missing __minus__ attr")
lhs.get_minus().expect("TODO: throw an error for missing __sub__ attr")
};
signal = Some(Signal::Call(fun, vec![lhs, rhs]));
signal = Some(Signal::Call(fun, vec![rhs]));
}
Inst::BinMul => {
let rhs = self.pop().unwrap();
@@ -302,9 +302,17 @@ impl<'c> Vm<'c> {
read_obj!(let lhs = lhs);
lhs.get_mul().expect("TODO: throw an error for missing __mul__ attr")
};
signal = Some(Signal::Call(fun, vec![lhs, rhs]));
signal = Some(Signal::Call(fun, vec![rhs]));
}
Inst::BinDiv => {
let rhs = self.pop().unwrap();
let lhs = self.pop().unwrap();
let fun = {
read_obj!(let lhs = lhs);
lhs.get_div().expect("TODO: throw an error for missing __div__ attr")
};
signal = Some(Signal::Call(fun, vec![rhs]));
}
Inst::BinDiv => todo!(),
Inst::BinEq => {
let rhs = self.pop().unwrap();
let lhs = self.pop().unwrap();
@@ -312,7 +320,7 @@ impl<'c> Vm<'c> {
read_obj!(let lhs = lhs);
lhs.get_eq().expect("TODO: throw an error for missing __eq__ attr")
};
signal = Some(Signal::Call(fun, vec![lhs, rhs]));
signal = Some(Signal::Call(fun, vec![rhs]));
}
Inst::BinNeq => todo!(),
Inst::BinLt => todo!(),