diff --git a/.github/workflows/Threading.yml b/.github/workflows/Threading.yml index 25766cc..ae5bbd0 100644 --- a/.github/workflows/Threading.yml +++ b/.github/workflows/Threading.yml @@ -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)' \ No newline at end of file + ./target/release/3body -c '给 cx 以 程心(); 给 星环公司 以 法则(name, y, limit) { 给 掩体纪年 以 y; 面壁 (掩体纪年 <= limit) { 冬眠(1000); 广播([name, 掩体纪年]); 掩体纪年 = 掩体纪年 + 1; } } cx.thread(星环公司, ["掩体工程", 0, 11]) 冬眠(5000) cx.thread(星环公司, ["研制曲率飞船", 5, 11]) 冬眠(6000)' diff --git a/interpreter/src/evaluator/builtins.rs b/interpreter/src/evaluator/builtins.rs index d42938a..e850700 100644 --- a/interpreter/src/evaluator/builtins.rs +++ b/interpreter/src/evaluator/builtins.rs @@ -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}; @@ -358,6 +359,20 @@ fn three_body_threading(args: Vec) -> Object { Object::Function(params, ast, env ) => { let stmts = ast.clone(); + let params = params.clone(); + + let literals: Vec = 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() @@ -367,7 +382,22 @@ fn three_body_threading(args: Vec) -> 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); }); @@ -382,7 +412,7 @@ fn three_body_threading(args: Vec) -> 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)); }