Browse Source

frontend/parse.c, introduce `alloc_pnode()'

pre-master-46
rlar 11 years ago
parent
commit
29dde7206a
  1. 22
      src/frontend/define.c
  2. 72
      src/frontend/parse.c
  3. 1
      src/include/ngspice/fteext.h

22
src/frontend/define.c

@ -358,36 +358,28 @@ trcopy(struct pnode *tree, char *args, struct pnode *nn)
} else if (tree->pn_func) { } else if (tree->pn_func) {
pn = alloc(struct pnode);
pn->pn_use = 0;
pn->pn_name = NULL;
pn->pn_value = NULL;
pn = alloc_pnode();
/* pn_func are pointers to a global constant struct */ /* pn_func are pointers to a global constant struct */
pn->pn_func = tree->pn_func; pn->pn_func = tree->pn_func;
pn->pn_op = NULL;
pn->pn_left = trcopy(tree->pn_left, args, nn); pn->pn_left = trcopy(tree->pn_left, args, nn);
pn->pn_left->pn_use++; pn->pn_left->pn_use++;
pn->pn_right = NULL;
pn->pn_next = NULL;
} else if (tree->pn_op) { } else if (tree->pn_op) {
pn = alloc(struct pnode);
pn->pn_use = 0;
pn->pn_name = NULL;
pn->pn_value = NULL;
pn->pn_func = NULL;
pn = alloc_pnode();
/* pn_op are pointers to a global constant struct */ /* pn_op are pointers to a global constant struct */
pn->pn_op = tree->pn_op; pn->pn_op = tree->pn_op;
pn->pn_left = trcopy(tree->pn_left, args, nn); pn->pn_left = trcopy(tree->pn_left, args, nn);
pn->pn_left->pn_use++; pn->pn_left->pn_use++;
if (pn->pn_op->op_arity == 2) { if (pn->pn_op->op_arity == 2) {
pn->pn_right = trcopy(tree->pn_right, args, nn); pn->pn_right = trcopy(tree->pn_right, args, nn);
pn->pn_right->pn_use++; pn->pn_right->pn_use++;
} else {
pn->pn_right = NULL;
} }
pn->pn_next = NULL;
} else { } else {
fprintf(cp_err, "trcopy: Internal Error: bad parse node\n"); fprintf(cp_err, "trcopy: Internal Error: bad parse node\n");

72
src/frontend/parse.c

@ -208,19 +208,18 @@ PP_mkbnode(int opnum, struct pnode *arg1, struct pnode *arg2)
fprintf(cp_err, "PP_mkbnode: Internal Error: no such op num %d\n", fprintf(cp_err, "PP_mkbnode: Internal Error: no such op num %d\n",
opnum); opnum);
p = alloc(struct pnode);
p->pn_use = 0;
p->pn_value = NULL;
p->pn_name = NULL; /* sjb */
p->pn_func = NULL;
p = alloc_pnode();
p->pn_op = o; p->pn_op = o;
p->pn_left = arg1; p->pn_left = arg1;
if (p->pn_left) if (p->pn_left)
p->pn_left->pn_use++; p->pn_left->pn_use++;
p->pn_right = arg2; p->pn_right = arg2;
if (p->pn_right) if (p->pn_right)
p->pn_right->pn_use++; p->pn_right->pn_use++;
p->pn_next = NULL;
return (p); return (p);
} }
@ -233,7 +232,8 @@ PP_mkunode(int op, struct pnode *arg)
struct pnode *p; struct pnode *p;
struct op *o; struct op *o;
p = alloc(struct pnode);
p = alloc_pnode();
for (o = uops; o->op_name; o++) for (o = uops; o->op_name; o++)
if (o->op_num == op) if (o->op_num == op)
break; break;
@ -243,15 +243,11 @@ PP_mkunode(int op, struct pnode *arg)
op); op);
p->pn_op = o; p->pn_op = o;
p->pn_use = 0;
p->pn_value = NULL;
p->pn_name = NULL; /* sjb */
p->pn_func = NULL;
p->pn_left = arg; p->pn_left = arg;
if (p->pn_left) if (p->pn_left)
p->pn_left->pn_use++; p->pn_left->pn_use++;
p->pn_right = NULL;
p->pn_next = NULL;
return (p); return (p);
} }
@ -314,17 +310,14 @@ PP_mkfnode(const char *func, struct pnode *arg)
return p; return p;
} }
p = alloc(struct pnode);
p->pn_use = 0;
p->pn_name = NULL;
p->pn_value = NULL;
p = alloc_pnode();
p->pn_func = f; p->pn_func = f;
p->pn_op = NULL;
p->pn_left = arg; p->pn_left = arg;
if (p->pn_left) if (p->pn_left)
p->pn_left->pn_use++; p->pn_left->pn_use++;
p->pn_right = NULL;
p->pn_next = NULL;
return (p); return (p);
} }
@ -337,16 +330,11 @@ PP_mknnode(double number)
struct pnode *p; struct pnode *p;
struct dvec *v; struct dvec *v;
p = alloc(struct pnode);
p = alloc_pnode();
v = alloc(struct dvec); v = alloc(struct dvec);
ZERO(v, struct dvec); ZERO(v, struct dvec);
p->pn_use = 0;
p->pn_name = NULL;
p->pn_value = v; p->pn_value = v;
p->pn_func = NULL;
p->pn_op = NULL;
p->pn_left = p->pn_right = NULL;
p->pn_next = NULL;
/* We don't use printnum because it screws up PP_mkfnode above. We have /* We don't use printnum because it screws up PP_mkfnode above. We have
* to be careful to deal properly with node numbers that are quite * to be careful to deal properly with node numbers that are quite
@ -375,13 +363,7 @@ PP_mksnode(const char *string)
struct dvec *v, *nv, *vs, *newv = NULL, *end = NULL; struct dvec *v, *nv, *vs, *newv = NULL, *end = NULL;
struct pnode *p; struct pnode *p;
p = alloc(struct pnode);
p->pn_use = 0;
p->pn_name = NULL;
p->pn_func = NULL;
p->pn_op = NULL;
p->pn_left = p->pn_right = NULL;
p->pn_next = NULL;
p = alloc_pnode();
v = vec_get(string); v = vec_get(string);
if (v == NULL) { if (v == NULL) {
nv = alloc(struct dvec); nv = alloc(struct dvec);
@ -390,7 +372,6 @@ PP_mksnode(const char *string)
nv->v_name = copy(string); nv->v_name = copy(string);
return (p); return (p);
} }
p->pn_value = NULL;
/* It's not obvious that we should be doing this, but... */ /* It's not obvious that we should be doing this, but... */
for (vs = v; vs; vs = vs->v_link2) { for (vs = v; vs; vs = vs->v_link2) {
@ -416,6 +397,27 @@ PP_mksnode(const char *string)
} }
struct pnode *
alloc_pnode(void)
{
struct pnode *pn = alloc(struct pnode);
pn->pn_use = 0;
pn->pn_name = NULL;
// fixme, thats actually a union ...
pn->pn_value = NULL;
pn->pn_func = NULL;
pn->pn_op = NULL;
pn->pn_left = NULL;
pn->pn_right = NULL;
pn->pn_next = NULL;
return pn;
}
/* Don't call this directly, always use the free_pnode() macro. /* Don't call this directly, always use the free_pnode() macro.
The linked pnodes do not necessarily form a perfect tree as some nodes get The linked pnodes do not necessarily form a perfect tree as some nodes get
reused. Hence, in this recursive walk trough the 'tree' we only free node reused. Hence, in this recursive walk trough the 'tree' we only free node

1
src/include/ngspice/fteext.h

@ -257,6 +257,7 @@ extern struct func ft_funcs[];
extern struct func func_not; extern struct func func_not;
extern struct func func_uminus; extern struct func func_uminus;
extern struct pnode * ft_getpnames(wordlist *wl, bool check); extern struct pnode * ft_getpnames(wordlist *wl, bool check);
extern struct pnode *alloc_pnode(void);
#define free_pnode(ptr) free_pnode_x(ptr); ptr=NULL #define free_pnode(ptr) free_pnode_x(ptr); ptr=NULL
extern void free_pnode_x(struct pnode *t); extern void free_pnode_x(struct pnode *t);

Loading…
Cancel
Save