chunk.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. ValueArray constants;// 常量池
  34. } Chunk;
  35. void initChunk(Chunk *chunk);
  36. void freeChunk(Chunk *chunk);
  37. void writeChunk(Chunk *chunk, uint8_t byte);
  38. /// 添加常量
  39. /// \param chunk 指令数组
  40. /// \param value 值
  41. /// \return index of constant 常量位置
  42. int addConstant(Chunk *chunk, Value value);
  43. #endif//CLOX__CHUNK_H_