Author: kjs
Date: Tue Apr 10 04:21:58 2007
New Revision: 18113

Added:
   trunk/compilers/pirc/src/pirutil.c
   trunk/compilers/pirc/src/pirutil.h
Modified:
   trunk/compilers/pirc/src/jsonout.c
   trunk/compilers/pirc/src/pirlexer.c
   trunk/compilers/pirc/src/pirlexer.h
   trunk/compilers/pirc/src/pirmain.c
   trunk/compilers/pirc/src/pirparser.c
   trunk/compilers/pirc/src/pirvtable.c
   trunk/compilers/pirc/t/stmts.pir
   trunk/config/gen/makefiles/pirc.in

Log:
compilers/pirc:
* refactored clone_string() to pirutil.c
* add debug and verbose flags functionality (a bit)

Modified: trunk/compilers/pirc/src/jsonout.c
==============================================================================
--- trunk/compilers/pirc/src/jsonout.c  (original)
+++ trunk/compilers/pirc/src/jsonout.c  Tue Apr 10 04:21:58 2007
@@ -4,6 +4,14 @@
 
 jsonout.c - JSON backend for PIRC
 
+=head1 DESCRIPTION
+
+Each vtable method is implemented by a private (static) function.
+This back-end emits a JSON representation of the source. Although JSON
+is quite a simple format, it is not very trivial to emit it. Due to some
+householding code, it isn't very straightforward (indention and commas).
+Some refactoring would be in order, once this back-end is finished.
+
 =cut
 
 */
@@ -50,7 +58,8 @@
  *
  */
 typedef struct emit_data {
-    FILE *outfile; /* output file */
+    FILE *outfile; /* output file -- XXX TODO: get filename from -o option in 
pirmain.c */
+
     int indent; /* keep track of indention */
     /* has the first item in a list already been emitted? If so, we need a 
comma as separator */
     int need_comma;

Modified: trunk/compilers/pirc/src/pirlexer.c
==============================================================================
--- trunk/compilers/pirc/src/pirlexer.c (original)
+++ trunk/compilers/pirc/src/pirlexer.c Tue Apr 10 04:21:58 2007
@@ -38,6 +38,7 @@
 
 */
 #include "pirlexer.h"
+#include "pirutil.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <malloc.h>
@@ -942,33 +943,7 @@
 }
 
 
-/*
-
-=item clone_string()
-
-clone a string. Copy the characters of src into dest
-and return dest. Memory allocation is done by this function, keeping
-this function's client code simple. Please free() the memory after usage!
-
-=cut
 
-*/
-char *
-clone_string(char const * src) {
-    int srclen;
-    char * dest, *ptr;
-
-    assert(src != NULL);
-    srclen = strlen(src);
-    /* dest is used as an iterator, ptr - still pointing to the beginning
-     * of the string - is returned
-     */
-    dest = ptr = (char *)calloc(srclen + 1, sizeof(char));
-    while (*src) {
-        *dest++ = *src++;
-    }
-    return ptr;
-}
 
 /*
 

Modified: trunk/compilers/pirc/src/pirlexer.h
==============================================================================
--- trunk/compilers/pirc/src/pirlexer.h (original)
+++ trunk/compilers/pirc/src/pirlexer.h Tue Apr 10 04:21:58 2007
@@ -189,9 +189,6 @@
 /* get the name of the current file being scanned */
 extern char * const get_current_file(struct lexer_state *s);
 
-/* clone the specified string; *do* free the allocated memory yourself */
-extern char *clone_string(char const * src);
-
 /* read a heredoc 'token': everything up to and including the heredoc label */
 extern token read_heredoc(struct lexer_state *lexer, char *heredoc_label);
 

Modified: trunk/compilers/pirc/src/pirmain.c
==============================================================================
--- trunk/compilers/pirc/src/pirmain.c  (original)
+++ trunk/compilers/pirc/src/pirmain.c  Tue Apr 10 04:21:58 2007
@@ -15,16 +15,7 @@
 #include "pirvtable.h" /* vtable api      */
 #include "jsonout.h"   /* for json output */
 #include "pbcout.h"    /* for PBC output  */
-
-
-
-
-/* command argument switches */
-typedef enum pirc_flags {
-        PIRC_VERBOSE     = 0,
-        PIRC_DEBUG       = 1,
-
-} pirc_flag;
+#include "pirutil.h"
 
 
 /* define output type: what kind of semantic routines
@@ -55,9 +46,9 @@
 print_help(void) {
     fprintf(stderr, "Usage: pirc [options] <file>\n");
     fprintf(stderr, "\tGeneral:\n");
-    fprintf(stderr, "\t-d  debug messages (not implemented yet)\n");
+    fprintf(stderr, "\t-d  debug messages\n");
     fprintf(stderr, "\t-h  print this help message\n");
-    fprintf(stderr, "\t-v  verbose (not implemented yet)\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");
@@ -97,12 +88,14 @@
         switch (opt) {
             case 'd':
                 flags |= PIRC_DEBUG;
+                printdebug("debug mode on\n");
                 break;
             case 'h':
                 print_help();
                 break;
             case 'v':
                 flags |= PIRC_VERBOSE;
+                printverbose("verbose mode on\n");
                 break;
             case 'p':
                 output = OUTPUT_PAST;
@@ -160,8 +153,12 @@
     p = new_parser(argv[0], vtable);
 
     /* start parsing */
+    if (flags & PIRC_VERBOSE) printverbose("start parsing...\n");
+
     TOP(p);
 
+    if (flags & PIRC_VERBOSE) printverbose("parsing done.\n");
+
     /* check for errors */
     if (get_parse_errors(p)) {
         fprintf(stderr, "\nThere were %d errors.\n", get_parse_errors(p));

Modified: trunk/compilers/pirc/src/pirparser.c
==============================================================================
--- trunk/compilers/pirc/src/pirparser.c        (original)
+++ trunk/compilers/pirc/src/pirparser.c        Tue Apr 10 04:21:58 2007
@@ -54,6 +54,7 @@
 #include "pirlexer.h"
 #include "pirparser.h"
 #include "pirvtable.h" /* vtable definition */
+#include "pirutil.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <assert.h>

Added: trunk/compilers/pirc/src/pirutil.c
==============================================================================
--- (empty file)
+++ trunk/compilers/pirc/src/pirutil.c  Tue Apr 10 04:21:58 2007
@@ -0,0 +1,92 @@
+/*
+
+=head1 NAME
+
+pirutil.c - various utility functions
+
+=cut
+
+*/
+#include "pirutil.h"
+#include <assert.h>
+#include <string.h>
+#include <malloc.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+/*
+
+=head1 FUNCTIONS
+
+=over 4
+
+=item clone_string()
+
+clone a string. Copy the characters of src into dest
+and return dest. Memory allocation is done by this function, keeping
+this function's client code simple. Please free() the memory after usage!
+
+=cut
+
+*/
+char *
+clone_string(char const * src) {
+    int srclen;
+    char * dest, *ptr;
+
+    assert(src != NULL);
+    srclen = strlen(src);
+    /* dest is used as an iterator, ptr - still pointing to the beginning
+     * of the string - is returned
+     */
+    dest = ptr = (char *)calloc(srclen + 1, sizeof(char));
+    while (*src) {
+        *dest++ = *src++;
+    }
+    return ptr;
+}
+
+
+/*
+
+=item verbose()
+
+Prints the specified message if the verbose flag was set
+
+=cut
+
+*/
+void
+printverbose(char *message) {
+    fprintf(stdout, message);
+}
+
+/*
+
+=item debug()
+
+Prints the specified message if the debug flag was set.
+
+=cut
+
+*/
+void
+printdebug(char *message) {
+    fprintf(stdout, message);
+}
+
+
+/*
+
+=back
+
+=cut
+
+*/
+
+/*
+ * Local variables:
+ *   c-file-style: "parrot"
+ * End:
+ * vim: expandtab shiftwidth=4:
+ */

Added: trunk/compilers/pirc/src/pirutil.h
==============================================================================
--- (empty file)
+++ trunk/compilers/pirc/src/pirutil.h  Tue Apr 10 04:21:58 2007
@@ -0,0 +1,36 @@
+#ifndef __PIRUTIL_H
+#  define __PIRUTIL_H
+
+
+
+/* command argument switches */
+typedef enum pirc_flags {
+        PIRC_VERBOSE     = 1,
+        PIRC_DEBUG       = 2,
+
+} pirc_flag;
+
+
+
+extern void printverbose(char *message);
+
+extern void printdebug(char *message);
+
+extern char *clone_string(char const *str);
+
+/* where do we leave the flags?
+#  define verbose(P,S)  if (P->flags & PIRC_VERBOSE) printverbose(S)
+#  define debug(P,S)    if (P->flags & PIRC_DEBUG)   printdebug(S)
+*/
+
+
+#endif
+
+
+
+/*
+ * Local variables:
+ *   c-file-style: "parrot"
+ * End:
+ * vim: expandtab shiftwidth=4:
+ */

Modified: trunk/compilers/pirc/src/pirvtable.c
==============================================================================
--- trunk/compilers/pirc/src/pirvtable.c        (original)
+++ trunk/compilers/pirc/src/pirvtable.c        Tue Apr 10 04:21:58 2007
@@ -100,7 +100,7 @@
     vtable->invocation_end   = not_implemented;
 
     /* set data to NULL, it's initialized in the backend module */
-    vtable->data           = NULL;
+    vtable->data = NULL;
 
     return vtable;
 }

Modified: trunk/compilers/pirc/t/stmts.pir
==============================================================================
--- trunk/compilers/pirc/t/stmts.pir    (original)
+++ trunk/compilers/pirc/t/stmts.pir    Tue Apr 10 04:21:58 2007
@@ -4,7 +4,7 @@
 # test .HLL statement
 .HLL "PIRC", "pircgroup"
 
-
+.global x
 
 .sub main
 # test local declarations

Modified: trunk/config/gen/makefiles/pirc.in
==============================================================================
--- trunk/config/gen/makefiles/pirc.in  (original)
+++ trunk/config/gen/makefiles/pirc.in  Tue Apr 10 04:21:58 2007
@@ -34,11 +34,12 @@
   src/pirout.c \
   src/pastout.c        \
   src/pirvtable.c \
-  src/jsonout.c
+  src/jsonout.c \
+  src/pirutil.c
 
 
-pirc: pirmain$(O) pirparser$(O)        pirlexer$(O) pirout$(O) pastout$(O)     
pirvtable$(O) jsonout$(O) pbcout$(O)
-       $(CC) -o pirc$(EXE)     pirmain$(O)     pirparser$(O) pirlexer$(O) 
pirout$(O) pastout$(O) pirvtable$(O) jsonout$(O) pbcout$(O)
+pirc: pirmain$(O) pirparser$(O)        pirlexer$(O) pirout$(O) pastout$(O)     
pirvtable$(O) jsonout$(O) pbcout$(O) pirutil$(O)
+       $(CC) -o pirc$(EXE)     pirmain$(O)     pirparser$(O) pirlexer$(O) 
pirout$(O) pastout$(O) pirvtable$(O) jsonout$(O) pbcout$(O) pirutil$(O)
 
 pirmain$(O): src/pirmain.c src/pirparser.h
        $(CC) $(CFLAGS) -c src/pirmain.c
@@ -64,6 +65,8 @@
 jsonout$(O): src/jsonout.c src/jsonout.h
        $(CC) $(CFLAGS) -c src/jsonout.c
 
+pirutil$(O): src/pirutil.c src/pirutil.h
+       $(CC) $(CFLAGS) -c src/pirutil.c
 
 
 # This is a    listing of all targets, that are meant to be called     by users
@@ -98,6 +101,7 @@
        pod2html --css=http://www.parrotcode.org/css/perl.css src/pirout.c > 
doc/pirout.html
        pod2html --css=http://www.parrotcode.org/css/perl.css src/jsonout.c > 
doc/jsonout.html
        pod2html --css=http://www.parrotcode.org/css/perl.css src/pastout.c > 
doc/pastout.html
+       pod2html --css=http://www.parrotcode.org/css/perl.css src/pirutil.c > 
doc/pirutil.html
        pod2html --css=http://www.parrotcode.org/css/perl.css doc/design.pod > 
doc/design.html
        pod2html --css=http://www.parrotcode.org/css/perl.css README.pod > 
doc/README.html
 

Reply via email to