Refactor native function registration and instantiation
Introduces `register_native` for direct registration of `NativeFunction` and `register_native_fn` for convenience from closures. The `Environment::run` method is removed, and its functionality is now handled by `Environment::instantiate`, which packages the linked AST into an invokable `NativeFunction`. This streamlines the execution path and better separates compilation/linking from runtime execution.
This commit is contained in:
+18
-18
@@ -39,7 +39,7 @@ fn register_arithmetic(env: &Environment) {
|
||||
ret: StaticType::DateTime,
|
||||
},
|
||||
]);
|
||||
env.register_native("+", add_ty, Purity::Pure, |args| {
|
||||
env.register_native_fn("+", add_ty, Purity::Pure, |args| {
|
||||
if args.len() == 2 {
|
||||
match (&args[0], &args[1]) {
|
||||
(Value::Int(a), Value::Int(b)) => Value::Int(a + b),
|
||||
@@ -99,7 +99,7 @@ fn register_arithmetic(env: &Environment) {
|
||||
ret: StaticType::DateTime,
|
||||
},
|
||||
]);
|
||||
env.register_native("-", sub_ty, Purity::Pure, |args| {
|
||||
env.register_native_fn("-", sub_ty, Purity::Pure, |args| {
|
||||
if args.is_empty() {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -152,7 +152,7 @@ fn register_arithmetic(env: &Environment) {
|
||||
ret: StaticType::Float,
|
||||
},
|
||||
]);
|
||||
env.register_native("*", mul_ty, Purity::Pure, |args| {
|
||||
env.register_native_fn("*", mul_ty, Purity::Pure, |args| {
|
||||
if args.len() == 2 {
|
||||
match (&args[0], &args[1]) {
|
||||
(Value::Int(a), Value::Int(b)) => Value::Int(a * b),
|
||||
@@ -189,7 +189,7 @@ fn register_arithmetic(env: &Environment) {
|
||||
ret: StaticType::Float,
|
||||
},
|
||||
]);
|
||||
env.register_native("/", div_ty, Purity::Pure, |args| {
|
||||
env.register_native_fn("/", div_ty, Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -215,7 +215,7 @@ fn register_arithmetic(env: &Environment) {
|
||||
params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]),
|
||||
ret: StaticType::Int,
|
||||
}));
|
||||
env.register_native("//", int_div_ty, Purity::Pure, |args| {
|
||||
env.register_native_fn("//", int_div_ty, Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -238,7 +238,7 @@ fn register_arithmetic(env: &Environment) {
|
||||
params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]),
|
||||
ret: StaticType::Int,
|
||||
}));
|
||||
env.register_native("%", mod_ty, Purity::Pure, |args| {
|
||||
env.register_native_fn("%", mod_ty, Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -272,7 +272,7 @@ fn register_comparison(env: &Environment) {
|
||||
]);
|
||||
|
||||
// --- Greater Than (>) ---
|
||||
env.register_native(">", cmp_ty.clone(), Purity::Pure, |args| {
|
||||
env.register_native_fn(">", cmp_ty.clone(), Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -287,7 +287,7 @@ fn register_comparison(env: &Environment) {
|
||||
});
|
||||
|
||||
// --- Less Than (<) ---
|
||||
env.register_native("<", cmp_ty.clone(), Purity::Pure, |args| {
|
||||
env.register_native_fn("<", cmp_ty.clone(), Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -302,7 +302,7 @@ fn register_comparison(env: &Environment) {
|
||||
});
|
||||
|
||||
// --- Greater Or Equal (>=) ---
|
||||
env.register_native(">=", cmp_ty.clone(), Purity::Pure, |args| {
|
||||
env.register_native_fn(">=", cmp_ty.clone(), Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -317,7 +317,7 @@ fn register_comparison(env: &Environment) {
|
||||
});
|
||||
|
||||
// --- Less Or Equal (<=) ---
|
||||
env.register_native("<=", cmp_ty.clone(), Purity::Pure, |args| {
|
||||
env.register_native_fn("<=", cmp_ty.clone(), Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -336,7 +336,7 @@ fn register_comparison(env: &Environment) {
|
||||
params: StaticType::Tuple(vec![StaticType::Any, StaticType::Any]),
|
||||
ret: StaticType::Bool,
|
||||
}));
|
||||
env.register_native("=", eq_ty.clone(), Purity::Pure, |args| {
|
||||
env.register_native_fn("=", eq_ty.clone(), Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -357,7 +357,7 @@ fn register_comparison(env: &Environment) {
|
||||
});
|
||||
|
||||
// --- Not Equal (<>) ---
|
||||
env.register_native("<>", eq_ty, Purity::Pure, |args| {
|
||||
env.register_native_fn("<>", eq_ty, Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -388,7 +388,7 @@ fn register_logic(env: &Environment) {
|
||||
ret: StaticType::Int,
|
||||
},
|
||||
]);
|
||||
env.register_native("not", not_ty, Purity::Pure, |args| {
|
||||
env.register_native_fn("not", not_ty, Purity::Pure, |args| {
|
||||
if args.len() != 1 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -410,7 +410,7 @@ fn register_logic(env: &Environment) {
|
||||
ret: StaticType::Int,
|
||||
},
|
||||
]);
|
||||
env.register_native("and", logic_op_ty.clone(), Purity::Pure, |args| {
|
||||
env.register_native_fn("and", logic_op_ty.clone(), Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -422,7 +422,7 @@ fn register_logic(env: &Environment) {
|
||||
});
|
||||
|
||||
// --- Or (or) ---
|
||||
env.register_native("or", logic_op_ty.clone(), Purity::Pure, |args| {
|
||||
env.register_native_fn("or", logic_op_ty.clone(), Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -434,7 +434,7 @@ fn register_logic(env: &Environment) {
|
||||
});
|
||||
|
||||
// --- Xor (xor) ---
|
||||
env.register_native("xor", logic_op_ty.clone(), Purity::Pure, |args| {
|
||||
env.register_native_fn("xor", logic_op_ty.clone(), Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -450,7 +450,7 @@ fn register_logic(env: &Environment) {
|
||||
params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]),
|
||||
ret: StaticType::Int,
|
||||
}));
|
||||
env.register_native("<<", shift_ty.clone(), Purity::Pure, |args| {
|
||||
env.register_native_fn("<<", shift_ty.clone(), Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -461,7 +461,7 @@ fn register_logic(env: &Environment) {
|
||||
});
|
||||
|
||||
// --- Shift Right (>>) ---
|
||||
env.register_native(">>", shift_ty, Purity::Pure, |args| {
|
||||
env.register_native_fn(">>", shift_ty, Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ pub fn register(env: &Environment) {
|
||||
ret: StaticType::DateTime,
|
||||
}));
|
||||
|
||||
env.register_native("date", date_ty, Purity::Pure, |args| {
|
||||
env.register_native_fn("date", date_ty, Purity::Pure, |args| {
|
||||
if let Value::Text(s) = &args[0] {
|
||||
// Try parse YYYY-MM-DD
|
||||
if let Ok(dt) = NaiveDate::parse_from_str(s, "%Y-%m-%d") {
|
||||
@@ -32,7 +32,7 @@ pub fn register(env: &Environment) {
|
||||
ret: StaticType::DateTime,
|
||||
}));
|
||||
|
||||
env.register_native("now", now_ty, Purity::SideEffectFree, |_| {
|
||||
env.register_native_fn("now", now_ty, Purity::SideEffectFree, |_| {
|
||||
let ts = chrono::Utc::now().timestamp_millis();
|
||||
Value::DateTime(ts)
|
||||
});
|
||||
|
||||
+8
-8
@@ -22,7 +22,7 @@ pub fn register(env: &Environment) {
|
||||
params: StaticType::Tuple(vec![StaticType::Float]),
|
||||
ret: StaticType::Float,
|
||||
}));
|
||||
env.register_native(name, ty, Purity::Pure, move |args| {
|
||||
env.register_native_fn(name, ty, Purity::Pure, move |args| {
|
||||
if let Some(x) = to_f64(&args[0]) {
|
||||
Value::Float(f(x))
|
||||
} else {
|
||||
@@ -37,7 +37,7 @@ pub fn register(env: &Environment) {
|
||||
params: StaticType::Tuple(vec![StaticType::Float]),
|
||||
ret: StaticType::Int,
|
||||
}));
|
||||
env.register_native(name, ty, Purity::Pure, move |args| {
|
||||
env.register_native_fn(name, ty, Purity::Pure, move |args| {
|
||||
if let Some(x) = to_f64(&args[0]) {
|
||||
Value::Int(f(x) as i64)
|
||||
} else {
|
||||
@@ -70,7 +70,7 @@ pub fn register(env: &Environment) {
|
||||
params: StaticType::Tuple(vec![StaticType::Float, StaticType::Float]),
|
||||
ret: StaticType::Float,
|
||||
}));
|
||||
env.register_native(name, ty, Purity::Pure, move |args| {
|
||||
env.register_native_fn(name, ty, Purity::Pure, move |args| {
|
||||
if let (Some(a), Some(b)) = (to_f64(&args[0]), to_f64(&args[1])) {
|
||||
Value::Float(f(a, b))
|
||||
} else {
|
||||
@@ -88,7 +88,7 @@ pub fn register(env: &Environment) {
|
||||
params: StaticType::Tuple(vec![StaticType::Any]),
|
||||
ret: StaticType::Any,
|
||||
}));
|
||||
env.register_native("abs", abs_ty, Purity::Pure, |args| {
|
||||
env.register_native_fn("abs", abs_ty, Purity::Pure, |args| {
|
||||
if args.len() != 1 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -105,7 +105,7 @@ pub fn register(env: &Environment) {
|
||||
ret: StaticType::Any,
|
||||
}));
|
||||
|
||||
env.register_native("min", variadic_ty.clone(), Purity::Pure, |args| {
|
||||
env.register_native_fn("min", variadic_ty.clone(), Purity::Pure, |args| {
|
||||
if args.is_empty() {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -120,7 +120,7 @@ pub fn register(env: &Environment) {
|
||||
best
|
||||
});
|
||||
|
||||
env.register_native("max", variadic_ty, Purity::Pure, |args| {
|
||||
env.register_native_fn("max", variadic_ty, Purity::Pure, |args| {
|
||||
if args.is_empty() {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -142,7 +142,7 @@ pub fn register(env: &Environment) {
|
||||
}));
|
||||
|
||||
let prng = env.prng.clone();
|
||||
env.register_native("random", random_ty, Purity::SideEffectFree, move |_| {
|
||||
env.register_native_fn("random", random_ty, Purity::SideEffectFree, move |_| {
|
||||
Value::Float(prng.borrow_mut().f64())
|
||||
});
|
||||
|
||||
@@ -152,7 +152,7 @@ pub fn register(env: &Environment) {
|
||||
}));
|
||||
|
||||
let prng_seed = env.prng.clone();
|
||||
env.register_native("seed!", seed_ty, Purity::Impure, move |args| {
|
||||
env.register_native_fn("seed!", seed_ty, Purity::Impure, move |args| {
|
||||
if let Value::Int(s) = args[0] {
|
||||
prng_seed.borrow_mut().seed(s as u64);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user