Skip to content

Commit 5bf619d

Browse files
committed
fix/typo: fix seg fault related to initialize function
1 parent d1a2192 commit 5bf619d

5 files changed

Lines changed: 15 additions & 19 deletions

File tree

src/chunk.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <stdint.h>
2+
#include <stdio.h>
23

34
#include "chunk.h"
45
#include "memory.h"
@@ -22,12 +23,12 @@ void write_chunk(Chunk* chunk, u8 byte, int line)
2223
{
2324
if (chunk->capacity < chunk->count + 1)
2425
{
25-
int oldCapacity = chunk->capacity;
26-
chunk->capacity = GROW_CAPACITY(oldCapacity);
26+
int old_capacity = chunk->capacity;
27+
chunk->capacity = GROW_CAPACITY(old_capacity);
2728
chunk->code =
28-
GROW_ARRAY(u8, chunk->code, oldCapacity, chunk->capacity);
29+
GROW_ARRAY(u8, chunk->code, old_capacity, chunk->capacity);
2930
chunk->lines =
30-
GROW_ARRAY(int, chunk->lines, oldCapacity, chunk->capacity);
31+
GROW_ARRAY(int, chunk->lines, old_capacity, chunk->capacity);
3132
}
3233
chunk->code[chunk->count] = byte;
3334
chunk->lines[chunk->count] = line;

src/compiler.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ static bool immutable_globals[UINT8_MAX];
7676

7777
Parser parser;
7878
Compiler* current = NULL;
79-
Chunk* compiling_chunk;
8079

8180
static Chunk* current_chunk()
8281
{
@@ -216,7 +215,7 @@ static void patch_jump(int offset)
216215
static void init_compiler(Compiler* compiler, FunctionType type)
217216
{
218217
compiler->enclosing = current;
219-
compiler->function = NULL;
218+
compiler->function = new_function();
220219
compiler->type = type;
221220
compiler->local_count = 0;
222221
compiler->scope_depth = 0;
@@ -462,8 +461,9 @@ static void or_(bool can_assign)
462461

463462
static void string(bool can_assign)
464463
{
465-
emit_constant(OBJ_VAL(
466-
copy_string(parser.previous.start + 1, parser.previous.length - 2)));
464+
ObjString* str =
465+
copy_string(parser.previous.start + 1, parser.previous.length - 2);
466+
emit_constant(OBJ_VAL(str));
467467
}
468468

469469
static void named_variable(Token name, bool can_assign)
@@ -857,12 +857,11 @@ static inline void init_parser()
857857
parser.panic_mode = false;
858858
}
859859

860-
ObjFunction* compile(const char* source, Chunk* chunk)
860+
ObjFunction* compile(const char* source)
861861
{
862862
init_scanner(source);
863863
Compiler compiler;
864864
init_compiler(&compiler, TYPE_SCRIPT);
865-
compiling_chunk = chunk;
866865
init_parser();
867866

868867
advance();
@@ -871,7 +870,6 @@ ObjFunction* compile(const char* source, Chunk* chunk)
871870
declaration();
872871
}
873872

874-
end_compiler();
875873
ObjFunction* function = end_compiler();
876874
return parser.had_error ? NULL : function;
877875
}

src/compiler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
#include "chunk.h"
55
#include "object.h"
66

7-
ObjFunction* compile(const char* source, Chunk* chunk);
7+
ObjFunction* compile(const char* source);
88

99
#endif

src/value.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ void write_value_array(ValueArray* array, Value value)
1717
{
1818
if (array->capacity < array->count + 1)
1919
{
20-
int oldCapacity = array->capacity;
21-
array->capacity = GROW_CAPACITY(oldCapacity);
20+
int old_capacity = array->capacity;
21+
array->capacity = GROW_CAPACITY(old_capacity);
2222
array->values =
23-
GROW_ARRAY(Value, array->values, oldCapacity, array->capacity);
23+
GROW_ARRAY(Value, array->values, old_capacity, array->capacity);
2424
}
2525

2626
array->values[array->count] = value;

src/vm.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,7 @@ static InterpretResult run()
276276

277277
InterpretResult interpret(char* source)
278278
{
279-
Chunk chunk;
280-
init_chunk(&chunk);
281-
282-
ObjFunction* function = compile(source, &chunk);
279+
ObjFunction* function = compile(source);
283280
if (function == NULL)
284281
return INTERPRET_COMPILE_ERROR;
285282

0 commit comments

Comments
 (0)