duplicate code cleanup

This commit is contained in:
Bill Currie 2001-06-26 02:46:02 +00:00
parent 908d265212
commit 749d460041
3 changed files with 59 additions and 77 deletions

View file

@ -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);

View file

@ -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)
{

View file

@ -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;
}