add constants
This commit is contained in:
parent
119edcbadd
commit
a2e5d43e1e
14
src/chunk.c
14
src/chunk.c
|
@ -6,6 +6,7 @@ void newChunk(Chunk* chunk) {
|
|||
chunk->count = 0;
|
||||
chunk->capacity = 0;
|
||||
chunk->code = NULL;
|
||||
newValueArray(&chunk->constants);
|
||||
}
|
||||
|
||||
void writeChunk(Chunk *chunk, uint8_t byte) {
|
||||
|
@ -18,5 +19,16 @@ void writeChunk(Chunk *chunk, uint8_t byte) {
|
|||
|
||||
chunk->code[chunk->count] = byte;
|
||||
chunk->count++;
|
||||
|
||||
}
|
||||
|
||||
void freeChunk(Chunk *chunk) {
|
||||
FREE_ARRAY(uint8_t, chunk->code, chunk->capacity);
|
||||
freeValueArray(&chunk->constants);
|
||||
newChunk(chunk);
|
||||
}
|
||||
|
||||
int addConstant(Chunk* chunk, Value value) {
|
||||
writeValueArray(&chunk->constants, value);
|
||||
return chunk->constants.count - 1;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,8 +3,11 @@
|
|||
|
||||
#include "common.h"
|
||||
#include "memory.h"
|
||||
#include "value.h"
|
||||
|
||||
typedef enum {
|
||||
OP_NOOP,
|
||||
OP_CONSTANT,
|
||||
OP_RETURN,
|
||||
} OpCode;
|
||||
|
||||
|
@ -12,10 +15,12 @@ typedef struct Chunk {
|
|||
int count;
|
||||
int capacity;
|
||||
uint8_t *code;
|
||||
ValueArray constants;
|
||||
} Chunk;
|
||||
|
||||
void newChunk(Chunk *chunk);
|
||||
void freeChunk(Chunk *chunk);
|
||||
void writeChunk(Chunk *chunk, uint8_t byte);
|
||||
int addConstant(Chunk *chunk, Value value);
|
||||
|
||||
#endif
|
||||
|
|
14
src/debug.c
14
src/debug.c
|
@ -1,6 +1,7 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#include "debug.h"
|
||||
#include "value.h"
|
||||
|
||||
void disassembleChunk(Chunk* chunk, const char* name) {
|
||||
printf("== %s ==\n", name);
|
||||
|
@ -10,6 +11,15 @@ void disassembleChunk(Chunk* chunk, const char* name) {
|
|||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static int simpleInstruction(const char* name, int offset) {
|
||||
printf("%s\n", name);
|
||||
return offset + 1;
|
||||
|
@ -20,6 +30,10 @@ int disassembleInstruction(Chunk* chunk, int offset) {
|
|||
|
||||
uint8_t instruction = chunk->code[offset];
|
||||
switch (instruction) {
|
||||
case OP_NOOP:
|
||||
return simpleInstruction("OP_NOOP", offset);
|
||||
case OP_CONSTANT:
|
||||
return constantInstruction("OP_CONSTANT", chunk, offset);
|
||||
case OP_RETURN:
|
||||
return simpleInstruction("OP_RETURN", offset);
|
||||
default:
|
||||
|
|
|
@ -5,6 +5,11 @@
|
|||
int main(int argc, const char** argv) {
|
||||
Chunk chunk;
|
||||
newChunk(&chunk);
|
||||
|
||||
int constant = addConstant(&chunk, 1.2);
|
||||
writeChunk(&chunk, OP_CONSTANT);
|
||||
writeChunk(&chunk, constant);
|
||||
|
||||
writeChunk(&chunk, OP_RETURN);
|
||||
|
||||
disassembleChunk(&chunk, "test chunk");
|
||||
|
|
|
@ -14,8 +14,3 @@ void* reallocate(void *pointer, size_t oldSize, size_t newSize) {
|
|||
return result;
|
||||
}
|
||||
|
||||
void freeChunk(Chunk *chunk) {
|
||||
FREE_ARRAY(uint8_t, chunk->code, chunk->capacity);
|
||||
newChunk(chunk);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#include "memory.h"
|
||||
#include "value.h"
|
||||
|
||||
void newValueArray(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);
|
||||
newValueArray(array);
|
||||
}
|
||||
|
||||
void printValue(Value value) {
|
||||
printf("%g", value);
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
#ifndef ztl_value_h
|
||||
#define ztl_value_h
|
||||
|
||||
#include "common.h"
|
||||
|
||||
typedef double Value;
|
||||
|
||||
typedef struct {
|
||||
int capacity;
|
||||
int count;
|
||||
Value *values;
|
||||
} ValueArray;
|
||||
|
||||
void newValueArray(ValueArray *array);
|
||||
void writeValueArray(ValueArray *array, Value value);
|
||||
void freeValueArray(ValueArray *array);
|
||||
void printValue(Value value);
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue