NEURON
modl.h
Go to the documentation of this file.
1 
2 #include <stdio.h>
3 #if HAVE_STRING_H
4 #include <string.h>
5 #else
6 #include <strings.h>
7 #endif
8 #include <assert.h>
9 
10 /**
11  * \dir
12  * \brief NMODL NMODL Translator (NOCMODL) Implementation
13  *
14  * \file
15  * \brief NMODL parser global flags / functions
16  */
17 
18 /**
19  * @defgroup NMODL NMODL Translator
20  * @brief Implementation of NMODL Source-to-Source translator (NOCMODL)
21  *
22  * @{
23  */
24 
25 
26 /*-
27  The central data structure throughout modl is the list. Items can be
28  inserted before a known item, inserted at the head of a list, or appended
29  to the tail of a list. Items can be removed from a list. Lists can contain
30  mixed types of items. Often an item which was an input token is edited and
31  replaced by a string. The main types of items are strings and symbols.
32  Lists used are:
33 
34  intoken Everytime the lexical analyser reads an input token it is
35  placed in this list. Much translation to C takes place during
36  parsing and most of that editing is done in this list. After
37  an entire block is processed, the tokens are moved as a group
38  to another list.
39 
40  initfunc main body of initmodel() from INITIAL. Automatic statements
41  of the form state = state0 are constructed here.
42  constructorfunc statements added to tail of allocation function
43  destructorfunc statements executed when POINT_PROCESS destroyed
44  termfunc main body of terminal() from TERMINAL
45  modelfunc main body of model() from EQUATION. SOLVE statements in the
46  equation block get expanded in this list
47  procfunc all remaining blocks get concatenated to this list. It
48  also gets the declarations. By prepending to procfunc, one
49  can guarantee that a declaration precedes usage.
50  initlist automatically generated statements that are to be executed
51  only once are constructed here. Contains setup of slist's
52  and dist's, etc. i.e. setup of indirect pointer lists to
53  state groups.
54 
55  Other lists (not global) but used in several files are:
56 
57  symlist[] symbol table. One list for every beginning ascii character.
58  see symbol.c and symbol.h. See the Symbol structure below.
59  symlistlist for a stack of local symbol lists for LOCAL variables.
60  Local variable 'name' is translated as _lname so that
61  there can be no conflict with the 'p' array.
62  syminorder Maintains order of variable declarations in the input file.
63  Used to get good order in the .var file.
64 
65  firstlist Statements that must appear before anything else in the
66  .c file. The only usage at this time is declaration of
67  LOCAL variables outside of blocks.
68 
69  plotlist variables to be plotted on first entry to scop
70 
71  Other lists used for local or very specific purposes are:
72 
73  indeplist Independent variable info.
74  parmlist parameters in a SENS statement
75  statelist states used in a block containing a SENS statement
76  sensinfo see sens.c
77  senstmt see sens.c
78  solvq Item location and method stored when SOLVE statement occurs.
79  Solve statements are processed after all input is read.
80  eqnq Linear equations assembled using this list.
81 
82  Kinetic.c should be modified to make uniform use of List's instead of
83  special structures for Reaction, Rlist, etc.
84  */
85 
86 /* For char buffers that might be called on to hold long path names */
87 /* Note that paths can exceed MAX_PATH from <limits.h> on some systems */
88 #define NRN_BUFSIZE 8192
89 #include <limits.h>
90 #if MAX_PATH > NRN_BUFSIZE
91 #undef NRN_BUFSIZE
92 #define NRN_BUFSIZE MAX_PATH
93 #endif
94 
95 typedef struct Item List; /* list of mixed items */
96 typedef struct Item {
97  short itemtype;
98  union {
99  struct Item *itm;
101  char *str;
102  struct Symbol *sym;
103  } element; /* pointer to the actual item */
104  struct Item *next;
105  struct Item *prev;
106 } Item;
107 #define ITEM0 (Item *)0
108 #define LIST0 (List *)0
109 
110 #define ITERATE(itm,lst) for (itm = (lst)->next; itm != (lst); itm = itm->next)
111 
112 /*-
113 The symbol structure gives info about tokens. Not all tokens need all
114 elements. Eg. the STRING uses only type and name. Much storage could be
115 saved and much greater clarity could be attained if each type had its own
116 sub stucture. Currently many of the structure elements serve very different
117 purposes depending on the type.
118 The following is a list of the current element usage:
119  type token number from parse1.ypp
120  subtype see definitions below
121  u.i integration method - flag for variable step
122  equation block - function number for generating variables
123  u.str scop variables - max,min,units for .var file
124  used state variable - temporary flag that it is used in an equation
125  equation block - number of state variables used (# unknowns)
126  in parout.c - the numeric order in the .var file. Generated
127  and used in parout.c for the plotlist.
128  usage a token is used as a variable (DEP) or function (FUNC)
129  Another field, EXPLICIT_DECL, is used to determine if a
130  variable appears in the input file or is automatically
131  created, thus helping to organize the .var file.
132  araydim arrays - dimension
133  discdim discrete variable - dimension
134  varnum state variable - during processing of a block containing
135  equations in which simultaneous equations result; column
136  number of state variable in the matrix.
137  level lowest submodel level number for declarations of this
138  symbol. Used for parameters ( in explicit_decl()).
139  The default value is 100.
140  name token name
141 */
142 typedef struct Symbol {
143  short type;
144  long subtype;
145 #if NMODL
146  short nrntype;
147  short assigned_to_;
148  int no_threadargs; /* introduced for FUNCTION_TABLE table_name */
149 #if CVODE
150  int* slist_info_; /* blunt instrument for retrieving ion concentration slist value */
151 #endif
152  int ioncount_; /* ppvar index for ions */
153 #endif
154  union {
155  int i;
156  char *str;
157  } u;
158  int used;
159  int usage;
160  int araydim;
161  int discdim;
162  int varnum; /* column number of state variable in
163  * equations */
164  short level;
165  char *name;
166 } Symbol;
167 #define SYM0 (Symbol *)0
168 
169 /*
170  * this is convenient way to get the element pointer if you know what type
171  * the item is
172  */
173 #define SYM(q) ((q)->element.sym)
174 #define STR(q) ((q)->element.str)
175 #define ITM(q) ((q)->element.itm)
176 #define LST(q) ((q)->element.lst)
177 /* types not defined in parser */
178 #define SPECIAL 1
179 #define SYMBOL 1
180 #define ITEM 2
181 #define LIST 3
182 /*
183  * An item type, STRING is also used as an item type
184  * An item type, VERBATIM is also used as an item type which is to be
185  * treated the same as a STRING but with no prepended space on output.
186  */
187 
188 /* subtypes */
189 #define KEYWORD 01
190 #define PARM 02
191 #define INDEP 04
192 #define DEP 010 /* also in usage field */
193 #define STAT 020
194 #define ARRAY 040
195 #define FUNCT 0100 /* also in usage field */
196 #define PROCED 0200
197 #define NEGATIVE 0400
198 #define SEMI 01 /* ";" */
199 #define BEGINBLK 02 /* "{" */
200 #define ENDBLK 04 /* "}" */
201 #define DERF 01000
202 #define KINF 02000
203 #define NLINF 04000
204 #define DISCF 010000
205 #define STEP1 020000
206 #define PARF 040000
207 #define EXTDEF 0100000
208 #define LINF 0200000
209 #define UNITDEF 0400000L
210 #define EXTDEF2 01000000L /* functions that can take array or function name arguments */
211 #define nmodlCONST 02000000L /* constants that do not appear in .var file */
212 #define EXTDEF3 04000000L /* get two extra reset arguments at beginning */
213 #define INTGER 010000000L /* must be cast to double in expr */
214 #define EXTDEF4 020000000L /* get extra NrnThread* arg at beginning */
215 #define EXTDEF5 040000000L /* not threadsafe from the extdef list */
216 #define EXPLICIT_DECL 01 /* usage field, variable occurs in input file */
217 
218 extern char
219  *emalloc(unsigned), /* malloc with out of space checking */
220  *stralloc(char*, char*), /* copies string to new space */
221  *inputline(), /* used only by parser to get title line */
222  *inputtopar(), /* used only by parser to get units */
223  *Gets(char*); /* used only in io.c to get string from fin. */
224 
225 #if 0
226 #if __TURBOC__ || SYSV || NeXT || LINUX
227 #else
228 extern char *sprintf();
229 #endif
230 #endif
231 
232 extern int nmodl_text; /* if 1, the text of the mod file is a string in the mod file */
233 extern List* filetxtlist;
234 
235 extern List
236  *newlist(), /* begins new empty list */
237  *inputtext(); /* used by parser to get block text from
238  * VERBATIM and COMMENT */
239 extern Item
240  *putintoken(char *s, short type), /* construct symbol and store input tokens */
241  *insertstr(Item* item, char* str), /* before a known Item */
242  *insertsym(List* list, Symbol* sym),
243  *linsertstr(List* list, char* str), /* prepend to list */
244  *lappendstr(List* list, char* str), /* append to list */
245  *linsertsym(List* list, Symbol* sym),
246  *lappendsym(List* list, Symbol* sym),
247  *lappenditem(List* list, Item* item),
248  *lappendlst(List* list, List* lst),
249  *next(), /* not used but should be instead of q->next */
250  *prev();
251 
252 extern Symbol
253  *install(char*, int), /* Install token in symbol table */
254  *lookup(char*), /* lookup name in symbol table */
255  *copylocal(Symbol*), /* install LOCAL variable symbol */
256  *ifnew_parminstall(char* name, char* num, char* units, char* limits); /* new .var info only if
257  * not already done. */
258 #include "nmodlfunc.h"
259 
260 extern char* finname; /* the input file prefix */
261 extern char buf[]; /* general purpose temporary buffer */
262 extern char* modprefix;
263 
264 extern List
265  *intoken, /* Main list of input tokens */
266  *initfunc, /* see discussion above */
267  *constructorfunc,
268  *destructorfunc,
269  *modelfunc,
270  *termfunc,
271  *procfunc,
272  *initlist,
273  *firstlist,
274  *plotlist;
275 
276 extern FILE
277  *fin, /* .mod input file descriptor */
278  *fparout, /* .var file */
279  *fcout; /* .c file */
280 
281 extern Symbol
282  *semi, /* ';'. When seen on output, causes newline */
283  *beginblk, /* '{'. Used for rudimentary indentation */
284  *endblk; /* on output. */
285 
286 extern void printlist(List*);
287 
288 /* the following is to get lint to shut up */
289 #if LINT
290 #undef assert
291 #define assert(arg) {if (arg) ;} /* so fprintf doesn't give lint */
292 extern char *clint;
293 extern int ilint;
294 extern Item *qlint;
295 #define Sprintf clint = sprintf
296 #define Fprintf ilint = fprintf
297 #define Fclose ilint = fclose
298 #define Fflush ilint = fflush
299 #define Printf ilint = printf
300 #define Strcpy clint = strcpy
301 #define Strcat clint = strcat
302 #define Insertstr qlint = insertstr
303 #define Insertsym qlint = insertsym
304 #define Linsertsym qlint = linsertsym
305 #define Linsertstr qlint = linsertstr
306 #define Lappendsym qlint = lappendsym
307 #define Lappendstr qlint = lappendstr
308 #define Lappenditem qlint = lappenditem
309 #define Lappendlst qlint = lappendlst
310 #define IGNORE(arg) {if (arg);}
311 #define Free(arg) free((char *)(arg))
312 #else
313 #define Sprintf sprintf
314 #define Fprintf fprintf
315 #define Fclose fclose
316 #define Fflush fflush
317 #define Printf printf
318 #define Strcpy strcpy
319 #define Strcat strcat
320 #define Insertstr insertstr
321 #define Insertsym insertsym
322 #define Linsertsym linsertsym
323 #define Linsertstr linsertstr
324 #define Lappendsym lappendsym
325 #define Lappendstr lappendstr
326 #define Lappenditem lappenditem
327 #define Lappendlst lappendlst
328 #define IGNORE(arg) arg
329 #define Free(arg) free((void *)(arg))
330 #endif
331 
332 
333 void verbatim_adjust(char* q);
334 /** @} */ // end of hoc_functions
Item * lappendstr(List *list, char *str)
List * lst
Definition: modl.h:100
char * inputline()
struct Item * itm
Definition: modl.h:99
short itemtype
Definition: model.h:16
Item * lappenditem(List *list, Item *item)
char * inputtopar()
List * filetxtlist
Definition: modl.cpp:68
short type
Definition: cabvars.h:10
FILE * fparout
Definition: model.cpp:33
List * constructorfunc
Definition: init.cpp:193
char * finname
Definition: model.cpp:38
Item * linsertsym(List *list, Symbol *sym)
List * initfunc
Definition: init.cpp:8
static HocParmLimits limits[]
Definition: extcelln.cpp:33
Item * insertsym(List *list, Symbol *sym)
List * inputtext()
Item * insertstr(Item *item, char *str)
char * modprefix
Definition: modl.cpp:57
struct Symbol Symbol
List * initlist
Definition: init.cpp:8
Symbol * endblk
Definition: init.cpp:11
struct Item * prev
Definition: model.h:20
struct Item * next
Definition: model.h:19
sprintf(buf," if (secondorder) {\ " int _i;\" " for(_i=0;_i< %d;++_i) {\" " _p[_slist%d[_i]]+=dt *_p[_dlist%d[_i]];\" " }}\", numeqn, listnum, listnum)
List * newlist()
struct Item Item
Symbol * beginblk
Definition: init.cpp:11
Definition: model.h:15
Symbol * copylocal(Symbol *)
Item * lappendlst(List *list, List *lst)
Symbol * ifnew_parminstall(char *name, char *num, char *units, char *limits)
Definition: parsact.cpp:177
_CONST char * s
Definition: system.cpp:74
char * Gets(char *)
Definition: io.cpp:97
List * procfunc
Definition: init.cpp:9
FILE * fin
Definition: model.cpp:33
Definition: model.h:57
Item * linsertstr(List *list, char *str)
char * name
Definition: init.cpp:16
int nmodl_text
Definition: modl.cpp:67
Item * putintoken(char *s, short type)
void * element
Definition: model.h:18
void units(unit *)
Definition: units.cpp:736
Symbol * lookup(char *)
Item * lappendsym(List *list, Symbol *sym)
Symbol * install(char *, int)
char * emalloc(unsigned)
Definition: list.cpp:189
struct Symbol * sym
Definition: modl.h:102
void verbatim_adjust(char *q)
Definition: modl.cpp:362
List * destructorfunc
Definition: init.cpp:193
char * str
Definition: modl.h:101
List * intoken
Definition: init.cpp:12
long subtype
Definition: init.cpp:122
#define i
Definition: md1redef.h:12
void printlist(List *)
Definition: model.cpp:163
Symbol * semi
Definition: init.cpp:11
List * firstlist
Definition: init.cpp:8
char buf[]
Definition: init.cpp:13
List * modelfunc
Definition: init.cpp:8
size_t q
List * termfunc
Definition: init.cpp:8
FILE * fcout
Definition: model.cpp:33
char * stralloc(char *, char *)
Definition: list.cpp:208
List * plotlist
Definition: init.cpp:9