1
0
Fork 0
forked from fun/fun

Initial code commit.

This commit is contained in:
Johannes Findeisen 2025-09-13 05:09:20 +02:00
commit 6a7add3e56
13 changed files with 891 additions and 0 deletions

43
src/bytecode.c Normal file
View file

@ -0,0 +1,43 @@
#include "bytecode.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
Bytecode *bytecode_new(void) {
Bytecode *bc = (Bytecode*)malloc(sizeof(Bytecode));
bc->instructions = NULL;
bc->instr_count = 0;
bc->constants = NULL;
bc->const_count = 0;
return bc;
}
int bytecode_add_constant(Bytecode *bc, Value v) {
bc->constants = (Value*)realloc(bc->constants, sizeof(Value) * (bc->const_count + 1));
bc->constants[bc->const_count] = copy_value(&v);
return bc->const_count++;
}
int bytecode_add_instruction(Bytecode *bc, OpCode op, int32_t operand) {
bc->instructions = (Instruction*)realloc(bc->instructions, sizeof(Instruction) * (bc->instr_count + 1));
bc->instructions[bc->instr_count].op = op;
bc->instructions[bc->instr_count].operand = operand;
return bc->instr_count++;
}
void bytecode_set_operand(Bytecode *bc, int idx, int32_t operand) {
if (idx >= 0 && idx < bc->instr_count) {
bc->instructions[idx].operand = operand;
}
}
void bytecode_free(Bytecode *bc) {
if (!bc) return;
for (int i = 0; i < bc->const_count; ++i) {
free_value(bc->constants[i]);
}
free(bc->constants);
free(bc->instructions);
free(bc);
}