Skip to content

Commit 202d9b8

Browse files
committed
Converting void vectors to typed, starting with functions
1 parent e11dab6 commit 202d9b8

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

src/interpreter.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var_value run(program_state* prog, char* start_func)
1111
fprintf(stderr, "Error: function '%s' not found.\n", func_name);
1212
exit(0);
1313
}
14-
function* func = GET_FUNCTION(&prog->functions, var->v.func_loc);
14+
function* func = &prog->functions.a[var->v.func_loc];
1515
prog->func = func;
1616
prog->stmt_list = &func->stmt_list;
1717
func->pc = 0;
@@ -24,7 +24,7 @@ var_value run(program_state* prog, char* start_func)
2424

2525
var_value ret = prog->func->ret_val;
2626

27-
cvec_free_void(&prog->functions);
27+
cvec_free_function(&prog->functions);
2828
cvec_free_str(&prog->global_variables);
2929
cvec_free_void(&prog->global_values);
3030
cvec_free_void(&prog->expressions);
@@ -421,7 +421,7 @@ var_value execute_expr(program_state* prog, expression* e)
421421
old_func = prog->func;
422422

423423
//look_up_value should never return NULL here, parsing should catch all errors like that
424-
func = GET_FUNCTION(&prog->functions, look_up_value(prog, e->left->tok.v.id, ONLY_GLOBAL)->v.func_loc);
424+
func = &prog->functions.a[look_up_value(prog, e->left->tok.v.id, ONLY_GLOBAL)->v.func_loc];
425425

426426
if (func->n_params) //could also check e->right->tok.type = VOID
427427
execute_expr_list(prog, func, e->right);

src/parser.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <stdio.h>
1212

1313

14+
CVEC_NEW_DEFS2(function, RESIZE)
1415

1516

1617
expression* make_expression(program_state* prog)
@@ -208,7 +209,7 @@ void parse_program_string(program_state* prog, char* string)
208209
prog->pc = NULL;
209210
prog->stmt_list = NULL;
210211
prog->bindings = NULL;
211-
cvec_void(&prog->functions, 0, 10, sizeof(function), free_function, init_function);
212+
cvec_function(&prog->functions, 0, 10, free_function, init_function);
212213
cvec_str(&prog->global_variables, 0, 20);
213214
cvec_void(&prog->global_values, 0, 20, sizeof(var_value), free_var_value, NULL);
214215

@@ -268,7 +269,7 @@ void parse_program_file(program_state* prog, FILE* file)
268269
prog->pc = NULL;
269270
prog->stmt_list = NULL;
270271
prog->bindings = NULL;
271-
cvec_void(&prog->functions, 0, 10, sizeof(function), free_function, init_function);
272+
cvec_function(&prog->functions, 0, 10, free_function, init_function);
272273
cvec_str(&prog->global_variables, 0, 20);
273274
cvec_void(&prog->global_values, 0, 20, sizeof(var_value), free_var_value, NULL);
274275

@@ -359,7 +360,7 @@ void function_declarator(parsing_state* p, program_state* prog, var_type vtype)
359360
//TODO make sure it's not already defined and make sure it matches previous declarations
360361

361362
function* func_ptr;
362-
cvec_push_void(&prog->functions, NULL); //initialization is done automatically in init_function
363+
cvec_push_function(&prog->functions, NULL); //initialization is done automatically in init_function
363364
assert(prog->functions.size <= 10); //for now prevent possibility of bug TODO what was this for?
364365

365366
var_value var;
@@ -369,7 +370,7 @@ void function_declarator(parsing_state* p, program_state* prog, var_type vtype)
369370
cvec_push_str(&prog->global_variables, tok->v.id);
370371
cvec_push_void(&prog->global_values, &var);
371372

372-
func_ptr = cvec_back_void(&prog->functions);
373+
func_ptr = cvec_back_function(&prog->functions);
373374

374375
func_ptr->n_params = 0;
375376
func_ptr->ret_val.type = vtype;

src/parser.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616

1717
#define GET_STMT(VEC, I) CVEC_GET_VOID(VEC, statement, I)
18-
#define GET_FUNCTION(VEC, I) CVEC_GET_VOID(VEC, function, I)
1918
#define GET_SYMBOL(VEC, I) CVEC_GET_VOID(VEC, symbol, I)
2019
#define GET_BINDING(VEC, I) CVEC_GET_VOID(VEC, binding, I)
2120

@@ -165,12 +164,14 @@ typedef struct function
165164
cvector_i label_locs;
166165
} function;
167166

167+
CVEC_NEW_DECLS2(function)
168168

169169

170+
#define RESIZE(x) ((x+1)*2)
170171

171172
typedef struct program_state
172173
{
173-
cvector_void functions;
174+
cvector_function functions;
174175
cvector_void* stmt_list;
175176
size_t* pc;
176177
function* func;

0 commit comments

Comments
 (0)