feat: implement Random and Time built-in effects
Add Random and Time effects for random number generation and time-based operations. These effects can be used in any effectful code block. Random effect operations: - Random.int(min, max) - random integer in range [min, max] - Random.float() - random float in range [0.0, 1.0) - Random.bool() - random boolean Time effect operations: - Time.now() - current Unix timestamp in milliseconds - Time.sleep(ms) - sleep for specified milliseconds Changes: - Add rand crate dependency - Add Random and Time effect definitions to types.rs - Add effects to built-in effects list in typechecker - Implement effect handlers in interpreter - Add 4 new tests for Random and Time effects - Add examples/random.lux demonstrating usage Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
44
src/main.rs
44
src/main.rs
@@ -1248,6 +1248,50 @@ c")"#;
|
||||
assert_eq!(result, "5");
|
||||
assert_eq!(final_state, "6");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_random_int() {
|
||||
let source = r#"
|
||||
fn getRandomInt(): Int with {Random} = Random.int(1, 10)
|
||||
let result = run getRandomInt() with {}
|
||||
"#;
|
||||
let (result, _) = run_with_effects(source, Value::Unit, Value::Unit).unwrap();
|
||||
let num: i64 = result.parse().expect("Should be an integer");
|
||||
assert!(num >= 1 && num <= 10, "Random int should be in range 1-10, got {}", num);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_random_float() {
|
||||
let source = r#"
|
||||
fn getRandomFloat(): Float with {Random} = Random.float()
|
||||
let result = run getRandomFloat() with {}
|
||||
"#;
|
||||
let (result, _) = run_with_effects(source, Value::Unit, Value::Unit).unwrap();
|
||||
let num: f64 = result.parse().expect("Should be a float");
|
||||
assert!(num >= 0.0 && num < 1.0, "Random float should be in range [0, 1), got {}", num);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_random_bool() {
|
||||
let source = r#"
|
||||
fn getRandomBool(): Bool with {Random} = Random.bool()
|
||||
let result = run getRandomBool() with {}
|
||||
"#;
|
||||
let (result, _) = run_with_effects(source, Value::Unit, Value::Unit).unwrap();
|
||||
assert!(result == "true" || result == "false", "Random bool should be true or false, got {}", result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_time_now() {
|
||||
let source = r#"
|
||||
fn getTime(): Int with {Time} = Time.now()
|
||||
let result = run getTime() with {}
|
||||
"#;
|
||||
let (result, _) = run_with_effects(source, Value::Unit, Value::Unit).unwrap();
|
||||
let timestamp: i64 = result.parse().expect("Should be a timestamp");
|
||||
// Timestamp should be a reasonable Unix time in milliseconds (after 2020)
|
||||
assert!(timestamp > 1577836800000, "Timestamp should be after 2020");
|
||||
}
|
||||
}
|
||||
|
||||
// Diagnostic rendering tests
|
||||
|
||||
Reference in New Issue
Block a user