| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- /**
- ******************************************************************************
- * @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_CONSTANT (index)+>
- OP_NIL,
- OP_TRUE,
- OP_FALSE,
- OP_POP,
- OP_DEFINE_GLOBAL,
- OP_GET_GLOBAL,
- OP_SET_GLOBAL,/// \brief setter
- OP_GET_LOCAL,
- OP_SET_LOCAL,/// \brief setter
- OP_GET_UPVALUE,
- OP_SET_UPVALUE,
- OP_NOT, /// \brief print !true; // "false"
- OP_NEGATE,/// \brief prefix -
- OP_EQUAL,
- OP_GREATER,
- OP_LESS,
- OP_ADD, /// \brief +
- OP_SUBTRACT,/// \brief -
- OP_MULTIPLY,/// \brief *
- OP_DIVIDE, /// \brief /
- OP_PRINT,
- OP_JUMP_IF_FALSE,/// <OP_JUMP_IF_FALSE + +>
- OP_JUMP, /// <OP_JUMP + +> 往后跳
- OP_LOOP, /// <OP_LOOP ++> 往前跳
- OP_CALL, /// <OP_CALL argCount>
- OP_RETURN, ///<OP_RETURN>
- OP_CLOSURE,
- } 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_
|