Chapter 26.7
This commit is contained in:
parent
38e4c37f0f
commit
ad6a98f6ad
3 changed files with 18 additions and 8 deletions
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include "chunk.h"
|
||||
#include "memory.h"
|
||||
#include "vm.h"
|
||||
|
||||
void initChunk(Chunk *chunk) {
|
||||
chunk->count = 0;
|
||||
|
@ -22,7 +23,8 @@ void writeChunk(Chunk* chunk, uint8_t byte, int line) {
|
|||
if (chunk->capacity < chunk->count + 1) {
|
||||
int oldCapacity = chunk->capacity;
|
||||
chunk->capacity = GROW_CAPACITY(oldCapacity);
|
||||
chunk->code = GROW_ARRAY(uint8_t, chunk->code, oldCapacity, chunk->capacity);
|
||||
chunk->code =
|
||||
GROW_ARRAY(uint8_t, chunk->code, oldCapacity, chunk->capacity);
|
||||
chunk->lines = GROW_ARRAY(int, chunk->lines, oldCapacity, chunk->capacity);
|
||||
}
|
||||
|
||||
|
@ -32,6 +34,8 @@ void writeChunk(Chunk* chunk, uint8_t byte, int line) {
|
|||
}
|
||||
|
||||
int addConstant(Chunk *chunk, Value value) {
|
||||
push(value);
|
||||
writeValueArray(&chunk->constants, value);
|
||||
pop();
|
||||
return chunk->constants.count - 1;
|
||||
}
|
||||
|
|
|
@ -58,7 +58,11 @@ static ObjString *allocateString(char *chars, int length, uint32_t hash) {
|
|||
string->length = length;
|
||||
string->chars = chars;
|
||||
string->hash = hash;
|
||||
|
||||
push(OBJ_VAL(string));
|
||||
tableSet(&vm.strings, string, NIL_VAL);
|
||||
pop();
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
|
|
|
@ -163,8 +163,8 @@ static bool isFalsey(Value value) {
|
|||
}
|
||||
|
||||
static void concatenate() {
|
||||
ObjString *b = AS_STRING(pop());
|
||||
ObjString *a = AS_STRING(pop());
|
||||
ObjString *b = AS_STRING(peek(0));
|
||||
ObjString *a = AS_STRING(peek(1));
|
||||
|
||||
int length = a->length + b->length;
|
||||
char *chars = ALLOCATE(char, length + 1);
|
||||
|
@ -173,6 +173,8 @@ static void concatenate() {
|
|||
chars[length] = '\0';
|
||||
|
||||
ObjString *result = takeString(chars, length);
|
||||
pop();
|
||||
pop();
|
||||
push(OBJ_VAL(result));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue