This commit is contained in:
Rafał Grodziński
2025-06-24 23:43:01 +09:00
parent a6a918bc58
commit ac9205e23e
4 changed files with 38 additions and 1 deletions

View File

@@ -12,6 +12,7 @@
#include "Parser/Statement/StatementVariable.h"
#include "Parser/Statement/StatementReturn.h"
#include "Parser/Statement/StatementExpression.h"
#include "Parser/Statement/StatementLoop.h"
#include "Parser/Statement/StatementMetaExternFunction.h"
#include "Parser/Statement/StatementBlock.h"
@@ -49,6 +50,9 @@ void ModuleBuilder::buildStatement(shared_ptr<Statement> statement) {
case StatementKind::RETURN:
buildReturn(dynamic_pointer_cast<StatementReturn>(statement));
break;
case StatementKind::LOOP:
buildLoop(dynamic_pointer_cast<StatementLoop>(statement));
break;
case StatementKind::META_EXTERN_FUNCTION:
buildMetaExternFunction(dynamic_pointer_cast<StatementMetaExternFunction>(statement));
break;
@@ -121,6 +125,29 @@ void ModuleBuilder::buildReturn(shared_ptr<StatementReturn> statement) {
}
}
void ModuleBuilder::buildLoop(shared_ptr<StatementLoop> statement) {
shared_ptr<Statement> initStatement = statement->getInitStatement();
shared_ptr<Expression> preExpression = statement->getPreConditionExpression();
shared_ptr<Expression> postExpression = statement->getPostConditionExpression();
shared_ptr<StatementBlock> bodyStatement= statement->getBodyBlockStatement();
llvm::BasicBlock *parentBlock = builder->GetInsertBlock();
if (initStatement != nullptr)
buildStatement(statement->getInitStatement());
llvm::Function *fun = builder->GetInsertBlock()->getParent();
llvm::BasicBlock *bodyBlock = llvm::BasicBlock::Create(*context, "loopBody", fun);
builder->SetInsertPoint(bodyBlock);
buildBlock(bodyStatement);
builder->CreateBr(bodyBlock);
//llvm::BasicBlock *loopEndBlock = builder->GetInsertBlock()
builder->SetInsertPoint(parentBlock);
}
void ModuleBuilder::buildMetaExternFunction(shared_ptr<StatementMetaExternFunction> statement) {
// get argument types
vector<llvm::Type *> types;

View File

@@ -26,6 +26,7 @@ class StatementFunction;
class StatementVariable;
class StatementReturn;
class StatementExpression;
class StatementLoop;
class StatementMetaExternFunction;
class StatementBlock;
@@ -54,6 +55,7 @@ private:
void buildVarDeclaration(shared_ptr<StatementVariable> statement);
void buildBlock(shared_ptr<StatementBlock> statement);
void buildReturn(shared_ptr<StatementReturn> statement);
void buildLoop(shared_ptr<StatementLoop> statement);
void buildMetaExternFunction(shared_ptr<StatementMetaExternFunction> statement);
void buildExpression(shared_ptr<StatementExpression> statement);

View File

@@ -194,7 +194,7 @@ shared_ptr<Statement> Parser::matchStatementLoop() {
// pre condition
shared_ptr<Expression> preConditionExpression = nextExpression();
if (preConditionExpression == nullptr || !preConditionExpression->isValid())
if (preConditionExpression != nullptr && !preConditionExpression->isValid())
return matchStatementInvalid("Expected pre-condition expression");
// post condition

8
test.brc Normal file
View File

@@ -0,0 +1,8 @@
test fun
/*rep i sint32 <- 0, i < 10
//i sint32 <- i + 1
;*/
rep i sint32 <- 13
abc sint32 <- 5
;
;