add type specification to iterator variable

pull/1/head
cosmonaut 2021-04-30 00:50:35 -07:00
parent 62f42e47b9
commit 3b43d44f35
3 changed files with 29 additions and 25 deletions

View File

@ -2,11 +2,9 @@ struct Program
{ {
static Main(): int static Main(): int
{ {
sum: int; sum: int = 0;
sum = 0; for (i: int in [1..1000])
for (i in [1..1000])
{ {
if ((i % 3 == 0) || (i % 5 == 0)) if ((i % 3 == 0) || (i % 5 == 0))
{ {

View File

@ -256,7 +256,7 @@ Conditional : IfStatement
$$ = MakeIfElseNode($1, $3); $$ = MakeIfElseNode($1, $3);
} }
ForStatement : FOR LEFT_PAREN Identifier IN LEFT_BRACKET Number POINT POINT Number RIGHT_BRACKET RIGHT_PAREN LEFT_BRACE Statements RIGHT_BRACE ForStatement : FOR LEFT_PAREN VariableDeclaration IN LEFT_BRACKET Number POINT POINT Number RIGHT_BRACKET RIGHT_PAREN LEFT_BRACE Statements RIGHT_BRACE
{ {
$$ = MakeForLoopNode($3, $6, $9, $13); $$ = MakeForLoopNode($3, $6, $9, $13);
} }

View File

@ -619,6 +619,23 @@ static LLVMBasicBlockRef CompileReturnVoid(LLVMBuilderRef builder, LLVMValueRef
return LLVMGetLastBasicBlock(function); return LLVMGetLastBasicBlock(function);
} }
/* FIXME: path for reference types */
static LLVMValueRef CompileFunctionVariableDeclaration(LLVMBuilderRef builder, LLVMValueRef function, Node *variableDeclaration)
{
LLVMValueRef variable;
char *variableName = variableDeclaration->children[1]->value.string;
char *ptrName = strdup(variableName);
strcat(ptrName, "_ptr");
variable = LLVMBuildAlloca(builder, ResolveType(variableDeclaration->children[0]), ptrName);
free(ptrName);
AddLocalVariable(scope, variable, NULL, variableName);
return variable;
}
static LLVMBasicBlockRef CompileAssignment(LLVMBuilderRef builder, LLVMValueRef function, Node *assignmentStatement) static LLVMBasicBlockRef CompileAssignment(LLVMBuilderRef builder, LLVMValueRef function, Node *assignmentStatement)
{ {
LLVMValueRef result = CompileExpression(builder, assignmentStatement->children[1]); LLVMValueRef result = CompileExpression(builder, assignmentStatement->children[1]);
@ -631,6 +648,10 @@ static LLVMBasicBlockRef CompileAssignment(LLVMBuilderRef builder, LLVMValueRef
{ {
identifier = FindVariablePointer(assignmentStatement->children[0]->value.string); identifier = FindVariablePointer(assignmentStatement->children[0]->value.string);
} }
else if (assignmentStatement->children[0]->syntaxKind == Declaration)
{
identifier = CompileFunctionVariableDeclaration(builder, function, assignmentStatement->children[0]);
}
else else
{ {
printf("Identifier not found!"); printf("Identifier not found!");
@ -642,23 +663,6 @@ static LLVMBasicBlockRef CompileAssignment(LLVMBuilderRef builder, LLVMValueRef
return LLVMGetLastBasicBlock(function); return LLVMGetLastBasicBlock(function);
} }
/* FIXME: path for reference types */
static LLVMBasicBlockRef CompileFunctionVariableDeclaration(LLVMBuilderRef builder, LLVMValueRef function, Node *variableDeclaration)
{
LLVMValueRef variable;
char *variableName = variableDeclaration->children[1]->value.string;
char *ptrName = strdup(variableName);
strcat(ptrName, "_ptr");
variable = LLVMBuildAlloca(builder, ResolveType(variableDeclaration->children[0]), ptrName);
free(ptrName);
AddLocalVariable(scope, variable, NULL, variableName);
return LLVMGetLastBasicBlock(function);
}
static LLVMBasicBlockRef CompileIfStatement(LLVMBuilderRef builder, LLVMValueRef function, Node *ifStatement) static LLVMBasicBlockRef CompileIfStatement(LLVMBuilderRef builder, LLVMValueRef function, Node *ifStatement)
{ {
uint32_t i; uint32_t i;
@ -729,7 +733,8 @@ static LLVMBasicBlockRef CompileForLoopStatement(LLVMBuilderRef builder, LLVMVal
LLVMBasicBlockRef checkBlock = LLVMAppendBasicBlock(function, "loopCheck"); LLVMBasicBlockRef checkBlock = LLVMAppendBasicBlock(function, "loopCheck");
LLVMBasicBlockRef bodyBlock = LLVMAppendBasicBlock(function, "loopBody"); LLVMBasicBlockRef bodyBlock = LLVMAppendBasicBlock(function, "loopBody");
LLVMBasicBlockRef afterLoopBlock = LLVMAppendBasicBlock(function, "afterLoop"); LLVMBasicBlockRef afterLoopBlock = LLVMAppendBasicBlock(function, "afterLoop");
char *iteratorVariableName = forLoopStatement->children[0]->value.string; char *iteratorVariableName = forLoopStatement->children[0]->children[1]->value.string;
LLVMTypeRef iteratorVariableType = ResolveType(forLoopStatement->children[0]->children[0]);
PushScopeFrame(scope); PushScopeFrame(scope);
@ -739,7 +744,7 @@ static LLVMBasicBlockRef CompileForLoopStatement(LLVMBuilderRef builder, LLVMVal
LLVMBuildBr(builder, checkBlock); LLVMBuildBr(builder, checkBlock);
LLVMPositionBuilderAtEnd(builder, checkBlock); LLVMPositionBuilderAtEnd(builder, checkBlock);
LLVMValueRef iteratorValue = LLVMBuildPhi(builder, LLVMInt64Type(), iteratorVariableName); LLVMValueRef iteratorValue = LLVMBuildPhi(builder, iteratorVariableType, iteratorVariableName);
AddLocalVariable(scope, NULL, iteratorValue, iteratorVariableName); AddLocalVariable(scope, NULL, iteratorValue, iteratorVariableName);
LLVMPositionBuilderAtEnd(builder, bodyBlock); LLVMPositionBuilderAtEnd(builder, bodyBlock);
@ -789,7 +794,8 @@ static LLVMBasicBlockRef CompileStatement(LLVMBuilderRef builder, LLVMValueRef f
return CompileAssignment(builder, function, statement); return CompileAssignment(builder, function, statement);
case Declaration: case Declaration:
return CompileFunctionVariableDeclaration(builder, function, statement); CompileFunctionVariableDeclaration(builder, function, statement);
return LLVMGetLastBasicBlock(function);
case ForLoop: case ForLoop:
return CompileForLoopStatement(builder, function, statement); return CompileForLoopStatement(builder, function, statement);