手写token解析器、语法解析器、LLVM IR生成器(GO语言)

时间:2022-12-11 16:15:18

最近开始尝试用go写点东西,正好在看LLVM的资料,就写了点相关的内容 - 前端解析器+中间代码生成(本地代码的汇编、执行则靠LLVM工具链完成)

https://github.com/daibinhua888/toy-compiler

go语言用下来感觉还好,类的实现比较面向过程,但还是OO的,package的用法import是基于文件夹相对路径的,go get安装这些要看具体库(如果和c强相关,就比较痛苦,要装一堆gcc、cmake之类的工具,比如llvm)

用go写的玩具编译器,最终目标是后端套用LLVM生成本机字节码

#Code:

parseCode("def test()")
parseCode("def test(p1,p2)")
parseCode("a1")
parseCode("1+1")
parseCode("p1(a,b)+p2(a,b)")
parseCode("1+(2+3)")
parseCode("def test(p1,p2) p1(a,b)+p2(a,b)")
parseCode("def showMessge(msg) toy_print(msg)")
parseCode("showMessge('test')")

#AST:

command>def test(), 解析AST:def-()-(SIG)test()Body Expr: empty-()
command>def test(p1,p2), 解析AST:def-()-(SIG)test(p1,p2,)Body Expr: empty-()
command>a1, 解析AST:identifier-a1()
command>1+1, 解析AST:Operator-(+)(LHS: 1)numeric-()---(RHS: 1)numeric-()---
command>p1(a,b)+p2(a,b), 解析AST:Operator-(+)(LHS: 0)identifier-p1()-(CALL)p1(a,b,)---(RHS: 0)identifier-p2()-(CALL)p2(a,b,)---
command>1+(2+3), 解析AST:Operator-(+)(LHS: 1)numeric-()---(RHS: 0)Operator-(+)(LHS: 2)numeric-()---(RHS: 3)numeric-()------
command>def test(p1,p2) p1(a,b)+p2(a,b), 解析AST:def-()-(SIG)test(p1,p2,)Body Expr: Operator-(+)(LHS: 0)identifier-p1()-(CALL)p1(a,b,)---(RHS: 0)identifier-p2()-(CALL)p2(a,b,)---
command>def showMessge(msg) toy_print(msg), 解析AST:def-()-(SIG)showMessge(msg,)Body Expr: identifier-toy_print()-(CALL)toy_print(msg,)
command>showMessge('test'), 解析AST:identifier-showMessge()-(CALL)showMessge('test',) *********RUN CODE**********
CODE 2 RUN>def showMessge(msg) toy_print(msg)
CODE 2 RUN>showMessge('test')

#LLVM IR:

declare i32 @puts(i8* nocapture) nounwind

@.test = private unnamed_addr constant [6 x i8] c"test\0A\00"

define void @showMessge(){
; %cast210 = getelementptr [6 x i8], [6 x i8]* @.test, i64 0, i64 0
call i32 @puts(i8* %cast210) ret void
} define i32 @main(){
; call void @showMessge()
ret i32 0
}

#llc

>>lli code.ll
>>test