Skip to content

Commit

Permalink
+ Object::Native
Browse files Browse the repository at this point in the history
  • Loading branch information
meloalright committed Apr 6, 2024
1 parent d49b072 commit 137b27b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
21 changes: 16 additions & 5 deletions interpreter/src/evaluator/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::HashMap;
extern crate rand;

use crate::evaluator::object::Object;
use crate::evaluator::object::NativeObject;

use rand::distributions::Uniform;
use rand::{thread_rng, Rng};
Expand Down Expand Up @@ -150,8 +151,13 @@ fn three_body_sophon_infer(args: Vec<Object>) -> Object {
match &args[0] {
Object::Hash(hash) => {
let model_ptr = match hash.get(&Object::String("model".to_owned())).unwrap() {
Object::NativeObject(model_ptr) => {
model_ptr.clone()
Object::Native(native_object) => {
match **native_object {
NativeObject::LLMModel(model_ptr) => {
model_ptr.clone()
},
_ => panic!()
}
},
_ => panic!()
};
Expand Down Expand Up @@ -225,8 +231,13 @@ fn three_body_sophon_close(args: Vec<Object>) -> Object {
match &args[0] {
Object::Hash(hash) => {
let model_ptr = match hash.get(&Object::String("model".to_owned())).unwrap() {
Object::NativeObject(model_ptr) => {
model_ptr.clone()
Object::Native(native_object) => {
match **native_object {
NativeObject::LLMModel(model_ptr) => {
model_ptr.clone()
},
_ => panic!()
}
},
_ => panic!()
};
Expand Down Expand Up @@ -306,7 +317,7 @@ fn three_body_sophon_engineering(args: Vec<Object>) -> Object {
let model_ptr = &mut *model as *mut dyn Model;

let mut session_hash = HashMap::new();
session_hash.insert(Object::String("model".to_owned()), Object::NativeObject(model_ptr));
session_hash.insert(Object::String("model".to_owned()), Object::Native(Box::new(NativeObject::LLMModel(model_ptr))));
session_hash.insert(Object::String("character".to_owned()), Object::String(character.to_string()));
session_hash.insert(Object::String("infer".to_owned()), Object::Builtin(2, three_body_sophon_infer));
session_hash.insert(Object::String("close".to_owned()), Object::Builtin(1, three_body_sophon_close));
Expand Down
9 changes: 7 additions & 2 deletions interpreter/src/evaluator/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ use crate::lexer::unescape::escape_str;

pub type BuiltinFunc = fn(Vec<Object>) -> Object;

#[derive(PartialEq, Clone, Debug)]
pub enum NativeObject {
LLMModel(*mut dyn llm::Model),
}

#[derive(PartialEq, Clone, Debug)]
pub enum Object {
Int(i64),
Expand All @@ -25,7 +30,7 @@ pub enum Object {
ContinueStatement,
Error(String),
Null,
NativeObject(*mut dyn llm::Model),
Native(Box<NativeObject>),
}

/// This is actually repr
Expand Down Expand Up @@ -74,7 +79,7 @@ impl fmt::Display for Object {
Object::ContinueStatement => write!(f, "ContinueStatement"),
Object::ReturnValue(ref value) => write!(f, "ReturnValue({})", value),
Object::Error(ref value) => write!(f, "Error({})", value),
Object::NativeObject(ref model) => write!(f, "NativeObject({:?})", (model)),
Object::Native(ref model) => write!(f, "NativeObject({:?})", (model)),
}
}
}
Expand Down

0 comments on commit 137b27b

Please sign in to comment.