chunk.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. ******************************************************************************
  3. * @file : chunk.h
  4. * @author : simon
  5. * @brief : Chunks contain almost all of the information
  6. * that the runtime needs from the user’s source code
  7. * @attention : None
  8. * @date : 2023/8/16
  9. ******************************************************************************
  10. */
  11. #ifndef CLOX__CHUNK_H_
  12. #define CLOX__CHUNK_H_
  13. #include "common.h"
  14. #include "value.h"
  15. typedef enum {
  16. OP_CONSTANT,///<OP_CONSTANT (index)+>
  17. OP_RETURN, ///<OP_RETURN>
  18. } OpCode;
  19. //============================================================================
  20. // Dynamic array of instructions 扩容步骤
  21. //1. Allocate a new array with more capacity.
  22. //2. Copy the existing elements from the old array to the new one.
  23. //3. Store the new capacity.
  24. //4. Delete the old array.
  25. //5. Update code to point to the new array.
  26. //6. Store the element in the new array now that there is room.
  27. //7. Update the count.
  28. //============================================================================
  29. typedef struct {
  30. int count; // 使用量
  31. int capacity; // 容量
  32. uint8_t *code; // unsigned char*
  33. int *lines; // 源代码行数
  34. ValueArray constants;// 常量池
  35. } Chunk;
  36. /// 初始化 chunk
  37. /// \param chunk 对象
  38. void initChunk(Chunk *chunk);
  39. /// 释放 chunk
  40. /// \param chunk 对象
  41. void freeChunk(Chunk *chunk);
  42. /// 写入 chunk
  43. /// \param chunk 对象
  44. /// \param byte 命令或数据
  45. /// \param line 源代码行数
  46. void writeChunk(Chunk *chunk, uint8_t byte, int line);
  47. /// 添加常量
  48. /// \param chunk 指令数组
  49. /// \param value 值
  50. /// \return index of constant 常量位置
  51. int addConstant(Chunk *chunk, Value value);
  52. #endif//CLOX__CHUNK_H_