All Projects → chai2010 → tinylang

chai2010 / tinylang

Licence: other
Tiny玩具语言(Go语言实现/包含Tiny编译器/CASL汇编器/COMET虚拟机/调试器/支持WebAssembly/LLVM)

Programming Languages

go
31211 projects - #10 most used programming language
c
50402 projects - #5 most used programming language
Yacc
648 projects
LLVM
166 projects
Makefile
30231 projects

Projects that are alternatives of or similar to tinylang

Go Ast Book
📚 《Go语法树入门——开启自制编程语言和编译器之旅》(开源免费图书/Go语言进阶/掌握抽象语法树/Go语言AST/凹语言)
Stars: ✭ 4,034 (+6513.11%)
Mutual labels:  llvm, goyacc
EmbedSanitizer
EmbedSantizer is a runtime race detection tool which extends ThreadSanitizer to detect data races in 32-bit ARM applications.
Stars: ✭ 16 (-73.77%)
Mutual labels:  llvm
Lesma
The Lesma Programming Language
Stars: ✭ 14 (-77.05%)
Mutual labels:  llvm
clangbuilder
Building Clang ♡ Utility and Environment
Stars: ✭ 101 (+65.57%)
Mutual labels:  llvm
NatsuLang
No description or website provided.
Stars: ✭ 96 (+57.38%)
Mutual labels:  llvm
nballerina-cpp
Ballerina compiler backend that generates platform-specific executables.
Stars: ✭ 16 (-73.77%)
Mutual labels:  llvm
EffectiveSan
Runtime type and bounds-error checking for C/C++
Stars: ✭ 95 (+55.74%)
Mutual labels:  llvm
geode
The Geode Programming Language
Stars: ✭ 16 (-73.77%)
Mutual labels:  llvm
vuo
A realtime visual programming language for interactive media.
Stars: ✭ 103 (+68.85%)
Mutual labels:  llvm
linux
Linux kernel source tree
Stars: ✭ 234 (+283.61%)
Mutual labels:  llvm
neo-c
Yet another modern compiler. It is also C compiler. Using LLVM wih boehmGC
Stars: ✭ 72 (+18.03%)
Mutual labels:  llvm
suicide
LLVM pass that detects one undefined behavior, and emits code to delete your hard drive
Stars: ✭ 33 (-45.9%)
Mutual labels:  llvm
qbicc
Experimental static compiler for Java programs.
Stars: ✭ 118 (+93.44%)
Mutual labels:  llvm
expressi
Expression-oriented toy programming language written in Rust
Stars: ✭ 17 (-72.13%)
Mutual labels:  llvm
homebrew-llvm
LLVM formulae for the Homebrew package manager
Stars: ✭ 23 (-62.3%)
Mutual labels:  llvm
SCAF
A Speculation-Aware Collaborative Dependence Analysis Framework
Stars: ✭ 25 (-59.02%)
Mutual labels:  llvm
TON-Compiler
Clang compiler for Free TON Virtual Machine
Stars: ✭ 56 (-8.2%)
Mutual labels:  llvm
nocc
A LLVM based self-hosting C compiler
Stars: ✭ 22 (-63.93%)
Mutual labels:  llvm
mlir-standalone-template
An out-of-tree MLIR dialect template.
Stars: ✭ 44 (-27.87%)
Mutual labels:  llvm
llrl
An experimental Lisp-like programming language
Stars: ✭ 126 (+106.56%)
Mutual labels:  llvm

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

版权

保留所有权利。

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].