MCPcopy Index your code
hub / github.com/chai2010/tinylang

github.com/chai2010/tinylang @v1.4.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.4.0 ↗ · + Follow
577 symbols 1,254 edges 63 files 242 documented · 42%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Tiny玩具语言(Go语言版)

Tiny语言是《编译原理及实践》书中定义的玩具语言。

这里是Go语言实现(注释采用Go语言风格)。

实现原理:

例子

以下的例子计算1到n之和:

// sum = 1 + 2 + ... + n

read n;
if 0 < n then
  sum := 0;
  repeat
    sum := sum + n;
    n := n - 1
  until n = 0;
  write sum
end

运行tiny程序:

$ tinylang sum.tiny 
100
5050

也可以通过-ast查看生成的语法树,通过-casl查看生成的CASL汇编程序,或通过-debug调试执行:

WebAssembly 支持

切换到 wasm 目录, 通过以下命令执行 hello.tiny:

$ go run main.go hello.tiny
READ: 100
5050

READ 表示需要输入一个整数, 这里的 100 表示计算 1 到 100 的和, 结果是 5050.

通过 -o 参数输出 WebAssembly 二进制模块:

$ go run main.go -o hello.wasm hello.tiny

实用 wasm2wat 将二进制格式转换为文本格式:

$ wasm2wat hello.wasm > hello.wast

输出的 hello.wast 内容如下:

(module $tinylang
  (type (;0;) (func (result i32)))
  (type (;1;) (func (param i32)))
  (type (;2;) (func))
  (import "env" "__tiny_read" (func (;0;) (type 0)))
  (import "env" "__tiny_write" (func (;1;) (type 1)))
  (func $_start (type 2)
    call 0
    set_global 1
    i32.const 0
    get_global 1
    i32.lt_s
    if  ;; label = @1
      i32.const 0
      set_global 2
      loop  ;; label = @2
        get_global 2
        get_global 1
        i32.add
        set_global 2
        get_global 1
        i32.const 1
        i32.sub
        set_global 1
        get_global 1
        i32.const 0
        i32.eq
        i32.eqz
        br_if 0 (;@2;)
      end
      get_global 2
      call 1
    end)
  (memory (;0;) 1 1)
  (global (;0;) i32 (i32.const 0))
  (global (;1;) (mut i32) (i32.const 0))
  (global (;2;) (mut i32) (i32.const 0))
  (export "memory" (memory 0))
  (export "_start" (func $_start)))

如果要单独执行输出的 WebAssembly 模块, 需要注入 __tiny_read__tiny_write 函数。

LLVM 支持

假设在 darwin/amd64 环境, 切换到 llvm 目录, 通过以下命令将 hello.tiny 编译为 LLVM-IR 格式:

$ go run main.go hello.tiny > a.out.ll

然后用Clang命令将 LLVM-IR 文件编译为本地课执行程序:

$ clang -Wno-override-module -o a.out.exe a.out.ll

执行输出的 a.out.exe 计算 1 到 100 的和, 结果是 5050.

$ ./a.out.exe 
READ: 100
5050

版权

保留所有权利。

Extension points exported contracts — how you extend this code

Instruction (Interface)
Instruction represents a single WASM instruction. [15 implementers]
wasm/pkg/wasm/instruction/instruction.go
Node (Interface)
Node 表示一个语法树节点. [13 implementers]
tiny/ast/ast.go
ImportDescriptor (Interface)
ImportDescriptor represents a WASM import descriptor. [4 implementers]
wasm/pkg/wasm/module/module.go
Stmt (Interface)
Stmt 表示一个语句节点. [6 implementers]
tiny/ast/ast.go
StructuredInstruction (Interface)
StructuredInstruction represents a structured control instruction like br_if. [3 implementers]
wasm/pkg/wasm/instruction/instruction.go
Expr (Interface)
Expr 表示一个表达式节点。 [4 implementers]
tiny/ast/ast.go

Core symbols most depended-on inside this repo

Pos
called by 40
tiny/ast/ast.go
emit
called by 35
tiny/compiler/compiler.go
writeByte
called by 34
wasm/pkg/wasm/encoding/writer.go
WriteVarUint32
called by 34
wasm/pkg/wasm/leb128/leb128.go
emitComment
called by 29
_tinylang-1.0.1/tiny/code.h
emitCASL
called by 28
_tinylang-1.0.1/tiny/code.h
yyDebugln
called by 27
tiny/parser/parser.go
ReadVarUint32
called by 25
wasm/pkg/wasm/leb128/leb128.go

Shape

Function 235
Method 209
Struct 106
Class 11
Interface 8
TypeAlias 8

Languages

Go83%
C++9%
C8%

Modules by API surface

wasm/pkg/wasm/module/module.go58 symbols
wasm/pkg/wasm/encoding/reader.go51 symbols
tiny/ast/ast.go48 symbols
wasm/pkg/wasm/instruction/numeric.go40 symbols
wasm/pkg/wasm/instruction/control.go34 symbols
wasm/pkg/wasm/encoding/writer.go30 symbols
tiny/ast/private.go23 symbols
_tinylang-1.0.1/casl/casl.c23 symbols
_tinylang-1.0.1/tiny/scan.h17 symbols
tiny/parser/tiny.y.go16 symbols
wasm/pkg/wasm/instruction/variable.go15 symbols
wasm/pkg/wasm/leb128/leb128.go14 symbols

For agents

$ claude mcp add tinylang \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact