Tcl 脚本解释器本地候选
///|
test "procedures have local variables and shared output" {
let t = @tcl.Interpreter::new()
assert_eq(
t.eval("set x outer; proc twice {x} {expr {$x * 2}}; twice 21"),
"42",
)
assert_eq(t.eval("set x"), "outer")
assert_eq(
t.eval("proc greet {who} {puts $who; return done; missing}; greet hello"),
"done",
)
assert_eq(t.printed(), "hello")
}
///|
test "recursive factorial" {
let t = @tcl.Interpreter::new()
assert_eq(
t.eval(
"proc fact {n} {if {$n <= 1} {return 1}; expr {$n * [fact [expr {$n - 1}]]}}; fact 6",
),
"720",
)
}
///|
test "procedure errors and depth budgets" {
let t = @tcl.Interpreter::new()
assert_true(
try {
ignore(t.eval("proc p {{a b c}} {}"))
false
} catch {
_ => true
},
)
assert_true(
try {
ignore(t.eval("proc p {a} {}; p"))
false
} catch {
_ => true
},
)
assert_true(
try {
ignore(t.eval("proc p {} {p}; p", budget=20))
false
} catch {
_ => true
},
)
}///|
test "documented default arguments and foreach" {
let t = @tcl.Interpreter::new()
assert_eq(
t.eval(
"proc scale {values {factor 2}} {set result {}; foreach n $values {lappend result [expr {$n * $factor}]}; return $result}; scale {1 2 3}",
),
"2 4 6",
)
assert_eq(t.eval("scale {1 2 3} 3"), "3 6 9")
}pub suberror TclError {
Invalid(String)
Return(String)
Break
Continue
Signal(Completion)
} derive(Debug)pub struct CacheStats {
scripts : Int
expressions : Int
source_units : Int
script_hits : Int
expression_hits : Int
} derive(Debug)fn Interpreter::eval_catch(self : Interpreter, source : String, budget? : Int) -> Evaluation raise TclErrorfn Interpreter::set_io(self : Interpreter, callback : (String, Array[String]) -> Result[String, String]) -> Unit