From cf4916e4de2bfc3007e7362496ed8fb2c24007bf Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Wed, 27 Sep 2023 17:37:38 +0900 Subject: [PATCH] [qfcc] Make expression lists more generally usable That much conflation was a bit excessive. --- tools/qfcc/include/expr.h | 10 +++-- tools/qfcc/source/expr.c | 79 +++++++++++++++++++++++++----------- tools/qfcc/source/expr_obj.c | 6 +-- tools/qfcc/source/qc-parse.y | 6 +-- tools/qfcc/source/qp-parse.y | 2 +- 5 files changed, 68 insertions(+), 35 deletions(-) diff --git a/tools/qfcc/include/expr.h b/tools/qfcc/include/expr.h index 89f54b04f..032642139 100644 --- a/tools/qfcc/include/expr.h +++ b/tools/qfcc/include/expr.h @@ -385,11 +385,13 @@ int list_count (const ex_list_t *list) __attribute__((pure)); void list_scatter (const ex_list_t *list, const expr_t **exprs); void list_scatter_rev (const ex_list_t *list, const expr_t **exprs); void list_gather (ex_list_t *dst, const expr_t **exprs, int count); +void list_append (ex_list_t *dst, const expr_t *exprs); +void list_prepend (ex_list_t *dst, const expr_t *exprs); expr_t *new_list_expr (const expr_t *first); -expr_t *list_append_expr (expr_t *list, const expr_t *expr); -expr_t *list_prepend_expr (expr_t *list, const expr_t *expr); -expr_t *list_append_list (expr_t *list, ex_list_t *append); -expr_t *list_prepend_list (expr_t *list, ex_list_t *prepend); +expr_t *expr_append_expr (expr_t *list, const expr_t *expr); +expr_t *expr_prepend_expr (expr_t *list, const expr_t *expr); +expr_t *expr_append_list (expr_t *list, ex_list_t *append); +expr_t *expr_prepend_list (expr_t *list, ex_list_t *prepend); /** Create a new expression node. diff --git a/tools/qfcc/source/expr.c b/tools/qfcc/source/expr.c index 094acc1de..cf985df21 100644 --- a/tools/qfcc/source/expr.c +++ b/tools/qfcc/source/expr.c @@ -263,40 +263,71 @@ new_listitem (const expr_t *e) return li; } -expr_t * -list_append_expr (expr_t *list, const expr_t *expr) +void +list_append (ex_list_t *list, const expr_t *expr) { - auto li = new_listitem (expr); - *list->list.tail = li; - list->list.tail = &li->next; - return list; -} - -expr_t * -list_prepend_expr (expr_t *list, const expr_t *expr) -{ - auto li = new_listitem (expr); - li->next = list->list.head; - list->list.head = li; - - if (list->list.tail == &list->list.head) { - list->list.tail = &li->next; + if (!list->tail) { + list->tail = &list->head; } + auto li = new_listitem (expr); + *list->tail = li; + list->tail = &li->next; +} + +void +list_prepend (ex_list_t *list, const expr_t *expr) +{ + if (!list->tail) { + list->tail = &list->head; + } + + auto li = new_listitem (expr); + li->next = list->head; + list->head = li; + + if (list->tail == &list->head) { + list->tail = &li->next; + } +} + +expr_t * +expr_append_expr (expr_t *list, const expr_t *expr) +{ + if (list->type != ex_list) { + internal_error (list, "not a list expression"); + } + list_append (&list->list, expr); return list; } expr_t * -list_append_list (expr_t *list, ex_list_t *append) +expr_prepend_expr (expr_t *list, const expr_t *expr) { + if (list->type != ex_list) { + internal_error (list, "not a list expression"); + } + list_prepend (&list->list, expr); + return list; +} + +expr_t * +expr_append_list (expr_t *list, ex_list_t *append) +{ + if (list->type != ex_list) { + internal_error (list, "not a list expression"); + } *list->list.tail = append->head; list->list.tail = append->tail; return list; } expr_t * -list_prepend_list (expr_t *list, ex_list_t *prepend) +expr_prepend_list (expr_t *list, ex_list_t *prepend) { + if (list->type != ex_list) { + internal_error (list, "not a list expression"); + } if (!list->list.head) { list->list.tail = prepend->tail; } @@ -313,7 +344,7 @@ new_list_expr (const expr_t *first) list->list.head = 0; list->list.tail = &list->list.head; if (first) { - list_append_expr (list, first); + expr_append_expr (list, first); } return list; } @@ -2164,7 +2195,7 @@ build_function_call (const expr_t *fexpr, const type_t *ftype, const expr_t *par // args is built in reverse order so it matches params for (int i = 0; i < arg_count; i++) { if (emit_args && i == param_count) { - list_prepend_expr (args, new_args_expr ()); + expr_prepend_expr (args, new_args_expr ()); emit_args = false; } @@ -2179,19 +2210,19 @@ build_function_call (const expr_t *fexpr, const type_t *ftype, const expr_t *par auto cast = cast_expr (arg_types[i], e); auto tmp = new_temp_def_expr (arg_types[i]); tmp = expr_file_line (tmp, e); - list_prepend_expr (args, tmp); + expr_prepend_expr (args, tmp); arg_exprs[arg_expr_count][0] = cast; arg_exprs[arg_expr_count][1] = tmp; arg_expr_count++; } else { e = cast_expr (arg_types[i], e); - list_prepend_expr (args, e); + expr_prepend_expr (args, e); } } if (emit_args) { emit_args = false; - list_prepend_expr (args, new_args_expr ()); + expr_prepend_expr (args, new_args_expr ()); } for (int i = 0; i < arg_expr_count - 1; i++) { auto assign = assign_expr (arg_exprs[i][1], arg_exprs[i][0]); diff --git a/tools/qfcc/source/expr_obj.c b/tools/qfcc/source/expr_obj.c index 03be29b44..6c2e6035c 100644 --- a/tools/qfcc/source/expr_obj.c +++ b/tools/qfcc/source/expr_obj.c @@ -228,11 +228,11 @@ message_expr (const expr_t *receiver, keywordarg_t *message) expr_t *args = expr_file_line (new_list_expr (0), receiver); for (m = message; m; m = m->next) { if (m->expr && m->expr->list.head) { - list_append_list (args, &m->expr->list); + expr_append_list (args, &m->expr->list); } } - list_append_expr (args, selector); - list_append_expr (args, receiver); + expr_append_expr (args, selector); + expr_append_expr (args, receiver); send_msg = expr_file_line (send_message (super), receiver); if (method) { diff --git a/tools/qfcc/source/qc-parse.y b/tools/qfcc/source/qc-parse.y index bafe77927..3f2a332cb 100644 --- a/tools/qfcc/source/qc-parse.y +++ b/tools/qfcc/source/qc-parse.y @@ -1746,7 +1746,7 @@ ident_expr vector_expr : '[' expr ',' { no_flush_dag = true; } expr_list ']' { - $$ = list_prepend_expr ($5, $2); + $$ = expr_prepend_expr ($5, $2); no_flush_dag = false; } ; @@ -1809,7 +1809,7 @@ comma_expr expr_list : expr { $$ = new_list_expr ($1); } - | expr_list ',' flush_dag expr { $$ = list_append_expr ($1, $4); } + | expr_list ',' flush_dag expr { $$ = expr_append_expr ($1, $4); } ; opt_arg_list @@ -1819,7 +1819,7 @@ opt_arg_list arg_list : arg_expr { $$ = new_list_expr ($1); } - | arg_list ',' arg_expr { $$ = list_prepend_expr ($1, $3); } + | arg_list ',' arg_expr { $$ = expr_prepend_expr ($1, $3); } ; arg_expr diff --git a/tools/qfcc/source/qp-parse.y b/tools/qfcc/source/qp-parse.y index 55609327a..f2a0df762 100644 --- a/tools/qfcc/source/qp-parse.y +++ b/tools/qfcc/source/qp-parse.y @@ -465,7 +465,7 @@ procedure_statement expression_list : expression { $$ = new_list_expr ($1); } - | expression_list ',' expression { $$ = list_prepend_expr ($1, $3); } + | expression_list ',' expression { $$ = expr_prepend_expr ($1, $3); } ; unary_expr