VM
This commit is contained in:
parent
021696daa1
commit
c983f166c8
14
include/\
Normal file
14
include/\
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef VM_INCLUDED
|
||||
#define VM_INLCUDED
|
||||
|
||||
#include "chunk.h"
|
||||
|
||||
|
||||
typedef struct {
|
||||
Chunk *chunk;
|
||||
} VM;
|
||||
|
||||
void initVM();
|
||||
void freeVM();
|
||||
|
||||
#endif
|
@ -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
14
include/vm.h
Normal 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
|
12
src/chunk.c
12
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;
|
||||
|
15
src/debug.c
15
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:
|
||||
|
14
src/vm.c
Normal file
14
src/vm.c
Normal file
@ -0,0 +1,14 @@
|
||||
#include "../include/common.h"
|
||||
#include "../include/vm.h"
|
||||
|
||||
|
||||
VM vm;
|
||||
|
||||
void initVM(void){
|
||||
|
||||
}
|
||||
|
||||
void freeVM(void){
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user