Skip to content

Commit

Permalink
AST implementation for return statement.
Browse files Browse the repository at this point in the history
  • Loading branch information
nthnn committed Aug 18, 2023
1 parent 0369999 commit bb5a762
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion src/ast_stmt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
BasicBlock,
Constant,
FunctionType,
ConstantInt
ConstantInt,
Value,
ReturnInst
} from "llvm-bindings";

import {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bb5a762

Please sign in to comment.