<commit_before>#include "LLVM/LLVMcodegen.h"

#define genError(...) errorMessage("LLVM codegen: " __VA_ARGS__)

// Definitions

LLVMCodeGenerator *createLLVMCodeGenerator(Vector *sourceFiles) {
	LLVMCodeGenerator *self = safeMalloc(sizeof(*self));
	self->abstractSyntaxTree = NULL;
	self->currentNode = 0;
	self->sourceFiles = sourceFiles;
	self->builder = LLVMCreateBuilder();
	return self;
}

void destroyLLVMCodeGenerator(LLVMCodeGenerator *self) {
	for (int i = 0; i < self->sourceFiles->size; i++) {
		SourceFile *sourceFile = getVectorItem(self->sourceFiles, i);
		LLVMDisposeModule(sourceFile->module);
		destroySourceFile(sourceFile);
		verboseModeMessage("Destroyed source files iteration %d", i);
	}

	free(self);
	verboseModeMessage("Destroyed compiler");
}

static void consumeAstNode(LLVMCodeGenerator *self) {
	self->currentNode += 1;
}

static void consumeAstNodeBy(LLVMCodeGenerator *self, int amount) {
	self->currentNode += amount;
}

LLVMValueRef genBinaryExpression(LLVMCodeGenerator *self, BinaryExpr *expr) {
	LLVMValueRef lhs = genExpression(self, expr->lhand);
	LLVMValueRef rhs = genExpression(self, expr->lhand);
	if (!lhs || !rhs) {
		genError("Invalid expression");
		// TODO
	}

	if (!strcmp(expr->binaryOp, "+")) {
		return LLVMBuildAdd(self->builder, lhs, rhs, "add");
	}
	else if (!strcmp(expr->binaryOp, "-")) {
		return LLVMBuildSub(self->builder, lhs, rhs, "sub");
	}
	else if (!strcmp(expr->binaryOp, "*")) {
		return LLVMBuildMul(self->builder, lhs, rhs, "mul");
	}
	else if (!strcmp(expr->binaryOp, "/")) {
		return LLVMBuildUDiv(self->builder, lhs, rhs, "div");
	}
}

LLVMValueRef genFunctionCall(LLVMCodeGenerator *self, Call *call) {
	char *funcName = getVectorItem(call->callee, 0);
	LLVMValueRef func = LLVMGetNamedFunction(self->currentSourceFile->module, funcName);

	if (!func) {
		genError("Function not found in module");<commit_msg>kind of fixing the float and integer arithmetic stuff<commit_after>#include "LLVM/LLVMcodegen.h"

#define genError(...) errorMessage("LLVM codegen: " __VA_ARGS__)

// Definitions

LLVMCodeGenerator *createLLVMCodeGenerator(Vector *sourceFiles) {
	LLVMCodeGenerator *self = safeMalloc(sizeof(*self));
	self->abstractSyntaxTree = NULL;
	self->currentNode = 0;
	self->sourceFiles = sourceFiles;
	self->builder = LLVMCreateBuilder();
	return self;
}

void destroyLLVMCodeGenerator(LLVMCodeGenerator *self) {
	for (int i = 0; i < self->sourceFiles->size; i++) {
		SourceFile *sourceFile = getVectorItem(self->sourceFiles, i);
		LLVMDisposeModule(sourceFile->module);
		destroySourceFile(sourceFile);
		verboseModeMessage("Destroyed source files iteration %d", i);
	}

	free(self);
	verboseModeMessage("Destroyed compiler");
}

static void consumeAstNode(LLVMCodeGenerator *self) {
	self->currentNode += 1;
}

static void consumeAstNodeBy(LLVMCodeGenerator *self, int amount) {
	self->currentNode += amount;
}

bool isFloatingType(LLVMValueRef type) {
	switch (LLVMGetTypeKind(type)) {
		case LLVMFloatTypeKind: 
		case LLVMFP128TypeKind:
		case LLVMDoubleTypeKind:
			return true;
		default: return false;
	}
}

LLVMValueRef genBinaryExpression(LLVMCodeGenerator *self, BinaryExpr *expr) {
	LLVMValueRef lhs = genExpression(self, expr->lhand);
	LLVMValueRef rhs = genExpression(self, expr->lhand);
	if (!lhs || !rhs) {
		genError("Invalid expression");
		// TODO
	}

	// FIXME
	bool floating = false; // isFloatingType(lhs) || isFloatingType(rhs)

	if (!strcmp(expr->binaryOp, "+")) {
		return floating ? LLVMBuildFAdd(self->builder, lhs, rhs, "add") : LLVMBuildAdd(self->builder, lhs, rhs, "add");
	}
	else if (!strcmp(expr->binaryOp, "-")) {
		return floating ? LLVMBuildFSub(self->builder, lhs, rhs, "sub") : LLVMBuildSub(self->builder, lhs, rhs, "add");
	}
	else if (!strcmp(expr->binaryOp, "*")) {
		return floating ? LLVMBuildFMul(self->builder, lhs, rhs, "mul") : LLVMBuildMul(self->builder, lhs, rhs, "mul");
	}
	else if (!strcmp(expr->binaryOp, "/")) {
		return floating ? LLVMBuildFDiv(self->builder, lhs, rhs, "div") : LLVMBuildUDiv(self->builder, lhs, rhs, "div");
	}
}

LLVMValueRef genFunctionCall(LLVMCodeGenerator *self, Call *call) {
	char *funcName = getVectorItem(call->callee, 0);
	LLVMValueRef func = LLVMGetNamedFunction(self->currentSourceFile->module, funcName);

	if (!func) {
		genError("Function not found in module");
{'fraction_non_alphanumeric': 0.14857671835223327, 'fraction_numerical': 0.0025457070122656793, 'mean_word_length': 5.784929356357928, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4308295', 'n_tokens_mistral': 1520, 'n_tokens_neox': 1481, 'n_words': 315}