Introduce a function to create a binary expression node.

This commit is contained in:
Olaf Seibert 2015-11-08 20:51:07 +01:00
parent 40ff236aaf
commit d6ff111149
3 changed files with 34 additions and 48 deletions

View File

@ -446,10 +446,7 @@ EX_TREE *evaluate(
}
/* Anything else returns verbatim */
res = new_ex_tree();
res->type = EX_ADD;
res->data.child.left = left;
res->data.child.right = right;
res = new_ex_bin(EX_ADD, left, right);
}
break;
@ -525,10 +522,7 @@ EX_TREE *evaluate(
}
/* Anything else returns verbatim */
res = new_ex_tree();
res->type = EX_SUB;
res->data.child.left = left;
res->data.child.right = right;
res = new_ex_bin(EX_SUB, left, right);
}
break;
@ -590,10 +584,7 @@ EX_TREE *evaluate(
}
/* Anything else returns verbatim */
res = new_ex_tree();
res->type = EX_MUL;
res->data.child.left = left;
res->data.child.right = right;
res = new_ex_bin(EX_MUL, left, right);
}
break;
@ -621,10 +612,7 @@ EX_TREE *evaluate(
}
/* Anything else returns verbatim */
res = new_ex_tree();
res->type = EX_DIV;
res->data.child.left = left;
res->data.child.right = right;
res = new_ex_bin(EX_DIV, left, right);
}
break;
@ -669,10 +657,7 @@ EX_TREE *evaluate(
}
/* Anything else returns verbatim */
res = new_ex_tree();
res->type = EX_AND;
res->data.child.left = left;
res->data.child.right = right;
res = new_ex_bin(EX_AND, left, right);
}
break;
@ -717,10 +702,7 @@ EX_TREE *evaluate(
}
/* Anything else returns verbatim */
res = new_ex_tree();
res->type = EX_OR;
res->data.child.left = left;
res->data.child.right = right;
res = new_ex_bin(EX_OR, left, right);
}
break;
default:
@ -773,3 +755,21 @@ EX_TREE *new_ex_lit(
return tp;
}
/* Create an EX_TREE representing a binary expression */
EX_TREE *new_ex_bin(
int type,
EX_TREE *left,
EX_TREE *right)
{
EX_TREE *tp;
tp = new_ex_tree();
tp->type = type;
tp->data.child.left = left;
tp->data.child.right = right;
return tp;
}

View File

@ -57,6 +57,10 @@ EX_TREE *new_ex_lit(
EX_TREE *ex_err(
EX_TREE *tp,
char *cp);
EX_TREE *new_ex_bin(
int type,
EX_TREE *left,
EX_TREE *right);
EX_TREE *evaluate(
EX_TREE *tp,

30
parse.c
View File

@ -434,10 +434,7 @@ EX_TREE *parse_binary(
return leftp;
rightp = parse_binary(cp + 1, term, ADD_PREC);
tp = new_ex_tree();
tp->type = EX_ADD;
tp->data.child.left = leftp;
tp->data.child.right = rightp;
tp = new_ex_bin(EX_ADD, leftp, rightp);
tp->cp = rightp->cp;
leftp = tp;
break;
@ -447,10 +444,7 @@ EX_TREE *parse_binary(
return leftp;
rightp = parse_binary(cp + 1, term, ADD_PREC);
tp = new_ex_tree();
tp->type = EX_SUB;
tp->data.child.left = leftp;
tp->data.child.right = rightp;
tp = new_ex_bin(EX_SUB, leftp, rightp);
tp->cp = rightp->cp;
leftp = tp;
break;
@ -460,10 +454,7 @@ EX_TREE *parse_binary(
return leftp;
rightp = parse_binary(cp + 1, term, MUL_PREC);
tp = new_ex_tree();
tp->type = EX_MUL;
tp->data.child.left = leftp;
tp->data.child.right = rightp;
tp = new_ex_bin(EX_MUL, leftp, rightp);
tp->cp = rightp->cp;
leftp = tp;
break;
@ -473,10 +464,7 @@ EX_TREE *parse_binary(
return leftp;
rightp = parse_binary(cp + 1, term, MUL_PREC);
tp = new_ex_tree();
tp->type = EX_DIV;
tp->data.child.left = leftp;
tp->data.child.right = rightp;
tp = new_ex_bin(EX_DIV, leftp, rightp);
tp->cp = rightp->cp;
leftp = tp;
break;
@ -486,10 +474,7 @@ EX_TREE *parse_binary(
return leftp;
rightp = parse_binary(cp + 1, term, OR_PREC);
tp = new_ex_tree();
tp->type = EX_OR;
tp->data.child.left = leftp;
tp->data.child.right = rightp;
tp = new_ex_bin(EX_OR, leftp, rightp);
tp->cp = rightp->cp;
leftp = tp;
break;
@ -499,10 +484,7 @@ EX_TREE *parse_binary(
return leftp;
rightp = parse_binary(cp + 1, term, AND_PREC);
tp = new_ex_tree();
tp->type = EX_AND;
tp->data.child.left = leftp;
tp->data.child.right = rightp;
tp = new_ex_bin(EX_AND, leftp, rightp);
tp->cp = rightp->cp;
leftp = tp;
break;