/** ****************************************************************************** * @file : chunk.h * @author : simon * @brief : Chunks contain almost all of the information * that the runtime needs from the user’s source code * @attention : None * @date : 2023/8/16 ****************************************************************************** */ #ifndef CLOX__CHUNK_H_ #define CLOX__CHUNK_H_ #include "common.h" #include "value.h" typedef enum { OP_CONSTANT,/// OP_RETURN, /// } OpCode; //============================================================================ // Dynamic array of instructions 扩容步骤 //1. Allocate a new array with more capacity. //2. Copy the existing elements from the old array to the new one. //3. Store the new capacity. //4. Delete the old array. //5. Update code to point to the new array. //6. Store the element in the new array now that there is room. //7. Update the count. //============================================================================ typedef struct { int count; // 使用量 int capacity; // 容量 uint8_t *code; // unsigned char* ValueArray constants;// 常量池 } Chunk; void initChunk(Chunk *chunk); void freeChunk(Chunk *chunk); void writeChunk(Chunk *chunk, uint8_t byte); /// 添加常量 /// \param chunk 指令数组 /// \param value 值 /// \return index of constant 常量位置 int addConstant(Chunk *chunk, Value value); #endif//CLOX__CHUNK_H_