Finish Chapter 15
Implement a basic VM that is capable of simple math. Signed-off-by: Daniel Henry <iamdanhenry@gmail.com>
This commit is contained in:
@@ -6,6 +6,11 @@
|
|||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
OP_CONSTANT,
|
OP_CONSTANT,
|
||||||
|
OP_ADD,
|
||||||
|
OP_SUBTRACT,
|
||||||
|
OP_MULTIPLY,
|
||||||
|
OP_DIVIDE,
|
||||||
|
OP_NEGATE,
|
||||||
OP_RETURN,
|
OP_RETURN,
|
||||||
} OpCode;
|
} OpCode;
|
||||||
|
|
||||||
|
|||||||
@@ -5,4 +5,6 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#define DEBUG_TRACE_EXECUTION
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
28
include/vm.h
Normal file
28
include/vm.h
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#ifndef clox_vm_h
|
||||||
|
#define clox_vm_h
|
||||||
|
|
||||||
|
#include "chunk.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 initVM();
|
||||||
|
void freeVM();
|
||||||
|
|
||||||
|
InterpretResult interpret(Chunk *chunk);
|
||||||
|
void push(Value value);
|
||||||
|
Value pop();
|
||||||
|
|
||||||
|
#endif /* clox_vm_h */
|
||||||
10
src/debug.c
10
src/debug.c
@@ -37,6 +37,16 @@ int disassembleInstruction(Chunk *chunk, int offset) {
|
|||||||
switch (instruction) {
|
switch (instruction) {
|
||||||
case OP_CONSTANT:
|
case OP_CONSTANT:
|
||||||
return constantInstruction("OP_CONSTANT", chunk, offset);
|
return constantInstruction("OP_CONSTANT", chunk, offset);
|
||||||
|
case OP_ADD:
|
||||||
|
return simpleInstruction("OP_ADD", offset);
|
||||||
|
case OP_SUBTRACT:
|
||||||
|
return simpleInstruction("OP_SUBTRACT", offset);
|
||||||
|
case OP_MULTIPLY:
|
||||||
|
return simpleInstruction("OP_MULTIPLY", offset);
|
||||||
|
case OP_DIVIDE:
|
||||||
|
return simpleInstruction("OP_DIVIDE", offset);
|
||||||
|
case OP_NEGATE:
|
||||||
|
return simpleInstruction("OP_NEGATE", offset);
|
||||||
case OP_RETURN:
|
case OP_RETURN:
|
||||||
return simpleInstruction("OP_RETURN", offset);
|
return simpleInstruction("OP_RETURN", offset);
|
||||||
default:
|
default:
|
||||||
|
|||||||
18
src/main.c
18
src/main.c
@@ -1,16 +1,34 @@
|
|||||||
#include "chunk.h"
|
#include "chunk.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
|
#include "vm.h"
|
||||||
|
|
||||||
int main(int argc, const char *argv[]) {
|
int main(int argc, const char *argv[]) {
|
||||||
|
initVM();
|
||||||
|
|
||||||
Chunk chunk;
|
Chunk chunk;
|
||||||
initChunk(&chunk);
|
initChunk(&chunk);
|
||||||
int constant = addConstant(&chunk, 1.2);
|
int constant = addConstant(&chunk, 1.2);
|
||||||
writeChunk(&chunk, OP_CONSTANT, 123);
|
writeChunk(&chunk, OP_CONSTANT, 123);
|
||||||
writeChunk(&chunk, constant, 123);
|
writeChunk(&chunk, constant, 123);
|
||||||
|
|
||||||
|
constant = addConstant(&chunk, 3.4);
|
||||||
|
writeChunk(&chunk, OP_CONSTANT, 123);
|
||||||
|
writeChunk(&chunk, constant, 123);
|
||||||
|
|
||||||
|
writeChunk(&chunk, OP_ADD, 123);
|
||||||
|
|
||||||
|
constant = addConstant(&chunk, 5.6);
|
||||||
|
writeChunk(&chunk, OP_CONSTANT, 123);
|
||||||
|
writeChunk(&chunk, constant, 123);
|
||||||
|
|
||||||
|
writeChunk(&chunk, OP_DIVIDE, 123);
|
||||||
|
writeChunk(&chunk, OP_NEGATE, 123);
|
||||||
writeChunk(&chunk, OP_RETURN, 123);
|
writeChunk(&chunk, OP_RETURN, 123);
|
||||||
|
|
||||||
disassembleChunk(&chunk, "test chunk");
|
disassembleChunk(&chunk, "test chunk");
|
||||||
|
interpret(&chunk);
|
||||||
|
freeVM();
|
||||||
freeChunk(&chunk);
|
freeChunk(&chunk);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
89
src/vm.c
Normal file
89
src/vm.c
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
#include "vm.h"
|
||||||
|
#include "chunk.h"
|
||||||
|
#include "common.h"
|
||||||
|
#include "debug.h"
|
||||||
|
#include "value.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
VM vm;
|
||||||
|
|
||||||
|
static void resetStack() {
|
||||||
|
vm.stackTop = vm.stack;
|
||||||
|
}
|
||||||
|
|
||||||
|
void initVM() {
|
||||||
|
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()])
|
||||||
|
#define BINARY_OP(op) \
|
||||||
|
do { \
|
||||||
|
double b = pop(); \
|
||||||
|
double a = pop(); \
|
||||||
|
push(a op b); \
|
||||||
|
} while (false)
|
||||||
|
|
||||||
|
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_ADD:
|
||||||
|
BINARY_OP(+);
|
||||||
|
break;
|
||||||
|
case OP_SUBTRACT:
|
||||||
|
BINARY_OP(-);
|
||||||
|
break;
|
||||||
|
case OP_MULTIPLY:
|
||||||
|
BINARY_OP(*);
|
||||||
|
break;
|
||||||
|
case OP_DIVIDE:
|
||||||
|
BINARY_OP(/);
|
||||||
|
break;
|
||||||
|
case OP_NEGATE:
|
||||||
|
push(-pop());
|
||||||
|
break;
|
||||||
|
case OP_RETURN:
|
||||||
|
printValue(pop());
|
||||||
|
printf("\n");
|
||||||
|
return INTERPRET_OK;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#undef READ_CONSTANT
|
||||||
|
#undef READ_BYTE
|
||||||
|
#undef BINARY_OP
|
||||||
|
}
|
||||||
|
|
||||||
|
InterpretResult interpret(Chunk *chunk) {
|
||||||
|
vm.chunk = chunk;
|
||||||
|
vm.ip = vm.chunk->code;
|
||||||
|
return run();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user