Move some work into expression parse tree constructors

This commit is contained in:
Olaf Seibert 2021-06-12 20:32:49 +02:00
parent 0058db9044
commit 7ba6ed13e6
3 changed files with 39 additions and 43 deletions

View File

@ -185,8 +185,7 @@ static EX_TREE *new_temp_sym(
sym->section = section;
sym->value = value;
tp = new_ex_tree();
tp->type = EX_TEMP_SYM;
tp = new_ex_tree(EX_TEMP_SYM);
tp->data.symbol = sym;
return tp;
@ -222,8 +221,7 @@ EX_TREE *dup_tree(
return NULL;
}
res = new_ex_tree();
res->type = tp->type;
res = new_ex_tree(tp->type);
res->cp = tp->cp;
switch (tp->type) {
@ -349,10 +347,7 @@ EX_TREE *evaluate(
free_tree(tp);
} else {
/* Copy verbatim. */
res = new_ex_tree();
res->type = EX_COM;
res->cp = tp->cp;
res->data.child.left = tp;
res = new_ex_una(EX_COM, tp);
}
break;
@ -374,10 +369,7 @@ EX_TREE *evaluate(
free_tree(tp);
} else {
/* Copy verbatim. */
res = new_ex_tree();
res->type = EX_NEG;
res->cp = tp->cp;
res->data.child.left = tp;
res = new_ex_una(EX_NEG, tp);
}
break;
@ -782,10 +774,12 @@ EX_TREE *evaluate(
/* Allocate an EX_TREE */
EX_TREE *new_ex_tree(
void)
int type)
{
EX_TREE *tr = memcheck(calloc(1, sizeof(EX_TREE)));
tr->type = type;
return tr;
}
@ -798,9 +792,8 @@ EX_TREE *ex_err(
{
EX_TREE *errtp;
errtp = new_ex_tree();
errtp = new_ex_tree(EX_ERR);
errtp->cp = cp;
errtp->type = EX_ERR;
errtp->data.child.left = tp;
return errtp;
@ -813,8 +806,7 @@ EX_TREE *new_ex_lit(
{
EX_TREE *tp;
tp = new_ex_tree();
tp->type = EX_LIT;
tp = new_ex_tree(EX_LIT);
tp->data.lit = value;
return tp;
@ -829,11 +821,29 @@ EX_TREE *new_ex_bin(
{
EX_TREE *tp;
tp = new_ex_tree();
tp->type = type;
tp = new_ex_tree(type);
tp->data.child.left = left;
tp->data.child.right = right;
tp->cp = right->cp;
return tp;
}
/* Create an EX_TREE representing a unary expression */
EX_TREE *new_ex_una(
int type,
EX_TREE *left)
{
EX_TREE *tp;
tp = new_ex_tree(type);
tp->data.child.left = left;
tp->data.child.right = NULL;
tp->cp = left->cp;
return tp;
}

View File

@ -51,7 +51,7 @@ typedef struct ex_tree {
EX_TREE *new_ex_tree(
void);
int type);
void free_tree(
EX_TREE *tp);
@ -64,6 +64,9 @@ EX_TREE *new_ex_bin(
int type,
EX_TREE *left,
EX_TREE *right);
EX_TREE *new_ex_una(
int type,
EX_TREE *left);
EX_TREE *evaluate(
EX_TREE *tp,

29
parse.c
View File

@ -657,7 +657,6 @@ EX_TREE *parse_binary(
rightp = parse_binary(cp + 1, term, ADD_PREC);
tp = new_ex_bin(EX_ADD, leftp, rightp);
tp->cp = rightp->cp;
leftp = tp;
break;
@ -667,7 +666,6 @@ EX_TREE *parse_binary(
rightp = parse_binary(cp + 1, term, ADD_PREC);
tp = new_ex_bin(EX_SUB, leftp, rightp);
tp->cp = rightp->cp;
leftp = tp;
break;
@ -677,7 +675,6 @@ EX_TREE *parse_binary(
rightp = parse_binary(cp + 1, term, MUL_PREC);
tp = new_ex_bin(EX_MUL, leftp, rightp);
tp->cp = rightp->cp;
leftp = tp;
break;
@ -687,7 +684,6 @@ EX_TREE *parse_binary(
rightp = parse_binary(cp + 1, term, MUL_PREC);
tp = new_ex_bin(EX_DIV, leftp, rightp);
tp->cp = rightp->cp;
leftp = tp;
break;
@ -697,7 +693,6 @@ EX_TREE *parse_binary(
rightp = parse_binary(cp + 1, term, OR_PREC);
tp = new_ex_bin(EX_OR, leftp, rightp);
tp->cp = rightp->cp;
leftp = tp;
break;
@ -707,7 +702,6 @@ EX_TREE *parse_binary(
rightp = parse_binary(cp + 1, term, AND_PREC);
tp = new_ex_bin(EX_AND, leftp, rightp);
tp->cp = rightp->cp;
leftp = tp;
break;
@ -717,7 +711,6 @@ EX_TREE *parse_binary(
rightp = parse_binary(cp + 1, term, LSH_PREC);
tp = new_ex_bin(EX_LSH, leftp, rightp);
tp->cp = rightp->cp;
leftp = tp;
break;
@ -899,8 +892,7 @@ EX_TREE *parse_unary(
return ex_err(NULL, cp);
/* This returns references to the built-in register symbols */
tp = new_ex_tree();
tp->type = EX_SYM;
tp = new_ex_tree(EX_SYM);
tp->data.symbol = reg_sym[reg];
tp->cp = cp;
return tp;
@ -908,10 +900,7 @@ EX_TREE *parse_unary(
/* Unary negate */
if (*cp == '-') {
tp = new_ex_tree();
tp->type = EX_NEG;
tp->data.child.left = parse_unary(cp + 1);
tp->cp = tp->data.child.left->cp;
tp = new_ex_una(EX_NEG, parse_unary(cp + 1));
return tp;
}
@ -925,10 +914,7 @@ EX_TREE *parse_unary(
switch (tolower((unsigned char)cp[1])) {
case 'c':
/* ^C, ones complement */
tp = new_ex_tree();
tp->type = EX_COM;
tp->data.child.left = parse_unary(cp + 2);
tp->cp = tp->data.child.left->cp;
tp = new_ex_una(EX_COM, parse_unary(cp + 2));
return tp;
case 'b':
/* ^B, binary radix modifier */
@ -999,8 +985,7 @@ EX_TREE *parse_unary(
if (sectsym && !islocal) {
SECTION *psect = sectsym->section;
tp = new_ex_tree();
tp->type = EX_SYM;
tp = new_ex_tree(EX_SYM);
tp->data.symbol = sectsym;
tp->cp = cp;
@ -1137,9 +1122,8 @@ EX_TREE *parse_unary(
}
if (sym != NULL && !(sym->flags & SYMBOLFLAG_UNDEFINED)) {
tp = new_ex_tree();
tp = new_ex_tree(EX_SYM);
tp->cp = cp;
tp->type = EX_SYM;
tp->data.symbol = sym;
free(label);
@ -1160,9 +1144,8 @@ EX_TREE *parse_unary(
sym->section = &absolute_section;
sym->value = 0;
tp = new_ex_tree();
tp = new_ex_tree(EX_UNDEFINED_SYM);
tp->cp = cp;
tp->type = EX_UNDEFINED_SYM;
tp->data.symbol = sym;
return tp;