vm.h 916 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 "value.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. } VM;
  21. typedef enum {
  22. INTERPRET_OK,
  23. INTERPRET_COMPILE_ERROR,
  24. INTERPRET_RUNTIME_ERROR
  25. } InterpretResult;
  26. void initVM();
  27. void freeVM();
  28. /// \brief interpret 执行指令
  29. /// \param source 源代码
  30. /// \return InterpretResult
  31. InterpretResult interpret(const char *source);
  32. void push(Value value);
  33. Value pop();
  34. #endif //CLOX__VM_H_