feat: usable?

This commit is contained in:
2025-05-05 11:31:46 +08:00
parent eea4a4ce9f
commit b9dcc83c39
24 changed files with 688 additions and 244 deletions

View File

@@ -2,8 +2,8 @@ use ecow::EcoString;
use rpds::{ht_map_sync, vector_sync};
use crate::compile::compile;
use crate::ty::public::*;
use crate::ty::common::Symbol;
use crate::ty::public::*;
use super::vm::run;
@@ -14,6 +14,12 @@ fn test_expr(expr: &str, expected: Value) {
assert_eq!(run(prog).unwrap(), expected);
}
macro_rules! thunk {
() => {
Value::Thunk
};
}
macro_rules! int {
($e:expr) => {
Value::Const(Const::Int($e))
@@ -125,7 +131,7 @@ fn test_attrs() {
test_expr(
"{ a = 1; }",
attrs! {
symbol!("a") => int!(1)
symbol!("a") => thunk!()
},
);
test_expr("{ a = 1; }.a", int!(1));
@@ -133,18 +139,18 @@ fn test_attrs() {
test_expr(
"{ a = { a = 1; }; }.a",
attrs! {
symbol!("a") => int!(1)
symbol!("a") => thunk!()
},
);
test_expr("{ a.b = 1; }.a.b", int!(1));
test_expr(
"{ a.b = 1; a.c = 2; }",
attrs! { symbol!("a") => attrs!{ symbol!("b") => int!(1), symbol!("c") => int!(2) } },
attrs! { symbol!("a") => attrs!{ symbol!("b") => thunk!(), symbol!("c") => thunk!() } },
);
test_expr("{ a.b = 1; } ? a.b", boolean!(true));
test_expr(
"{ a.b = 1; } // { a.c = 2 }",
attrs! { symbol!("a") => attrs!{ symbol!("c") => int!(2) } },
attrs! { symbol!("a") => attrs!{ symbol!("c") => thunk!() } },
);
}
@@ -161,10 +167,11 @@ fn test_with() {
#[test]
fn test_let() {
test_expr(r#"let a = 1; in a"#, int!(1));
test_expr(r#"let a = 1; b = a; in b"#, int!(1));
test_expr(r#"let a = { a = 1; }; b = "a"; in a.${b}"#, int!(1));
test_expr(
r#"let b = "c"; in { a.b = 1; } // { a."a${b}" = 2 }"}"#,
attrs! { symbol!("a") => attrs!{ symbol!("ac") => int!(2) } },
r#"let b = "c"; in { a.b = 1; } // { a."a${b}" = 2; }"#,
attrs! { symbol!("a") => attrs!{ symbol!("ac") => thunk!() } },
);
}
@@ -175,5 +182,8 @@ fn test_func() {
test_expr("(x: y: x + y) 1 1", int!(2));
test_expr("({ x, y }: x + y) { x = 1; y = 2; }", int!(3));
test_expr("({ x, y, ... }: x + y) { x = 1; y = 2; z = 3; }", int!(3));
test_expr("(inputs@{ x, y, ... }: x + inputs.y) { x = 1; y = 2; z = 3; }", int!(3));
test_expr(
"(inputs@{ x, y, ... }: x + inputs.y) { x = 1; y = 2; z = 3; }",
int!(3),
);
}