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