From 749d4600414d16643a1e9fc008995d68674fc477 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Tue, 26 Jun 2001 02:46:02 +0000 Subject: [PATCH] duplicate code cleanup --- tools/qfcc/include/expr.h | 4 +- tools/qfcc/source/expr.c | 25 +++++++- tools/qfcc/source/qc-parse.y | 107 +++++++++++------------------------ 3 files changed, 59 insertions(+), 77 deletions(-) diff --git a/tools/qfcc/include/expr.h b/tools/qfcc/include/expr.h index 00a8ed29f..cd9e6c913 100644 --- a/tools/qfcc/include/expr.h +++ b/tools/qfcc/include/expr.h @@ -37,7 +37,9 @@ typedef struct expr_s { } expr_t; expr_t *new_expr (void); -expr_t *label_expr (void); +expr_t *new_label_expr (void); +expr_t *new_binary_expr (int op, expr_t *e1, expr_t *e2); +expr_t *new_unary_expr (int op, expr_t *e1); void print_expr (expr_t *e); expr_t *binary_expr (int op, expr_t *e1, expr_t *e2); expr_t *unary_expr (int op, expr_t *e); diff --git a/tools/qfcc/source/expr.c b/tools/qfcc/source/expr.c index 93d3da478..52f1b6a79 100644 --- a/tools/qfcc/source/expr.c +++ b/tools/qfcc/source/expr.c @@ -65,7 +65,7 @@ new_expr (void) } expr_t * -label_expr (void) +new_label_expr (void) { static int label = 0; int lnum = ++label; @@ -79,6 +79,29 @@ label_expr (void) return l; } +expr_t * +new_binary_expr (int op, expr_t *e1, expr_t *e2) +{ + expr_t *e = new_expr (); + + e->type = ex_expr; + e->e.expr.op = op; + e->e.expr.e1 = e1; + e->e.expr.e2 = e2; + return e; +} + +expr_t * +new_unary_expr (int op, expr_t *e1) +{ + expr_t *e = new_expr (); + + e->type = ex_uexpr; + e->e.expr.op = op; + e->e.expr.e1 = e1; + return e; +} + void print_expr (expr_t *e) { diff --git a/tools/qfcc/source/qc-parse.y b/tools/qfcc/source/qc-parse.y index cbcb07cb3..df74adeca 100644 --- a/tools/qfcc/source/qc-parse.y +++ b/tools/qfcc/source/qc-parse.y @@ -301,64 +301,44 @@ statement | statement_block { $$ = $1; } | RETURN expr ';' { - $$ = new_expr (); - $$->type = ex_uexpr; - $$->e.expr.op = 'r'; - $$->e.expr.e1 = $2; + $$ = new_unary_expr ('r', $2); } | RETURN ';' { - $$ = new_expr (); - $$->type = ex_uexpr; - $$->e.expr.op = 'r'; + $$ = new_unary_expr ('r', 0); } | WHILE '(' expr ')' statement { - expr_t *e = new_expr (); - expr_t *l1 = label_expr (); - expr_t *l2 = label_expr (); + expr_t *l1 = new_label_expr (); + expr_t *l2 = new_label_expr (); + + expr_t *e = new_binary_expr ('n', $3, l2); $$ = e; - e->type = ex_expr; - e->e.expr.op = 'n'; - e->e.expr.e1 = $3; - e->e.expr.e2 = l2; - - e->next = l1; - e = e->next; + e = (e->next = l1); e->next = $5; while (e->next) e = e->next; - e->next = new_expr (); - e = e->next; - e->type = ex_expr; - e->e.expr.op = 'i'; - e->e.expr.e1 = $3; - e->e.expr.e2 = l1; + e = (e->next = new_binary_expr ('i', $3, l1)); e->next = l2; } | DO statement WHILE '(' expr ')' ';' { - expr_t *e; - expr_t *l1 = label_expr (); + expr_t *l1 = new_label_expr (); - $$ = e = l1; + expr_t *e = l1; + + $$ = e; e->next = $2; while (e->next) e = e->next; - e->next = new_expr (); - e = e->next; - e->type = ex_expr; - e->e.expr.op = 'i'; - e->e.expr.e1 = $5; - e->e.expr.e2 = l1; - + e->next = new_binary_expr ('i', $5, l1); } | LOCAL type { @@ -367,16 +347,12 @@ statement def_list ';' { $$ = 0; } | IF '(' expr ')' statement { - expr_t *e = new_expr (); - expr_t *l1 = label_expr (); + expr_t *l1 = new_label_expr (); + + expr_t *e = new_binary_expr ('n', $3, l1); $$ = e; - e->type = ex_expr; - e->e.expr.op = 'n'; - e->e.expr.e1 = $3; - e->e.expr.e2 = l1; - e->next = $5; while (e->next) e = e->next; @@ -385,29 +361,20 @@ statement } | IF '(' expr ')' statement ELSE statement { - expr_t *e = new_expr (); - expr_t *l1 = label_expr (); - expr_t *l2 = label_expr (); + expr_t *l1 = new_label_expr (); + expr_t *l2 = new_label_expr (); + + expr_t *e = new_binary_expr ('n', $3, l1); $$ = e; - e->type = ex_expr; - e->e.expr.op = 'n'; - e->e.expr.e1 = $3; - e->e.expr.e2 = l1; - e->next = $5; while (e->next) e = e->next; - e->next = new_expr (); - e = e->next; - e->type = ex_uexpr; - e->e.expr.op = 'g'; - e->e.expr.e1 = l2; + e = (e->next = new_unary_expr ('g', l2)); - e->next = l1; - e = e->next; + e = (e->next = l1); e->next = $7; while (e->next) @@ -417,24 +384,19 @@ statement } | FOR '(' expr ';' expr ';' expr ')' statement { - expr_t *e; - expr_t *l1 = label_expr (); - expr_t *l2 = label_expr (); + expr_t *l1 = new_label_expr (); + expr_t *l2 = new_label_expr (); - $$ = e = $3; - if (e) { - e->next = new_expr (); - e = e->next; + expr_t *e = new_binary_expr ('n', $5, l2); + + $$ = $3; + if ($$) { + $$->next = e; } else { - e = new_expr (); + $$ = e; } - e->type = ex_expr; - e->e.expr.op = 'n'; - e->e.expr.e1 = $5; - e->e.expr.e2 = l2; - e->next = l1; - e = e->next; + e = (e->next = l1); e->next = $9; while (e->next) @@ -444,12 +406,7 @@ statement if (e->next) e = e->next; - e->next = new_expr (); - e = e->next; - e->type = ex_expr; - e->e.expr.op = 'i'; - e->e.expr.e1 = $5; - e->e.expr.e2 = l1; + e = (e->next = new_binary_expr ('i', $5, l1)); e->next = l2; }