tcl

    Tcl 脚本解释器本地候选

    Download zip
    Version
    0.17.0
    License
    MIT AND Unicode-3.0
    Last updated
    4 hours ago
    Downloads
    1

    #可执行 API 示例

    增加 proc、默认/可变参数、局部变量、共享输出、递归与 return。这些例子调用公开 API,并随 moon test 执行。

    ///|
    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
    },
    )
    }

    已实现 namespace export/import/forget/origin/path/upvar/unknown 和 ensemble 的部分能力;完整 Tcl 兼容、网络/进程/事件 I/O 与扩展生态仍未完成。各命令的覆盖范围和资源限制见 README.mdFEATURES.md

    #过程、列表和通用循环

    ///|
    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")
    }

    TclError

    pub suberror TclError {
    Invalid(String)
    Return(String)
    Break
    Continue
    Signal(Completion)
    } derive(
    Debug
    )

    ArrayObject

    type ArrayObject derive(
    Debug
    )

    Binding

    CacheStats

    pub struct CacheStats {
    scripts : Int
    expressions : Int
    source_units : Int
    script_hits : Int
    expression_hits : Int
    } derive(
    Debug
    )

    Command

    CommandBody

    type CommandBody derive(
    Debug
    )

    Completion

    type Completion derive(
    Debug
    )

    DictObject

    type DictObject derive(
    Debug
    )

    Ensemble

    Evaluation

    pub struct Evaluation {
    code : Int
    result : String
    options : String
    } derive(
    Debug
    )

    Handler

    Interpreter

    pub struct Interpreter {
    state : State
    frame : Frame
    output : Array[String]
    output_size : Array[Int]
    budget :
    Ref
    [Int]
    } derive(
    Debug
    )

    Interpreter::cache_stats

    fn Interpreter::cache_stats(self : Interpreter) -> CacheStats

    Interpreter::clear_cache

    fn Interpreter::clear_cache(self : Interpreter) -> Unit

    Interpreter::clear_output

    fn Interpreter::clear_output(self : Interpreter) -> Unit

    Interpreter::eval

    fn Interpreter::eval(self : Interpreter, source : String, budget? : Int) -> String raise TclError

    Interpreter::eval_catch

    fn Interpreter::eval_catch(self : Interpreter, source : String, budget? : Int) -> Evaluation raise TclError

    Interpreter::new

    Interpreter::output_text

    fn Interpreter::output_text(self : Interpreter) -> String

    Interpreter::printed

    fn Interpreter::printed(self : Interpreter) -> String

    Interpreter::set_io

    fn Interpreter::set_io(self : Interpreter, callback : (String, Array[String]) -> Result[String, String]) -> Unit

    Install a synchronous host filesystem/channel adapter. The core never accesses operating-system files unless the embedding application supplies it.

    ListObject

    type ListObject derive(
    Debug
    )

    ParseCache

    type ParseCache derive(
    Debug
    )

    ParsedCommand

    type ParsedCommand derive(
    Debug
    )

    Procedure

    type Procedure derive(
    Debug
    )

    ScriptParser

    type ScriptParser derive(
    Debug
    )

    ScriptProgram

    type ScriptProgram derive(
    Debug
    )

    TclValue

    ValuePayload

    type ValuePayload derive(
    Debug
    )

    VariableValue

    type VariableValue derive(
    Debug
    )

    format_list

    fn format_list(values : Array[String]) -> String raise TclError

    Construct a Tcl list with escaping that preserves every input element.

    is_complete

    fn is_complete(source : String) -> Bool raise TclError

    parse_list

    fn parse_list(text : String) -> Array[String] raise TclError

    Parse Tcl lists without variable or command substitution.