vm.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. ******************************************************************************
  3. * @file : vm.h
  4. * @author : simon
  5. * @brief : hand it a chunk of code—literally a Chunk—and it runs it
  6. * @attention : None
  7. * @date : 2023/8/17
  8. ******************************************************************************
  9. */
  10. #ifndef CLOX__VM_H_
  11. #define CLOX__VM_H_
  12. #include "compiler.h"
  13. #include "object.h"
  14. #include "table.h"
  15. #define FRAMES_MAX 64
  16. #define STACK_MAX (FRAMES_MAX * UINT8_COUNT)
  17. typedef struct {
  18. ObjClosure *closure;
  19. uint8_t *ip;
  20. Value *slots;// points into the VM’s value stack
  21. // at the first slot that this function can use
  22. } CallFrame;
  23. typedef struct VM {
  24. CallFrame frames[FRAMES_MAX];
  25. int frameCount;
  26. Value stack[STACK_MAX];
  27. Value *stackTop;// 栈指针
  28. Table globals;
  29. Table strings; //
  30. ObjString *initString;// 初始化方法
  31. ObjUpvalue *openUpvalues;
  32. size_t bytesAllocated;
  33. size_t nextGC;
  34. Obj *objects;// 管理分配的 heap 内存
  35. int grayCount;
  36. int grayCapacity;
  37. Obj **grayStack;
  38. } VM;
  39. typedef enum {
  40. INTERPRET_OK,
  41. INTERPRET_COMPILE_ERROR,
  42. INTERPRET_RUNTIME_ERROR
  43. } InterpretResult;
  44. extern VM vm;
  45. void initVM();
  46. void freeVM();
  47. /// \brief interpret 执行指令
  48. /// \param source 源代码
  49. /// \return InterpretResult
  50. InterpretResult interpret(const char *source);
  51. void push(Value value);
  52. Value pop();
  53. #endif//CLOX__VM_H_