Skip to content

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

Notifications You must be signed in to change notification settings

daibinhua888/toy-compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

用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(), 解析ASTdef-()-(SIG)test()Body Expr: empty-()
command>def test(p1,p2), 解析ASTdef-()-(SIG)test(p1,p2,)Body Expr: empty-()
command>a1, 解析ASTidentifier-a1()
command>1+1, 解析ASTOperator-(+)(LHS: 1)numeric-()---(RHS: 1)numeric-()---
command>p1(a,b)+p2(a,b), 解析ASTOperator-(+)(LHS: 0)identifier-p1()-(CALL)p1(a,b,)---(RHS: 0)identifier-p2()-(CALL)p2(a,b,)---
command>1+(2+3), 解析ASTOperator-(+)(LHS: 1)numeric-()---(RHS: 0)Operator-(+)(LHS: 2)numeric-()---(RHS: 3)numeric-()------
command>def test(p1,p2) p1(a,b)+p2(a,b), 解析ASTdef-()-(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), 解析ASTdef-()-(SIG)showMessge(msg,)Body Expr: identifier-toy_print()-(CALL)toy_print(msg,)
command>showMessge('test'), 解析ASTidentifier-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
}

#lli

>>lli code.ll
>>test

About

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

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages