NEURON
model.cpp
Go to the documentation of this file.
1 #include <../../nmodlconf.h>
2 /* /local/src/master/nrn/src/modlunit/model.c,v 1.6 1998/07/12 13:19:02 hines Exp */
3 
4 /*
5  * int main(int argc, char *argv[]) --- returns 0 if translation is
6  * successful. Diag will exit with 1 if error.
7  *
8  * ---The overall strategy of the translation consists of three phases.
9  *
10  * 1) read in the whole file as a sequence of tokens, building a parse tree.
11  * The entire file can be printed exactly by printing the intoken list. No
12  * translation is done here but the symbol table is constructed.
13  * 2) Manipulate the blocks.
14  * 3) Output the lists.
15  *
16  * void openfiles(int argc, char *argv[]) parse the argument list, and open
17  * files. Print usage message and exit if no argument
18  *
19  */
20 
21 /*
22  * In order to interface this process with merge, a second argument is
23  * allowed which gives the complete input filename. The first argument
24  * still gives the prefix of the .c and .var files.
25  */
26 
27 #include "model.h"
28 #include "parse1.hpp"
29 
30 extern int yyparse();
31 
32 FILE
33  * fin, /* input file descriptor for filename.mod */
34  /* or file2 from the second argument */
35  *fparout, /* output file descriptor for filename.var */
36  *fcout; /* output file descriptor for filename.c */
37 
38 char finname[NRN_BUFSIZE]; /* filename.mod or second argument */
41 
42 #if LINT
43 char *clint;
44 int ilint;
45 Item *qlint;
46 #endif
47 
48 static char pgm_name[] = "model";
49 extern char *RCS_version;
50 extern char *RCS_date;
51 static void openfiles(int, char**);
52 static void debug_item(Item* q, int indent, FILE* file);
53 #if MAC
54 #include <sioux.h>
55 #endif
56 
57 int
58 main(int argc, char* argv[])
59 {
60  /*
61  * arg 1 is the prefix to the input file and output .c and .par
62  * files
63  * We first look for a .mrg file and then a .mod file
64  */
65 #if MAC
66  SIOUXSettings.asktosaveonclose = false;
67 #endif
68  Fprintf(stderr, "%s %s %s\n",
70 
71 
72  init(); /* keywords into symbol table, initialize
73  * lists, etc. */
74  unit_init();
75  nrn_unit_init();
76 #if MAC
77  mac_cmdline(&argc, &argv);
78 #endif
79  openfiles(argc, argv); /* .mrg else .mod, .var, .c */
80  Fprintf(stderr, "Checking units of %s\n", finname);
81 
82  lex_start();
83  /* declare all used variables */
84  parsepass(1);
85  IGNORE(yyparse());
87  /* At this point The input file is in the intoken list */
88 #if 0
91 #endif
92  /* give all names their proper units */
93  /* all variables used consistently (arrays) */
94  parsepass(2);
95  yyparse();
96  /*
97  * NAME's can be used in many cases before they were declared and
98  * no checking up to this point has been done to make sure that
99  * names have been used in only one way.
100  *
101  */
102  consistency();
103  /* check unit consistency */
104  parsepass(3);
105  yyparse();
106 #if MAC
107  printf("Units consistent in %s\n", finname);
108  SIOUXSettings.autocloseonquit = true;
109 #endif
110 #if 0
111  parout(); /* print .var file.
112  * Also #defines which used to be in defs.h
113  * are printed into .c file at beginning.
114  */
115  cout(); /* print .c file */
116 #endif
117 #if 0
118  IGNORE(fclose(fparout));
119  IGNORE(fclose(fcout));
120  memory_usage();
121 #endif
122 #if LINT
123  { /* for lex */
124  extern int yytchar, yylineno;
125  extern FILE *yyin;
126  IGNORE(yyin);
127  IGNORE(yytchar);
128  IGNORE(yylineno);
129  IGNORE(yyinput());
130  yyunput(ilint);
131  yyoutput(ilint);
132  }
133 #endif
134  return 0;
135 }
136 
137 static void openfiles(int argc, char* argv[])
138 {
139  char *cp, modprefix[NRN_BUFSIZE - 5];
140  if (argc > 1) {
141  assert(strlen(argv[1]) < NRN_BUFSIZE);
142  sprintf(modprefix, "%s", argv[1]);
143  cp = strstr(modprefix, ".mod");
144  if (cp) {
145  *cp = '\0';
146  }
147  }
148  if (argc == 2) {
149  Sprintf(finname, "%s.mrg", modprefix);
150  } else if (argc == 3) {
151  Sprintf(finname, "%s", argv[2]);
152  } else {
153  diag("Usage:", "modl prefixto.mod [inputfile]");
154  }
155  if ((fin = fopen(finname, "r")) == (FILE *) 0) {
156  Sprintf(finname, "%s.mod", modprefix);
157  if ((fin = fopen(finname, "r")) == (FILE *) 0) {
158  diag("Can't open input file: ", finname);
159  }
160  }
161 }
162 
163 void printlist(List* list)
164 {
165  Item *q;
166 
167  ITERATE(q, list) {
168  printitem(q, fcout);
169  }
170 }
171 
172 void printitems(Item* q1, Item* q2)
173 {
174  Item *q;
175 
176  for (q = q1; q->prev != q2; q = q->next) {
177  printitem(q, stderr);
178  }
179 }
180 
181 void printitem(Item* q, FILE* fp)
182 {
183  switch (q->itemtype) {
184 
185  case SYMBOL:
186  Fprintf(fp, "%s", SYM(q)->name);
187  break;
188  case STRING:
189  Fprintf(fp, "%s", STR(q));
190  break;
191  case NEWLINE:
192  Fprintf(fp, "\n");
193  break;
194  default:
195  Fprintf(stderr, "\nq->itemtype = %d\n", q->itemtype);
196  diag("printlist handles only a few types of items",
197  (char *)0);
198  break;
199  }
200  fflush(fp);
201 }
202 
204 {
205  debug_item(q, 0, stderr);
206 }
207 
208 static void debug_item(Item* q, int indent, FILE* file)
209 {
210  int i;
211  List *list;
212  Item *q1;
213 
214  for (i=0; i<indent; i++) {
215  Fprintf(file, " ");
216  }
217  if (!q) {
218  Fprintf(file, "NULL ITEM\n");
219  }else
220  switch (q->itemtype) {
221  case SYMBOL:
222  Fprintf(file, "SYMBOL |%s| %p\n", SYM(q)->name, SYM(q));
223  break;
224  case STRING:
225  Fprintf(file, "STRING |%s|\n", STR(q));
226  break;
227  case LIST:
228  Fprintf(file, "LIST\n");
229  list = LST(q);
230  ITERATE(q1, list) {
231  debug_item(q1, indent+2, file);
232  }
233  break;
234  case 0:
235  list = (List *)q;
236  Fprintf(file, "HEAD/TAIL of list\n");
237  ITERATE(q1, list) {
238  debug_item(q1, indent, file);
239  }
240  break;
241  case ITEM:
242  Fprintf(file, "ITEM\n");
243  debug_item(ITM(q), indent+2, file);
244  break;
245  case ITEMARRAY: {Item **qa; int i; long n;
246  qa = ITMA(q);
247  n = (size_t)qa[-1];
248  Fprintf(file, "ITEMARRAY %ld\n", n);
249  for (i=0; i<n; i++) {
250  debug_item(qa[i], indent+2, file);
251  }
252  }
253  break;
254  case NEWLINE:
255  Fprintf(file, "NEWLINE %d\n", q->itemsubtype);
256  break;
257  default:
258  Fprintf(stderr, "\nq->itemtype = %d\n", q->itemtype);
259  diag("unknown itemtype",
260  (char *)0);
261  break;
262  }
263  fflush(file);
264 }
265 
266 /* model.c,v
267  * Revision 1.6 1998/07/12 13:19:02 hines
268  * error when no args to modelunit fixed
269  *
270  * Revision 1.5 1997/12/01 14:51:39 hines
271  * mac port to codewarrior pro2 more complete
272  *
273  * Revision 1.4 1997/11/28 14:57:52 hines
274  * more changes for port to mac of modlunit
275  *
276  * Revision 1.3 1997/11/24 16:19:12 hines
277  * modlunit port to MAC (not complete)
278  *
279  * Revision 1.2 1997/10/20 14:58:07 hines
280  * modlunit file.mod accepted (ie suffix allowed)
281  *
282  * Revision 1.1.1.1 1994/10/12 17:22:49 hines
283  * NEURON 3.0 distribution
284  *
285  * Revision 1.6 1994/05/18 18:08:13 hines
286  * INCLUDE "file"
287  * tries originalpath/file ./file MODL_INCLUDEpaths/file
288  *
289  * Revision 1.5 1993/02/01 15:15:48 hines
290  * static functions should be declared before use
291  *
292  * Revision 1.4 91/02/09 16:39:35 hines
293  * special neuron variables checked for correct units.
294  *
295  * Revision 1.3 91/01/07 14:17:10 hines
296  * in kinunit, wrong itemsubtype. Fix lint messages
297  *
298  * Revision 1.2 90/11/16 07:53:34 hines
299  * take out the .c and .var file
300  *
301  * Revision 1.1 90/11/13 16:10:21 hines
302  * Initial revision
303  * */
304 
short itemsubtype
Definition: model.h:17
short itemtype
Definition: model.h:16
char * strstr(cs, ct) char *cs
#define ITEMARRAY
Definition: model.h:105
#define assert(ex)
Definition: hocassrt.h:26
FILE * fparout
Definition: model.cpp:33
char finname[NRN_BUFSIZE]
Definition: model.cpp:38
#define diag(s)
Definition: fmenu.cpp:188
void parout()
Definition: nocpout.cpp:225
#define ITERATE(itm, lst)
Definition: model.h:25
void unit_init()
Definition: units.cpp:625
#define SYM(q)
Definition: model.h:86
void parsepass(int)
Definition: passn.cpp:21
void debugitem(Item *q)
Definition: model.cpp:203
char * RCS_version
Definition: version.cpp:4
char * modprefix
Definition: modl.cpp:57
void printitems(Item *q1, Item *q2)
Definition: model.cpp:172
#define NRN_BUFSIZE
Definition: model.h:13
int main(int argc, char *argv[])
Definition: model.cpp:58
struct Item * prev
Definition: model.h:20
#define Fprintf
Definition: model.h:249
#define LIST
Definition: model.h:104
#define SYMBOL
Definition: model.h:102
static void openfiles(int, char **)
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)
#define IGNORE(arg)
Definition: model.h:262
Definition: model.h:15
void init()
Definition: init.cpp:169
static Frame * fp
Definition: code.cpp:154
static int indent
Definition: noccout.cpp:514
int const size_t const size_t n
Definition: nrngsl.h:12
#define printf
Definition: mwprefix.h:26
static char pgm_name[]
Definition: model.cpp:48
#define STRING
Definition: bbslsrv.cpp:9
#define ITEM
Definition: model.h:103
FILE * fin
Definition: model.cpp:33
char * RCS_date
Definition: version.cpp:5
void declare_implied()
Definition: declare.cpp:158
char * name
Definition: init.cpp:16
#define LST(q)
Definition: model.h:90
#define STR(q)
Definition: model.h:87
void memory_usage()
Definition: list.cpp:203
List * intoken
Definition: init.cpp:12
int yyparse()
#define i
Definition: md1redef.h:12
void printlist(List *list)
Definition: model.cpp:163
Item * parseroot
Definition: model.cpp:39
static void debug_item(Item *q, int indent, FILE *file)
Definition: model.cpp:208
Item * title
Definition: model.cpp:40
void lex_start()
#define Sprintf
Definition: model.h:248
void nrn_unit_init()
Definition: nrnunit.cpp:19
static int argc
Definition: inithoc.cpp:53
void printitem(Item *q, FILE *fp)
Definition: model.cpp:181
size_t q
#define ITMA(q)
Definition: model.h:89
FILE * fopen()
FILE * fcout
Definition: model.cpp:33
void consistency()
Definition: consist.cpp:21
static char ** argv
Definition: inithoc.cpp:54
#define ITM(q)
Definition: model.h:88