forth

    可嵌入、具执行预算的 Forth 子集

    Download zip
    Author
    Version
    0.19.0
    License
    MIT
    Last updated
    7 days ago
    Downloads
    2

    #可执行 API 示例

    增加 nip/tuck/2dup/2drop/2swap 标准栈操作及下溢检查。这些例子调用公开 API,并随 moon test 执行。

    ///|
    test "extended standard stack words" {
    let m = @forth.Machine::new()
    m.eval("1 2 2dup 2swap 2drop nip")
    assert_eq(m.values(), [2])
    let t = @forth.Machine::new()
    t.eval("1 2 tuck")
    assert_eq(t.values(), [2, 1, 2])
    assert_true(
    try {
    let x = @forth.Machine::new()
    x.eval("2dup")
    false
    } catch {
    _ => true
    },
    )
    }

    限制:不是完整 ANS Forth;词汇、内存模型和控制流仍为子集。

    ForthError

    pub suberror ForthError {
    Invalid(String)
    Thrown(Int)
    } derive(
    Debug
    )

    ForthError::code

    fn ForthError::code(self : ForthError) -> Int

    Machine

    pub struct Machine {
    // private fields
    } derive(
    Debug
    )

    Machine::clear_output

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

    Machine::eval

    fn Machine::eval(self : Machine, source : String, budget? : Int) -> Unit raise ForthError

    State changes before an error are retained. Integer arithmetic uses 32-bit cells.

    Machine::feed

    fn Machine::feed(self : Machine, source : String, budget? : Int) -> Bool raise ForthError

    Feed a complete input buffer, keeping an unfinished definition for the next call.

    Machine::is_compiling

    fn Machine::is_compiling(self : Machine) -> Bool

    Machine::new

    fn Machine::new() -> Machine

    Machine::output_bytes

    fn Machine::output_bytes(self : Machine) -> Bytes

    Machine::output_text

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

    Machine::printed

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

    Machine::values

    fn Machine::values(self : Machine) -> Array[Int]