Cosmo/src/cchunk.h

42 lines
1.2 KiB
C
Raw Permalink Normal View History

2020-10-28 05:16:30 +00:00
#ifndef CCHUNK_H
#define CCHUNK_H
#include "coperators.h"
2023-02-09 18:32:48 +00:00
#include "cosmo.h"
2020-10-28 05:16:30 +00:00
#include "cvalue.h"
2023-02-09 18:32:48 +00:00
struct CChunk
{
size_t capacity; // the amount of space we've allocated for
size_t count; // the space we're currently using
INSTRUCTION *buf; // whole chunk
2020-10-28 05:16:30 +00:00
CValueArray constants; // holds constants
size_t lineCapacity;
int *lineInfo;
2021-02-15 22:20:04 +00:00
};
2020-10-28 05:16:30 +00:00
2023-02-09 18:32:48 +00:00
CChunk *newChunk(CState *state, size_t startCapacity);
void initChunk(CState *state, CChunk *chunk, size_t startCapacity);
void cleanChunk(CState *state, CChunk *chunk); // frees everything but the struct
void freeChunk(CState *state, CChunk *chunk); // frees everything including the struct
int addConstant(CState *state, CChunk *chunk, CValue value);
2020-10-28 05:16:30 +00:00
bool validateChunk(CState *state, CChunk *chunk);
2020-10-28 05:16:30 +00:00
// write to chunk
2023-02-09 18:32:48 +00:00
void writeu8Chunk(CState *state, CChunk *chunk, INSTRUCTION i, int line);
void writeu16Chunk(CState *state, CChunk *chunk, uint16_t i, int line);
2020-10-28 05:16:30 +00:00
// read from chunk
2023-02-09 18:32:48 +00:00
static inline INSTRUCTION readu8Chunk(CChunk *chunk, int offset)
{
2020-10-28 05:16:30 +00:00
return chunk->buf[offset];
}
2023-02-09 18:32:48 +00:00
static inline uint16_t readu16Chunk(CChunk *chunk, int offset)
{
return *((uint16_t *)(&chunk->buf[offset]));
2020-10-28 05:16:30 +00:00
}
2021-01-02 05:06:24 +00:00
#endif