/** ****************************************************************************** * @file : vm.h * @author : simon * @brief : hand it a chunk of code—literally a Chunk—and it runs it * @attention : None * @date : 2023/8/17 ****************************************************************************** */ #ifndef CLOX__VM_H_ #define CLOX__VM_H_ #include "compiler.h" #include "object.h" #include "table.h" #define FRAMES_MAX 64 #define STACK_MAX (FRAMES_MAX * UINT8_COUNT) typedef struct { ObjClosure *closure; uint8_t *ip; Value *slots;// points into the VM’s value stack // at the first slot that this function can use } CallFrame; typedef struct VM { CallFrame frames[FRAMES_MAX]; int frameCount; Value stack[STACK_MAX]; Value *stackTop;// 栈指针 Table globals; Table strings; // ObjString *initString;// 初始化方法 ObjUpvalue *openUpvalues; size_t bytesAllocated; size_t nextGC; Obj *objects;// 管理分配的 heap 内存 int grayCount; int grayCapacity; Obj **grayStack; } VM; typedef enum { INTERPRET_OK, INTERPRET_COMPILE_ERROR, INTERPRET_RUNTIME_ERROR } InterpretResult; extern VM vm; void initVM(); void freeVM(); /// \brief interpret 执行指令 /// \param source 源代码 /// \return InterpretResult InterpretResult interpret(const char *source); void push(Value value); Value pop(); #endif//CLOX__VM_H_