diff --git a/src/obj/mod.rs b/src/obj/mod.rs index 5ef2907..eb70140 100644 --- a/src/obj/mod.rs +++ b/src/obj/mod.rs @@ -14,9 +14,10 @@ pub mod prelude { pub use crate::obj::int::*; pub use crate::obj::quote::*; pub use crate::obj::str::*; - pub use crate::obj::{Obj, ObjPtr, VTable}; + pub use crate::obj::{Obj, ObjPtr, VTable, VTableBuilder}; } +use crate::obj::builtin::{BuiltinFn, BuiltinFnObj, BuiltinFnPtr}; use crate::syn::span::Span; use crate::vm::{error::*, machine::Machine}; use gc::{Finalize, Gc, Trace}; @@ -27,6 +28,28 @@ use std::fmt::Debug; pub type ObjPtr = Gc; pub type VTable = HashMap; +#[derive(Debug, Clone, Default)] +pub struct VTableBuilder { + vtable: VTable, +} + +impl VTableBuilder { + pub fn with(mut self, name: impl ToString, obj: ObjPtr) -> Self { + self.vtable.insert(name.to_string(), obj); + self + } + + pub fn with_builtin(self, name: impl ToString, fun: BuiltinFnPtr) -> Self { + let name = name.to_string(); + let fun = BuiltinFnObj::new(BuiltinFn::new(name.clone(), fun)); + self.with(name, fun) + } + + pub fn finish(self) -> VTable { + self.vtable + } +} + /// An object that has a vtable and common functions. pub trait Obj: Trace + Finalize + Debug { /// Gets the vtable for this object.