Skip to content

Commit

Permalink
refact: Remove duplicate node-creation codes
Browse files Browse the repository at this point in the history
  • Loading branch information
keyvank committed Dec 8, 2024
1 parent 1ec9efa commit d37778b
Show file tree
Hide file tree
Showing 24 changed files with 51 additions and 140 deletions.
5 changes: 1 addition & 4 deletions parser/asm.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ parser_node *parse_asm(typed_token **tkns_ptr)
tkn = tkn->next;
*tkns_ptr = tkn;

parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_compound_statement));
node->debug = asm_debug;
node->apply = asm_apply;
parser_node *node = new_node(asm_debug, asm_apply, sizeof(node_asm));
node_asm *as = (node_asm *)node->data;
as->lines = lines;

Expand Down
8 changes: 2 additions & 6 deletions parser/break.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ parser_node *parse_break(typed_token **tkns_ptr)
if (tkn->next->type_id != TKN_SEMICOLON) {
return NULL;
}
parser_node *break_node = (parser_node *)malloc(sizeof(parser_node));
break_node->debug = break_debug;
break_node->apply = break_apply;
parser_node *break_node = new_node(break_debug, break_apply, 0);
*tkns_ptr = tkn->next->next;
return break_node;
}
Expand Down Expand Up @@ -58,9 +56,7 @@ parser_node *parse_continue(typed_token **tkns_ptr)
if (tkn->next->type_id != TKN_SEMICOLON) {
return NULL;
}
parser_node *continue_node = (parser_node *)malloc(sizeof(parser_node));
continue_node->debug = continue_debug;
continue_node->apply = continue_apply;
parser_node *continue_node = new_node(continue_debug, continue_apply, 0);
*tkns_ptr = tkn->next->next;
return continue_node;
}
Expand Down
5 changes: 1 addition & 4 deletions parser/expr/access.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ parser_node *parse_access(typed_token **tkns_ptr, parser_node *obj)
typed_token *field_tkn = tkn;
tkn = tkn->next;
*tkns_ptr = tkn;
parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_access));
node->debug = access_debug;
node->apply = access_apply;
parser_node *node = new_node(access_debug, access_apply, sizeof(node_access));
node_access *acc = (node_access *)node->data;
acc->object = obj;
acc->field_name = cc_asprintf("%s", field_tkn->data);
Expand Down
5 changes: 1 addition & 4 deletions parser/expr/deref.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ parser_node *parse_deref(typed_token **tkns_ptr)
if (n)
{
*tkns_ptr = tkn;
parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_deref));
node->debug = deref_debug;
node->apply = deref_apply;
parser_node *node = new_node(deref_debug, deref_apply, sizeof(node_deref));
node_deref *ref = (node_deref *)node->data;
ref->var = n;

Expand Down
30 changes: 6 additions & 24 deletions parser/expr/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,7 @@ parser_node *parse_sizeof(typed_token **tkns_ptr)
{
tkn = tkn->next;
*tkns_ptr = tkn;
parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_sizeof));
node->debug = sizeof_debug;
node->apply = sizeof_apply;
parser_node *node = new_node(sizeof_debug, sizeof_apply, sizeof(node_sizeof));
node_sizeof *cast = (node_sizeof *)node->data;
cast->type = type;
return node;
Expand All @@ -501,10 +498,7 @@ parser_node *parse_cast(typed_token **tkns_ptr)
if (val)
{
*tkns_ptr = tkn;
parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_cast));
node->debug = cast_debug;
node->apply = cast_apply;
parser_node *node = new_node(cast_debug, cast_apply, sizeof(node_cast));
node_cast *cast = (node_cast *)node->data;
cast->type = tp;
cast->val = val;
Expand Down Expand Up @@ -664,10 +658,7 @@ parser_node *parse_expr_prec(typed_token **tkns_ptr, parser_node *lhs, int min_p
}
rhs = parse_expr_prec(&tkn, rhs, look_prec);
}
parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_binary_op));
node->debug = binary_op_debug;
node->apply = binary_op_apply;
parser_node *node = new_node(binary_op_debug, binary_op_apply, sizeof(node_binary_op));
node_binary_op *binop = (node_binary_op *)node->data;
binop->left = lhs;
binop->right = rhs;
Expand All @@ -691,10 +682,7 @@ parser_node *parse_expr_prec(typed_token **tkns_ptr, parser_node *lhs, int min_p
if (no)
{

parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_cond));
node->debug = cond_debug;
node->apply = cond_apply;
parser_node *node = new_node(cond_debug, cond_apply, sizeof(node_cond));
node_cond *binop = (node_cond *)node->data;
binop->cond = lhs;
binop->true_val = yes;
Expand Down Expand Up @@ -738,10 +726,7 @@ parser_node *parse_unary(typed_token **tkns_ptr)
return NULL;
}

parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_unary_op));
node->debug = unary_op_debug;
node->apply = unary_op_apply;
parser_node *node = new_node(unary_op_debug, unary_op_apply, sizeof(node_unary_op));
node_unary_op *unary = (node_unary_op *)node->data;
unary->op = unary_op;
unary->exp = unary_expr;
Expand All @@ -767,10 +752,7 @@ parser_node *parse_postfix(typed_token **tkns_ptr, parser_node *curr)
return NULL;
}

parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_postfix));
node->debug = postfix_op_debug;
node->apply = postfix_op_apply;
parser_node *node = new_node(postfix_op_debug, postfix_op_apply, sizeof(node_postfix));
node_postfix *postfix_op = (node_postfix *)node->data;
postfix_op->op = postfix_unary_op;
postfix_op->exp = curr;
Expand Down
5 changes: 1 addition & 4 deletions parser/expr/func_call.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,7 @@ parser_node *parse_func_call(typed_token **tkns_ptr, parser_node *func)
tkn = tkn->next;
*tkns_ptr = tkn;

parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_func_call));
node->debug = func_call_debug;
node->apply = func_call_apply;
parser_node *node = new_node(func_call_debug, func_call_apply, sizeof(node_func_call));
node_func_call *call = (node_func_call *)node->data;

call->func = func;
Expand Down
5 changes: 1 addition & 4 deletions parser/expr/index.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ parser_node *parse_index(typed_token **tkns_ptr, parser_node *arr)
{
tkn = tkn->next;
*tkns_ptr = tkn;
parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_index));
node->debug = index_debug;
node->apply = index_apply;
parser_node *node = new_node(index_debug, index_apply, sizeof(node_index));
node_index *idx = (node_index *)node->data;
idx->arr = arr;
idx->ind = ind;
Expand Down
5 changes: 1 addition & 4 deletions parser/expr/literal.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ parser_node *parse_literal(typed_token **tkns_ptr)
tkn = tkn->next;
*tkns_ptr = tkn;

parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_literal));
node->debug = literal_debug;
node->apply = literal_apply;
parser_node *node = new_node(literal_debug, literal_apply, sizeof(node_literal));
node_literal *lit = (node_literal *)node->data;

lit->type = val_tkn->type_id;
Expand Down
5 changes: 1 addition & 4 deletions parser/expr/ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ parser_node *parse_ref(typed_token **tkns_ptr)
if (n)
{
*tkns_ptr = tkn;
parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_ref));
node->debug = ref_debug;
node->apply = ref_apply;
parser_node *node = new_node(ref_debug, ref_apply, sizeof(node_ref));
node_ref *ref = (node_ref *)node->data;
ref->var = n;

Expand Down
5 changes: 1 addition & 4 deletions parser/expr/var.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ parser_node *parse_var(typed_token **tkns_ptr)
tkn = tkn->next;
*tkns_ptr = tkn;

parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_var));
node->debug = var_debug;
node->apply = var_apply;
parser_node *node = new_node(var_debug, var_apply, sizeof(node_var));
node_var *var = (node_var *)node->data;
var->var_name = varname;

Expand Down
5 changes: 1 addition & 4 deletions parser/extern.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ parser_node *parse_extern(typed_token **tkns_ptr)
{
*tkns_ptr = tkn;

parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_extern));
node->debug = extern_debug;
node->apply = extern_apply;
parser_node *node = new_node(extern_debug, extern_apply, sizeof(node_extern));
node_extern *ex = (node_extern *)node->data;
ex->var_decl = decl;

Expand Down
5 changes: 1 addition & 4 deletions parser/for.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,7 @@ parser_node *parse_for(typed_token **tkns_ptr)
{
*tkns_ptr = tkn;

parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_for));
node->debug = for_debug;
node->apply = for_apply;
parser_node *node = new_node(for_debug, for_apply, sizeof(node_for));
node_for *f = (node_for *)node->data;
f->act = act;
f->body = body;
Expand Down
10 changes: 2 additions & 8 deletions parser/func.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,7 @@ parser_node *parse_function(typed_token **tkns_ptr)
tkn = tkn->next;
*tkns_ptr = tkn;

parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_func_def));
node->debug = func_def_debug;
node->apply = func_def_apply;
parser_node *node = new_node(func_def_debug, func_def_apply, sizeof(node_func_def));
node_func_def *decl = (node_func_def *)node->data;

decl->identity = (char *)name_tkn->data;
Expand Down Expand Up @@ -215,10 +212,7 @@ parser_node *parse_function(typed_token **tkns_ptr)
tkn = tkn->next;
*tkns_ptr = tkn;

parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_func_def));
node->debug = func_def_debug;
node->apply = func_def_apply;
parser_node *node = new_node(func_def_debug, func_def_apply, sizeof(node_func_def));
node_func_def *func = (node_func_def *)node->data;

func->identity = (char *)name_tkn->data;
Expand Down
10 changes: 2 additions & 8 deletions parser/goto.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ parser_node *parse_label(typed_token **tkns_ptr)
{
tkn = tkn->next;
*tkns_ptr = tkn;
parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_label));
node->debug = label_debug;
node->apply = label_apply;
parser_node *node = new_node(label_debug, label_apply, sizeof(node_label));
node_label *lab = (node_label *)node->data;
lab->name = (char *)name_tkn->data;

Expand Down Expand Up @@ -72,10 +69,7 @@ parser_node *parse_goto(typed_token **tkns_ptr)
{
tkn = tkn->next;
*tkns_ptr = tkn;
parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_goto));
node->debug = goto_debug;
node->apply = goto_apply;
parser_node *node = new_node(goto_debug, goto_apply, sizeof(node_goto));
node_goto *go = (node_goto *)node->data;
go->label = (char *)label_tkn->data;

Expand Down
5 changes: 1 addition & 4 deletions parser/if.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ parser_node *parse_if(typed_token **tkns_ptr)

*tkns_ptr = tkn;

parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_if));
node->debug = if_debug;
node->apply = if_apply;
parser_node *node = new_node(if_debug, if_apply, sizeof(node_if));
node_if *f = (node_if *)node->data;
f->body = body;
f->else_body = else_body;
Expand Down
14 changes: 12 additions & 2 deletions parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,20 @@ void printtabs(int depth)
}
}

apply_result *new_result(char *code, general_type *type) {
apply_result *ret = (apply_result*)malloc(sizeof(apply_result));
apply_result *new_result(char *code, general_type *type)
{
apply_result *ret = (apply_result *)malloc(sizeof(apply_result));
ret->code = code;
ret->type = type;
ret->addr_code = NULL;
return ret;
}

parser_node *new_node(void (*debug)(int, parser_node *), apply_result *(*apply)(parser_node *, context *), int sz)
{
parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = malloc(sz);
node->debug = debug;
node->apply = apply;
return node;
}
5 changes: 4 additions & 1 deletion parser/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ typedef struct parser_node_
struct apply_result_ *(*apply)(struct parser_node_ *, context *ctx);
} parser_node;

typedef struct apply_result_ {
typedef struct apply_result_
{
char *code;
general_type *type;
char *addr_code;
} apply_result;

apply_result *new_result(char *code, general_type *type);

parser_node *new_node(void (*debug)(int, parser_node *), apply_result *(*apply)(parser_node *, context *), int sz);

#endif
5 changes: 1 addition & 4 deletions parser/program.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,7 @@ parser_node *parse_program(typed_token **tkn_ptr)
{
if (tkn->type_id == TKN_EOF)
{
parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_program));
node->debug = program_debug;
node->apply = program_apply;
parser_node *node = new_node(program_debug, program_apply, sizeof(node_program));
node_program *prog = (node_program *)node->data;
prog->externs = externs;
prog->num_functions = func_count;
Expand Down
15 changes: 3 additions & 12 deletions parser/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ parser_node *parse_return(typed_token **tkns_ptr)
{
tkn = tkn->next;
*tkns_ptr = tkn;
parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_return));
node->debug = return_debug;
node->apply = return_apply;
parser_node *node = new_node(return_debug, return_apply, sizeof(node_return));
node_return *ret = (node_return *)node->data;
ret->exp = NULL;

Expand All @@ -102,10 +99,7 @@ parser_node *parse_return(typed_token **tkns_ptr)
tkn = tkn->next;
*tkns_ptr = tkn;

parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_return));
node->debug = return_debug;
node->apply = return_apply;
parser_node *node = new_node(return_debug, return_apply, sizeof(node_return));
node_return *ret = (node_return *)node->data;
ret->exp = exp;

Expand Down Expand Up @@ -136,10 +130,7 @@ parser_node *parse_compound_statement(typed_token **tkns_ptr)
tkn = tkn->next;
*tkns_ptr = tkn;

parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_compound_statement));
node->debug = compound_statement_debug;
node->apply = compound_statement_apply;
parser_node *node = new_node(compound_statement_debug, compound_statement_apply, sizeof(node_compound_statement));
node_compound_statement *comp = (node_compound_statement *)node->data;
comp->num_statements = num_stmts;
comp->statements = stmts;
Expand Down
10 changes: 2 additions & 8 deletions parser/struct_def.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ parser_node *parse_struct_def(typed_token **tkns_ptr)
{
tkn = tkn->next;
*tkns_ptr = tkn;
parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_struct_def));
node->debug = struct_def_debug;
node->apply = struct_def_apply;
parser_node *node = new_node(struct_def_debug, struct_def_apply, sizeof(node_struct_def));
node_struct_def *sd = (node_struct_def *)node->data;
sd->typedef_name = NULL;
sd->struct_name = (char *)struct_name_tkn->data;
Expand Down Expand Up @@ -105,10 +102,7 @@ parser_node *parse_struct_def(typed_token **tkns_ptr)
{
tkn = tkn->next;
*tkns_ptr = tkn;
parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_struct_def));
node->debug = struct_def_debug;
node->apply = struct_def_apply;
parser_node *node = new_node(struct_def_debug, struct_def_apply, sizeof(node_struct_def));
node_struct_def *sd = (node_struct_def *)node->data;

sd->struct_name = NULL;
Expand Down
Loading

0 comments on commit d37778b

Please sign in to comment.