NEURON
getsym.cpp
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 /* /local/src/master/nrn/src/oc/getsym.cpp,v 1.2 1996/02/16 16:19:26 hines Exp */
3 /*
4 getsym.cpp,v
5  * Revision 1.2 1996/02/16 16:19:26 hines
6  * OCSMALL used to throw out things not needed by teaching programs
7  *
8  * Revision 1.1.1.1 1994/10/12 17:22:08 hines
9  * NEURON 3.0 distribution
10  *
11  * Revision 1.4 92/08/18 07:31:38 hines
12  * arrays in different objects can have different sizes.
13  * Now one uses araypt(symbol, SYMBOL) or araypt(symbol, OBJECTVAR) to
14  * return index of an array variable.
15  *
16  * Revision 1.3 91/10/18 14:40:36 hines
17  * symbol tables now are type Symlist containing pointers to first and last
18  * symbols. New symbols get added onto the end.
19  *
20  * Revision 1.2 91/10/17 15:01:28 hines
21  * VAR, STRING now handled with pointer to value located in object data space
22  * to allow future multiple objects. Ie symbol for var, string, objectvar
23  * has offset into a pointer data space.
24  *
25  * Revision 1.1 91/10/11 11:12:03 hines
26  * Initial revision
27  *
28  * Revision 3.9 89/07/20 09:52:17 mlh
29  * code functions no longer in hoc.h
30  *
31  * Revision 3.7 89/07/13 08:21:30 mlh
32  * stack functions involve specific types instead of Datum
33  *
34  * Revision 3.4 89/07/12 10:27:00 mlh
35  * Lint free
36  *
37  * Revision 3.3 89/07/10 15:46:00 mlh
38  * Lint pass1 is silent. Inst structure changed to union.
39  *
40  * Revision 2.0 89/07/07 11:36:58 mlh
41  * *** empty log message ***
42  *
43  * Revision 1.1 89/07/07 11:16:01 mlh
44  * Initial revision
45  *
46 */
47 
48 /* Psym *hoc_getsym("variable") returns a pointer to a new Psym structure
49  that contains info used to find where the variable keeps its data.
50  Array indices cannot involve local variables.
51  Example: hoc_getsym("a[i][j]") depends on the values of i and j when
52  hoc_getsym is called; and not on their values when the Psym is used.
53 
54  double hoc_getsymval(Psym *p) returns the value of the variable. If an
55  array, the indices used are the values they had when hoc_getsym
56  was called.
57 
58  hoc_assignsym(Psym *p, double val) sets the value of the variable.
59 
60  hoc_execstr(char *s) compiles and executes the string
61 */
62 #if OCSMALL
63 #else
64 
65 #include "hocgetsym.h"
66 #include "parse.hpp"
67 #include "hocparse.h"
68 #include "code.h"
69 
70 Psym *hoc_getsym(const char* cp) {
71  Symbol *sp, *sym;
72  Symlist *symlist = (Symlist *)0;
73  Inst *last, *pcsav;
74  int i, n;
75  char s[256];
76  Psym *p=0;
77 
78  Sprintf(s, "{%s}\n", cp);
79  sp = hoc_install("", PROCEDURE, 0., &symlist);
80  sp->u.u_proc->defn.in = STOP;
81  sp->u.u_proc->list = (Symlist *)0;
82  sp->u.u_proc->nauto = 0;
83  n = hoc_xopen_run(sp, s);
84  last = (Inst *)sp->u.u_proc->defn.in + n;
85  if (n < 5 || last[-3].pf != hoc_eval) {
86  hoc_execerror(s, " not a variable");
87  }
88  last[-3].in = STOP; /*before doing last EVAL*/
89  pcsav = hoc_pc;
90  hoc_execute(sp->u.u_proc->defn.in);
91  hoc_pc = pcsav;
92 
93  sym = hoc_spop();
94  switch(sym->type) {
95  case UNDEF:
96  hoc_execerror(s, " is undefined");
97  case VAR:
98  if (ISARRAY(sym)) {
99  Arrayinfo* a;
100  if (sym->subtype == NOTUSER) {
101  a = OPARINFO(sym);
102  }else{
103  a = sym->arayinfo;
104  }
105  p = (Psym *)emalloc((unsigned)(sizeof(Psym) +
106  a->nsub));
107  p->arayinfo = a;
108  ++a->refcount;
109  p->nsub = a->nsub;
110  for (i=p->nsub; i > 0;) {
111  p->sub[--i] = hoc_xpop();
112  }
113  } else {
114  p = (Psym *)emalloc(sizeof(Psym));
115  p->arayinfo = 0;
116  p->nsub = 0;
117  }
118  p->sym = sym;
119  break;
120  case AUTO:
121  hoc_execerror(s, " is local variable");
122  default:
123  hoc_execerror(s, " not a variable");
124  }
125  hoc_free_list(&symlist);
126  return p;
127 }
128 
129 static void arayonstack(Psym* p) {
130  int i;
131  double d;
132 
133  if (p->nsub) {
134  if (!ISARRAY(p->sym) || p->nsub != p->arayinfo->nsub) {
135  hoc_execerror("wrong number of subscripts for ", p->sym->name);
136  }
137  for (i=0; i < p->nsub; i++) {
138  d = p->sub[i];
139  hoc_pushx(d);
140  }
141  }
142 }
143 
144 double hoc_getsymval(Psym* p) {
145  arayonstack(p);
146  hoc_pushs(p->sym);
147  hoc_eval();
148  return hoc_xpop();
149 }
150 
151 void hoc_assignsym(Psym* p, double val){
152  arayonstack(p);
153  hoc_pushx(val);
154  hoc_pushs(p->sym);
155  hoc_assign();
156  hoc_nopop();
157 }
158 
159 void hoc_execstr(const char* cp) {
160  Symbol *sp;
161  Symlist *symlist = (Symlist *)0;
162  Inst *pcsav;
163  char s[256];
164 
165  Sprintf(s, "{%s}\n", cp);
166  sp = hoc_install("", PROCEDURE, 0., &symlist);
167  sp->u.u_proc->defn.in = STOP;
168  sp->u.u_proc->list = (Symlist *)0;
169  sp->u.u_proc->nauto = 0;
170  IGNORE(hoc_xopen_run(sp, s));
171  pcsav = hoc_pc;
172  hoc_execute(sp->u.u_proc->defn.in);
173  hoc_pc = pcsav;
174  hoc_free_list(&symlist);
175 }
176 #endif /*OCSMALL*/
void hoc_nopop()
void hoc_execute(Inst *)
Definition: hocdec.h:84
struct Psym Psym
void hoc_free_list(Symlist **)
Psym * hoc_getsym(const char *cp)
Definition: getsym.cpp:70
if(status)
short type
Definition: model.h:58
int nsub
Definition: hocdec.h:70
size_t p
Arrayinfo * arayinfo
Definition: hocgetsym.h:8
char * name
Definition: model.h:72
#define OPARINFO(sym)
Definition: hocdec.h:309
HocStruct Symlist * list
Definition: hocdec.h:78
int sub[1]
Definition: hocgetsym.h:10
Definition: hocdec.h:51
Symbol * hoc_install(const char *, int, double, Symlist **)
int refcount
Definition: hocdec.h:71
#define IGNORE(arg)
Definition: model.h:262
int hoc_xopen_run(Symbol *sp, const char *str)
Definition: code.cpp:584
#define NOTUSER
Definition: hocdec.h:91
int const size_t const size_t n
Definition: nrngsl.h:12
Symbol * hoc_spop(void)
_CONST char * s
Definition: system.cpp:74
int val
Definition: dll.cpp:167
#define STOP
Definition: hocdec.h:66
int nauto
Definition: hocdec.h:80
Arrayinfo * arayinfo
Definition: hocdec.h:158
#define ISARRAY(arg)
Definition: hocdec.h:163
double hoc_xpop(void)
void hoc_execerror(const char *, const char *)
Definition: hoc.cpp:741
Proc * u_proc
Definition: hocdec.h:144
Inst defn
Definition: hocdec.h:76
Definition: model.h:57
char * emalloc(unsigned n)
Definition: list.cpp:189
void hoc_pushs(Symbol *)
long subtype
Definition: model.h:59
void hoc_pushx(double)
Definition: hocgetsym.h:6
#define symlist
Definition: cabcode.cpp:17
static void arayonstack(Psym *p)
Definition: getsym.cpp:129
void hoc_execstr(const char *cp)
Definition: getsym.cpp:159
Inst * hoc_pc
#define i
Definition: md1redef.h:12
int nsub
Definition: hocgetsym.h:9
void hoc_assignsym(Psym *p, double val)
Definition: getsym.cpp:151
#define Sprintf
Definition: model.h:248
union Symbol::@18 u
Symbol * sym
Definition: hocgetsym.h:7
double hoc_getsymval(Psym *p)
Definition: getsym.cpp:144
HocUnion Inst * in
Definition: hocdec.h:60