Chapter 15.3
This commit is contained in:
parent
73dfe7efa5
commit
2adc0cc33a
4 changed files with 20 additions and 18 deletions
|
@ -6,20 +6,21 @@
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
OP_CONSTANT,
|
OP_CONSTANT,
|
||||||
|
OP_NEGATE,
|
||||||
OP_RETURN,
|
OP_RETURN,
|
||||||
} OpCode;
|
} OpCode;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int count;
|
int count;
|
||||||
int capacity;
|
int capacity;
|
||||||
uint8_t* code;
|
uint8_t *code;
|
||||||
int* lines;
|
int *lines;
|
||||||
ValueArray constants;
|
ValueArray constants;
|
||||||
} Chunk;
|
} Chunk;
|
||||||
|
|
||||||
void initChunk(Chunk* chunk);
|
void initChunk(Chunk *chunk);
|
||||||
void freeChunk(Chunk* chunk);
|
void freeChunk(Chunk *chunk);
|
||||||
void writeChunk(Chunk* chunk, uint8_t byte, int line);
|
void writeChunk(Chunk *chunk, uint8_t byte, int line);
|
||||||
int addConstant(Chunk* chunk, Value value);
|
int addConstant(Chunk *chunk, Value value);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
#include "value.h"
|
#include "value.h"
|
||||||
|
|
||||||
void disassembleChunk(Chunk* chunk, const char* name) {
|
void disassembleChunk(Chunk *chunk, const char *name) {
|
||||||
printf("== %s ==\n", name);
|
printf("== %s ==\n", name);
|
||||||
|
|
||||||
for (int offset = 0; offset < chunk->count;) {
|
for (int offset = 0; offset < chunk->count;) {
|
||||||
|
@ -11,7 +11,7 @@ void disassembleChunk(Chunk* chunk, const char* name) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int constantInstruction(const char* name, Chunk* chunk, int offset) {
|
static int constantInstruction(const char *name, Chunk *chunk, int offset) {
|
||||||
uint8_t constant = chunk->code[offset + 1];
|
uint8_t constant = chunk->code[offset + 1];
|
||||||
printf("%-16s %4d '", name, constant);
|
printf("%-16s %4d '", name, constant);
|
||||||
printValue(chunk->constants.values[constant]);
|
printValue(chunk->constants.values[constant]);
|
||||||
|
@ -19,15 +19,14 @@ static int constantInstruction(const char* name, Chunk* chunk, int offset) {
|
||||||
return offset + 2;
|
return offset + 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int simpleInstruction(const char* name, int offset) {
|
static int simpleInstruction(const char *name, int offset) {
|
||||||
printf("%s\n", name);
|
printf("%s\n", name);
|
||||||
return offset + 1;
|
return offset + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int disassembleInstruction(Chunk* chunk, int offset) {
|
int disassembleInstruction(Chunk *chunk, int offset) {
|
||||||
printf("%04d ", offset);
|
printf("%04d ", offset);
|
||||||
if (offset > 0 &&
|
if (offset > 0 && chunk->lines[offset] == chunk->lines[offset - 1]) {
|
||||||
chunk->lines[offset] == chunk->lines[offset - 1]) {
|
|
||||||
printf(" | ");
|
printf(" | ");
|
||||||
} else {
|
} else {
|
||||||
printf("%4d ", chunk->lines[offset]);
|
printf("%4d ", chunk->lines[offset]);
|
||||||
|
@ -37,6 +36,8 @@ 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_NEGATE:
|
||||||
|
return simpleInstruction("OP_NEGATE", offset);
|
||||||
case OP_RETURN:
|
case OP_RETURN:
|
||||||
return simpleInstruction("OP_RETURN", offset);
|
return simpleInstruction("OP_RETURN", offset);
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -12,6 +12,7 @@ int main(int argc, const char *argv[]) {
|
||||||
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);
|
||||||
|
writeChunk(&chunk, OP_NEGATE, 123);
|
||||||
|
|
||||||
writeChunk(&chunk, OP_RETURN, 123);
|
writeChunk(&chunk, OP_RETURN, 123);
|
||||||
|
|
||||||
|
|
|
@ -6,13 +6,9 @@
|
||||||
|
|
||||||
VM vm;
|
VM vm;
|
||||||
|
|
||||||
static void resetStack() {
|
static void resetStack() { vm.stackTop = vm.stack; }
|
||||||
vm.stackTop = vm.stack;
|
|
||||||
}
|
|
||||||
|
|
||||||
void initVM() {
|
void initVM() { resetStack(); }
|
||||||
resetStack();
|
|
||||||
}
|
|
||||||
|
|
||||||
void freeVM() {}
|
void freeVM() {}
|
||||||
|
|
||||||
|
@ -49,6 +45,9 @@ static InterpretResult run() {
|
||||||
push(constant);
|
push(constant);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case OP_NEGATE:
|
||||||
|
push(-pop());
|
||||||
|
break;
|
||||||
case OP_RETURN: {
|
case OP_RETURN: {
|
||||||
printValue(pop());
|
printValue(pop());
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
Loading…
Reference in a new issue