2 次代码提交 592ccd454f ... 890548e796

作者 SHA1 备注 提交日期
  Matt Coles 890548e796 more progress 8 年之前
  Matt Coles f2d5f34754 progress 8 年之前
共有 9 个文件被更改,包括 1403 次插入1284 次删除
  1. 6 11
      cw/main.c
  2. 4 11
      cw/simple.c
  3. 12 0
      cw/tests/scope.c
  4. 5 2
      cw/tests/test.sh
  5. 1027 978
      tac/C.tab.c
  6. 61 47
      tac/C.tab.h
  7. 124 138
      tac/lex.yy.c
  8. 149 94
      tac/main.c
  9. 15 3
      tac/simple.c

+ 6 - 11
cw/main.c

200
   BIND* existing;
200
   BIND* existing;
201
   NODE* var_name = tree->left->left;
201
   NODE* var_name = tree->left->left;
202
   TOKEN* name_token = (TOKEN*) var_name;
202
   TOKEN* name_token = (TOKEN*) var_name;
203
-  if (debug) printf("assigning %s:\n", name_token->lexeme);
204
   BIND* node = recursive_interpret(tree->right, env_ptr);
203
   BIND* node = recursive_interpret(tree->right, env_ptr);
205
   if ((existing = find_name_in_env(name_token, env_ptr)) == NULL) {
204
   if ((existing = find_name_in_env(name_token, env_ptr)) == NULL) {
206
     if (env_ptr->bindings == NULL) {
205
     if (env_ptr->bindings == NULL) {
263
         if(debug)print_tree(var_bind->tree);
262
         if(debug)print_tree(var_bind->tree);
264
       }
263
       }
265
       return var_bind;
264
       return var_bind;
266
-      /* if (var_bind->tree->type == CONSTANT) { */
267
-      /*   return var_bind->tree; */
268
-      /* } else { */
269
-      /*   print_tree(var_bind->tree); */
270
-      /*   printf("I GOT A %d from %s\n", var_bind->tree->type, var_bind->name->lexeme); */
271
-      /*   printf("Maybe got a function?\n"); */
272
-      /*   return NULL; */
273
-      /* } */
274
     }
265
     }
275
   }
266
   }
276
   if (tree->type=='D') {
267
   if (tree->type=='D') {
290
       printf("Could not find binding for function with name %s\n", func_name->lexeme);
281
       printf("Could not find binding for function with name %s\n", func_name->lexeme);
291
       exit(1);
282
       exit(1);
292
     }
283
     }
284
+    if (func->tree->type != 'D') {
285
+      printf("Tried to call non-function %s as a function\n", func_name->lexeme);
286
+      exit(1);
287
+    }
293
     ENV* new_func_env = create_new_function_env(func->env);
288
     ENV* new_func_env = create_new_function_env(func->env);
294
     if (debug) printf("Calling function %s\n", func_name->lexeme);
289
     if (debug) printf("Calling function %s\n", func_name->lexeme);
295
     if (tree->right != NULL) {
290
     if (tree->right != NULL) {
439
   }
434
   }
440
   // print ref_main to make sure we really got it
435
   // print ref_main to make sure we really got it
441
   printf("Located %s, ready to run!\n", ref_main->name->lexeme);
436
   printf("Located %s, ready to run!\n", ref_main->name->lexeme);
442
-  recursive_interpret(ref_main->tree->right, ref_main->env);
443
-  /* printf("%d\n", ((TOKEN*)recursive_interpret(ref_main->tree->right, ref_main->env))->value); */
437
+  ENV* main_env = create_new_function_env(ref_main->env);
438
+  recursive_interpret(ref_main->tree->right, main_env);
444
   printf("%d\n", ((TOKEN*)ret_val->tree)->value);
439
   printf("%d\n", ((TOKEN*)ret_val->tree)->value);
445
 }
440
 }
446
 
441
 

+ 4 - 11
cw/simple.c

1
-int whammy(int a) {
2
-  return a + 5;
3
-}
4
-
5
-function twice(function f) {
6
-  int g(int x) { return f(f(x)); }
7
-  return g;
8
-}
9
-
10
 int main() {
1
 int main() {
11
-  x = twice(whammy);
2
+  int b = 4;
3
+
4
+  int a = b();
12
 
5
 
13
-  return x(2);
6
+  return x;
14
 }
7
 }
15
 
8
 

+ 12 - 0
cw/tests/scope.c

1
+/* ##answer: Could not find variable b */
2
+int whammy(int a) {
3
+  return a + b;
4
+}
5
+
6
+int main() {
7
+  int b = 4;
8
+  x = whammy(5);
9
+
10
+  return x;
11
+}
12
+

+ 5 - 2
cw/tests/test.sh

4
   date="gdate"
4
   date="gdate"
5
   readlink="greadlink"
5
   readlink="greadlink"
6
   wc="gwc"
6
   wc="gwc"
7
+  sed="gsed"
7
 else
8
 else
8
   date="date"
9
   date="date"
9
   readlink="readlink"
10
   readlink="readlink"
10
   wc="wc"
11
   wc="wc"
12
+  sed="sed"
11
 fi
13
 fi
12
 start=$($date +%s.%N)
14
 start=$($date +%s.%N)
13
 absdir=$($readlink -f $(dirname $0))
15
 absdir=$($readlink -f $(dirname $0))
23
 fmt=`echo "%-$num""s"`
25
 fmt=`echo "%-$num""s"`
24
 for i in $(ls $absdir | grep .\*.c | sort); do
26
 for i in $(ls $absdir | grep .\*.c | sort); do
25
   answer=`$absdir/../mycc < $absdir/$i | tail -n 1`
27
   answer=`$absdir/../mycc < $absdir/$i | tail -n 1`
26
-  exp_ans=`grep --color=none "##answer" $absdir/$i | sed 's/[^0-9]\+\([0-9]\+\).\+/\1/g'`
28
+  # exp_ans=`grep --color=none "##answer" $absdir/$i | $sed 's/[^0-9]\+\([0-9]\+\).\+/\1/g'`
29
+  exp_ans=`grep --color=none "##answer" $absdir/$i | $sed 's/\/\* ##answer:\s\+\([^\*]\+\)\b\s*\*\//\1/g'`
27
   printf $fmt "${i%.*}"
30
   printf $fmt "${i%.*}"
28
   echo -n " ... "
31
   echo -n " ... "
29
-  if [ "$answer" -eq "$exp_ans" ]; then
32
+  if [ "$answer" == "$exp_ans" ]; then
30
     echo -e "$pass"
33
     echo -e "$pass"
31
     passes=$((passes+1))
34
     passes=$((passes+1))
32
   else
35
   else

文件差异内容过多而无法显示
+ 1027 - 978
tac/C.tab.c


+ 61 - 47
tac/C.tab.h

1
-/* A Bison parser, made by GNU Bison 3.0.4.  */
1
+/* A Bison parser, made by GNU Bison 2.3.  */
2
 
2
 
3
-/* Bison interface for Yacc-like parsers in C
3
+/* Skeleton interface for Bison's Yacc-like parsers in C
4
 
4
 
5
-   Copyright (C) 1984, 1989-1990, 2000-2015 Free Software Foundation, Inc.
5
+   Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005, 2006
6
+   Free Software Foundation, Inc.
6
 
7
 
7
-   This program is free software: you can redistribute it and/or modify
8
+   This program is free software; you can redistribute it and/or modify
8
    it under the terms of the GNU General Public License as published by
9
    it under the terms of the GNU General Public License as published by
9
-   the Free Software Foundation, either version 3 of the License, or
10
-   (at your option) any later version.
10
+   the Free Software Foundation; either version 2, or (at your option)
11
+   any later version.
11
 
12
 
12
    This program is distributed in the hope that it will be useful,
13
    This program is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
    GNU General Public License for more details.
16
    GNU General Public License for more details.
16
 
17
 
17
    You should have received a copy of the GNU General Public License
18
    You should have received a copy of the GNU General Public License
18
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
+   along with this program; if not, write to the Free Software
20
+   Foundation, Inc., 51 Franklin Street, Fifth Floor,
21
+   Boston, MA 02110-1301, USA.  */
19
 
22
 
20
 /* As a special exception, you may create a larger work that contains
23
 /* As a special exception, you may create a larger work that contains
21
    part or all of the Bison parser skeleton and distribute that work
24
    part or all of the Bison parser skeleton and distribute that work
30
    This special exception was added by the Free Software Foundation in
33
    This special exception was added by the Free Software Foundation in
31
    version 2.2 of Bison.  */
34
    version 2.2 of Bison.  */
32
 
35
 
33
-#ifndef YY_YY_C_TAB_H_INCLUDED
34
-# define YY_YY_C_TAB_H_INCLUDED
35
-/* Debug traces.  */
36
-#ifndef YYDEBUG
37
-# define YYDEBUG 1
38
-#endif
39
-#if YYDEBUG
40
-extern int yydebug;
41
-#endif
42
-
43
-/* Token type.  */
36
+/* Tokens.  */
44
 #ifndef YYTOKENTYPE
37
 #ifndef YYTOKENTYPE
45
 # define YYTOKENTYPE
38
 # define YYTOKENTYPE
46
-  enum yytokentype
47
-  {
48
-    IDENTIFIER = 258,
49
-    CONSTANT = 259,
50
-    STRING_LITERAL = 260,
51
-    LE_OP = 261,
52
-    GE_OP = 262,
53
-    EQ_OP = 263,
54
-    NE_OP = 264,
55
-    EXTERN = 265,
56
-    AUTO = 266,
57
-    INT = 267,
58
-    VOID = 268,
59
-    FUNCTION = 269,
60
-    APPLY = 270,
61
-    LEAF = 271,
62
-    IF = 272,
63
-    ELSE = 273,
64
-    WHILE = 274,
65
-    CONTINUE = 275,
66
-    BREAK = 276,
67
-    RETURN = 277
68
-  };
39
+   /* Put the tokens into the symbol table, so that GDB and other debuggers
40
+      know about them.  */
41
+   enum yytokentype {
42
+     IDENTIFIER = 258,
43
+     CONSTANT = 259,
44
+     STRING_LITERAL = 260,
45
+     LE_OP = 261,
46
+     GE_OP = 262,
47
+     EQ_OP = 263,
48
+     NE_OP = 264,
49
+     EXTERN = 265,
50
+     AUTO = 266,
51
+     INT = 267,
52
+     VOID = 268,
53
+     FUNCTION = 269,
54
+     APPLY = 270,
55
+     LEAF = 271,
56
+     IF = 272,
57
+     ELSE = 273,
58
+     WHILE = 274,
59
+     CONTINUE = 275,
60
+     BREAK = 276,
61
+     RETURN = 277
62
+   };
69
 #endif
63
 #endif
64
+/* Tokens.  */
65
+#define IDENTIFIER 258
66
+#define CONSTANT 259
67
+#define STRING_LITERAL 260
68
+#define LE_OP 261
69
+#define GE_OP 262
70
+#define EQ_OP 263
71
+#define NE_OP 264
72
+#define EXTERN 265
73
+#define AUTO 266
74
+#define INT 267
75
+#define VOID 268
76
+#define FUNCTION 269
77
+#define APPLY 270
78
+#define LEAF 271
79
+#define IF 272
80
+#define ELSE 273
81
+#define WHILE 274
82
+#define CONTINUE 275
83
+#define BREAK 276
84
+#define RETURN 277
85
+
86
+
87
+
70
 
88
 
71
-/* Value type.  */
72
 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
89
 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
73
 typedef int YYSTYPE;
90
 typedef int YYSTYPE;
74
-# define YYSTYPE_IS_TRIVIAL 1
91
+# define yystype YYSTYPE /* obsolescent; will be withdrawn */
75
 # define YYSTYPE_IS_DECLARED 1
92
 # define YYSTYPE_IS_DECLARED 1
93
+# define YYSTYPE_IS_TRIVIAL 1
76
 #endif
94
 #endif
77
 
95
 
78
-
79
 extern YYSTYPE yylval;
96
 extern YYSTYPE yylval;
80
 
97
 
81
-int yyparse (void);
82
-
83
-#endif /* !YY_YY_C_TAB_H_INCLUDED  */

+ 124 - 138
tac/lex.yy.c

7
 
7
 
8
 #define FLEX_SCANNER
8
 #define FLEX_SCANNER
9
 #define YY_FLEX_MAJOR_VERSION 2
9
 #define YY_FLEX_MAJOR_VERSION 2
10
-#define YY_FLEX_MINOR_VERSION 6
11
-#define YY_FLEX_SUBMINOR_VERSION 1
10
+#define YY_FLEX_MINOR_VERSION 5
11
+#define YY_FLEX_SUBMINOR_VERSION 35
12
 #if YY_FLEX_SUBMINOR_VERSION > 0
12
 #if YY_FLEX_SUBMINOR_VERSION > 0
13
 #define FLEX_BETA
13
 #define FLEX_BETA
14
 #endif
14
 #endif
46
 typedef uint16_t flex_uint16_t;
46
 typedef uint16_t flex_uint16_t;
47
 typedef int32_t flex_int32_t;
47
 typedef int32_t flex_int32_t;
48
 typedef uint32_t flex_uint32_t;
48
 typedef uint32_t flex_uint32_t;
49
+typedef uint64_t flex_uint64_t;
49
 #else
50
 #else
50
 typedef signed char flex_int8_t;
51
 typedef signed char flex_int8_t;
51
 typedef short int flex_int16_t;
52
 typedef short int flex_int16_t;
53
 typedef unsigned char flex_uint8_t; 
54
 typedef unsigned char flex_uint8_t; 
54
 typedef unsigned short int flex_uint16_t;
55
 typedef unsigned short int flex_uint16_t;
55
 typedef unsigned int flex_uint32_t;
56
 typedef unsigned int flex_uint32_t;
57
+#endif /* ! C99 */
56
 
58
 
57
 /* Limits of integral types. */
59
 /* Limits of integral types. */
58
 #ifndef INT8_MIN
60
 #ifndef INT8_MIN
83
 #define UINT32_MAX             (4294967295U)
85
 #define UINT32_MAX             (4294967295U)
84
 #endif
86
 #endif
85
 
87
 
86
-#endif /* ! C99 */
87
-
88
 #endif /* ! FLEXINT_H */
88
 #endif /* ! FLEXINT_H */
89
 
89
 
90
-/* TODO: this is always defined, so inline it */
91
-#define yyconst const
90
+#ifdef __cplusplus
91
+
92
+/* The "const" storage-class-modifier is valid. */
93
+#define YY_USE_CONST
94
+
95
+#else	/* ! __cplusplus */
96
+
97
+/* C99 requires __STDC__ to be defined as 1. */
98
+#if defined (__STDC__)
99
+
100
+#define YY_USE_CONST
101
+
102
+#endif	/* defined (__STDC__) */
103
+#endif	/* ! __cplusplus */
92
 
104
 
93
-#if defined(__GNUC__) && __GNUC__ >= 3
94
-#define yynoreturn __attribute__((__noreturn__))
105
+#ifdef YY_USE_CONST
106
+#define yyconst const
95
 #else
107
 #else
96
-#define yynoreturn
108
+#define yyconst
97
 #endif
109
 #endif
98
 
110
 
99
 /* Returned upon end-of-file. */
111
 /* Returned upon end-of-file. */
129
 
141
 
130
 /* Size of default input buffer. */
142
 /* Size of default input buffer. */
131
 #ifndef YY_BUF_SIZE
143
 #ifndef YY_BUF_SIZE
132
-#ifdef __ia64__
133
-/* On IA-64, the buffer size is 16k, not 8k.
134
- * Moreover, YY_BUF_SIZE is 2*YY_READ_BUF_SIZE in the general case.
135
- * Ditto for the __ia64__ case accordingly.
136
- */
137
-#define YY_BUF_SIZE 32768
138
-#else
139
 #define YY_BUF_SIZE 16384
144
 #define YY_BUF_SIZE 16384
140
-#endif /* __ia64__ */
141
 #endif
145
 #endif
142
 
146
 
143
 /* The state buf must be large enough to hold one state per character in the main buffer.
147
 /* The state buf must be large enough to hold one state per character in the main buffer.
154
 typedef size_t yy_size_t;
158
 typedef size_t yy_size_t;
155
 #endif
159
 #endif
156
 
160
 
157
-extern int yyleng;
161
+extern yy_size_t yyleng;
158
 
162
 
159
 extern FILE *yyin, *yyout;
163
 extern FILE *yyin, *yyout;
160
 
164
 
163
 #define EOB_ACT_LAST_MATCH 2
167
 #define EOB_ACT_LAST_MATCH 2
164
 
168
 
165
     #define YY_LESS_LINENO(n)
169
     #define YY_LESS_LINENO(n)
166
-    #define YY_LINENO_REWIND_TO(ptr)
167
     
170
     
168
 /* Return all but the first "n" matched characters back to the input stream. */
171
 /* Return all but the first "n" matched characters back to the input stream. */
169
 #define yyless(n) \
172
 #define yyless(n) \
193
 	/* Size of input buffer in bytes, not including room for EOB
196
 	/* Size of input buffer in bytes, not including room for EOB
194
 	 * characters.
197
 	 * characters.
195
 	 */
198
 	 */
196
-	int yy_buf_size;
199
+	yy_size_t yy_buf_size;
197
 
200
 
198
 	/* Number of characters read into yy_ch_buf, not including EOB
201
 	/* Number of characters read into yy_ch_buf, not including EOB
199
 	 * characters.
202
 	 * characters.
200
 	 */
203
 	 */
201
-	int yy_n_chars;
204
+	yy_size_t yy_n_chars;
202
 
205
 
203
 	/* Whether we "own" the buffer - i.e., we know we created it,
206
 	/* Whether we "own" the buffer - i.e., we know we created it,
204
 	 * and can realloc() it to grow it, and should free() it to
207
 	 * and can realloc() it to grow it, and should free() it to
221
 
224
 
222
     int yy_bs_lineno; /**< The line count. */
225
     int yy_bs_lineno; /**< The line count. */
223
     int yy_bs_column; /**< The column count. */
226
     int yy_bs_column; /**< The column count. */
224
-
227
+    
225
 	/* Whether to try to fill the input buffer when we reach the
228
 	/* Whether to try to fill the input buffer when we reach the
226
 	 * end of it.
229
 	 * end of it.
227
 	 */
230
 	 */
249
 /* Stack of input buffers. */
252
 /* Stack of input buffers. */
250
 static size_t yy_buffer_stack_top = 0; /**< index of top of stack. */
253
 static size_t yy_buffer_stack_top = 0; /**< index of top of stack. */
251
 static size_t yy_buffer_stack_max = 0; /**< capacity of stack. */
254
 static size_t yy_buffer_stack_max = 0; /**< capacity of stack. */
252
-static YY_BUFFER_STATE * yy_buffer_stack = NULL; /**< Stack as an array. */
255
+static YY_BUFFER_STATE * yy_buffer_stack = 0; /**< Stack as an array. */
253
 
256
 
254
 /* We provide macros for accessing buffer states in case in the
257
 /* We provide macros for accessing buffer states in case in the
255
  * future we want to put the buffer states in a more general
258
  * future we want to put the buffer states in a more general
268
 
271
 
269
 /* yy_hold_char holds the character lost when yytext is formed. */
272
 /* yy_hold_char holds the character lost when yytext is formed. */
270
 static char yy_hold_char;
273
 static char yy_hold_char;
271
-static int yy_n_chars;		/* number of characters read into yy_ch_buf */
272
-int yyleng;
274
+static yy_size_t yy_n_chars;		/* number of characters read into yy_ch_buf */
275
+yy_size_t yyleng;
273
 
276
 
274
 /* Points to current character in buffer. */
277
 /* Points to current character in buffer. */
275
-static char *yy_c_buf_p = NULL;
278
+static char *yy_c_buf_p = (char *) 0;
276
 static int yy_init = 0;		/* whether we need to initialize */
279
 static int yy_init = 0;		/* whether we need to initialize */
277
 static int yy_start = 0;	/* start state number */
280
 static int yy_start = 0;	/* start state number */
278
 
281
 
297
 
300
 
298
 YY_BUFFER_STATE yy_scan_buffer (char *base,yy_size_t size  );
301
 YY_BUFFER_STATE yy_scan_buffer (char *base,yy_size_t size  );
299
 YY_BUFFER_STATE yy_scan_string (yyconst char *yy_str  );
302
 YY_BUFFER_STATE yy_scan_string (yyconst char *yy_str  );
300
-YY_BUFFER_STATE yy_scan_bytes (yyconst char *bytes,int len  );
303
+YY_BUFFER_STATE yy_scan_bytes (yyconst char *bytes,yy_size_t len  );
301
 
304
 
302
 void *yyalloc (yy_size_t  );
305
 void *yyalloc (yy_size_t  );
303
 void *yyrealloc (void *,yy_size_t  );
306
 void *yyrealloc (void *,yy_size_t  );
331
 
334
 
332
 typedef unsigned char YY_CHAR;
335
 typedef unsigned char YY_CHAR;
333
 
336
 
334
-FILE *yyin = NULL, *yyout = NULL;
337
+FILE *yyin = (FILE *) 0, *yyout = (FILE *) 0;
335
 
338
 
336
 typedef int yy_state_type;
339
 typedef int yy_state_type;
337
 
340
 
340
 int yylineno = 1;
343
 int yylineno = 1;
341
 
344
 
342
 extern char *yytext;
345
 extern char *yytext;
343
-#ifdef yytext_ptr
344
-#undef yytext_ptr
345
-#endif
346
 #define yytext_ptr yytext
346
 #define yytext_ptr yytext
347
 
347
 
348
 static yy_state_type yy_get_previous_state (void );
348
 static yy_state_type yy_get_previous_state (void );
349
 static yy_state_type yy_try_NUL_trans (yy_state_type current_state  );
349
 static yy_state_type yy_try_NUL_trans (yy_state_type current_state  );
350
 static int yy_get_next_buffer (void );
350
 static int yy_get_next_buffer (void );
351
-static void yynoreturn yy_fatal_error (yyconst char* msg  );
351
+static void yy_fatal_error (yyconst char msg[]  );
352
 
352
 
353
 /* Done after the current pattern has been matched and before the
353
 /* Done after the current pattern has been matched and before the
354
  * corresponding action - sets up yytext.
354
  * corresponding action - sets up yytext.
355
  */
355
  */
356
 #define YY_DO_BEFORE_ACTION \
356
 #define YY_DO_BEFORE_ACTION \
357
 	(yytext_ptr) = yy_bp; \
357
 	(yytext_ptr) = yy_bp; \
358
-	yyleng = (int) (yy_cp - yy_bp); \
358
+	yyleng = (yy_size_t) (yy_cp - yy_bp); \
359
 	(yy_hold_char) = *yy_cp; \
359
 	(yy_hold_char) = *yy_cp; \
360
 	*yy_cp = '\0'; \
360
 	*yy_cp = '\0'; \
361
 	(yy_c_buf_p) = yy_cp;
361
 	(yy_c_buf_p) = yy_cp;
384
 
384
 
385
     } ;
385
     } ;
386
 
386
 
387
-static yyconst YY_CHAR yy_ec[256] =
387
+static yyconst flex_int32_t yy_ec[256] =
388
     {   0,
388
     {   0,
389
         1,    1,    1,    1,    1,    1,    1,    1,    2,    3,
389
         1,    1,    1,    1,    1,    1,    1,    1,    2,    3,
390
         2,    2,    1,    1,    1,    1,    1,    1,    1,    1,
390
         2,    2,    1,    1,    1,    1,    1,    1,    1,    1,
416
         1,    1,    1,    1,    1
416
         1,    1,    1,    1,    1
417
     } ;
417
     } ;
418
 
418
 
419
-static yyconst YY_CHAR yy_meta[46] =
419
+static yyconst flex_int32_t yy_meta[46] =
420
     {   0,
420
     {   0,
421
         1,    1,    1,    1,    1,    1,    2,    1,    1,    1,
421
         1,    1,    1,    1,    1,    1,    2,    1,    1,    1,
422
         1,    1,    1,    1,    3,    1,    1,    1,    1,    1,
422
         1,    1,    1,    1,    3,    1,    1,    1,    1,    1,
425
         3,    3,    3,    1,    1
425
         3,    3,    3,    1,    1
426
     } ;
426
     } ;
427
 
427
 
428
-static yyconst flex_uint16_t yy_base[101] =
428
+static yyconst flex_int16_t yy_base[101] =
429
     {   0,
429
     {   0,
430
         0,    0,  130,  131,  131,  131,  110,   41,  131,  104,
430
         0,    0,  130,  131,  131,  131,  110,   41,  131,  104,
431
       131,  131,  131,  131,  131,  131,  117,   32,  131,  131,
431
       131,  131,  131,  131,  131,  131,  117,   32,  131,  131,
455
 
455
 
456
     } ;
456
     } ;
457
 
457
 
458
-static yyconst flex_uint16_t yy_nxt[177] =
458
+static yyconst flex_int16_t yy_nxt[177] =
459
     {   0,
459
     {   0,
460
         4,    5,    6,    7,    8,    9,   10,   11,   12,   13,
460
         4,    5,    6,    7,    8,    9,   10,   11,   12,   13,
461
        14,   15,   16,   17,   18,   19,   20,   21,   22,   23,
461
        14,   15,   16,   17,   18,   19,   20,   21,   22,   23,
567
 
567
 
568
 FILE *yyget_in (void );
568
 FILE *yyget_in (void );
569
 
569
 
570
-void yyset_in  (FILE * _in_str  );
570
+void yyset_in  (FILE * in_str  );
571
 
571
 
572
 FILE *yyget_out (void );
572
 FILE *yyget_out (void );
573
 
573
 
574
-void yyset_out  (FILE * _out_str  );
574
+void yyset_out  (FILE * out_str  );
575
 
575
 
576
-			int yyget_leng (void );
576
+yy_size_t yyget_leng (void );
577
 
577
 
578
 char *yyget_text (void );
578
 char *yyget_text (void );
579
 
579
 
580
 int yyget_lineno (void );
580
 int yyget_lineno (void );
581
 
581
 
582
-void yyset_lineno (int _line_number  );
582
+void yyset_lineno (int line_number  );
583
 
583
 
584
 /* Macros after this point can all be overridden by user definitions in
584
 /* Macros after this point can all be overridden by user definitions in
585
  * section 1.
585
  * section 1.
593
 #endif
593
 #endif
594
 #endif
594
 #endif
595
 
595
 
596
-#ifndef YY_NO_UNPUT
597
-    
598
     static void yyunput (int c,char *buf_ptr  );
596
     static void yyunput (int c,char *buf_ptr  );
599
     
597
     
600
-#endif
601
-
602
 #ifndef yytext_ptr
598
 #ifndef yytext_ptr
603
 static void yy_flex_strncpy (char *,yyconst char *,int );
599
 static void yy_flex_strncpy (char *,yyconst char *,int );
604
 #endif
600
 #endif
619
 
615
 
620
 /* Amount of stuff to slurp up with each read. */
616
 /* Amount of stuff to slurp up with each read. */
621
 #ifndef YY_READ_BUF_SIZE
617
 #ifndef YY_READ_BUF_SIZE
622
-#ifdef __ia64__
623
-/* On IA-64, the buffer size is 16k, not 8k */
624
-#define YY_READ_BUF_SIZE 16384
625
-#else
626
 #define YY_READ_BUF_SIZE 8192
618
 #define YY_READ_BUF_SIZE 8192
627
-#endif /* __ia64__ */
628
 #endif
619
 #endif
629
 
620
 
630
 /* Copy whatever the last rule matched to the standard output. */
621
 /* Copy whatever the last rule matched to the standard output. */
632
 /* This used to be an fputs(), but since the string might contain NUL's,
623
 /* This used to be an fputs(), but since the string might contain NUL's,
633
  * we now use fwrite().
624
  * we now use fwrite().
634
  */
625
  */
635
-#define ECHO do { if (fwrite( yytext, (size_t) yyleng, 1, yyout )) {} } while (0)
626
+#define ECHO fwrite( yytext, yyleng, 1, yyout )
636
 #endif
627
 #endif
637
 
628
 
638
 /* Gets input and stuffs it into "buf".  number of characters read, or YY_NULL,
629
 /* Gets input and stuffs it into "buf".  number of characters read, or YY_NULL,
643
 	if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \
634
 	if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \
644
 		{ \
635
 		{ \
645
 		int c = '*'; \
636
 		int c = '*'; \
646
-		size_t n; \
637
+		yy_size_t n; \
647
 		for ( n = 0; n < max_size && \
638
 		for ( n = 0; n < max_size && \
648
 			     (c = getc( yyin )) != EOF && c != '\n'; ++n ) \
639
 			     (c = getc( yyin )) != EOF && c != '\n'; ++n ) \
649
 			buf[n] = (char) c; \
640
 			buf[n] = (char) c; \
656
 	else \
647
 	else \
657
 		{ \
648
 		{ \
658
 		errno=0; \
649
 		errno=0; \
659
-		while ( (result = (int) fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) \
650
+		while ( (result = fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) \
660
 			{ \
651
 			{ \
661
 			if( errno != EINTR) \
652
 			if( errno != EINTR) \
662
 				{ \
653
 				{ \
711
 
702
 
712
 /* Code executed at the end of each rule. */
703
 /* Code executed at the end of each rule. */
713
 #ifndef YY_BREAK
704
 #ifndef YY_BREAK
714
-#define YY_BREAK /*LINTED*/break;
705
+#define YY_BREAK break;
715
 #endif
706
 #endif
716
 
707
 
717
 #define YY_RULE_SETUP \
708
 #define YY_RULE_SETUP \
721
  */
712
  */
722
 YY_DECL
713
 YY_DECL
723
 {
714
 {
724
-	yy_state_type yy_current_state;
725
-	char *yy_cp, *yy_bp;
726
-	int yy_act;
715
+	register yy_state_type yy_current_state;
716
+	register char *yy_cp, *yy_bp;
717
+	register int yy_act;
727
     
718
     
719
+#line 28 "C.flex"
720
+
721
+#line 722 "lex.yy.c"
722
+
728
 	if ( !(yy_init) )
723
 	if ( !(yy_init) )
729
 		{
724
 		{
730
 		(yy_init) = 1;
725
 		(yy_init) = 1;
751
 		yy_load_buffer_state( );
746
 		yy_load_buffer_state( );
752
 		}
747
 		}
753
 
748
 
754
-	{
755
-#line 28 "C.flex"
756
-
757
-#line 758 "lex.yy.c"
758
-
759
-	while ( /*CONSTCOND*/1 )		/* loops until end-of-file is reached */
749
+	while ( 1 )		/* loops until end-of-file is reached */
760
 		{
750
 		{
761
 		yy_cp = (yy_c_buf_p);
751
 		yy_cp = (yy_c_buf_p);
762
 
752
 
772
 yy_match:
762
 yy_match:
773
 		do
763
 		do
774
 			{
764
 			{
775
-			YY_CHAR yy_c = yy_ec[YY_SC_TO_UI(*yy_cp)] ;
765
+			register YY_CHAR yy_c = yy_ec[YY_SC_TO_UI(*yy_cp)];
776
 			if ( yy_accept[yy_current_state] )
766
 			if ( yy_accept[yy_current_state] )
777
 				{
767
 				{
778
 				(yy_last_accepting_state) = yy_current_state;
768
 				(yy_last_accepting_state) = yy_current_state;
784
 				if ( yy_current_state >= 98 )
774
 				if ( yy_current_state >= 98 )
785
 					yy_c = yy_meta[(unsigned int) yy_c];
775
 					yy_c = yy_meta[(unsigned int) yy_c];
786
 				}
776
 				}
787
-			yy_current_state = yy_nxt[yy_base[yy_current_state] + (flex_int16_t) yy_c];
777
+			yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];
788
 			++yy_cp;
778
 			++yy_cp;
789
 			}
779
 			}
790
 		while ( yy_base[yy_current_state] != 131 );
780
 		while ( yy_base[yy_current_state] != 131 );
1011
 #line 76 "C.flex"
1001
 #line 76 "C.flex"
1012
 ECHO;
1002
 ECHO;
1013
 	YY_BREAK
1003
 	YY_BREAK
1014
-#line 1015 "lex.yy.c"
1004
+#line 1005 "lex.yy.c"
1015
 case YY_STATE_EOF(INITIAL):
1005
 case YY_STATE_EOF(INITIAL):
1016
 	yyterminate();
1006
 	yyterminate();
1017
 
1007
 
1142
 			"fatal flex scanner internal error--no action found" );
1132
 			"fatal flex scanner internal error--no action found" );
1143
 	} /* end of action switch */
1133
 	} /* end of action switch */
1144
 		} /* end of scanning one token */
1134
 		} /* end of scanning one token */
1145
-	} /* end of user's declarations */
1146
 } /* end of yylex */
1135
 } /* end of yylex */
1147
 
1136
 
1148
 /* yy_get_next_buffer - try to read in a new buffer
1137
 /* yy_get_next_buffer - try to read in a new buffer
1154
  */
1143
  */
1155
 static int yy_get_next_buffer (void)
1144
 static int yy_get_next_buffer (void)
1156
 {
1145
 {
1157
-    	char *dest = YY_CURRENT_BUFFER_LVALUE->yy_ch_buf;
1158
-	char *source = (yytext_ptr);
1159
-	int number_to_move, i;
1146
+    	register char *dest = YY_CURRENT_BUFFER_LVALUE->yy_ch_buf;
1147
+	register char *source = (yytext_ptr);
1148
+	register int number_to_move, i;
1160
 	int ret_val;
1149
 	int ret_val;
1161
 
1150
 
1162
 	if ( (yy_c_buf_p) > &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] )
1151
 	if ( (yy_c_buf_p) > &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] )
1185
 	/* Try to read more data. */
1174
 	/* Try to read more data. */
1186
 
1175
 
1187
 	/* First move last chars to start of buffer. */
1176
 	/* First move last chars to start of buffer. */
1188
-	number_to_move = (int) ((yy_c_buf_p) - (yytext_ptr) - 1);
1177
+	number_to_move = (int) ((yy_c_buf_p) - (yytext_ptr)) - 1;
1189
 
1178
 
1190
 	for ( i = 0; i < number_to_move; ++i )
1179
 	for ( i = 0; i < number_to_move; ++i )
1191
 		*(dest++) = *(source++);
1180
 		*(dest++) = *(source++);
1198
 
1187
 
1199
 	else
1188
 	else
1200
 		{
1189
 		{
1201
-			int num_to_read =
1190
+			yy_size_t num_to_read =
1202
 			YY_CURRENT_BUFFER_LVALUE->yy_buf_size - number_to_move - 1;
1191
 			YY_CURRENT_BUFFER_LVALUE->yy_buf_size - number_to_move - 1;
1203
 
1192
 
1204
 		while ( num_to_read <= 0 )
1193
 		while ( num_to_read <= 0 )
1205
 			{ /* Not enough room in the buffer - grow it. */
1194
 			{ /* Not enough room in the buffer - grow it. */
1206
 
1195
 
1207
 			/* just a shorter name for the current buffer */
1196
 			/* just a shorter name for the current buffer */
1208
-			YY_BUFFER_STATE b = YY_CURRENT_BUFFER_LVALUE;
1197
+			YY_BUFFER_STATE b = YY_CURRENT_BUFFER;
1209
 
1198
 
1210
 			int yy_c_buf_p_offset =
1199
 			int yy_c_buf_p_offset =
1211
 				(int) ((yy_c_buf_p) - b->yy_ch_buf);
1200
 				(int) ((yy_c_buf_p) - b->yy_ch_buf);
1212
 
1201
 
1213
 			if ( b->yy_is_our_buffer )
1202
 			if ( b->yy_is_our_buffer )
1214
 				{
1203
 				{
1215
-				int new_size = b->yy_buf_size * 2;
1204
+				yy_size_t new_size = b->yy_buf_size * 2;
1216
 
1205
 
1217
 				if ( new_size <= 0 )
1206
 				if ( new_size <= 0 )
1218
 					b->yy_buf_size += b->yy_buf_size / 8;
1207
 					b->yy_buf_size += b->yy_buf_size / 8;
1225
 				}
1214
 				}
1226
 			else
1215
 			else
1227
 				/* Can't grow it, we don't own it. */
1216
 				/* Can't grow it, we don't own it. */
1228
-				b->yy_ch_buf = NULL;
1217
+				b->yy_ch_buf = 0;
1229
 
1218
 
1230
 			if ( ! b->yy_ch_buf )
1219
 			if ( ! b->yy_ch_buf )
1231
 				YY_FATAL_ERROR(
1220
 				YY_FATAL_ERROR(
1267
 	else
1256
 	else
1268
 		ret_val = EOB_ACT_CONTINUE_SCAN;
1257
 		ret_val = EOB_ACT_CONTINUE_SCAN;
1269
 
1258
 
1270
-	if (((yy_n_chars) + number_to_move) > YY_CURRENT_BUFFER_LVALUE->yy_buf_size) {
1259
+	if ((yy_size_t) ((yy_n_chars) + number_to_move) > YY_CURRENT_BUFFER_LVALUE->yy_buf_size) {
1271
 		/* Extend the array by 50%, plus the number we really need. */
1260
 		/* Extend the array by 50%, plus the number we really need. */
1272
-		int new_size = (yy_n_chars) + number_to_move + ((yy_n_chars) >> 1);
1261
+		yy_size_t new_size = (yy_n_chars) + number_to_move + ((yy_n_chars) >> 1);
1273
 		YY_CURRENT_BUFFER_LVALUE->yy_ch_buf = (char *) yyrealloc((void *) YY_CURRENT_BUFFER_LVALUE->yy_ch_buf,new_size  );
1262
 		YY_CURRENT_BUFFER_LVALUE->yy_ch_buf = (char *) yyrealloc((void *) YY_CURRENT_BUFFER_LVALUE->yy_ch_buf,new_size  );
1274
 		if ( ! YY_CURRENT_BUFFER_LVALUE->yy_ch_buf )
1263
 		if ( ! YY_CURRENT_BUFFER_LVALUE->yy_ch_buf )
1275
 			YY_FATAL_ERROR( "out of dynamic memory in yy_get_next_buffer()" );
1264
 			YY_FATAL_ERROR( "out of dynamic memory in yy_get_next_buffer()" );
1288
 
1277
 
1289
     static yy_state_type yy_get_previous_state (void)
1278
     static yy_state_type yy_get_previous_state (void)
1290
 {
1279
 {
1291
-	yy_state_type yy_current_state;
1292
-	char *yy_cp;
1280
+	register yy_state_type yy_current_state;
1281
+	register char *yy_cp;
1293
     
1282
     
1294
 	yy_current_state = (yy_start);
1283
 	yy_current_state = (yy_start);
1295
 
1284
 
1296
 	for ( yy_cp = (yytext_ptr) + YY_MORE_ADJ; yy_cp < (yy_c_buf_p); ++yy_cp )
1285
 	for ( yy_cp = (yytext_ptr) + YY_MORE_ADJ; yy_cp < (yy_c_buf_p); ++yy_cp )
1297
 		{
1286
 		{
1298
-		YY_CHAR yy_c = (*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : 1);
1287
+		register YY_CHAR yy_c = (*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : 1);
1299
 		if ( yy_accept[yy_current_state] )
1288
 		if ( yy_accept[yy_current_state] )
1300
 			{
1289
 			{
1301
 			(yy_last_accepting_state) = yy_current_state;
1290
 			(yy_last_accepting_state) = yy_current_state;
1307
 			if ( yy_current_state >= 98 )
1296
 			if ( yy_current_state >= 98 )
1308
 				yy_c = yy_meta[(unsigned int) yy_c];
1297
 				yy_c = yy_meta[(unsigned int) yy_c];
1309
 			}
1298
 			}
1310
-		yy_current_state = yy_nxt[yy_base[yy_current_state] + (flex_int16_t) yy_c];
1299
+		yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];
1311
 		}
1300
 		}
1312
 
1301
 
1313
 	return yy_current_state;
1302
 	return yy_current_state;
1320
  */
1309
  */
1321
     static yy_state_type yy_try_NUL_trans  (yy_state_type yy_current_state )
1310
     static yy_state_type yy_try_NUL_trans  (yy_state_type yy_current_state )
1322
 {
1311
 {
1323
-	int yy_is_jam;
1324
-    	char *yy_cp = (yy_c_buf_p);
1312
+	register int yy_is_jam;
1313
+    	register char *yy_cp = (yy_c_buf_p);
1325
 
1314
 
1326
-	YY_CHAR yy_c = 1;
1315
+	register YY_CHAR yy_c = 1;
1327
 	if ( yy_accept[yy_current_state] )
1316
 	if ( yy_accept[yy_current_state] )
1328
 		{
1317
 		{
1329
 		(yy_last_accepting_state) = yy_current_state;
1318
 		(yy_last_accepting_state) = yy_current_state;
1335
 		if ( yy_current_state >= 98 )
1324
 		if ( yy_current_state >= 98 )
1336
 			yy_c = yy_meta[(unsigned int) yy_c];
1325
 			yy_c = yy_meta[(unsigned int) yy_c];
1337
 		}
1326
 		}
1338
-	yy_current_state = yy_nxt[yy_base[yy_current_state] + (flex_int16_t) yy_c];
1327
+	yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];
1339
 	yy_is_jam = (yy_current_state == 97);
1328
 	yy_is_jam = (yy_current_state == 97);
1340
 
1329
 
1341
-		return yy_is_jam ? 0 : yy_current_state;
1330
+	return yy_is_jam ? 0 : yy_current_state;
1342
 }
1331
 }
1343
 
1332
 
1344
-#ifndef YY_NO_UNPUT
1345
-
1346
-    static void yyunput (int c, char * yy_bp )
1333
+    static void yyunput (int c, register char * yy_bp )
1347
 {
1334
 {
1348
-	char *yy_cp;
1335
+	register char *yy_cp;
1349
     
1336
     
1350
     yy_cp = (yy_c_buf_p);
1337
     yy_cp = (yy_c_buf_p);
1351
 
1338
 
1355
 	if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 )
1342
 	if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 )
1356
 		{ /* need to shift things up to make room */
1343
 		{ /* need to shift things up to make room */
1357
 		/* +2 for EOB chars. */
1344
 		/* +2 for EOB chars. */
1358
-		int number_to_move = (yy_n_chars) + 2;
1359
-		char *dest = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[
1345
+		register yy_size_t number_to_move = (yy_n_chars) + 2;
1346
+		register char *dest = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[
1360
 					YY_CURRENT_BUFFER_LVALUE->yy_buf_size + 2];
1347
 					YY_CURRENT_BUFFER_LVALUE->yy_buf_size + 2];
1361
-		char *source =
1348
+		register char *source =
1362
 				&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move];
1349
 				&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move];
1363
 
1350
 
1364
 		while ( source > YY_CURRENT_BUFFER_LVALUE->yy_ch_buf )
1351
 		while ( source > YY_CURRENT_BUFFER_LVALUE->yy_ch_buf )
1367
 		yy_cp += (int) (dest - source);
1354
 		yy_cp += (int) (dest - source);
1368
 		yy_bp += (int) (dest - source);
1355
 		yy_bp += (int) (dest - source);
1369
 		YY_CURRENT_BUFFER_LVALUE->yy_n_chars =
1356
 		YY_CURRENT_BUFFER_LVALUE->yy_n_chars =
1370
-			(yy_n_chars) = (int) YY_CURRENT_BUFFER_LVALUE->yy_buf_size;
1357
+			(yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_buf_size;
1371
 
1358
 
1372
 		if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 )
1359
 		if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 )
1373
 			YY_FATAL_ERROR( "flex scanner push-back overflow" );
1360
 			YY_FATAL_ERROR( "flex scanner push-back overflow" );
1380
 	(yy_c_buf_p) = yy_cp;
1367
 	(yy_c_buf_p) = yy_cp;
1381
 }
1368
 }
1382
 
1369
 
1383
-#endif
1384
-
1385
 #ifndef YY_NO_INPUT
1370
 #ifndef YY_NO_INPUT
1386
 #ifdef __cplusplus
1371
 #ifdef __cplusplus
1387
     static int yyinput (void)
1372
     static int yyinput (void)
1406
 
1391
 
1407
 		else
1392
 		else
1408
 			{ /* need more input */
1393
 			{ /* need more input */
1409
-			int offset = (yy_c_buf_p) - (yytext_ptr);
1394
+			yy_size_t offset = (yy_c_buf_p) - (yytext_ptr);
1410
 			++(yy_c_buf_p);
1395
 			++(yy_c_buf_p);
1411
 
1396
 
1412
 			switch ( yy_get_next_buffer(  ) )
1397
 			switch ( yy_get_next_buffer(  ) )
1531
 	if ( ! b )
1516
 	if ( ! b )
1532
 		YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
1517
 		YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
1533
 
1518
 
1534
-	b->yy_buf_size = (yy_size_t)size;
1519
+	b->yy_buf_size = size;
1535
 
1520
 
1536
 	/* yy_ch_buf has to be 2 characters longer than the size given because
1521
 	/* yy_ch_buf has to be 2 characters longer than the size given because
1537
 	 * we need to put in 2 end-of-buffer characters.
1522
 	 * we need to put in 2 end-of-buffer characters.
1566
 	yyfree((void *) b  );
1551
 	yyfree((void *) b  );
1567
 }
1552
 }
1568
 
1553
 
1554
+#ifndef __cplusplus
1555
+extern int isatty (int );
1556
+#endif /* __cplusplus */
1557
+    
1569
 /* Initializes or reinitializes a buffer.
1558
 /* Initializes or reinitializes a buffer.
1570
  * This function is sometimes called more than once on the same buffer,
1559
  * This function is sometimes called more than once on the same buffer,
1571
  * such as during a yyrestart() or at EOF.
1560
  * such as during a yyrestart() or at EOF.
1678
  */
1667
  */
1679
 static void yyensure_buffer_stack (void)
1668
 static void yyensure_buffer_stack (void)
1680
 {
1669
 {
1681
-	int num_to_alloc;
1670
+	yy_size_t num_to_alloc;
1682
     
1671
     
1683
 	if (!(yy_buffer_stack)) {
1672
 	if (!(yy_buffer_stack)) {
1684
 
1673
 
1686
 		 * scanner will even need a stack. We use 2 instead of 1 to avoid an
1675
 		 * scanner will even need a stack. We use 2 instead of 1 to avoid an
1687
 		 * immediate realloc on the next call.
1676
 		 * immediate realloc on the next call.
1688
          */
1677
          */
1689
-      num_to_alloc = 1; /* After all that talk, this was set to 1 anyways... */
1678
+		num_to_alloc = 1;
1690
 		(yy_buffer_stack) = (struct yy_buffer_state**)yyalloc
1679
 		(yy_buffer_stack) = (struct yy_buffer_state**)yyalloc
1691
 								(num_to_alloc * sizeof(struct yy_buffer_state*)
1680
 								(num_to_alloc * sizeof(struct yy_buffer_state*)
1692
 								);
1681
 								);
1693
 		if ( ! (yy_buffer_stack) )
1682
 		if ( ! (yy_buffer_stack) )
1694
 			YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" );
1683
 			YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" );
1695
-
1684
+								  
1696
 		memset((yy_buffer_stack), 0, num_to_alloc * sizeof(struct yy_buffer_state*));
1685
 		memset((yy_buffer_stack), 0, num_to_alloc * sizeof(struct yy_buffer_state*));
1697
-
1686
+				
1698
 		(yy_buffer_stack_max) = num_to_alloc;
1687
 		(yy_buffer_stack_max) = num_to_alloc;
1699
 		(yy_buffer_stack_top) = 0;
1688
 		(yy_buffer_stack_top) = 0;
1700
 		return;
1689
 		return;
1703
 	if ((yy_buffer_stack_top) >= ((yy_buffer_stack_max)) - 1){
1692
 	if ((yy_buffer_stack_top) >= ((yy_buffer_stack_max)) - 1){
1704
 
1693
 
1705
 		/* Increase the buffer to prepare for a possible push. */
1694
 		/* Increase the buffer to prepare for a possible push. */
1706
-		yy_size_t grow_size = 8 /* arbitrary grow size */;
1695
+		int grow_size = 8 /* arbitrary grow size */;
1707
 
1696
 
1708
 		num_to_alloc = (yy_buffer_stack_max) + grow_size;
1697
 		num_to_alloc = (yy_buffer_stack_max) + grow_size;
1709
 		(yy_buffer_stack) = (struct yy_buffer_state**)yyrealloc
1698
 		(yy_buffer_stack) = (struct yy_buffer_state**)yyrealloc
1723
  * @param base the character buffer
1712
  * @param base the character buffer
1724
  * @param size the size in bytes of the character buffer
1713
  * @param size the size in bytes of the character buffer
1725
  * 
1714
  * 
1726
- * @return the newly allocated buffer state object.
1715
+ * @return the newly allocated buffer state object. 
1727
  */
1716
  */
1728
 YY_BUFFER_STATE yy_scan_buffer  (char * base, yy_size_t  size )
1717
 YY_BUFFER_STATE yy_scan_buffer  (char * base, yy_size_t  size )
1729
 {
1718
 {
1733
 	     base[size-2] != YY_END_OF_BUFFER_CHAR ||
1722
 	     base[size-2] != YY_END_OF_BUFFER_CHAR ||
1734
 	     base[size-1] != YY_END_OF_BUFFER_CHAR )
1723
 	     base[size-1] != YY_END_OF_BUFFER_CHAR )
1735
 		/* They forgot to leave room for the EOB's. */
1724
 		/* They forgot to leave room for the EOB's. */
1736
-		return NULL;
1725
+		return 0;
1737
 
1726
 
1738
 	b = (YY_BUFFER_STATE) yyalloc(sizeof( struct yy_buffer_state )  );
1727
 	b = (YY_BUFFER_STATE) yyalloc(sizeof( struct yy_buffer_state )  );
1739
 	if ( ! b )
1728
 	if ( ! b )
1742
 	b->yy_buf_size = size - 2;	/* "- 2" to take care of EOB's */
1731
 	b->yy_buf_size = size - 2;	/* "- 2" to take care of EOB's */
1743
 	b->yy_buf_pos = b->yy_ch_buf = base;
1732
 	b->yy_buf_pos = b->yy_ch_buf = base;
1744
 	b->yy_is_our_buffer = 0;
1733
 	b->yy_is_our_buffer = 0;
1745
-	b->yy_input_file = NULL;
1734
+	b->yy_input_file = 0;
1746
 	b->yy_n_chars = b->yy_buf_size;
1735
 	b->yy_n_chars = b->yy_buf_size;
1747
 	b->yy_is_interactive = 0;
1736
 	b->yy_is_interactive = 0;
1748
 	b->yy_at_bol = 1;
1737
 	b->yy_at_bol = 1;
1765
 YY_BUFFER_STATE yy_scan_string (yyconst char * yystr )
1754
 YY_BUFFER_STATE yy_scan_string (yyconst char * yystr )
1766
 {
1755
 {
1767
     
1756
     
1768
-	return yy_scan_bytes(yystr,(int) strlen(yystr) );
1757
+	return yy_scan_bytes(yystr,strlen(yystr) );
1769
 }
1758
 }
1770
 
1759
 
1771
 /** Setup the input buffer state to scan the given bytes. The next call to yylex() will
1760
 /** Setup the input buffer state to scan the given bytes. The next call to yylex() will
1772
  * scan from a @e copy of @a bytes.
1761
  * scan from a @e copy of @a bytes.
1773
- * @param yybytes the byte buffer to scan
1774
- * @param _yybytes_len the number of bytes in the buffer pointed to by @a bytes.
1762
+ * @param bytes the byte buffer to scan
1763
+ * @param len the number of bytes in the buffer pointed to by @a bytes.
1775
  * 
1764
  * 
1776
  * @return the newly allocated buffer state object.
1765
  * @return the newly allocated buffer state object.
1777
  */
1766
  */
1778
-YY_BUFFER_STATE yy_scan_bytes  (yyconst char * yybytes, int  _yybytes_len )
1767
+YY_BUFFER_STATE yy_scan_bytes  (yyconst char * yybytes, yy_size_t  _yybytes_len )
1779
 {
1768
 {
1780
 	YY_BUFFER_STATE b;
1769
 	YY_BUFFER_STATE b;
1781
 	char *buf;
1770
 	char *buf;
1782
-	yy_size_t n;
1783
-	int i;
1771
+	yy_size_t n, i;
1784
     
1772
     
1785
 	/* Get memory for full buffer, including space for trailing EOB's. */
1773
 	/* Get memory for full buffer, including space for trailing EOB's. */
1786
-	n = (yy_size_t) (_yybytes_len + 2);
1774
+	n = _yybytes_len + 2;
1787
 	buf = (char *) yyalloc(n  );
1775
 	buf = (char *) yyalloc(n  );
1788
 	if ( ! buf )
1776
 	if ( ! buf )
1789
 		YY_FATAL_ERROR( "out of dynamic memory in yy_scan_bytes()" );
1777
 		YY_FATAL_ERROR( "out of dynamic memory in yy_scan_bytes()" );
1809
 #define YY_EXIT_FAILURE 2
1797
 #define YY_EXIT_FAILURE 2
1810
 #endif
1798
 #endif
1811
 
1799
 
1812
-static void yynoreturn yy_fatal_error (yyconst char* msg )
1800
+static void yy_fatal_error (yyconst char* msg )
1813
 {
1801
 {
1814
-			(void) fprintf( stderr, "%s\n", msg );
1802
+    	(void) fprintf( stderr, "%s\n", msg );
1815
 	exit( YY_EXIT_FAILURE );
1803
 	exit( YY_EXIT_FAILURE );
1816
 }
1804
 }
1817
 
1805
 
1839
  */
1827
  */
1840
 int yyget_lineno  (void)
1828
 int yyget_lineno  (void)
1841
 {
1829
 {
1842
-    
1830
+        
1843
     return yylineno;
1831
     return yylineno;
1844
 }
1832
 }
1845
 
1833
 
1862
 /** Get the length of the current token.
1850
 /** Get the length of the current token.
1863
  * 
1851
  * 
1864
  */
1852
  */
1865
-int yyget_leng  (void)
1853
+yy_size_t yyget_leng  (void)
1866
 {
1854
 {
1867
         return yyleng;
1855
         return yyleng;
1868
 }
1856
 }
1877
 }
1865
 }
1878
 
1866
 
1879
 /** Set the current line number.
1867
 /** Set the current line number.
1880
- * @param _line_number line number
1868
+ * @param line_number
1881
  * 
1869
  * 
1882
  */
1870
  */
1883
-void yyset_lineno (int  _line_number )
1871
+void yyset_lineno (int  line_number )
1884
 {
1872
 {
1885
     
1873
     
1886
-    yylineno = _line_number;
1874
+    yylineno = line_number;
1887
 }
1875
 }
1888
 
1876
 
1889
 /** Set the input stream. This does not discard the current
1877
 /** Set the input stream. This does not discard the current
1890
  * input buffer.
1878
  * input buffer.
1891
- * @param _in_str A readable stream.
1879
+ * @param in_str A readable stream.
1892
  * 
1880
  * 
1893
  * @see yy_switch_to_buffer
1881
  * @see yy_switch_to_buffer
1894
  */
1882
  */
1895
-void yyset_in (FILE *  _in_str )
1883
+void yyset_in (FILE *  in_str )
1896
 {
1884
 {
1897
-        yyin = _in_str ;
1885
+        yyin = in_str ;
1898
 }
1886
 }
1899
 
1887
 
1900
-void yyset_out (FILE *  _out_str )
1888
+void yyset_out (FILE *  out_str )
1901
 {
1889
 {
1902
-        yyout = _out_str ;
1890
+        yyout = out_str ;
1903
 }
1891
 }
1904
 
1892
 
1905
 int yyget_debug  (void)
1893
 int yyget_debug  (void)
1907
         return yy_flex_debug;
1895
         return yy_flex_debug;
1908
 }
1896
 }
1909
 
1897
 
1910
-void yyset_debug (int  _bdebug )
1898
+void yyset_debug (int  bdebug )
1911
 {
1899
 {
1912
-        yy_flex_debug = _bdebug ;
1900
+        yy_flex_debug = bdebug ;
1913
 }
1901
 }
1914
 
1902
 
1915
 static int yy_init_globals (void)
1903
 static int yy_init_globals (void)
1918
      * This function is called from yylex_destroy(), so don't allocate here.
1906
      * This function is called from yylex_destroy(), so don't allocate here.
1919
      */
1907
      */
1920
 
1908
 
1921
-    (yy_buffer_stack) = NULL;
1909
+    (yy_buffer_stack) = 0;
1922
     (yy_buffer_stack_top) = 0;
1910
     (yy_buffer_stack_top) = 0;
1923
     (yy_buffer_stack_max) = 0;
1911
     (yy_buffer_stack_max) = 0;
1924
-    (yy_c_buf_p) = NULL;
1912
+    (yy_c_buf_p) = (char *) 0;
1925
     (yy_init) = 0;
1913
     (yy_init) = 0;
1926
     (yy_start) = 0;
1914
     (yy_start) = 0;
1927
 
1915
 
1930
     yyin = stdin;
1918
     yyin = stdin;
1931
     yyout = stdout;
1919
     yyout = stdout;
1932
 #else
1920
 #else
1933
-    yyin = NULL;
1934
-    yyout = NULL;
1921
+    yyin = (FILE *) 0;
1922
+    yyout = (FILE *) 0;
1935
 #endif
1923
 #endif
1936
 
1924
 
1937
     /* For future reference: Set errno on error, since we are called by
1925
     /* For future reference: Set errno on error, since we are called by
1969
 #ifndef yytext_ptr
1957
 #ifndef yytext_ptr
1970
 static void yy_flex_strncpy (char* s1, yyconst char * s2, int n )
1958
 static void yy_flex_strncpy (char* s1, yyconst char * s2, int n )
1971
 {
1959
 {
1972
-		
1973
-	int i;
1960
+	register int i;
1974
 	for ( i = 0; i < n; ++i )
1961
 	for ( i = 0; i < n; ++i )
1975
 		s1[i] = s2[i];
1962
 		s1[i] = s2[i];
1976
 }
1963
 }
1979
 #ifdef YY_NEED_STRLEN
1966
 #ifdef YY_NEED_STRLEN
1980
 static int yy_flex_strlen (yyconst char * s )
1967
 static int yy_flex_strlen (yyconst char * s )
1981
 {
1968
 {
1982
-	int n;
1969
+	register int n;
1983
 	for ( n = 0; s[n]; ++n )
1970
 	for ( n = 0; s[n]; ++n )
1984
 		;
1971
 		;
1985
 
1972
 
1989
 
1976
 
1990
 void *yyalloc (yy_size_t  size )
1977
 void *yyalloc (yy_size_t  size )
1991
 {
1978
 {
1992
-			return malloc(size);
1979
+	return (void *) malloc( size );
1993
 }
1980
 }
1994
 
1981
 
1995
 void *yyrealloc  (void * ptr, yy_size_t  size )
1982
 void *yyrealloc  (void * ptr, yy_size_t  size )
1996
 {
1983
 {
1997
-		
1998
 	/* The cast to (char *) in the following accommodates both
1984
 	/* The cast to (char *) in the following accommodates both
1999
 	 * implementations that use char* generic pointers, and those
1985
 	 * implementations that use char* generic pointers, and those
2000
 	 * that use void* generic pointers.  It works with the latter
1986
 	 * that use void* generic pointers.  It works with the latter
2002
 	 * any pointer type to void*, and deal with argument conversions
1988
 	 * any pointer type to void*, and deal with argument conversions
2003
 	 * as though doing an assignment.
1989
 	 * as though doing an assignment.
2004
 	 */
1990
 	 */
2005
-	return realloc(ptr, size);
1991
+	return (void *) realloc( (char *) ptr, size );
2006
 }
1992
 }
2007
 
1993
 
2008
 void yyfree (void * ptr )
1994
 void yyfree (void * ptr )
2009
 {
1995
 {
2010
-			free( (char *) ptr );	/* see yyrealloc() for (char *) cast */
1996
+	free( (char *) ptr );	/* see yyrealloc() for (char *) cast */
2011
 }
1997
 }
2012
 
1998
 
2013
 #define YYTABLES_NAME "yytables"
1999
 #define YYTABLES_NAME "yytables"

+ 149 - 94
tac/main.c

7
 #include "types.h"
7
 #include "types.h"
8
 #include "list.h"
8
 #include "list.h"
9
 
9
 
10
-int asprintf(char **strp, const char *fmt, ...);
11
 extern TOKEN* lookup_token(char*);
10
 extern TOKEN* lookup_token(char*);
12
 
11
 
13
-/*
14
- * Existing problems/todo:
15
- * return from control block, need a "return address" to jump to -- call levels
16
- * --multiple assignments `int x,y,z = 1`--
17
- * new block scope for if statements?
18
- * arguments for functions
19
- * difference between tilde and semicolon
20
- * while loops
21
- * control flow `continue` and `break` linked to return address sort of? call levels
22
- */
23
-
24
 TOKEN* gen_tmp(void) {
12
 TOKEN* gen_tmp(void) {
25
-  char* tmp;
26
-  static char letter = 'a';
13
+  char tmp[10];
27
   static char num = 0;
14
   static char num = 0;
28
-  if (num == 100) {
29
-    num = 0;
30
-    if (++letter == 'r') letter++;
31
-  }
32
-  if (letter == '{') exit(1);
33
-  asprintf(&tmp, "\\%c%d", letter, num);
34
-  num++;
15
+  snprintf(tmp, 10, "$t%d", num++);
16
+  return lookup_token(tmp);
17
+}
18
+
19
+TOKEN* gen_label(void) {
20
+  char tmp[10];
21
+  static char label = 0;
22
+  snprintf(tmp, 10, "L%d", label++);
35
   return lookup_token(tmp);
23
   return lookup_token(tmp);
36
 }
24
 }
37
 
25
 
107
     else {
95
     else {
108
       for(i=0; i<level; i++) putchar(' ');
96
       for(i=0; i<level; i++) putchar(' ');
109
       printf("%s\n", named(tree->type));
97
       printf("%s\n", named(tree->type));
110
-
111
-/*       if (tree->type=='~') { */
112
-/*         for(i=0; i<level+2; i++) putchar(' '); */
113
-/*         printf("%p\n", tree->left); */
114
-/*       } */
115
-/*       else */
116
-        print_tree0(tree->left, level+2);
98
+      print_tree0(tree->left, level+2);
117
       print_tree0(tree->right, level+2);
99
       print_tree0(tree->right, level+2);
118
     }
100
     }
119
 }
101
 }
123
     print_tree0(tree, 0);
105
     print_tree0(tree, 0);
124
 }
106
 }
125
 
107
 
126
-// void add_function_to_env(NODE *tree, ENV *env_ptr) {
127
-//   BIND* existing;
128
-//   NODE* func_name = tree->left->right->left->left;
129
-//   TOKEN* name_token = (TOKEN*) func_name;
130
-//   if ((existing = find_name_in_env(name_token->lexeme, env_ptr)) == NULL) {
131
-//     printf("Added function name %s to environment with value: \n", name_token->lexeme);
132
-//     print_tree(tree);
133
-//     if (env_ptr->bindings == NULL) {
134
-//       env_ptr->bindings = create_list(create_binding(name_token->lexeme, tree, env_ptr), NULL);
135
-//     } else {
136
-//       append_list(env_ptr->bindings, create_binding(name_token->lexeme, tree, env_ptr));
137
-//     }
138
-//   } else {
139
-//     printf("Updating function name %s with value: \n", name_token->lexeme);
140
-//     print_tree(tree);
141
-//     existing->tree = tree;
142
-//     existing->env = env_ptr;
143
-//   }
144
-// }
145
-
146
-
147
-// void add_var_to_env(NODE *tree, ENV *env_ptr) {
148
-//   BIND* existing;
149
-//   NODE* var_name = tree->left->left;
150
-//   TOKEN* name_token = (TOKEN*) var_name;
151
-//   TOKEN* tok = new_token(INT);
152
-//   tok->value = recursive_interpret(tree->right, env_ptr);
153
-//   asprintf(&tok->lexeme, "%d", tok->value);
154
-//   if ((existing = find_name_in_env(name_token->lexeme, env_ptr)) == NULL) {
155
-//     printf("Added variable name %s to environment with value: %s\n", name_token->lexeme, tok->lexeme);
156
-//     if (env_ptr->bindings == NULL) {
157
-//       env_ptr->bindings = create_list(create_binding(name_token->lexeme, (NODE*) tok, NULL), NULL);
158
-//     } else {
159
-//       append_list(env_ptr->bindings, create_binding(name_token->lexeme, (NODE*) tok, NULL));
160
-//     }
161
-//   } else {
162
-//     printf("Updating variable name %s with value: %s\n", name_token->lexeme, tok->lexeme);
163
-//     existing->tree = (NODE *) tok;
164
-//   } 
165
-// }
166
-
167
 void new_tac(TACS* tacs, TOKEN* dst, TOKEN* src, TOKEN* tgt, int op) {
108
 void new_tac(TACS* tacs, TOKEN* dst, TOKEN* src, TOKEN* tgt, int op) {
168
   if (tacs->list == NULL) {
109
   if (tacs->list == NULL) {
169
     TAC_T* tac;
110
     TAC_T* tac;
184
   while (ptr != NULL) {
125
   while (ptr != NULL) {
185
     int op = ptr->elem->op;
126
     int op = ptr->elem->op;
186
     if (op == '=') printf("%s := %s\n", stok(ptr->elem->dst), stok(ptr->elem->src));
127
     if (op == '=') printf("%s := %s\n", stok(ptr->elem->dst), stok(ptr->elem->src));
187
-    if (op == '+') printf("%s := %s + %s\n", stok(ptr->elem->dst), stok(ptr->elem->src), stok(ptr->elem->tgt));
188
-    if (op == '-') printf("%s := %s - %s\n", stok(ptr->elem->dst), stok(ptr->elem->src), stok(ptr->elem->tgt));
189
-    if (op == '/') printf("%s := %s / %s\n", stok(ptr->elem->dst), stok(ptr->elem->src), stok(ptr->elem->tgt));
190
-    if (op == '*') printf("%s := %s * %s\n", stok(ptr->elem->dst), stok(ptr->elem->src), stok(ptr->elem->tgt));
191
-    if (op == '%') printf("%s := %s %% %s\n", stok(ptr->elem->dst), stok(ptr->elem->src), stok(ptr->elem->tgt));
128
+    if (op=='+' || op=='-' || op=='*' || op=='/' || op =='%') printf("%s := %s %c %s\n", stok(ptr->elem->dst), stok(ptr->elem->src), op, stok(ptr->elem->tgt));
192
     if (op == RETURN) printf("ret %s\n", stok(ptr->elem->src));
129
     if (op == RETURN) printf("ret %s\n", stok(ptr->elem->src));
130
+    if (op == 'D') printf("\nfunc %s\n", stok(ptr->elem->dst));
131
+    if (op == '>' || op == '<') printf("if %s %c %s then %s\n", stok(ptr->elem->src), op, stok(ptr->elem->tgt), stok(ptr->elem->dst));
132
+    if (op == GE_OP) printf("if %s >= %s then %s\n", stok(ptr->elem->src), stok(ptr->elem->tgt), stok(ptr->elem->dst));
133
+    if (op == LE_OP) printf("if %s <= %s then %s\n", stok(ptr->elem->src), stok(ptr->elem->tgt), stok(ptr->elem->dst));
134
+    if (op == EQ_OP) printf("if %s == %s then %s\n", stok(ptr->elem->src), stok(ptr->elem->tgt), stok(ptr->elem->dst));
135
+    if (op == NE_OP) printf("if %s != %s then %s\n", stok(ptr->elem->src), stok(ptr->elem->tgt), stok(ptr->elem->dst));
136
+    if (op == IDENTIFIER) printf("%s\n", stok(ptr->elem->dst));
137
+    if (op == VOID) printf("goto %s\n", stok(ptr->elem->dst));
138
+    if (op == AUTO) printf("param %s\n", stok(ptr->elem->dst));
139
+    if (op == APPLY) printf("call %s, %s\n", stok(ptr->elem->dst), stok(ptr->elem->tgt));
140
+    if (op == 278) printf("arg %s\n", stok(ptr->elem->dst));
141
+    if (op == 279) printf("end %s\n", stok(ptr->elem->dst));
193
     ptr = ptr->next;
142
     ptr = ptr->next;
194
   }
143
   }
195
 }
144
 }
196
 
145
 
146
+int invert_op(int op) {
147
+  if (op == LE_OP) {
148
+    return '>';
149
+  } else if (op == GE_OP) {
150
+    return '<';
151
+  } else if (op == '>') {
152
+    return LE_OP;
153
+  } else if (op == '<') {
154
+    return GE_OP;
155
+  } else if (op == EQ_OP) {
156
+    return NE_OP;
157
+  } else if (op == NE_OP) {
158
+    return EQ_OP;
159
+  } else {
160
+    printf("Unknown operator: %c", op);
161
+    exit(1);
162
+  }
163
+}
164
+
165
+int count_args(NODE *tree) { 
166
+  NODE *tmp = tree;
167
+  int a = 1;
168
+  while (tmp != NULL) {
169
+    if (tmp->type != ',') {
170
+      break;
171
+    }
172
+    tmp = tmp->left;
173
+    a++;
174
+  }
175
+  return a;
176
+}
177
+
178
+void populate_arg_list(NODE *tree, TOKEN** arr, int arr_len) {
179
+  if (tree->type == '~') {
180
+    for (int i = 0; i < arr_len; i++) {
181
+      if (arr[i] == NULL) {
182
+        TOKEN* name = (TOKEN*)tree->right->left;
183
+        arr[i] = name;
184
+        break;
185
+      }
186
+    }
187
+  } else {
188
+    populate_arg_list(tree->left, arr, arr_len);
189
+    populate_arg_list(tree->right, arr, arr_len);
190
+  }
191
+}
192
+
193
+TOKEN** get_argument_list(NODE *tree) {
194
+  int num_args = count_args(tree->left->right->right);
195
+  TOKEN** arr = (TOKEN**) malloc(sizeof(TOKEN*) * num_args);
196
+  populate_arg_list(tree->left->right->right, arr, num_args);
197
+  return arr;
198
+}
199
+
200
+void populate_val_list(NODE* tree, NODE** arr, int num_args) {
201
+  if (tree->type == ',') {
202
+    populate_val_list(tree->left, arr, num_args);
203
+    populate_val_list(tree->right, arr, num_args);
204
+  } else {
205
+    for (int i = 0; i < num_args; i++) {
206
+      if (arr[i] == NULL) {
207
+        arr[i] = tree;
208
+        break;
209
+      }
210
+    }
211
+  }
212
+}
213
+
214
+NODE** get_val_list(NODE *tree) {
215
+  int num_args = count_args(tree);
216
+  NODE** arr = (NODE**) malloc(sizeof(NODE*) * num_args);
217
+  populate_val_list(tree, arr, num_args);
218
+  return arr;
219
+}
220
+
197
 TOKEN* build_tac(NODE *tree, TACS* tac) {
221
 TOKEN* build_tac(NODE *tree, TACS* tac) {
198
   if (tree == NULL) return NULL;
222
   if (tree == NULL) return NULL;
199
   int type = tree->type;
223
   int type = tree->type;
209
     TOKEN* tgt = build_tac(tree->right, tac);
233
     TOKEN* tgt = build_tac(tree->right, tac);
210
     new_tac(tac, dst, src, tgt, tree->type);
234
     new_tac(tac, dst, src, tgt, tree->type);
211
     return dst;
235
     return dst;
236
+  } else if (type == IF) {
237
+    int op = invert_op(tree->left->type);
238
+    TOKEN* lhs = build_tac(tree->left->left, tac);
239
+    TOKEN* rhs = build_tac(tree->left->right, tac);
240
+    TOKEN* label = gen_label();
241
+    new_tac(tac, label, lhs, rhs, op);
242
+    if (tree->right->type == ELSE) {
243
+      TOKEN* cont = gen_label();
244
+      build_tac(tree->right->left, tac);
245
+      new_tac(tac, cont, NULL, NULL, VOID);
246
+      new_tac(tac, label, NULL, NULL, IDENTIFIER);
247
+      build_tac(tree->right->right, tac);
248
+      new_tac(tac, cont, NULL, NULL, IDENTIFIER);
249
+    } else {
250
+      build_tac(tree->right, tac);
251
+      new_tac(tac, label, NULL, NULL, IDENTIFIER);
252
+    }
212
   } else if (type == RETURN) {
253
   } else if (type == RETURN) {
213
     TOKEN* src = build_tac(tree->left, tac);
254
     TOKEN* src = build_tac(tree->left, tac);
214
     new_tac(tac, NULL, src, NULL, RETURN);
255
     new_tac(tac, NULL, src, NULL, RETURN);
256
+  } else if (type == 'D') {
257
+    new_tac(tac, (TOKEN*) tree->left->right->left->left, NULL, NULL, 'D');
258
+    if (tree->left->right->right != NULL) {
259
+      TOKEN** arg_list = get_argument_list(tree);
260
+      int arg_count = count_args(tree->left->right->right);
261
+      for (int i = 0; i < arg_count; i++) {
262
+        new_tac(tac, arg_list[i], NULL, NULL, AUTO);
263
+      }
264
+    }
265
+    build_tac(tree->right, tac);
266
+    new_tac(tac, (TOKEN*) tree->left->right->left->left, NULL, NULL, 279);
267
+  } else if (type == APPLY) {
268
+    TOKEN* ret_val = gen_tmp();
269
+    if (tree->right != NULL) {
270
+      int num_args;
271
+      NODE **val_list;
272
+      num_args = count_args(tree->right);
273
+      val_list = get_val_list(tree->right);
274
+      for (int i = 0; i < num_args; i++) {
275
+        TOKEN* arg_token = build_tac(val_list[i], tac);
276
+        new_tac(tac, arg_token, NULL, NULL, 278);
277
+      }
278
+    }
279
+    new_tac(tac, (TOKEN*) tree->left->left, NULL, ret_val, APPLY);
280
+    return ret_val;
215
   } else {
281
   } else {
216
     build_tac(tree->left, tac);
282
     build_tac(tree->left, tac);
217
     build_tac(tree->right, tac);
283
     build_tac(tree->right, tac);
284
+    return NULL;
218
   }
285
   }
286
+  return NULL;
219
 }
287
 }
220
 
288
 
221
 
289
 
285
 
353
 
286
 void compile_tac(TLIST* tacs) {
354
 void compile_tac(TLIST* tacs) {
287
   printf("=====\n");
355
   printf("=====\n");
288
-  printf("main:\n");
289
   init_reg();
356
   init_reg();
290
   while(tacs != NULL) {
357
   while(tacs != NULL) {
291
     TAC_T* t = tacs->elem;
358
     TAC_T* t = tacs->elem;
293
       int src_idx = alloc_register(t->src);
360
       int src_idx = alloc_register(t->src);
294
       printf("or $%d, $0, $%d\n", store_var(t->dst), src_idx);
361
       printf("or $%d, $0, $%d\n", store_var(t->dst), src_idx);
295
     }
362
     }
363
+    if (t->op == 'D') {
364
+      printf("%s:\n", t->dst->lexeme);
365
+    }
296
     if (t->op == '+') {
366
     if (t->op == '+') {
297
       int tgt_idx = alloc_register(t->tgt);
367
       int tgt_idx = alloc_register(t->tgt);
298
       int src_idx = alloc_register(t->src);
368
       int src_idx = alloc_register(t->src);
323
     }
393
     }
324
     if (t->op == RETURN) {
394
     if (t->op == RETURN) {
325
       int src_idx = alloc_register(t->src);
395
       int src_idx = alloc_register(t->src);
326
-      printf("or $k0, $0, $%d\n", src_idx);
396
+      printf("or $v1, $0, $%d\n", src_idx);
327
     }
397
     }
328
     tacs = tacs->next;
398
     tacs = tacs->next;
329
   }
399
   }
331
   printf("syscall\n");
401
   printf("syscall\n");
332
 }
402
 }
333
 
403
 
334
-// ENV* cons_global_env(NODE *tree) {
335
-//   ENV* global = create_new_function_env(NULL);
336
-//   recursive_interpret(tree, global);
337
-//   return global;
338
-// }
339
-
340
 void interpret_tree(NODE *tree) {
404
 void interpret_tree(NODE *tree) {
341
-  // ENV* global_env = cons_global_env(tree);
342
-  // BIND* ref_main = find_name_in_env("main", global_env);
343
-  // if (ref_main == NULL) {
344
-  //   printf("Could not find main, cannot run!\n");
345
-  //   exit(1);
346
-  // }
347
-  // print ref_main to make sure we really got it
348
-  // printf("Located %s, ready to run!\n", ref_main->name);
349
-  // printf("%d\n", recursive_interpret(ref_main->tree->right, ref_main->env));
350
   TACS* gen_tac = (TACS*) malloc(sizeof(TACS));
405
   TACS* gen_tac = (TACS*) malloc(sizeof(TACS));
351
   build_tac(tree, gen_tac);
406
   build_tac(tree, gen_tac);
352
   print_tac(gen_tac->list);
407
   print_tac(gen_tac->list);
354
 }
409
 }
355
 
410
 
356
 extern int yydebug;
411
 extern int yydebug;
357
-/* extern NODE* yyparse(void); */
412
+#ifdef __APPLE__
413
+extern NODE* yyparse(void);
414
+#endif
358
 extern NODE* ans;
415
 extern NODE* ans;
359
 extern void init_symbtable(void);
416
 extern void init_symbtable(void);
360
 
417
 
369
     printf("parse finished\n");
426
     printf("parse finished\n");
370
     print_tree(tree);
427
     print_tree(tree);
371
     interpret_tree(tree);
428
     interpret_tree(tree);
372
-    // int i;
373
-    // for (i = 0; i < 2600; i++) printf("%s\n", gen_tmp());
374
     return 0;
429
     return 0;
375
 }
430
 }

+ 15 - 3
tac/simple.c

1
+int inner_fact(int n, int a) {
2
+  if (n == 0) return a;
3
+  return inner_fact(n-1, a*n);
4
+}
5
+
6
+int fact(int n) {
7
+  return inner_fact(n, 1);
8
+}
9
+
1
 int main() {
10
 int main() {
2
-  int z = 5;
3
-  int y = 5 + 2;
4
-  int x = 5 + y * z;
11
+  int x = blah(2);
12
+  if (x > 0) {
13
+    x = 10;
14
+  } else {
15
+    x = 5;
16
+  }
5
   return x;
17
   return x;
6
 }
18
 }