From bb5a762792426fd40bb063409b7cc92e4c10ae57 Mon Sep 17 00:00:00 2001 From: nthnn Date: Sat, 19 Aug 2023 02:25:56 +0800 Subject: [PATCH] AST implementation for return statement. --- src/ast_stmt.ts | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/ast_stmt.ts b/src/ast_stmt.ts index ccf514f..f64b9ba 100644 --- a/src/ast_stmt.ts +++ b/src/ast_stmt.ts @@ -5,7 +5,9 @@ import { BasicBlock, Constant, FunctionType, - ConstantInt + ConstantInt, + Value, + ReturnInst } from "llvm-bindings"; import { @@ -122,6 +124,47 @@ class StmtASTRender implements StatementAST { } } +class StmtASTReturn implements StatementAST { + private mark: Token; + private hasValue: boolean; + private value?: ExpressionAST; + + public constructor( + mark: Token, + hasValue: boolean, + value?: ExpressionAST + ) { + this.mark = mark; + this.hasValue = hasValue; + this.value = value; + } + + public visit( + builder: IRBuilder, + module: Module + ): void { + if(this.hasValue) + builder.CreateRet( + this.value?.visit( + builder, + module + ) as Value + ); + else builder.CreateRetVoid(); + } + + public resolve( + results: ASTResolveResults + ): void { + if(this.hasValue) + this.value?.resolve(results); + } + + public marker(): Token { + return this.mark; + } +} + export { StmtASTMain, StmtASTRender