while loops work!

This commit is contained in:
SpookyDervish
2025-10-15 18:44:15 +11:00
parent 549f650b54
commit 9f6fff9977
7 changed files with 155 additions and 86 deletions

View File

@@ -1,48 +1,6 @@
{
"type": "Program",
"statements": [
{
"FunctionStatement": {
"type": "FunctionStatement",
"name": {
"type": "IdentifierLiteral",
"value": "add"
},
"return_type": "Int",
"parameters": [
{
"type": "FunctionParameter",
"name": "a",
"value_type": "Int"
},
{
"type": "FunctionParameter",
"name": "b",
"value_type": "Int"
}
],
"body": {
"type": "BlockStatement",
"statements": [
{
"type": "ReturnStatement",
"return_value": {
"type": "InfixExpression",
"left_node": {
"type": "IdentifierLiteral",
"value": "a"
},
"operator": "+",
"right_node": {
"type": "IdentifierLiteral",
"value": "b"
}
}
}
]
}
}
},
{
"FunctionStatement": {
"type": "FunctionStatement",
@@ -56,34 +14,72 @@
"type": "BlockStatement",
"statements": [
{
"type": "ExpressionStatement",
"expr": {
"type": "CallExpression",
"function": {
"type": "AssignmentStatement",
"name": {
"type": "IdentifierLiteral",
"value": "a"
},
"value": {
"type": "IntegerLiteral",
"value": 0
},
"value_type": "Int"
},
{
"type": "WhileStatement",
"condition": {
"type": "InfixExpression",
"left_node": {
"type": "IdentifierLiteral",
"value": "print"
"value": "a"
},
"arguments": [
"operator": "<",
"right_node": {
"type": "IntegerLiteral",
"value": 10
}
},
"body": {
"type": "BlockStatement",
"statements": [
{
"type": "StringLiteral",
"value": "apples %i"
"type": "ExpressionStatement",
"expr": {
"type": "CallExpression",
"function": {
"type": "IdentifierLiteral",
"value": "print"
},
"arguments": [
{
"type": "StringLiteral",
"value": "a = %i\\n"
},
{
"type": "IdentifierLiteral",
"value": "a"
}
]
}
},
{
"type": "CallExpression",
"function": {
"type": "ReassignStatement",
"ident": {
"type": "IdentifierLiteral",
"value": "add"
"value": "a"
},
"arguments": [
{
"right_value": {
"type": "InfixExpression",
"left_node": {
"type": "IdentifierLiteral",
"value": "a"
},
"operator": "+",
"right_node": {
"type": "IntegerLiteral",
"value": 1
},
{
"type": "IntegerLiteral",
"value": 2
}
]
}
}
]
}
@@ -91,8 +87,8 @@
{
"type": "ReturnStatement",
"return_value": {
"type": "IntegerLiteral",
"value": 0
"type": "IdentifierLiteral",
"value": "a"
}
}
]

View File

@@ -1,30 +1,34 @@
; ModuleID = "main"
target triple = "arm64-apple-darwin24.5.0"
target triple = "x86_64-pc-windows-msvc"
target datalayout = ""
declare i32 @"printf"(i8* %".1", ...)
@"true" = constant i1 1
@"false" = constant i1 0
define i32 @"add"(i32 %".1", i32 %".2")
{
add_entry:
%".4" = alloca i32
store i32 %".1", i32* %".4"
%".6" = alloca i32
store i32 %".2", i32* %".6"
%".8" = load i32, i32* %".4"
%".9" = load i32, i32* %".6"
%".10" = add i32 %".8", %".9"
ret i32 %".10"
}
define i32 @"main"()
{
main_entry:
%".2" = call i32 @"add"(i32 1, i32 2)
%".3" = alloca i32
store i32 %".2", i32* %".3"
%".5" = load i32, i32* %".3"
ret i32 %".5"
%".2" = alloca i32
store i32 0, i32* %".2"
%".4" = load i32, i32* %".2"
%".5" = icmp slt i32 %".4", 10
br i1 %".5", label %"while_loop_entry_1", label %"while_loop_otherwise_1"
while_loop_entry_1:
%".7" = load i32, i32* %".2"
%".8" = alloca [9 x i8]*
store [9 x i8]* @"__str_2", [9 x i8]** %".8"
%".10" = bitcast [9 x i8]* @"__str_2" to i8*
%".11" = call i32 (i8*, ...) @"printf"(i8* %".10", i32 %".7")
%".12" = load i32, i32* %".2"
%".13" = add i32 %".12", 1
store i32 %".13", i32* %".2"
%".15" = load i32, i32* %".2"
%".16" = icmp slt i32 %".15", 10
br i1 %".16", label %"while_loop_entry_1", label %"while_loop_otherwise_1"
while_loop_otherwise_1:
%".18" = load i32, i32* %".2"
ret i32 %".18"
}
@"__str_2" = internal constant [9 x i8] c"a = %i\0a\00\00"