Init and store var

This commit is contained in:
Rafał Grodziński
2025-06-09 19:08:03 +09:00
parent 406fccd0b1
commit 29e1464168
4 changed files with 15 additions and 5 deletions

View File

@@ -23,6 +23,9 @@ void ModuleBuilder::buildStatement(shared_ptr<Statement> statement) {
case StatementKind::FUNCTION_DECLARATION:
buildFunctionDeclaration(dynamic_pointer_cast<StatementFunctionDeclaration>(statement));
break;
case StatementKind::VAR_DECLARATION:
buildVarDeclaration(dynamic_pointer_cast<StatementVarDeclaration>(statement));
break;
case StatementKind::BLOCK:
buildBlock(dynamic_pointer_cast<StatementBlock>(statement));
break;
@@ -45,6 +48,12 @@ void ModuleBuilder::buildFunctionDeclaration(shared_ptr<StatementFunctionDeclara
buildStatement(statement->getStatementBlock());
}
void ModuleBuilder::buildVarDeclaration(shared_ptr<StatementVarDeclaration> statement) {
llvm::Value *value = valueForExpression(statement->getExpression());
llvm::AllocaInst *alloca = builder->CreateAlloca(typeForValueType(statement->getValueType()), nullptr, statement->getName());
builder->CreateStore(value, alloca);
}
void ModuleBuilder::buildBlock(shared_ptr<StatementBlock> statement) {
for (shared_ptr<Statement> &innerStatement : statement->getStatements()) {
buildStatement(innerStatement);
@@ -99,7 +108,7 @@ llvm::Value *ModuleBuilder::valueForGrouping(shared_ptr<ExpressionGrouping> expr
}
llvm::Value *ModuleBuilder::valueForBinary(shared_ptr<ExpressionBinary> expression) {
switch (expression->getValueType()) {
switch (expression->getLeft()->getValueType()) {
case ValueType::BOOL:
return valueForBinaryBool(expression);
case ValueType::SINT32:

View File

@@ -27,6 +27,7 @@ private:
void buildStatement(shared_ptr<Statement> statement);
void buildFunctionDeclaration(shared_ptr<StatementFunctionDeclaration> statement);
void buildVarDeclaration(shared_ptr<StatementVarDeclaration> statement);
void buildBlock(shared_ptr<StatementBlock> statement);
void buildReturn(shared_ptr<StatementReturn> statement);
void buildExpression(shared_ptr<StatementExpression> statement);

View File

@@ -333,7 +333,7 @@ shared_ptr<Expression> Parser::matchExpressionIfElse() {
else if (!thenBlock->isValid())
return matchExpressionInvalid(); // FIXME
// Match else blcok
// Match else block
shared_ptr<Statement> elseBlock;
if (tokens.at(currentIndex)->getKind() == TokenKind::COLON) {

View File

@@ -51,9 +51,9 @@ int main(int argc, char **argv) {
}
cout << endl << endl;
//ModuleBuilder moduleBuilder(statements);
//shared_ptr<llvm::Module> module = moduleBuilder.getModule();
//module->print(llvm::outs(), nullptr);
ModuleBuilder moduleBuilder(statements);
shared_ptr<llvm::Module> module = moduleBuilder.getModule();
module->print(llvm::outs(), nullptr);
//CodeGenerator codeGenerator(module);
//codeGenerator.generateObjectFile("dummy.s");