clox/chunk.h

44 lines
991 B
C

#ifndef _CHUNK_H
#define _CHUNK_H
#include "common.h"
#include "value.h"
// max 256 opcodes
typedef enum {
OP_CONSTANT, OP_CONSTANT_LONG,
OP_ADD, OP_SUBTRACT,
OP_MULTIPLY, OP_DIVIDE,
OP_NEGATE,
OP_RETURN,
NUM_OP, // total number of valid opcodes
} OpCode;
typedef struct {
int count;
int capacity;
bool bDebug; // if line numbers are kept or not
uint8_t* code;
uint16_t* lines; // 16bit line numbers
ValueArray constants;
} Chunk;
void initChunk( Chunk* chunk );
void freeChunk( Chunk* chunk );
// write single byte to code memory
void writeChunk( Chunk* chunk, uint8_t byte, uint16_t line );
// u16 write / read
void writeChunk16( Chunk* chunk, uint16_t word, uint16_t line );
uint16_t readChunk16( Chunk* chunk, int offset );
// add constant val to chunk's array. return index where it was added
uint16_t addConstant( Chunk* chunk, Value val );
// high level for adding and writing chunk constants
void writeConstant( Chunk* chunk, Value val, uint16_t line );
#endif