f8ebc57c09ab2975810806efb2d49fd79bb8d34f
[ghc-hetmet.git] / ghc / compiler / parser / util.c
1 /**********************************************************************
2 *                                                                     *
3 *                                                                     *
4 *      Declarations                                                   *
5 *                                                                     *
6 *                                                                     *
7 **********************************************************************/
8
9 #include "hspincl.h"
10 #include "constants.h"
11 #include "utils.h"
12
13 #define PARSER_VERSION "1.3-???"
14
15 tree root;              /* The root of the built syntax tree. */
16 list Lnil;
17
18 BOOLEAN nonstandardFlag = FALSE;  /* Set if non-std Haskell extensions to be used. */
19 BOOLEAN acceptPrim = FALSE;       /* Set if Int#, etc., may be used                */
20 BOOLEAN haskell1_2Flag = FALSE;   /* Set if we are compiling for 1.2               */
21 BOOLEAN etags = FALSE;            /* Set if we're parsing only to produce tags.    */
22 BOOLEAN hashIds = FALSE;          /* Set if Identifiers should be hashed.          */
23                                   
24 BOOLEAN ignoreSCC = TRUE;         /* Set if we ignore/filter scc expressions.      */
25                                   
26 static BOOLEAN verbose = FALSE;         /* Set for verbose messages. */
27
28 /* Forward decls */
29 static void who_am_i PROTO((void));
30
31 /**********************************************************************
32 *                                                                     *
33 *                                                                     *
34 *     Utility Functions                                               *
35 *                                                                     *
36 *                                                                     *
37 **********************************************************************/
38
39 # include <stdio.h>
40 # include "constants.h"
41 # include "hspincl.h"
42 # include "utils.h"
43
44 void
45 process_args(argc,argv)
46   int argc;
47   char **argv;
48 {
49     BOOLEAN keep_munging_option = FALSE;
50
51     argc--, argv++;
52
53     while (argc > 0 && argv[0][0] == '-') {
54
55         keep_munging_option = TRUE;
56
57         while (keep_munging_option && *++*argv != '\0') {
58             switch(**argv) {
59
60             case 'v':
61                     who_am_i(); /* identify myself */
62                     verbose = TRUE;
63                     break;
64
65             case 'N':
66                     nonstandardFlag = TRUE;
67                     acceptPrim = TRUE;
68                     break;
69
70             case '2':
71                     haskell1_2Flag = TRUE;
72                     break;
73
74             case 'S':
75                     ignoreSCC = FALSE;
76                     break;
77
78             case 'D':
79 #ifdef HSP_DEBUG
80                     { extern int yydebug;
81                       yydebug = 1;
82                     }
83 #endif
84                     break;
85
86             /* -Hn -- Use Hash Table, Size n (if given) */
87             case 'H':
88                     hashIds = TRUE;
89                     if(*(*argv+1)!= '\0')
90                       hash_table_size = atoi(*argv+1);
91                     break;
92             case 'E':
93                     etags = TRUE;
94                     break;
95             }
96         }
97         argc--, argv++;
98     }
99
100     if(argc >= 1 && freopen(argv[0], "r", stdin) == NULL) {
101             fprintf(stderr, "Cannot open %s.\n", argv[0]);
102             exit(1);
103     }
104
105     if(argc >= 2 && freopen(argv[1], "w", stdout) == NULL) {
106             fprintf(stderr, "Cannot open %s.\n", argv[1]);
107             exit(1);
108     }
109
110     if (verbose) {
111         fprintf(stderr,"Hash Table Contains %d entries\n",hash_table_size);
112         if(acceptPrim)
113           fprintf(stderr,"Allowing special syntax for Unboxed Values\n");
114     }
115 }
116
117 void
118 error(s)
119   char *s;
120 {
121         fprintf(stderr, "PARSER: Error %s\n", s);
122         exit(1);
123 }
124
125 static void
126 who_am_i(void)
127 {
128   fprintf(stderr,"Glasgow Haskell parser, version %s\n", PARSER_VERSION);
129 }
130
131 list
132 lconc(l1, l2)
133   list l1;
134   list l2;
135 {
136         list t;
137
138         if (tlist(l1) == lnil)
139                 return(l2);
140         for(t = l1; tlist(ltl(t)) != lnil; t = ltl(t))
141                 ;
142         ltl(t) = l2;
143         return(l1);
144 }
145
146 list
147 lapp(list l1, VOID_STAR l2)
148 {
149         list t;
150
151         if (tlist(l1) == lnil)
152                 return(mklcons(l2, mklnil()));
153         for(t = l1; tlist(ltl(t)) != lnil; t = ltl(t))
154                 ;
155         ltl(t) = mklcons(l2, mklnil());
156         return(l1);
157 }