feat: error handling (WIP)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use derive_more::Constructor;
|
||||
|
||||
use crate::vm::VM;
|
||||
use crate::error::Result;
|
||||
|
||||
use super::Value;
|
||||
|
||||
@@ -8,7 +9,7 @@ use super::Value;
|
||||
pub struct PrimOp<'vm> {
|
||||
pub name: &'static str,
|
||||
arity: u8,
|
||||
func: fn(&'vm VM<'_>, Vec<Value<'vm>>) -> Value<'vm>,
|
||||
func: fn(&'vm VM<'_>, Vec<Value<'vm>>) -> Result<Value<'vm>>,
|
||||
}
|
||||
|
||||
impl PartialEq for PrimOp<'_> {
|
||||
@@ -18,14 +19,14 @@ impl PartialEq for PrimOp<'_> {
|
||||
}
|
||||
|
||||
impl<'vm> PrimOp<'vm> {
|
||||
pub fn call(self, vm: &'vm VM<'_>, args: Vec<Value<'vm>>) -> Value<'vm> {
|
||||
pub fn call(self, vm: &'vm VM<'_>, args: Vec<Value<'vm>>) -> Result<Value<'vm>> {
|
||||
if (args.len() as u8) < self.arity {
|
||||
Value::PartialPrimOp(PartialPrimOp {
|
||||
name: self.name,
|
||||
arity: self.arity - args.len() as u8,
|
||||
args,
|
||||
func: self.func,
|
||||
})
|
||||
}).ok()
|
||||
} else if args.len() as u8 == self.arity {
|
||||
(self.func)(vm, args)
|
||||
} else {
|
||||
@@ -39,7 +40,7 @@ pub struct PartialPrimOp<'vm> {
|
||||
pub name: &'static str,
|
||||
arity: u8,
|
||||
args: Vec<Value<'vm>>,
|
||||
func: fn(&'vm VM<'_>, Vec<Value<'vm>>) -> Value<'vm>,
|
||||
func: fn(&'vm VM<'_>, Vec<Value<'vm>>) -> Result<Value<'vm>>,
|
||||
}
|
||||
|
||||
impl PartialEq for PartialPrimOp<'_> {
|
||||
@@ -49,7 +50,7 @@ impl PartialEq for PartialPrimOp<'_> {
|
||||
}
|
||||
|
||||
impl<'vm> PartialPrimOp<'vm> {
|
||||
pub fn call(mut self, vm: &'vm VM<'_>, args: Vec<Value<'vm>>) -> Value<'vm> {
|
||||
pub fn call(mut self, vm: &'vm VM<'_>, args: Vec<Value<'vm>>) -> Result<Value<'vm>> {
|
||||
let len = args.len() as u8;
|
||||
self.args.extend(args);
|
||||
if len < self.arity {
|
||||
@@ -58,7 +59,7 @@ impl<'vm> PartialPrimOp<'vm> {
|
||||
arity: self.arity - len,
|
||||
args: self.args,
|
||||
func: self.func,
|
||||
})
|
||||
}).ok()
|
||||
} else if len == self.arity {
|
||||
(self.func)(vm, self.args)
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user