vm.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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;
  21. } CallFrame;
  22. typedef struct VM {
  23. CallFrame frames[FRAMES_MAX];
  24. int frameCount;
  25. Value stack[STACK_MAX];
  26. Value *stackTop;// 栈指针
  27. Table globals;
  28. Table strings;//
  29. ObjUpvalue *openUpvalues;
  30. size_t bytesAllocated;
  31. size_t nextGC;
  32. Obj *objects;// 管理分配的 heap 内存
  33. int grayCount;
  34. int grayCapacity;
  35. Obj** grayStack;
  36. } VM;
  37. typedef enum {
  38. INTERPRET_OK,
  39. INTERPRET_COMPILE_ERROR,
  40. INTERPRET_RUNTIME_ERROR
  41. } InterpretResult;
  42. extern VM vm;
  43. void initVM();
  44. void freeVM();
  45. /// \brief interpret 执行指令
  46. /// \param source 源代码
  47. /// \return InterpretResult
  48. InterpretResult interpret(const char *source);
  49. void push(Value value);
  50. Value pop();
  51. #endif//CLOX__VM_H_