/** ****************************************************************************** * @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_NEGATE, /// \brief prefix - OP_ADD, /// \brief + OP_SUBTRACT, /// \brief - OP_MULTIPLY, /// \brief * OP_DIVIDE, /// \brief / 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* int *lines; // 源代码行数 ValueArray constants;// 常量池 } Chunk; /// 初始化 chunk /// \param chunk 对象 void initChunk(Chunk *chunk); /// 释放 chunk /// \param chunk 对象 void freeChunk(Chunk *chunk); /// 写入 chunk /// \param chunk 对象 /// \param byte 命令或数据 /// \param line 源代码行数 void writeChunk(Chunk *chunk, uint8_t byte, int line); /// 添加常量 /// \param chunk 指令数组 /// \param value 值 /// \return index of constant 常量位置 int addConstant(Chunk *chunk, Value value); #endif//CLOX__CHUNK_H_