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 (is_array(*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) + a->nsub));
106  p->arayinfo = a;
107  ++a->refcount;
108  p->nsub = a->nsub;
109  for (i = p->nsub; i > 0;) {
110  p->sub[--i] = hoc_xpop();
111  }
112  } else {
113  p = (Psym*) emalloc(sizeof(Psym));
114  p->arayinfo = 0;
115  p->nsub = 0;
116  }
117  p->sym = sym;
118  break;
119  case AUTO:
120  hoc_execerror(s, " is local variable");
121  default:
122  hoc_execerror(s, " not a variable");
123  }
125  return p;
126 }
127 
128 static void arayonstack(Psym* p) {
129  int i;
130  double d;
131 
132  if (p->nsub) {
133  if (!is_array(*p->sym) || p->nsub != p->arayinfo->nsub) {
134  hoc_execerror("wrong number of subscripts for ", p->sym->name);
135  }
136  for (i = 0; i < p->nsub; i++) {
137  d = p->sub[i];
138  hoc_pushx(d);
139  }
140  }
141 }
142 
143 double hoc_getsymval(Psym* p) {
144  arayonstack(p);
145  hoc_pushs(p->sym);
146  hoc_eval();
147  return hoc_xpop();
148 }
149 
150 void hoc_assignsym(Psym* p, double val) {
151  arayonstack(p);
152  hoc_pushx(val);
153  hoc_pushs(p->sym);
154  hoc_assign();
155  hoc_nopop();
156 }
157 
158 void hoc_execstr(const char* cp) {
159  Symbol* sp;
160  Symlist* symlist = (Symlist*) 0;
161  Inst* pcsav;
162  char s[256];
163 
164  Sprintf(s, "{%s}\n", cp);
165  sp = hoc_install("", PROCEDURE, 0., &symlist);
166  sp->u.u_proc->defn.in = STOP;
167  sp->u.u_proc->list = (Symlist*) 0;
168  sp->u.u_proc->nauto = 0;
169  IGNORE(hoc_xopen_run(sp, s));
170  pcsav = hoc_pc;
171  hoc_execute(sp->u.u_proc->defn.in);
172  hoc_pc = pcsav;
174 }
175 #endif /*OCSMALL*/
#define symlist
Definition: cabcode.cpp:49
void hoc_assign()
Definition: code.cpp:2159
void hoc_eval(void)
Definition: code.cpp:1827
#define i
Definition: md1redef.h:19
double hoc_getsymval(Psym *p)
Definition: getsym.cpp:143
static void arayonstack(Psym *p)
Definition: getsym.cpp:128
void hoc_assignsym(Psym *p, double val)
Definition: getsym.cpp:150
void hoc_execstr(const char *cp)
Definition: getsym.cpp:158
Psym * hoc_getsym(const char *cp)
Definition: getsym.cpp:70
double hoc_xpop()
Definition: code.cpp:903
Symbol * hoc_install(const char *, int, double, Symlist **)
Definition: symbol.cpp:77
void hoc_free_list(Symlist **)
Definition: symbol.cpp:254
int hoc_xopen_run(Symbol *sp, const char *str)
Definition: code.cpp:667
Symbol * hoc_spop()
Definition: code.cpp:928
void hoc_nopop()
Definition: code.cpp:972
#define NOTUSER
Definition: hocdec.h:82
#define OPARINFO(sym)
Definition: hocdec.h:239
bool is_array(const Symbol &sym)
Definition: hocdec.h:136
#define STOP
Definition: hocdec.h:57
struct Psym Psym
void hoc_pushx(double)
Definition: code.cpp:779
#define IGNORE(arg)
Definition: model.h:224
void hoc_execerror(const char *s1, const char *s2)
Definition: nrnoc_aux.cpp:39
static void * emalloc(size_t size)
Definition: mpispike.cpp:30
int Sprintf(char(&buf)[N], const char *fmt, Args &&... args)
Redirect sprintf to snprintf if the buffer size can be deduced.
Definition: wrap_sprintf.h:14
if(ncell==0)
Definition: cellorder.cpp:785
int const size_t const size_t n
Definition: nrngsl.h:10
size_t p
s
Definition: multisend.cpp:521
void hoc_pushs(Symbol *)
Definition: code.cpp:841
Inst * hoc_pc
Definition: code.cpp:78
void hoc_execute(Inst *)
Definition: code.cpp:2531
int nsub
Definition: hocdec.h:61
int refcount
Definition: hocdec.h:62
int nauto
Definition: hocdec.h:71
Inst defn
Definition: hocdec.h:67
Symlist * list
Definition: hocdec.h:69
Definition: hocgetsym.h:5
Definition: model.h:47
Proc * u_proc
Definition: hocdec.h:120
union Symbol::@28 u
short type
Definition: model.h:48
long subtype
Definition: model.h:49
Arrayinfo * arayinfo
Definition: hocdec.h:130
Definition: hocdec.h:75
Definition: hocdec.h:42
Inst * in
Definition: hocdec.h:51