diff --git "a/include/\\" "b/include/\\" new file mode 100644 index 0000000..502e094 --- /dev/null +++ "b/include/\\" @@ -0,0 +1,14 @@ +#ifndef VM_INCLUDED +#define VM_INLCUDED + +#include "chunk.h" + + +typedef struct { + Chunk *chunk; +} VM; + +void initVM(); +void freeVM(); + +#endif diff --git a/include/chunk.h b/include/chunk.h index 56f40a9..ad65318 100644 --- a/include/chunk.h +++ b/include/chunk.h @@ -5,6 +5,7 @@ #include "value.h" typedef enum { + OP_CONSTANT_LONG, OP_CONSTANT, OP_RETURN, OP_COUNT, @@ -21,6 +22,7 @@ typedef struct { void initChunk(Chunk *chunk); void freeChunk(Chunk *chunk); void writeChunk(Chunk *chunk, uint8_t byte,int line); +void writeConstant(Chunk *chunk, Value value,int line); int addConstant(Chunk *chunk, Value value); #endif diff --git a/include/vm.h b/include/vm.h new file mode 100644 index 0000000..264b216 --- /dev/null +++ b/include/vm.h @@ -0,0 +1,14 @@ +#ifndef VM_INCLUDED +#define VM_INLCUDED + +#include "chunk.h" + + +typedef struct { + Chunk *chunk; +} VM; + +void initVM(void); +void freeVM(void); + +#endif diff --git a/src/chunk.c b/src/chunk.c index cdf793b..e8b1085 100644 --- a/src/chunk.c +++ b/src/chunk.c @@ -24,6 +24,18 @@ void writeChunk(Chunk *chunk, uint8_t byte, int line){ chunk->lines[chunk->len] = line; chunk->len++; } +void writeConstant(Chunk* chunk, Value value, int line){ + int index = addConstant(chunk, value); // Get the index + if (index < 256) { // If it's less than a byte + writeChunk(chunk, OP_CONSTANT, line); // Use the short one instead. + writeChunk(chunk, (uint8_t)index, line); + } else { // It's bigger than a byte, mask off 24 bytes into write chunk. + writeChunk(chunk, OP_CONSTANT_LONG, line); + writeChunk(chunk, (uint8_t)(index & 0xff), line); + writeChunk(chunk, (uint8_t)((index >> 8) & 0xff), line); + writeChunk(chunk, (uint8_t)((index >> 16) & 0xff), line); + } +} int addConstant(Chunk *chunk, Value value){ writeValueArray(&chunk->constants,value); return chunk->constants.len-1; diff --git a/src/debug.c b/src/debug.c index d834772..fae8016 100644 --- a/src/debug.c +++ b/src/debug.c @@ -19,6 +19,16 @@ static int simpleInstruction(const char* name, int offset){ printf("%s\n",name); return offset +1; } +static int longConstantInstruction(const char* name, Chunk* chunk, + int offset) { + uint32_t constant = chunk->code[offset + 1] | + (chunk->code[offset + 2] << 8) | + (chunk->code[offset + 3] << 16); + printf("%-16s %4d '", name, constant); + printValue(chunk->constants.values[constant]); + printf("'\n"); + return offset + 4; +} int disassembleInstruction(Chunk *chunk, int offset){ printf("%04d\t",offset); @@ -33,7 +43,10 @@ int disassembleInstruction(Chunk *chunk, int offset){ switch(instruction){ case OP_CONSTANT: return constantInstruction("OP_CONSTANT", chunk,offset); - + + case OP_CONSTANT_LONG: + return longConstantInstruction("OP_CONSTANT_LONG", chunk, offset); + case OP_RETURN: return simpleInstruction("OP_RETURN",offset); default: diff --git a/src/vm.c b/src/vm.c new file mode 100644 index 0000000..470c7f3 --- /dev/null +++ b/src/vm.c @@ -0,0 +1,14 @@ +#include "../include/common.h" +#include "../include/vm.h" + + +VM vm; + +void initVM(void){ + +} + +void freeVM(void){ + +} + diff --git a/tests/01_main.c b/tests/01_main.c index c7074bd..ac83ce9 100644 --- a/tests/01_main.c +++ b/tests/01_main.c @@ -1,17 +1,21 @@ #include "../include/common.h" #include "../include/chunk.h" #include "../include/debug.h" +#include "../include/vm.h" int main(void){ + initVM(); + Chunk chunk; initChunk(&chunk); int constant = addConstant(&chunk,1.2); writeChunk(&chunk, OP_CONSTANT,10); writeChunk(&chunk,constant,10); - writeChunk(&chunk, OP_RETURN,10); + // writeChunk(&chunk, OP_RETURN,10); disassembleChunk(&chunk,"Test Chunk"); + freeVM(); freeChunk(&chunk); return 0;