Skip to content

Commit 88bdeb8

Browse files
author
Jonathan D.A. Jewell
committed
feat(stdlib): add Option/Result helper methods (is_some, is_none, is_ok, is_err)
Added helper builtins for checking Option and Result variants: - is_some(option) - returns true if Option is Some - is_none(option) - returns true if Option is None - is_ok(result) - returns true if Result is Ok - is_err(result) - returns true if Result is Err Test results improved: - Conformance tests: 20/32 → 22/32 passing (68.75%) - Valid tests: 62.5% → 68.75% pass rate Remaining 10 failures are mostly parse errors (pattern matching, struct literal syntax) rather than missing stdlib functions.
1 parent 89049c2 commit 88bdeb8

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

compiler/eclexia-interp/src/builtins.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,39 @@ pub fn register(env: &Environment) {
702702
func: builtin_shadow_price,
703703
}),
704704
);
705+
706+
// Option/Result helper methods (as standalone functions)
707+
env.define(
708+
SmolStr::new("is_some"),
709+
Value::Builtin(BuiltinFn {
710+
name: "is_some",
711+
func: builtin_is_some,
712+
}),
713+
);
714+
715+
env.define(
716+
SmolStr::new("is_none"),
717+
Value::Builtin(BuiltinFn {
718+
name: "is_none",
719+
func: builtin_is_none,
720+
}),
721+
);
722+
723+
env.define(
724+
SmolStr::new("is_ok"),
725+
Value::Builtin(BuiltinFn {
726+
name: "is_ok",
727+
func: builtin_is_ok,
728+
}),
729+
);
730+
731+
env.define(
732+
SmolStr::new("is_err"),
733+
Value::Builtin(BuiltinFn {
734+
name: "is_err",
735+
func: builtin_is_err,
736+
}),
737+
);
705738
}
706739

707740
fn builtin_println(args: &[Value]) -> RuntimeResult<Value> {
@@ -2425,6 +2458,62 @@ fn builtin_shadow_price(args: &[Value]) -> RuntimeResult<Value> {
24252458
Ok(Value::Float(price))
24262459
}
24272460

2461+
/// Check if an Option value is Some.
2462+
/// Usage: is_some(option_value)
2463+
fn builtin_is_some(args: &[Value]) -> RuntimeResult<Value> {
2464+
if args.len() != 1 {
2465+
return Err(RuntimeError::custom("is_some requires exactly 1 argument"));
2466+
}
2467+
2468+
match &args[0] {
2469+
Value::Struct { name, .. } if name.as_str() == "Some" => Ok(Value::Bool(true)),
2470+
Value::Struct { name, .. } if name.as_str() == "None" => Ok(Value::Bool(false)),
2471+
other => Err(RuntimeError::type_error("Option", other.type_name())),
2472+
}
2473+
}
2474+
2475+
/// Check if an Option value is None.
2476+
/// Usage: is_none(option_value)
2477+
fn builtin_is_none(args: &[Value]) -> RuntimeResult<Value> {
2478+
if args.len() != 1 {
2479+
return Err(RuntimeError::custom("is_none requires exactly 1 argument"));
2480+
}
2481+
2482+
match &args[0] {
2483+
Value::Struct { name, .. } if name.as_str() == "None" => Ok(Value::Bool(true)),
2484+
Value::Struct { name, .. } if name.as_str() == "Some" => Ok(Value::Bool(false)),
2485+
other => Err(RuntimeError::type_error("Option", other.type_name())),
2486+
}
2487+
}
2488+
2489+
/// Check if a Result value is Ok.
2490+
/// Usage: is_ok(result_value)
2491+
fn builtin_is_ok(args: &[Value]) -> RuntimeResult<Value> {
2492+
if args.len() != 1 {
2493+
return Err(RuntimeError::custom("is_ok requires exactly 1 argument"));
2494+
}
2495+
2496+
match &args[0] {
2497+
Value::Struct { name, .. } if name.as_str() == "Ok" => Ok(Value::Bool(true)),
2498+
Value::Struct { name, .. } if name.as_str() == "Err" => Ok(Value::Bool(false)),
2499+
other => Err(RuntimeError::type_error("Result", other.type_name())),
2500+
}
2501+
}
2502+
2503+
/// Check if a Result value is Err.
2504+
/// Usage: is_err(result_value)
2505+
fn builtin_is_err(args: &[Value]) -> RuntimeResult<Value> {
2506+
if args.len() != 1 {
2507+
return Err(RuntimeError::custom("is_err requires exactly 1 argument"));
2508+
}
2509+
2510+
match &args[0] {
2511+
Value::Struct { name, .. } if name.as_str() == "Err" => Ok(Value::Bool(true)),
2512+
Value::Struct { name, .. } if name.as_str() == "Ok" => Ok(Value::Bool(false)),
2513+
other => Err(RuntimeError::type_error("Result", other.type_name())),
2514+
}
2515+
}
2516+
24282517
// ============================================================================
24292518
// Tests for collection builtins
24302519
// ============================================================================

0 commit comments

Comments
 (0)