vm.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. Obj *objects;// 管理分配的 heap 内存
  31. } VM;
  32. typedef enum {
  33. INTERPRET_OK,
  34. INTERPRET_COMPILE_ERROR,
  35. INTERPRET_RUNTIME_ERROR
  36. } InterpretResult;
  37. extern VM vm;
  38. void initVM();
  39. void freeVM();
  40. /// \brief interpret 执行指令
  41. /// \param source 源代码
  42. /// \return InterpretResult
  43. InterpretResult interpret(const char *source);
  44. void push(Value value);
  45. Value pop();
  46. #endif//CLOX__VM_H_