手寫token解析器、語法解析器、LLVM IR生成器(GO語言)


最近開始嘗試用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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM