<commit_before>#include "compiler.h"

compiler *create_compiler() {
	compiler *self = malloc(sizeof(*self));
	self->ast = NULL;
	self->current_instruction = 0;
	self->max_bytecode_size = 32;
	self->bytecode = malloc(sizeof(*self->bytecode) * self->max_bytecode_size);
	self->current_ast_node = 0;
	self->table = create_hashmap(128);
	self->global_count = 0;
	self->llvm_error_message = NULL;
	self->refs = create_vector();

	self->module = LLVMModuleCreateWithName("j4");
	self->builder = LLVMCreateBuilder();
	LLVMInitializeNativeTarget();
	LLVMLinkInJIT();

	// create execution engine
	if (LLVMCreateExecutionEngineForModule(&self->engine, self->module, &self->llvm_error_message)) {
		fprintf(stderr, "%s\n", self->llvm_error_message);
		LLVMDisposeMessage(self->llvm_error_message);
		exit(1);
	}

	// optimizations
	self->pass_manager = LLVMCreateFunctionPassManagerForModule(self->module);
	LLVMAddTargetData(LLVMGetExecutionEngineTargetData(self->engine), self->pass_manager);
	LLVMAddPromoteMemoryToRegisterPass(self->pass_manager);
	LLVMAddInstructionCombiningPass(self->pass_manager);
	LLVMAddReassociatePass(self->pass_manager);
	LLVMAddGVNPass(self->pass_manager);
	LLVMAddCFGSimplificationPass(self->pass_manager);
	LLVMInitializeFunctionPassManager(self->pass_manager);

	return self;
}

variable_info *create_variable_info() {
	variable_info *vinfo = malloc(sizeof(*vinfo));
	if (!vinfo) {
		perror("malloc: failed to allocate memory for variable info");
		exit(1);
	}
	vinfo->allocation = NULL;
	vinfo->type = TYPE_NULL;
	vinfo->name = "";
	return vinfo;
}

void destroy_variable_info(variable_info *vinfo) {
	if (!vinfo) {
		free(vinfo);
	}
}

void consume_ast_node(compiler *self) {
	self->current_ast_node += 1;
}

void consume_ast_nodes(compiler *self, int amount) {
	self->current_ast_node += amount;
}

/**
 * Different data types
 */
// typedef enum {
// 	TYPE_INTEGER = 0, TYPE_STR, TYPE_DOUBLE, TYPE_FLOAT, TYPE_BOOL, TYPE_VOID,
// 	TYPE_CHAR
// } data_type;

LLVMTypeRef get_type_ref(data_type type) {
	switch (type) {
		case TYPE_INTEGER: return LLVMInt32Type();
		case TYPE_STR:
			error_message("strings are unimplemented\n");
			break;
		case TYPE_DOUBLE: return LLVMDoubleType();
		case TYPE_FLOAT: return LLVMFloatType();
		case TYPE_BOOL:
			error_message("bools are unimplemented\n");
			break;
		case TYPE_VOID: return LLVMVoidType();
		case TYPE_CHAR:
			error_message("chars are unimplemented\n");
			break;
		default:
			error_message("unsupported data type `%d`\n", type);
			break;
	}
	error_message("we should've thrown an error for type references, why are you here?\n");
	return NULL;
}

LLVMValueRef evaluate_expression_ast_node(compiler *self, expression_ast_node *expr) {
	error_message("expressions unimplemented\n");
	return NULL;
}

LLVMValueRef generate_variable_definition_code(compiler *self, variable_declare_ast_node *vdn) {

	return NULL;
}<commit_msg>fixed some errors forgot what lol<commit_after>#include "compiler.h"

compiler *create_compiler() {
	compiler *self = malloc(sizeof(*self));
	self->ast = NULL;
	self->current_instruction = 0;
	self->max_bytecode_size = 32;
	self->bytecode = malloc(sizeof(*self->bytecode) * self->max_bytecode_size);
	self->current_ast_node = 0;
	self->table = create_hashmap(128);
	self->global_count = 0;
	self->llvm_error_message = NULL;
	self->refs = create_vector();

	self->module = LLVMModuleCreateWithName("j4");
	self->builder = LLVMCreateBuilder();
	LLVMInitializeNativeTarget();
	LLVMLinkInJIT();

	// create execution engine
	if (LLVMCreateExecutionEngineForModule(&self->engine, self->module, &self->llvm_error_message)) {
		fprintf(stderr, "%s\n", self->llvm_error_message);
		LLVMDisposeMessage(self->llvm_error_message);
		exit(1);
	}

	// optimizations
	self->pass_manager = LLVMCreateFunctionPassManagerForModule(self->module);
	LLVMAddTargetData(LLVMGetExecutionEngineTargetData(self->engine), self->pass_manager);
	LLVMAddPromoteMemoryToRegisterPass(self->pass_manager);
	LLVMAddInstructionCombiningPass(self->pass_manager);
	LLVMAddReassociatePass(self->pass_manager);
	LLVMAddGVNPass(self->pass_manager);
	LLVMAddCFGSimplificationPass(self->pass_manager);
	LLVMInitializeFunctionPassManager(self->pass_manager);

	return self;
}

variable_info *create_variable_info() {
	variable_info *vinfo = malloc(sizeof(*vinfo));
	if (!vinfo) {
		perror("malloc: failed to allocate memory for variable info");
		exit(1);
	}
	vinfo->allocation = NULL;
	vinfo->type = TYPE_NULL;
	vinfo->name = "";
	return vinfo;
}

void destroy_variable_info(variable_info *vinfo) {
	if (!vinfo) {
		free(vinfo);
	}
}

void consume_ast_node(compiler *self) {
	self->current_ast_node += 1;
}

void consume_ast_nodes(compiler *self, int amount) {
	self->current_ast_node += amount;
}

/**
 * Different data types
 */
// typedef enum {
// 	TYPE_INTEGER = 0, TYPE_STR, TYPE_DOUBLE, TYPE_FLOAT, TYPE_BOOL, TYPE_VOID,
// 	TYPE_CHAR
// } data_type;

LLVMTypeRef get_type_ref(data_type type) {
	printf("given %d\n", type);
	switch (type) {
		case TYPE_INTEGER: return LLVMInt32Type();
		case TYPE_STR:
			error_message("strings are unimplemented\n");
			break;
		case TYPE_DOUBLE: return LLVMDoubleType();
		case TYPE_FLOAT: return LLVMFloatType();
		case TYPE_BOOL:
			error_message("bools are unimplemented\n");
			break;
		case TYPE_VOID: return LLVMVoidType();
		case TYPE_CHAR:
			error_message("chars are unimplemented\n");
			break;
		default:
			error_message("unsupported data type `%d`\n", type);
			break;
	}
	error_message("we should've thrown an error for type references, why are you here?\n");
	return NULL;
}

LLVMValueRef evaluate_expression_ast_node(compiler *self, expression_ast_node *expr) {
	error_message("expressions unimplemented\n");
	return NULL;
}

LLVMValueRef generate_variable_definition_code(compiler *self, variable_declare_ast_node *vdn) {

	return NULL;
}
{'fraction_non_alphanumeric': 0.12753623188405797, 'fraction_numerical': 0.005115089514066497, 'mean_word_length': 5.99165673420739, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 69, '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': '4175857', 'n_tokens_mistral': 2115, 'n_tokens_neox': 2018, 'n_words': 384}