diff --git a/src/Compiler/ModuleBuilder.cpp b/src/Compiler/ModuleBuilder.cpp index a5de296..dd66e44 100644 --- a/src/Compiler/ModuleBuilder.cpp +++ b/src/Compiler/ModuleBuilder.cpp @@ -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) { case StatementKind::RETURN: buildReturn(dynamic_pointer_cast(statement)); break; + case StatementKind::LOOP: + buildLoop(dynamic_pointer_cast(statement)); + break; case StatementKind::META_EXTERN_FUNCTION: buildMetaExternFunction(dynamic_pointer_cast(statement)); break; @@ -121,6 +125,29 @@ void ModuleBuilder::buildReturn(shared_ptr statement) { } } +void ModuleBuilder::buildLoop(shared_ptr statement) { + shared_ptr initStatement = statement->getInitStatement(); + shared_ptr preExpression = statement->getPreConditionExpression(); + shared_ptr postExpression = statement->getPostConditionExpression(); + shared_ptr 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 statement) { // get argument types vector types; diff --git a/src/Compiler/ModuleBuilder.h b/src/Compiler/ModuleBuilder.h index b3cafba..7552a5d 100644 --- a/src/Compiler/ModuleBuilder.h +++ b/src/Compiler/ModuleBuilder.h @@ -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 statement); void buildBlock(shared_ptr statement); void buildReturn(shared_ptr statement); + void buildLoop(shared_ptr statement); void buildMetaExternFunction(shared_ptr statement); void buildExpression(shared_ptr statement); diff --git a/src/Parser/Parser.cpp b/src/Parser/Parser.cpp index 203a328..7c4e25d 100644 --- a/src/Parser/Parser.cpp +++ b/src/Parser/Parser.cpp @@ -194,7 +194,7 @@ shared_ptr Parser::matchStatementLoop() { // pre condition shared_ptr preConditionExpression = nextExpression(); - if (preConditionExpression == nullptr || !preConditionExpression->isValid()) + if (preConditionExpression != nullptr && !preConditionExpression->isValid()) return matchStatementInvalid("Expected pre-condition expression"); // post condition diff --git a/test.brc b/test.brc new file mode 100644 index 0000000..b0d395e --- /dev/null +++ b/test.brc @@ -0,0 +1,8 @@ +test fun + /*rep i sint32 <- 0, i < 10 + //i sint32 <- i + 1 + ;*/ + rep i sint32 <- 13 + abc sint32 <- 5 + ; +; \ No newline at end of file