Author: kjs
Date: Thu Apr 12 02:06:34 2007
New Revision: 18159

Modified:
   trunk/compilers/pirc/src/pirmain.c
   trunk/compilers/pirc/src/pirparser.c
   trunk/compilers/pirc/src/pirutil.c

Log:
compilers/pirc:
* changes so the emitter works correctly for including files.
* start of support for specifying output file

Modified: trunk/compilers/pirc/src/pirmain.c
==============================================================================
--- trunk/compilers/pirc/src/pirmain.c  (original)
+++ trunk/compilers/pirc/src/pirmain.c  Thu Apr 12 02:06:34 2007
@@ -46,14 +46,15 @@
 print_help(void) {
     fprintf(stderr, "Usage: pirc [options] <file>\n");
     fprintf(stderr, "\tGeneral:\n");
-    fprintf(stderr, "\t-d  debug messages\n");
-    fprintf(stderr, "\t-h  print this help message\n");
-    fprintf(stderr, "\t-v  verbose\n");
+    fprintf(stderr, "\t-d         debug messages\n");
+    fprintf(stderr, "\t-h         print this help message\n");
+    fprintf(stderr, "\t-v         verbose\n");
     fprintf(stderr, "\n\tOutput:\n");
-    fprintf(stderr, "\t-r  print PIR output\n");
-    fprintf(stderr, "\t-p  print PAST output\n");
-    fprintf(stderr, "\t-j  print JSON output\n");
-    fprintf(stderr, "\t-b  print PBC output (not implemented)\n");
+    fprintf(stderr, "\t-r         PIR output\n");
+    fprintf(stderr, "\t-p         PAST output\n");
+    fprintf(stderr, "\t-j         JSON output\n");
+    fprintf(stderr, "\t-b         PBC output (not implemented)\n");
+    fprintf(stderr, "\t-o <file>  save output to <file>\n");
     fprintf(stderr, "\n");
 }
 
@@ -73,6 +74,7 @@
     pirvtable *vtable      = NULL;        /* create a vtable for semantic 
actions */
     int flags              = 0;           /* argument parsing */
     outputtype output      = OUTPUT_NONE; /* what kind of output? */
+    char *outputfile       = NULL;        /* output file */
 
     /* skip program name */
     argv++;
@@ -109,6 +111,15 @@
             case 'b':
                 output = OUTPUT_PBC;
                 break;
+            case 'o':
+                argv++; /* next argument */
+                argc--;
+                /* store a pointer to the output file name, it's there during 
the
+                 * whole program's lifetime, as it's an argument to main(), so 
no need
+                 * to clone the string
+                 */
+                outputfile = argv[0];
+                break;
             default:
                 fprintf(stderr, "Unknown option: '%c'\n", opt);
                 print_help();
@@ -167,6 +178,8 @@
         fprintf(stderr, "\nparsed successfully.\n");
     }
 
+    if (outputfile != NULL) printf("outputfile written to: '%s'\n", 
outputfile);
+
     /* clean up and exit */
     exit_parser(p);
     return 0;

Modified: trunk/compilers/pirc/src/pirparser.c
==============================================================================
--- trunk/compilers/pirc/src/pirparser.c        (original)
+++ trunk/compilers/pirc/src/pirparser.c        Thu Apr 12 02:06:34 2007
@@ -104,6 +104,8 @@
 #define next(P) P->curtoken = next_token(P->lexer)
 
 
+/* forward declaration; program is called by include. */
+static void program(parser_state *p);
 
 /*
 
@@ -2189,6 +2191,7 @@
     match(p, T_ENDM);
 }
 
+
 /*
 
 =item *
@@ -2215,7 +2218,8 @@
         open_include_file(p->lexer);
         next(p);
 
-        TOP(p); /* go parse it */
+        program(p); /* go parse it, don't call TOP() which initializes the 
emitter */
+
         /* switch back to other file that included the one above */
         close_include_file(p->lexer);
         next(p); /* get next token from this file */
@@ -2383,7 +2387,7 @@
 
 =item *
 
-  program -> {'\n'} compilation_unit { '\n' compilation_unit }
+  program -> {'\n'} compilation_unit { '\n' compilation_unit } EOF
 
 =cut
 
@@ -2393,8 +2397,6 @@
     /* the file may have some initial newlines; eat them */
     if (p->curtoken == T_NEWLINE) next(p);
 
-    emit_init(p); /* initialize emitter */
-
     compilation_unit(p);
 
     while (p->curtoken != T_EOF ) {
@@ -2402,14 +2404,17 @@
         compilation_unit(p);
     }
 
-    emit_end(p); /* close down emitter */
+    /* do NOT match T_EOF; match() tries to read the next token instead, do a 
manual check. */
+    if (p->curtoken != T_EOF) {
+        syntax_error(p, 3, "end of file expected in file '", 
get_current_file(p->lexer), "'\n");
+    }
 }
 
 /*
 
 =item *
 
-  TOP -> program EOF
+  TOP -> program
 
 =cut
 
@@ -2417,12 +2422,9 @@
 void
 TOP(parser_state *p) {
     /* file -> program EOF */
+    emit_init(p); /* initialize emitter */
     program(p);
-
-    /* do NOT match T_EOF; match() tries to read the next token instead, do a 
manual check. */
-    if (p->curtoken != T_EOF) {
-        syntax_error(p, 3, "end of file expected in file '", 
get_current_file(p->lexer), "'\n");
-    }
+    emit_end(p); /* close down emitter */
 }
 
 

Modified: trunk/compilers/pirc/src/pirutil.c
==============================================================================
--- trunk/compilers/pirc/src/pirutil.c  (original)
+++ trunk/compilers/pirc/src/pirutil.c  Thu Apr 12 02:06:34 2007
@@ -77,7 +77,7 @@
 
 
 /* Array holding all parrot ops */
-static char const *parrot_ops[] = {
+static char *parrot_ops[] = {
         "yield",
         "xor",
         "warningson",
@@ -413,8 +413,6 @@
     char const *iter = parrot_ops[0];
     int index = 0;
 
-    assert(id != NULL);
-
     /* very inefficient implementation, but for now it works */
     /* suggestions: hashtable, binary search */
 
@@ -425,6 +423,7 @@
         iter = parrot_ops[++index];
     }
 
+
     return 0;
 }
 

Reply via email to