Add VTableBuilder
Experimenting with using a builder instead of a macro for making vtables. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -14,9 +14,10 @@ pub mod prelude {
|
|||||||
pub use crate::obj::int::*;
|
pub use crate::obj::int::*;
|
||||||
pub use crate::obj::quote::*;
|
pub use crate::obj::quote::*;
|
||||||
pub use crate::obj::str::*;
|
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::syn::span::Span;
|
||||||
use crate::vm::{error::*, machine::Machine};
|
use crate::vm::{error::*, machine::Machine};
|
||||||
use gc::{Finalize, Gc, Trace};
|
use gc::{Finalize, Gc, Trace};
|
||||||
@@ -27,6 +28,28 @@ use std::fmt::Debug;
|
|||||||
pub type ObjPtr = Gc<dyn Obj + 'static>;
|
pub type ObjPtr = Gc<dyn Obj + 'static>;
|
||||||
pub type VTable = HashMap<String, ObjPtr>;
|
pub type VTable = HashMap<String, ObjPtr>;
|
||||||
|
|
||||||
|
#[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.
|
/// An object that has a vtable and common functions.
|
||||||
pub trait Obj: Trace + Finalize + Debug {
|
pub trait Obj: Trace + Finalize + Debug {
|
||||||
/// Gets the vtable for this object.
|
/// Gets the vtable for this object.
|
||||||
|
|||||||
Reference in New Issue
Block a user