Skip to content

Commit

Permalink
feat: support params values into thread
Browse files Browse the repository at this point in the history
  • Loading branch information
meloalright committed Oct 8, 2024
1 parent 1009675 commit ae250cd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/Threading.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ jobs:
- name: Threading
run: |
./target/release/3body -V
./target/release/3body -c '给 cx 以 程心(); 给 研制曲率飞船 以 法则() { 给 掩体纪年 以 5; 面壁 (掩体纪年 <= 11) { 冬眠(1000); 广播(["研制曲率飞船", 掩体纪年]); 掩体纪年 = 掩体纪年 + 1; } } 给 星环公司 以 [cx.thread(研制曲率飞船), cx.thread(研制曲率飞船), cx.thread(研制曲率飞船), cx.thread(研制曲率飞船), cx.thread(研制曲率飞船)]; 冬眠(12000)'
./target/release/3body -c '给 cx 以 程心(); 给 星环公司 以 法则(name, y, limit) { 给 掩体纪年 以 y; 面壁 (掩体纪年 <= limit) { 冬眠(1000); 广播([name, 掩体纪年]); 掩体纪年 = 掩体纪年 + 1; } } cx.thread(星环公司, ["掩体工程", 0, 11]) 冬眠(5000) cx.thread(星环公司, ["研制曲率飞船", 5, 11]) 冬眠(6000)'
34 changes: 32 additions & 2 deletions interpreter/src/evaluator/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::evaluator::object::Object;
use crate::evaluator::object::NativeObject;
use crate::evaluator::env::Env;
use crate::evaluator::Evaluator;
use crate::ast;

use rand::distributions::Uniform;
use rand::{thread_rng, Rng};
Expand Down Expand Up @@ -358,6 +359,20 @@ fn three_body_threading(args: Vec<Object>) -> Object {
Object::Function(params, ast, env ) => {

let stmts = ast.clone();
let params = params.clone();

let literals: Vec<crate::ast::Literal> = match &args[1] {
Object::Array(arr) => {
arr.iter().map(|o| match o {
Object::Int(i) => ast::Literal::Int(i.clone()),
Object::String(str) => ast::Literal::String(str.clone()),
Object::Bool(bool) => ast::Literal::Bool(bool.clone()),
_ => todo!(),
}).collect()
},
_ => panic!()
};

let mut handle = std::thread::spawn(move || {
let local_set = tokio::task::LocalSet::new();
let rt = tokio::runtime::Builder::new_current_thread()
Expand All @@ -367,7 +382,22 @@ fn three_body_threading(args: Vec<Object>) -> Object {

local_set.spawn_local(async move {
let mut ev = Evaluator {
env: Rc::new(RefCell::new(Env::from(new_builtins()))),
env: {
let scoped_env = Rc::new(RefCell::new(Env::from(new_builtins())));

for (i, ident) in params.iter().enumerate() {
let crate::ast::Ident(name) = ident.clone();
let o = match &literals[i] {
ast::Literal::Int(i) => Object::Int(i.clone()),
ast::Literal::String(str) => Object::String(str.clone()),
ast::Literal::Bool(bo) => Object::Bool(bo.clone()),
_ => todo!(),
};
scoped_env.borrow_mut().set(name, o.clone());
}

scoped_env
},
};
ev.eval(&stmts);
});
Expand All @@ -382,7 +412,7 @@ fn three_body_threading(args: Vec<Object>) -> Object {
_ => panic!()
}
}
session_hash.insert(Object::String("thread".to_owned()), Object::Builtin(1, three_body_thread_new));
session_hash.insert(Object::String("thread".to_owned()), Object::Builtin(2, three_body_thread_new));
}


Expand Down

0 comments on commit ae250cd

Please sign in to comment.