feat: 添加数组和字符串的属性与方法支持,包括 length、push、pop、upper、lower、trim、contains、starts_with、ends_with、substring、replace 和 split

This commit is contained in:
2026-06-17 00:23:43 +08:00
parent 8e49df8062
commit f0bba2f2aa
4 changed files with 333 additions and 21 deletions
+20 -20
View File
@@ -3,7 +3,7 @@
mod core;
mod io;
mod os;
mod string;
pub mod string;
use crate::interpreter::{Env, Value};
use std::cell::RefCell;
@@ -16,34 +16,34 @@ pub fn register_all(env: &Rc<RefCell<Env>>) {
// io
let mut io = HashMap::new();
io.insert("print".into(), Value::NativeFunction(io::print));
io.insert("input".into(), Value::NativeFunction(io::input));
io.insert("print".into(), Value::NativeFunction(Rc::new(io::print)));
io.insert("input".into(), Value::NativeFunction(Rc::new(io::input)));
e.define("io".into(), Value::Object(Rc::new(RefCell::new(io))), true);
// input and print can be used as global functions for convenience
e.define("print".into(), Value::NativeFunction(io::print), true);
e.define("input".into(), Value::NativeFunction(io::input), true);
e.define("print".into(), Value::NativeFunction(Rc::new(io::print)), true);
e.define("input".into(), Value::NativeFunction(Rc::new(io::input)), true);
// os
let mut os = HashMap::new();
os.insert("clock".into(), Value::NativeFunction(os::clock));
os.insert("clock".into(), Value::NativeFunction(Rc::new(os::clock)));
e.define("os".into(), Value::Object(Rc::new(RefCell::new(os))), true);
// clock can also be used as a global function for convenience
e.define("clock".into(), Value::NativeFunction(os::clock), true);
e.define("clock".into(), Value::NativeFunction(Rc::new(os::clock)), true);
// core — len, typeof, push, pop
e.define("len".into(), Value::NativeFunction(core::len), true);
e.define("typeof".into(), Value::NativeFunction(core::typeof_fn), true);
e.define("push".into(), Value::NativeFunction(core::push), true);
e.define("pop".into(), Value::NativeFunction(core::pop), true);
e.define("len".into(), Value::NativeFunction(Rc::new(core::len)), true);
e.define("typeof".into(), Value::NativeFunction(Rc::new(core::typeof_fn)), true);
e.define("push".into(), Value::NativeFunction(Rc::new(core::push)), true);
e.define("pop".into(), Value::NativeFunction(Rc::new(core::pop)), true);
// string — split, trim, substring, replace, contains, upper, lower, starts_with, ends_with
e.define("split".into(), Value::NativeFunction(string::split), true);
e.define("trim".into(), Value::NativeFunction(string::trim), true);
e.define("substring".into(), Value::NativeFunction(string::substring), true);
e.define("replace".into(), Value::NativeFunction(string::replace), true);
e.define("contains".into(), Value::NativeFunction(string::contains), true);
e.define("upper".into(), Value::NativeFunction(string::upper), true);
e.define("lower".into(), Value::NativeFunction(string::lower), true);
e.define("starts_with".into(), Value::NativeFunction(string::starts_with), true);
e.define("ends_with".into(), Value::NativeFunction(string::ends_with), true);
e.define("split".into(), Value::NativeFunction(Rc::new(string::split)), true);
e.define("trim".into(), Value::NativeFunction(Rc::new(string::trim)), true);
e.define("substring".into(), Value::NativeFunction(Rc::new(string::substring)), true);
e.define("replace".into(), Value::NativeFunction(Rc::new(string::replace)), true);
e.define("contains".into(), Value::NativeFunction(Rc::new(string::contains)), true);
e.define("upper".into(), Value::NativeFunction(Rc::new(string::upper)), true);
e.define("lower".into(), Value::NativeFunction(Rc::new(string::lower)), true);
e.define("starts_with".into(), Value::NativeFunction(Rc::new(string::starts_with)), true);
e.define("ends_with".into(), Value::NativeFunction(Rc::new(string::ends_with)), true);
}