Initial commit
* Parser, compiler, objects, and VM base implementations * Stuff will print out, functions called, etc * There are probably plenty of bugs but this is a good starting point to start committing changes into git Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
53
src/obj/int.rs
Normal file
53
src/obj/int.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
use crate::obj::prelude::*;
|
||||
use std::{any::Any, sync::LazyLock};
|
||||
|
||||
pub type IntValue = i64;
|
||||
|
||||
static INT_ATTRS: LazyLock<AttrsPtr> = LazyLock::new(|| {
|
||||
attrs_ptr! {
|
||||
//
|
||||
"__str__" => builtin_fun_ptr!("__str__", |_vm, args, _state| {
|
||||
if args.len() != 1 {
|
||||
todo!("Throw exception: arity error");
|
||||
}
|
||||
let arg_any = &args[0].as_any();
|
||||
let obj = arg_any.downcast_ref::<Int>().expect("Int.__str__ did not receive Int?");
|
||||
|
||||
BuiltinFunResult::Return(Some(ObjPtr::new(Str::new(obj.value().to_string()))))
|
||||
}),
|
||||
}
|
||||
});
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Int {
|
||||
attrs: AttrsPtr,
|
||||
value: IntValue,
|
||||
}
|
||||
|
||||
impl Int {
|
||||
pub fn new(value: IntValue) -> Self {
|
||||
let attrs = AttrsPtr::clone(&INT_ATTRS);
|
||||
// TODO intern
|
||||
Int { attrs, value }
|
||||
}
|
||||
|
||||
pub fn value(&self) -> IntValue {
|
||||
self.value
|
||||
}
|
||||
}
|
||||
|
||||
impl Obj for Int {
|
||||
fn attrs(&self) -> AttrsPtr {
|
||||
AttrsPtr::clone(&self.attrs)
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &(dyn Any + 'static) {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl ToString for Int {
|
||||
fn to_string(&self) -> String {
|
||||
format!("{}", self.value())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user