vm.h 1015 B

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