vm.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 "object.h"
  13. #include "compiler.h"
  14. #include "table.h"
  15. #define FRAMES_MAX 64
  16. #define STACK_MAX (FRAMES_MAX * UINT8_COUNT)
  17. typedef struct {
  18. ObjFunction *function;
  19. uint8_t *ip;
  20. Value *slots;
  21. } CallFrame;
  22. typedef struct {
  23. CallFrame frames[FRAMES_MAX];
  24. int frameCount;
  25. Value stack[STACK_MAX];
  26. Value *stackTop;// 栈指针
  27. Table globals;
  28. Table strings;//
  29. Obj *objects; // 管理分配的 heap 内存
  30. } VM;
  31. typedef enum {
  32. INTERPRET_OK,
  33. INTERPRET_COMPILE_ERROR,
  34. INTERPRET_RUNTIME_ERROR
  35. } InterpretResult;
  36. extern VM vm;
  37. void initVM();
  38. void freeVM();
  39. /// \brief interpret 执行指令
  40. /// \param source 源代码
  41. /// \return InterpretResult
  42. InterpretResult interpret(const char *source);
  43. void push(Value value);
  44. Value pop();
  45. #endif//CLOX__VM_H_