fixed reassignment, starting work on if statements

This commit is contained in:
SpookyDervish
2025-10-14 20:02:22 +11:00
parent 655e5d1d12
commit 48e7488a63
6 changed files with 45 additions and 15 deletions

View File

@@ -1,7 +1,7 @@
from llvmlite import ir
from AST import Node, NodeType, Program, Expression
from AST import ExpressionStatement, AssignmentStatement, BlockStatement, ReturnStatement, FunctionStatement
from AST import ExpressionStatement, AssignmentStatement, BlockStatement, ReturnStatement, FunctionStatement, ReassignStatement
from AST import InfixExpression
from AST import IntegerLiteral, FloatLiteral, IdentifierLiteral
@@ -23,6 +23,7 @@ class Compiler:
self.module: ir.Module = ir.Module("main")
self.builder: ir.IRBuilder = ir.IRBuilder()
self.environment: Environment = Environment()
self.errors: list[str] = []
def compile(self, node: Node) -> None:
match node.type():
@@ -40,6 +41,8 @@ class Compiler:
self.__visit_block_statement(node)
case NodeType.ReturnStatement:
self.__visit_return_statement(node)
case NodeType.ReassignStatement:
self.__visit_reassign_statement(node)
# Expressions
case NodeType.InfixExpression:
@@ -115,6 +118,17 @@ class Compiler:
self.builder = previous_builder
def __visit_reassign_statement(self, node: ReassignStatement) -> None:
name: str = node.ident.value
value: Expression = node.right_value
value, Type = self.__resolve_value(value)
if self.environment.lookup(name) is None:
self.errors.append(f"Identifier {name} has not been declared before it was re-assigned.")
else:
ptr, _ = self.environment.lookup(name)
self.builder.store(value, ptr)
# endregion
# region Expressions

View File

@@ -2,17 +2,14 @@
target triple = "x86_64-pc-windows-msvc"
target datalayout = ""
define i32 @"main"()
{
main_entry:
%".2" = alloca i32
store i32 0, i32* %".2"
%".4" = load i32, i32* %".2"
ret i32 %".4"
}
define i32 @"test"()
{
test_entry:
ret i32 0
}
define i32 @"main"()
{
main_entry:
ret i32 123
}

View File

@@ -23,6 +23,13 @@ class TokenType(Enum):
# Assignment symbols
EQ = "EQ"
# Comparison symbols
LT = "<"
GT = ">"
EQ_EQ = "=="
LT_EQ = "<="
GT_EQ = ">="
# Symbols
LPAREN = "LPAREN"
RPAREN = "RPAREN"
@@ -35,6 +42,10 @@ class TokenType(Enum):
# Keywords
RETURN = "RETURN"
IF = "IF"
UNLESS = "UNLESS"
TRUE = "TRUE"
FALSE = "FALSE"
# Typing
TYPE = "TYPE"
@@ -54,7 +65,11 @@ class Token:
KEYWORDS: dict[str, TokenType] = {
"return": TokenType.RETURN
"return": TokenType.RETURN,
"if": TokenType.IF,
"unless": TokenType.UNLESS,
"true": TokenType.TRUE,
"false": TokenType.FALSE
}
ALT_KEYWORDS: dict[str, TokenType] = {

View File

@@ -12,7 +12,7 @@ from ctypes import CFUNCTYPE, c_int, c_float
LEXER_DEBUG: bool = False
PARSER_DEBUG: bool = False
COMPILER_DEBUG: bool = True
RUN_CODE: bool = False
RUN_CODE: bool = True
if __name__ == "__main__":

View File

@@ -1,6 +1,6 @@
depend "io.pla"
if (1 + 2 == 3) {
if 1 + 2 == 3 {
print("The universe is functional!");
}
unless

View File

@@ -1,7 +1,11 @@
main = Func(): Int {
x: Int = 0;
x: Int = 123;
x = x * 2;
if x == 123 {
x = 0;
} unless {
x = 1;
}
return x;
}