From 39cc0429daaad163ec4098067a09ff65111aad62 Mon Sep 17 00:00:00 2001 From: SpookyDervish <78246495+SpookyDervish@users.noreply.github.com> Date: Wed, 15 Oct 2025 07:37:01 +1100 Subject: [PATCH] WE CAN PASS ARGUMENTS TO FUNCTIONS!!!!!! --- compiler.py | 29 ++++++++++++++++++++++++----- debug/ir.ll | 16 +++++++++++----- main.py | 6 +++--- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/compiler.py b/compiler.py index 3b98da0..2e85c61 100644 --- a/compiler.py +++ b/compiler.py @@ -4,6 +4,7 @@ from AST import Node, NodeType, Program, Expression from AST import ExpressionStatement, AssignmentStatement, BlockStatement, ReturnStatement, FunctionStatement, ReassignStatement, IfStatement from AST import InfixExpression, CallExpression from AST import IntegerLiteral, FloatLiteral, IdentifierLiteral, BooleanLiteral +from AST import FunctionParameter from environment import Environment @@ -116,8 +117,9 @@ class Compiler: name: str = node.name.value body: BlockStatement = node.body - params: list[IdentifierLiteral] = node.parameters - param_types: list[ir.Type] = [] # TODO + params: list[FunctionParameter] = node.parameters + param_names: list[str] = [p.name for p in params] + param_types: list[ir.Type] = [self.type_map[p.value_type] for p in params] return_type: ir.Type = self.type_map[node.return_type] @@ -130,9 +132,22 @@ class Compiler: self.builder = ir.IRBuilder(block) - previous_env = self.environment + # storing the pointers to each parameter + params_ptr = [] + for i, typ in enumerate(param_types): + ptr = self.builder.alloca(typ) + self.builder.store(func.args[i], ptr) + params_ptr.append(ptr) + # adding the params to the environment + previous_env = self.environment self.environment = Environment(parent=self.environment) + for i, x in enumerate(zip(param_types, param_names)): + typ = param_types[i] + ptr = params_ptr[i] + + self.environment.define(x[1], ptr, typ) + self.environment.define(name, func, return_type) self.compile(body) @@ -260,13 +275,17 @@ class Compiler: return value, Type - def __visit_call_expression(self, node: CallExpression) -> None: + def __visit_call_expression(self, node: CallExpression) -> tuple[ir.Instruction, ir.Type]: name: str = node.function.value params: list[Expression] = node.arguments args = [] types = [] - # TODO + if len(params) > 0: + for x in params: + p_val, p_type = self.__resolve_value(x) + args.append(p_val) + types.append(p_type) match name: case _: diff --git a/debug/ir.ll b/debug/ir.ll index cdd6834..3b32f62 100644 --- a/debug/ir.ll +++ b/debug/ir.ll @@ -4,16 +4,22 @@ target datalayout = "" @"true" = constant i1 1 @"false" = constant i1 0 -define i32 @"test"() +define i32 @"add"(i32 %".1", i32 %".2") { -test_entry: - %".2" = add i32 77, 33 - ret 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 @"test"() + %".2" = call i32 @"add"(i32 2, i32 3) ret i32 %".2" } diff --git a/main.py b/main.py index b287b44..ad60a43 100644 --- a/main.py +++ b/main.py @@ -10,9 +10,9 @@ import llvmlite.binding as llvm from ctypes import CFUNCTYPE, c_int, c_float LEXER_DEBUG: bool = False -PARSER_DEBUG: bool = True -COMPILER_DEBUG: bool = False -RUN_CODE: bool = False +PARSER_DEBUG: bool = False +COMPILER_DEBUG: bool = True +RUN_CODE: bool = True if __name__ == "__main__":