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