This commit is contained in:
Randy Jordan 2024-09-01 17:43:28 -05:00
parent 021696daa1
commit c983f166c8
No known key found for this signature in database
GPG Key ID: BF8CCF4CDDE47493
7 changed files with 75 additions and 2 deletions

14
include/\ Normal file
View File

@ -0,0 +1,14 @@
#ifndef VM_INCLUDED
#define VM_INLCUDED
#include "chunk.h"
typedef struct {
Chunk *chunk;
} VM;
void initVM();
void freeVM();
#endif

View File

@ -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

14
include/vm.h Normal file
View File

@ -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

View File

@ -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;

View File

@ -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);
@ -34,6 +44,9 @@ int disassembleInstruction(Chunk *chunk, int offset){
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:

14
src/vm.c Normal file
View File

@ -0,0 +1,14 @@
#include "../include/common.h"
#include "../include/vm.h"
VM vm;
void initVM(void){
}
void freeVM(void){
}

View File

@ -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;