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;
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;
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 *emalloc(unsigned), /* malloc with out of space checking */
219  *stralloc(char*, char*), /* copies string to new space */
220  *inputline(), /* used only by parser to get title line */
221  *inputtopar(), /* used only by parser to get units */
222  *Gets(char*); /* used only in io.c to get string from fin. */
223 
224 #if 0
225 #if __TURBOC__ || SYSV || NeXT || LINUX
226 #else
227 extern char *sprintf();
228 #endif
229 #endif
230 
231 extern int nmodl_text; /* if 1, the text of the mod file is a string in the mod file */
232 extern List* filetxtlist;
233 
234 extern List *newlist(), /* begins new empty list */
235  *inputtext(); /* used by parser to get block text from
236  * VERBATIM and COMMENT */
237 extern Item *putintoken(char*s, short type), /* construct symbol and store input tokens */
238  *insertstr(Item*item, char*str), /* before a known Item */
239  *insertsym(List*list, Symbol*sym), *linsertstr(List*list, char*str), /* prepend to list */
240  *lappendstr(List*list, char*str), /* append to list */
241  *linsertsym(List*list, Symbol*sym), *lappendsym(List*list, Symbol*sym),
242  *lappenditem(List*list, Item*item), *lappendlst(List*list, List*lst),
243  *next(), /* not used but should be instead of q->next */
244  *prev();
245 
246 extern Symbol *install(char*, int), /* Install token in symbol table */
247  *lookup(char*), /* lookup name in symbol table */
248  *copylocal(Symbol*), /* install LOCAL variable symbol */
249  *ifnew_parminstall(char*name, char*num, char*units, char*limits); /* new .var info only if
250  * not already done. */
251 #include "nmodlfunc.h"
252 
253 extern char* finname; /* the input file prefix */
254 extern char buf[]; /* general purpose temporary buffer */
255 extern char* modprefix;
256 
257 extern List *intoken, /* Main list of input tokens */
258  *initfunc, /* see discussion above */
261 
262 extern FILE *fin, /* .mod input file descriptor */
263  *fparout, /* .var file */
264  *fcout; /* .c file */
265 
266 extern Symbol *semi, /* ';'. When seen on output, causes newline */
267  *beginblk, /* '{'. Used for rudimentary indentation */
268  *endblk; /* on output. */
269 
270 extern void printlist(List*);
271 
272 /* the following is to get lint to shut up */
273 #if LINT
274 #undef assert
275 #define assert(arg) \
276  { \
277  if (arg) \
278  ; \
279  } /* so fprintf doesn't give lint */
280 extern char* clint;
281 extern int ilint;
282 extern Item* qlint;
283 #define Sprintf clint = sprintf
284 #define Fprintf ilint = fprintf
285 #define Fclose ilint = fclose
286 #define Fflush ilint = fflush
287 #define Printf ilint = printf
288 #define Strcpy clint = strcpy
289 #define Strcat clint = strcat
290 #define Insertstr qlint = insertstr
291 #define Insertsym qlint = insertsym
292 #define Linsertsym qlint = linsertsym
293 #define Linsertstr qlint = linsertstr
294 #define Lappendsym qlint = lappendsym
295 #define Lappendstr qlint = lappendstr
296 #define Lappenditem qlint = lappenditem
297 #define Lappendlst qlint = lappendlst
298 #define IGNORE(arg) \
299  { \
300  if (arg) \
301  ; \
302  }
303 #define Free(arg) free((char*) (arg))
304 #else
305 #define Sprintf sprintf
306 #define Fprintf fprintf
307 #define Fclose fclose
308 #define Fflush fflush
309 #define Printf printf
310 #define Strcpy strcpy
311 #define Strcat strcat
312 #define Insertstr insertstr
313 #define Insertsym insertsym
314 #define Linsertsym linsertsym
315 #define Linsertstr linsertstr
316 #define Lappendsym lappendsym
317 #define Lappendstr lappendstr
318 #define Lappenditem lappenditem
319 #define Lappendlst lappendlst
320 #define IGNORE(arg) arg
321 #define Free(arg) free((void*) (arg))
322 #endif
323 
324 
325 void verbatim_adjust(char* q);
326 /** @} */ // end of hoc_functions
short type
Definition: cabvars.h:9
sprintf(buf, " if (secondorder) {\n" " int _i;\n" " for (_i = 0; _i < %d; ++_i) {\n" " _p[_slist%d[_i]] += dt*_p[_dlist%d[_i]];\n" " }}\n", numeqn, listnum, listnum)
static HocParmLimits limits[]
Definition: extcelln.cpp:38
Item * linsertstr(List *list, char *str)
List * destructorfunc
Definition: modl.h:259
char * Gets(char *)
Definition: io.cpp:93
List * plotlist
Definition: modl.h:260
List * initfunc
Definition: modl.h:258
List * filetxtlist
Definition: modl.cpp:67
Item * lappendsym(List *list, Symbol *sym)
Item * insertsym(List *list, Symbol *sym)
List * procfunc
Definition: modl.h:259
FILE * fcout
Definition: modl.h:264
Item * lappenditem(List *list, Item *item)
void verbatim_adjust(char *q)
Definition: modl.cpp:374
char * finname
Definition: model.cpp:37
void printlist(List *)
Definition: model.cpp:158
Item * putintoken(char *s, short type)
Symbol * ifnew_parminstall(char *name, char *num, char *units, char *limits)
Definition: parsact.cpp:174
Symbol * install(char *, int)
char * modprefix
Definition: modl.cpp:56
char * emalloc(unsigned)
Definition: list.cpp:166
Item * next()
Symbol * beginblk
Definition: modl.h:267
char * inputline()
List * firstlist
Definition: modl.h:259
struct Item Item
char buf[]
Definition: init.cpp:13
List * newlist()
Item * prev()
Symbol * lookup(char *)
Symbol * semi
Definition: init.cpp:11
List * initlist
Definition: modl.h:259
FILE * fparout
Definition: modl.h:263
List * intoken
Definition: init.cpp:12
List * termfunc
Definition: modl.h:259
FILE * fin
Definition: model.cpp:32
Symbol * endblk
Definition: modl.h:268
struct Symbol Symbol
Item * lappendlst(List *list, List *lst)
Symbol * copylocal(Symbol *)
List * constructorfunc
Definition: modl.h:259
char * inputtopar()
Item * insertstr(Item *item, char *str)
List * inputtext()
Item * linsertsym(List *list, Symbol *sym)
char * stralloc(char *, char *)
Definition: list.cpp:184
List * modelfunc
Definition: modl.h:259
int nmodl_text
Definition: modl.cpp:66
Item * lappendstr(List *list, char *str)
char * name
Definition: init.cpp:16
void units(unit *)
Definition: units.cpp:733
size_t q
Definition: model.h:15
struct Item * prev
Definition: model.h:20
struct Symbol * sym
Definition: modl.h:102
List * lst
Definition: modl.h:100
void * element
Definition: model.h:18
struct Item * itm
Definition: modl.h:99
char * str
Definition: modl.h:101
short itemtype
Definition: model.h:16
struct Item * next
Definition: model.h:19
Definition: model.h:57
int i
Definition: model.h:62
int usage
Definition: model.h:66
short type
Definition: model.h:58
short level
Definition: model.h:71
int araydim
Definition: model.h:67
long subtype
Definition: model.h:59
union Symbol::@18 u
char * name
Definition: model.h:72
char * str
Definition: model.h:63
int discdim
Definition: model.h:68
int used
Definition: model.h:65
int varnum
Definition: model.h:69