-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
144 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <stdio.h> | ||
|
||
#include "debug.h" | ||
|
||
void disassembleChunk(Chunk* chunk, const char* name) { | ||
printf("== %s ==\n", name); | ||
|
||
for (int offset = 0; offset < chunk->count;) { | ||
offset = disassembleInstruction(chunk, offset); | ||
} | ||
} | ||
|
||
static int simpleInstruction(const char* name, int offset) { | ||
printf("%s\n", name); | ||
return offset + 1; | ||
} | ||
|
||
static int constantInstruction(const char* name, Chunk* chunk, int offset) { | ||
uint8_t constant = chunk->code[offset + 1]; | ||
printf("%-16s %4d '", name, constant); | ||
printValue(chunk->constants.values[constant]); | ||
printf("'\n"); | ||
return offset + 2; | ||
} | ||
|
||
int disassembleInstruction(Chunk* chunk, int offset) { | ||
printf("%04d ", offset); | ||
printf("%4d ", chunk->lines[offset]); | ||
|
||
uint8_t instruction = chunk->code[offset]; | ||
switch (instruction) { | ||
case OP_RETURN: | ||
return simpleInstruction("OP_RETURN", offset); | ||
case OP_CONSTANT: | ||
return constantInstruction("OP_CONSTANT", chunk, offset); | ||
default: | ||
printf("Unknown opcode %d\n", instruction); | ||
return offset + 1; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#ifndef clox_debug_h | ||
#define clox_debug_h | ||
|
||
#include "chunk.h" | ||
|
||
void disassembleChunk(Chunk* chunk, const char* name); | ||
int disassembleInstruction(Chunk* chunk, int offset); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,19 @@ | ||
#include "common.h" | ||
#include "chunk.h" | ||
#include "debug.h" | ||
|
||
int main(int argc, const char* argv[]) { | ||
Chunk chunk; | ||
initChunk(&chunk); | ||
|
||
int constant = addConstant(&chunk, 1.2); | ||
writeChunk(&chunk, OP_CONSTANT, 123); | ||
writeChunk(&chunk, OP_CONSTANT, 123); | ||
writeChunk(&chunk, OP_CONSTANT, 123); | ||
writeChunk(&chunk, OP_CONSTANT, 123); | ||
writeChunk(&chunk, constant, 123); | ||
|
||
disassembleChunk(&chunk, "test chunk"); | ||
freeChunk(&chunk); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
#include <stdlib.h> | ||
|
||
#include "memory.h" | ||
#include "common.h" | ||
|
||
void* reallocate(void* pointer, size_t oldSize, size_t newSize) { | ||
if (newSize == 0) { | ||
free(pointer); | ||
return NULL; | ||
} | ||
void* result = realloc(pointer, newSize); | ||
if (result == NULL) exit(1); | ||
return result; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <stdio.h> | ||
|
||
#include "memory.h" | ||
#include "value.h" | ||
|
||
void initValueArray(ValueArray* array) { | ||
array->values = NULL; | ||
array->capacity = 0; | ||
array->count = 0; | ||
} | ||
|
||
void writeValueArray(ValueArray* array, Value value) { | ||
if (array->capacity < array->count + 1) { | ||
int oldCapacity = array->capacity; | ||
array->capacity = GROW_CAPACITY(oldCapacity); | ||
array->values = GROW_ARRAY(Value, array->values, oldCapacity, array->capacity); | ||
} | ||
array->values[array->count] = value; | ||
array->count++; | ||
} | ||
|
||
void freeValueArray(ValueArray* array) { | ||
FREE_ARRAY(Value, array->values, array->capacity); | ||
initValueArray(array); | ||
} | ||
|
||
void printValue(Value value) { | ||
printf("%g", value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef clox_value_h | ||
#define clox_value_h | ||
|
||
typedef double Value; | ||
|
||
typedef struct { | ||
int capacity; | ||
int count; | ||
Value* values; | ||
} ValueArray; | ||
|
||
void initValueArray(ValueArray* array); | ||
void writeValueArray(ValueArray* array, Value value); | ||
void freeValueArray(ValueArray* array); | ||
void printValue(Value value); | ||
|
||
#endif |