This commit is contained in:
zongor 2025-05-03 23:18:20 -04:00
parent 72c5830637
commit 5a1a399d4f
5 changed files with 154 additions and 0 deletions

47
src/' Normal file
View File

@ -0,0 +1,47 @@
#include "common.h"
#include "vm.h"
VM vm;
void newVM() {
}
void freeVM() {
}
static InterpretResult run() {
#define READ_BYTE() (*vm.ip++)
#define READ_CONSTANT() (vm.chunk->constants.values[READ_BYTE()])
for (;;) {
#ifdef DEBUG_TRACE_EXECUTION
disassembleInstruction(vm.chunk,
(int)(vm.ip - vm.chunk->code));
#endif
uint8_t instruction;
switch (instruction = READ_BYTE()) {
case OP_CONSTANT: {
Value constant = READ_CONSTANT();
printValue(constant);
printf("\n");
break;
}
case OP_RETURN: {
return INTERPRET_OK;
}
}
}
#undef READ_BYTE
#undef READ_CONSTANT
}
InterpretResult interpret(Chunk* chunk) {
vm.chunk = chunk;
vm.ip = vm.chunk->code;
return run();
}

View File

@ -5,4 +5,6 @@
#include <stddef.h>
#include <stdint.h>
#define DEBUG_TRACE_EXECUTION
#endif

View File

@ -1,8 +1,11 @@
#include "common.h"
#include "chunk.h"
#include "debug.h"
#include "vm.h"
int main(int argc, const char** argv) {
newVM();
Chunk chunk;
newChunk(&chunk);
@ -13,6 +16,7 @@ int main(int argc, const char** argv) {
writeChunk(&chunk, OP_RETURN, 1);
disassembleChunk(&chunk, "test chunk");
freeVM();
freeChunk(&chunk);
return 0;
}

72
src/vm.c Normal file
View File

@ -0,0 +1,72 @@
#include "common.h"
#include "debug.h"
#include "vm.h"
#include <stdio.h>
VM vm;
static void resetStack() {
vm.stackTop = vm.stack;
}
void newVM() {
resetStack();
}
void freeVM() {
}
void push(Value value) {
*vm.stackTop = value;
vm.stackTop++;
}
Value pop() {
vm.stackTop--;
return *vm.stackTop;
}
static InterpretResult run() {
#define READ_BYTE() (*vm.ip++)
#define READ_CONSTANT() (vm.chunk->constants.values[READ_BYTE()])
for (;;) {
#ifdef DEBUG_TRACE_EXECUTION
printf(" ");
for (Value* slot = vm.stack; slot < vm.stackTop; slot++) {
printf("[ ");
printValue(*slot);
printf(" ]");
}
printf("\n");
disassembleInstruction(vm.chunk,
(int)(vm.ip - vm.chunk->code));
#endif
uint8_t instruction;
switch (instruction = READ_BYTE()) {
case OP_CONSTANT: {
Value constant = READ_CONSTANT();
push(constant);
break;
}
case OP_RETURN: {
printValue(pop());
printf("\n");
return INTERPRET_OK;
}
}
}
#undef READ_BYTE
#undef READ_CONSTANT
}
InterpretResult interpret(Chunk* chunk) {
vm.chunk = chunk;
vm.ip = vm.chunk->code;
return run();
}

29
src/vm.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef ztl_vm_h
#define ztl_vm_h
#include "chunk.h"
#include "value.h"
#define STACK_MAX 256
typedef struct {
Chunk *chunk;
uint8_t *ip;
Value stack[STACK_MAX];
Value *stackTop;
} VM;
typedef enum {
INTERPRET_OK,
INTERPRET_COMPILE_ERROR,
INTERPRET_RUNTIME_ERROR
} InterpretResult;
void newVM();
void freeVM();
InterpretResult interpret(Chunk *chunk);
void push(Value value);
Value pop();
#endif