#ifndef BASIC_CODE_H #define BASIC_CODE_H #include "Buffer.h" #include "Error.h" #include "Types.h" typedef enum : u8 { VT_INT32, VT_ARRAY, VT_CONSTANT_STRING } ValueType; typedef struct { ValueType type; u8 visitMarker; u8 reserved2; u8 reserved3; i32 data; } Value; typedef struct Allocation Allocation; struct Allocation { Allocation* next; Value values[]; }; #define INT_VALUE(value) ((Value){.type = VT_INT32, .data = (value)}) static_assert(sizeof(Value) == 8); typedef enum : u8 { ADD, SUB, MUL, DIV, PUSH_CONSTANT_STRING, PUSH_INT32, POP, JUMP, JUMP_ON_0, JUMP_SUB, RETURN_VALUE, READ_VARIABLE, READ_VARIABLE_ARRAY, SET_VARIABLE, SET_VARIABLE_ARRAY, PUSH_STACK_VARIABLES, AND, OR, NOT, EQUAL, NOT_EQUAL, GREATER, SMALLER, GREATER_OR_EQUAL, SMALLER_OR_EQUAL, CALL_SYSTEM, STOP } Instruction; typedef struct { Buffer code; Error error; Value* stack; Allocation* allocations; size_t maxStackSize; size_t stackIndex; i32 localVariableIndex; } Code; void codeInit(Code* code); void codeDestroy(Code* code); void codeReset(Code* code); void codeRun(Code* code); bool codeHasRunError(const Code* code); const char* codeGetRunError(const Code* code); void codeRunCollector(Code* code); void codeDump(const Code* code); #define SET_ERROR(format, ...) \ snprintf( \ c->error.text, sizeof(c->error.text), "%zu | " format, \ c->code.readIndex __VA_OPT__(, ) __VA_ARGS__) #endif