Author: kjs
Date: Wed Apr  4 01:30:03 2007
New Revision: 17973

Added:
   trunk/compilers/pirc/src/pbcout.c   (contents, props changed)
   trunk/compilers/pirc/src/pbcout.h   (contents, props changed)
Modified:
   trunk/compilers/pirc/src/jsonout.c
   trunk/compilers/pirc/src/pastout.c
   trunk/compilers/pirc/src/pirlexer.c
   trunk/compilers/pirc/src/pirmain.c
   trunk/compilers/pirc/src/pirout.c
   trunk/compilers/pirc/src/pirparser.c
   trunk/compilers/pirc/src/pirvtable.c
   trunk/compilers/pirc/src/pirvtable.h
   trunk/config/gen/makefiles/pirc.in

Log:
compilers/pirc:
* added dummy back-end for pbc output
* changed default argument to all vtable methods to some emit data structure
* made emitting modules threadsafe (due to emit data structure)

Modified: trunk/compilers/pirc/src/jsonout.c
==============================================================================
--- trunk/compilers/pirc/src/jsonout.c  (original)
+++ trunk/compilers/pirc/src/jsonout.c  Wed Apr  4 01:30:03 2007
@@ -9,87 +9,131 @@
 */
 #include "jsonout.h"
 #include "pirvtable.h"
-#include "pirparser.h"
 #include <stdio.h>
+#include <stdlib.h>
+#include <malloc.h>
 
-#define OUT  stdout
+#define OUT          stdout
+#define INDENT       4
 
-/* todo: re-use this code (see pastout.c) , and make it threadsafe: */
-static int indent = 0;
+/* make it look like a function call */
+#define indent(D)    D->indent += INDENT
+#define dedent(D)    D->indent -= INDENT
 
-#define indent()    indent += 4
-#define dedent()    indent -= 4
 
+/* Private declaration of emit_data.
+ *
+ *
+ */
+typedef struct emit_data {
+    FILE *outfile;
+    int indent;
+
+} emit_data;
+
+
+/* Implementation of methods */
 
 static void
-json_init(struct parser_state *p) {
+json_init(emit_data *data) {
     fprintf(OUT, "{\n");
-    indent();
+    indent(data);
+}
+
+
+static void
+json_end(emit_data *data) {
+    dedent(data);
+    fprintf(OUT, "%*s}\n", data->indent, (data->indent == 0) ? "" : " ");
+}
+
+static void
+json_sub_start(emit_data *data, char *source, int pos) {
+    fprintf(OUT, "%*s\"sub\" : {\n", data->indent, " ");
+    indent(data);
+    fprintf(OUT, "%*s\"source\" : \"%s\",\n", data->indent, " ", source);
+    fprintf(OUT, "%*s\"pos\" : %d,\n", data->indent, " ", pos);
 }
 
+static void
+json_name(emit_data *data, char *name) {
+    fprintf(OUT, "%*s\"name\" : \"%s\"\n", data->indent, " ", name);
+}
 
 static void
-json_end(struct parser_state *p) {
-    dedent();
-    fprintf(OUT, "%*s}\n", indent, (indent == 0) ? "" : " ");
+json_stmts_start(emit_data *data) {
+    fprintf(OUT, "%*s\"instructions\" : [\n", data->indent, " ");
+    indent(data);
 }
 
 static void
-json_sub_start(struct parser_state *p, char *source, int pos) {
-    fprintf(OUT, "%*s'sub' : {\n", indent, " ");
-    indent();
-    fprintf(OUT, "%*s'source' : '%s',\n", indent, " ", source);
-    fprintf(OUT, "%*s'pos' : %d,\n", indent, " ", pos);
+json_param_start(emit_data *data) {
+    fprintf(OUT, "%*s\"parameters\" : [\n", data->indent, " ");
+    indent(data);
 }
 
 static void
-json_name(struct parser_state *p, char *name) {
-    fprintf(OUT, "%*s'name' : '%s'\n", indent, " ", name);
+json_sub_flag_start(emit_data *data) {
+    fprintf(OUT, "%*s\"subflags\" : [\n", data->indent, " ");
+    indent(data);
 }
 
 static void
-json_stmts_start(struct parser_state *p) {
-    fprintf(OUT, "%*s'instructions' : [\n", indent, " ");
-    indent();
+json_sub_flag_end(emit_data *data) {
+    dedent(data);
+    fprintf(OUT, "%*s]\n", data->indent, " ");
 }
 
 static void
-json_param_start(struct parser_state *p) {
-    fprintf(OUT, "%*s'parameters' : [\n", indent, " ");
-    indent();
+json_sub_flag(emit_data *data, int flag) {
+    fprintf(OUT, "%*s{ \"subflag\" : \"%s\" }\n", data->indent, " ", "TODO");
 }
 
 static void
-json_sub_flag_start(struct parser_state *p) {
-    fprintf(OUT, "%*s'subflags' : [\n", indent, " ");
-    indent();
+json_list_start(emit_data *data) {
+    fprintf(OUT, "%*s[\n", data->indent, " ");
+    indent(data);
 }
 
 static void
-json_sub_flag_end(struct parser_state *p) {
-    dedent();
-    fprintf(OUT, "%*s]\n", indent, " ");
+json_list_end(emit_data *data) {
+    dedent(data);
+    fprintf(OUT, "%*s]\n", data->indent, " ");
 }
 
 static void
-json_sub_flag(struct parser_state *p, int flag) {
-    fprintf(OUT, "%*s{ 'subflag' : '%s' }\n", indent, " ", "TODO");
+json_op_start(emit_data *data, char *op) {
+    fprintf(OUT, "%*s{ \"op\" : \"%s\",\n", data->indent, " ", op);
+    fprintf(OUT, "%*s\"args\" : [\n", data->indent, " ");
+    indent(data);
 }
 
 static void
-json_list_start(struct parser_state *p) {
-    fprintf(OUT, "%*s[\n", indent, " ");
-    indent();
+json_op_end(emit_data *data) {
+    fprintf(OUT, "%*s]\n", data->indent, " ");
+    dedent(data);
+    fprintf(OUT, "%*s}\n", data->indent, " ");
 }
 
 static void
-json_list_end(struct parser_state *p) {
-    dedent();
-    fprintf(OUT, "%*s]\n", indent, " ");
+json_expr(emit_data *data, char *expr) {
+    fprintf(OUT, "%*s{ \"expr\" : \"%s\" }\n", data->indent, " ", expr);
 }
+
 /*
+
+Destructor for emit_data structure
+
+*/
 static void
-json_next(struct parser_state *p) {
+json_destroy(emit_data *data) {
+    free(data);
+    data = NULL;
+}
+
+/*
+static void
+json_next(void) {
     fprintf(OUT, ", ");
 }
 */
@@ -99,7 +143,9 @@
 init_json_vtable(void) {
     pirvtable *vtable = new_pirvtable();
 
+    /* set vtable methods to the appropiate implementation */
     vtable->initialize     = json_init;
+    vtable->destroy        = json_destroy;
     vtable->sub_start      = json_sub_start;
     vtable->sub_end        = json_end;
     vtable->end            = json_end;
@@ -107,13 +153,23 @@
     vtable->stmts_end      = json_list_end;
     vtable->name           = json_name;
     vtable->sub_flag       = json_sub_flag;
-
+    vtable->op_start       = json_op_start;
+    vtable->op_end         = json_op_end;
     vtable->param_start    = json_param_start;
     vtable->param_end      = json_list_end;
     vtable->sub_flag_start = json_sub_flag_start;
     vtable->sub_flag_end   = json_sub_flag_end;
     vtable->list_start     = json_list_start;
     vtable->list_end       = json_list_end;
+    vtable->expression     = json_expr;
+
+
+    vtable->data = (emit_data *)malloc(sizeof(emit_data));
+    if (vtable->data == NULL) {
+        fprintf(stderr, "Failed to allocate memory for vtable data\n");
+        exit(1);
+    }
+    vtable->data->indent = 0;
 
     /* vtable->next_expr   = json_next; */
 

Modified: trunk/compilers/pirc/src/pastout.c
==============================================================================
--- trunk/compilers/pirc/src/pastout.c  (original)
+++ trunk/compilers/pirc/src/pastout.c  Wed Apr  4 01:30:03 2007
@@ -13,19 +13,29 @@
 */
 
 #include "pastout.h"
-#include "pirparser.h"
+#include "pirvtable.h"
 #include <stdio.h>
+#include <stdlib.h>
+#include <malloc.h>
 
 /* keep outputfile possibility easy */
 #define OUT         stderr
 #define INDENT      4
 
-/* this is not threadsafe, fix this later */
-static int indent = 0;
 
-/* for easy indenting/dedenting */
-#define indent()    indent += INDENT
-#define dedent()    indent -= INDENT
+#define indent(D)    D->indent += INDENT
+#define dedent(D)    D->indent -= INDENT
+
+
+/* Private declaration of emit_data.
+ *
+ *
+ */
+typedef struct emit_data {
+    int indent;
+
+} emit_data;
+
 
 /*
 
@@ -41,9 +51,9 @@
 
 */
 static void
-past_init(struct parser_state *p) {
+past_init(struct emit_data *data) {
     fprintf(OUT, "\"past\" => PMC 'PAST::Block'  {\n");
-    indent();
+    indent(data);
 }
 
 /*
@@ -56,11 +66,11 @@
 
 */
 static void
-past_block(struct parser_state *p, char *source, int pos) {
-    fprintf(OUT, "%*sPMC 'PAST::Block'  {\n", indent, " ");
-    indent();
-    fprintf(OUT, "%*s<source> => \"%s\"\n", indent, " ", source);
-    fprintf(OUT, "%*s<pos> => %d\n", indent, " ", pos);
+past_block(struct emit_data *data, char *source, int pos) {
+    fprintf(OUT, "%*sPMC 'PAST::Block'  {\n", data->indent, " ");
+    indent(data);
+    fprintf(OUT, "%*s<source> => \"%s\"\n", data->indent, " ", source);
+    fprintf(OUT, "%*s<pos> => %d\n", data->indent, " ", pos);
 }
 
 /*
@@ -74,12 +84,12 @@
 
 */
 static void
-past_close(struct parser_state *p) {
-    dedent();
+past_close(struct emit_data *data) {
+    dedent(data);
     /* check for indent == 0, if so, then print no space, this is
      * so otherwise a single space is printed.
      */
-    fprintf(OUT, "%*s}\n", indent, (indent == 0) ? "" : " ");
+    fprintf(OUT, "%*s}\n", data->indent, (data->indent == 0) ? "" : " ");
 
 }
 
@@ -93,8 +103,8 @@
 
 */
 static void
-past_name(struct parser_state *p, char *name) {
-    fprintf(OUT, "%*s<name> => \"%s\"\n", indent, " ", name);
+past_name(struct emit_data *data, char *name) {
+    fprintf(OUT, "%*s<name> => \"%s\"\n", data->indent, " ", name);
 }
 
 /*
@@ -107,50 +117,54 @@
 
 */
 static void
-past_stmts(struct parser_state *p) {
-    fprintf(OUT, "%*s[%d] => PMC 'PAST::Stmts'  {\n", indent, " ", 0); /* fix 
array index */
-    indent();
+past_stmts(struct emit_data *data) {
+    fprintf(OUT, "%*s[%d] => PMC 'PAST::Stmts'  {\n", data->indent, " ", 0); 
/* fix array index */
+    indent(data);
 }
 
 
 static void
-past_param(struct parser_state *p) {
-    fprintf(OUT, "%*sFIXTHIS => PMC 'PAST::Var'  {\n", indent, " ");
-    indent();
-    fprintf(OUT, "%*s<scope> => \"parameter\"\n", indent, " ");
+past_param(struct emit_data *data) {
+    fprintf(OUT, "%*sFIXTHIS => PMC 'PAST::Var'  {\n", data->indent, " ");
+    indent(data);
+    fprintf(OUT, "%*s<scope> => \"parameter\"\n", data->indent, " ");
 }
 
 static void
-past_type(struct parser_state *p, char *type) {
-    fprintf(OUT, "%*s<type> => \"%s\"\n", indent, " ", type);
+past_type(struct emit_data *data, char *type) {
+    fprintf(OUT, "%*s<type> => \"%s\"\n", data->indent, " ", type);
 }
 
 static void
-past_subflag(struct parser_state *p, int flag) {
-    /* fprintf(OUT, "%*s<???> => \"%s\"\n", indent, " ", type);
+past_subflag(struct emit_data *data, int flag) {
+    /* fprintf(OUT, "%*s<???> => \"%s\"\n", data->indent, " ", type);
     */
 }
 
 
 static void
-past_op(struct parser_state *p, char *op) {
-    fprintf(OUT, "%*sFIXME => PMC 'PAST::Op'  {\n", indent, " ");
-    indent();
-    fprintf(OUT, "%*s<pirop> => \"%s\"\n", indent, " ", op);
+past_op(struct emit_data *data, char *op) {
+    fprintf(OUT, "%*sFIXME => PMC 'PAST::Op'  {\n", data->indent, " ");
+    indent(data);
+    fprintf(OUT, "%*s<pirop> => \"%s\"\n", data->indent, " ", op);
 
 }
 
 static void
-past_expr(struct parser_state *p, char *expr) {
-    fprintf(OUT, "%*s[%d] => \"%s\"\n", indent, " ", 0, expr); /* fix index */
+past_expr(struct emit_data *data, char *expr) {
+    fprintf(OUT, "%*s[%d] => \"%s\"\n", data->indent, " ", 0, expr); /* fix 
index */
 }
 
 static void
-past_next(struct parser_state *p) {
+past_next(struct emit_data *data) {
     /* increment index */
 }
 
-
+static void
+past_destroy(emit_data *data) {
+    free(data);
+    data = NULL;
+}
 /*
 
 =item init_past_vtable()
@@ -167,6 +181,7 @@
 
     /* override vtable methods */
     vtable->initialize   = past_init;
+    vtable->destroy      = past_destroy;
     vtable->sub_start    = past_block;
     vtable->sub_end      = past_close;
     vtable->name         = past_name;
@@ -182,6 +197,13 @@
     vtable->op_end       = past_close;
     vtable->next_expr    = past_next;
 
+    vtable->data = (emit_data *)malloc(sizeof(emit_data));
+    if (vtable->data == NULL) {
+        fprintf(stderr, "Failed to allocate memory for vtable data\n");
+        exit(1);
+    }
+    vtable->data->indent = 0;
+
     return vtable;
 }
 

Added: trunk/compilers/pirc/src/pbcout.c
==============================================================================
--- (empty file)
+++ trunk/compilers/pirc/src/pbcout.c   Wed Apr  4 01:30:03 2007
@@ -0,0 +1,19 @@
+/*
+
+=head1 NAME
+
+pbcout.c - Back-end for emitting PBC. Not implemented.
+
+=cut
+
+*/
+#include "pbcout.h"
+#include "pirvtable.h"
+
+
+pirvtable *
+init_pbc_vtable(void) {
+    pirvtable *vtable = new_pirvtable();
+
+    return vtable;
+}

Added: trunk/compilers/pirc/src/pbcout.h
==============================================================================
--- (empty file)
+++ trunk/compilers/pirc/src/pbcout.h   Wed Apr  4 01:30:03 2007
@@ -0,0 +1,9 @@
+#ifndef __PBCOUT_H
+#  define __PBCOUT_H
+
+
+
+extern struct pirvtable *init_pbc_vtable(void);
+
+#endif
+

Modified: trunk/compilers/pirc/src/pirlexer.c
==============================================================================
--- trunk/compilers/pirc/src/pirlexer.c (original)
+++ trunk/compilers/pirc/src/pirlexer.c Wed Apr  4 01:30:03 2007
@@ -893,9 +893,12 @@
         file_buffer *tmp = buf; /* store current */
         buf = tmp->prevbuffer; /* get 'prevbuffer' */
         destroy_buffer(tmp);   /* destroy current */
+        tmp = NULL;
     }
     free(lexer->token_chars);
+    lexer->token_chars = NULL;
     free(lexer);
+    lexer = NULL;
 }
 
 

Modified: trunk/compilers/pirc/src/pirmain.c
==============================================================================
--- trunk/compilers/pirc/src/pirmain.c  (original)
+++ trunk/compilers/pirc/src/pirmain.c  Wed Apr  4 01:30:03 2007
@@ -14,6 +14,7 @@
 #include "pastout.h"   /* for PAST output */
 #include "pirvtable.h" /* vtable api      */
 #include "jsonout.h"   /* for json output */
+#include "pbcout.h"    /* for PBC output  */
 
 
 
@@ -33,8 +34,8 @@
         OUTPUT_NONE,    /* do nothing  */
         OUTPUT_PIR,     /* output PIR  */
         OUTPUT_PAST,    /* output PAST */
-        OUTPUT_JSON     /* output JSON */
-/* future: OUTPUT_PBC  for outputting PBC files */
+        OUTPUT_JSON,    /* output JSON */
+        OUTPUT_PBC      /* output PBC  */
 
 } outputtype;
 
@@ -53,13 +54,15 @@
 static void
 print_help(void) {
     fprintf(stderr, "Usage: pirc [options] <file>\n");
-    fprintf(stderr, "\toptions:\n");
+    fprintf(stderr, "\tGeneral:\n");
     fprintf(stderr, "\t-d  debug messages (not implemented yet)\n");
     fprintf(stderr, "\t-h  print this help message\n");
     fprintf(stderr, "\t-v  verbose (not implemented yet)\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, "\n");
 }
 
@@ -110,6 +113,9 @@
             case 'j':
                 output = OUTPUT_JSON;
                 break;
+            case 'b':
+                output = OUTPUT_PBC;
+                break;
             default:
                 fprintf(stderr, "Unknown option: '%c'\n", opt);
                 print_help();
@@ -141,6 +147,9 @@
         case OUTPUT_JSON:
             vtable = init_json_vtable();
             break;
+        case OUTPUT_PBC:
+            vtable = init_pbc_vtable();
+            break;
         default:
             fprintf(stderr, "Unknown output type specified\n");
             exit(1);

Modified: trunk/compilers/pirc/src/pirout.c
==============================================================================
--- trunk/compilers/pirc/src/pirout.c   (original)
+++ trunk/compilers/pirc/src/pirout.c   Wed Apr  4 01:30:03 2007
@@ -1,10 +1,20 @@
 #include "pirout.h"
+#include "pirvtable.h"
 #include "pirlexer.h"
-#include "pirparser.h"
 #include <stdio.h>
 #include <malloc.h>
+#include <malloc.h>
+#include <stdlib.h>
+
 
+/* Private declaration of emit_data.
+ *
+ *
+ */
+typedef struct emit_data {
+    int indent;
 
+} emit_data;
 
 #define OUT stderr
 
@@ -12,124 +22,88 @@
 
 =head1 API
 
-=over 4
-
-=item pirout()
-
-Prints the current token from the specified parser.
-NOT COMPLETE YET, and no fancy output as well...
-
 =cut
 
 */
-static void
-pirout(struct parser_state *p) {
-    struct lexer_state const *l = get_lexer(p);
-    token t = get_token(p);
-
-    /*fprintf(stderr, "pirout:\n");
-    */
-
-    switch (t) {
-        case T_RPAREN:            /* HACK */
-            fprintf(stderr, ")"); /* and print newline as well, nec. for 
macros */
-        case T_NEWLINE:
-            fprintf(stderr, "\n");
-            break;
-        case T_INTEGER_CONSTANT:
-        case T_NUMBER_CONSTANT:
-        case T_STRING_CONSTANT:
-        case T_PARROT_OP:
-        case T_IDENTIFIER:
-        case T_PASM_PREG:
-        case T_PASM_NREG:
-        case T_PASM_IREG:
-        case T_PASM_SREG:
-        case T_PREG:
-        case T_NREG:
-        case T_SREG:
-        case T_IREG:
-            fprintf(stderr, " %s ", get_current_token(l));
-            break;
-        default:
-            fprintf(stderr, "%s ", find_keyword(t));
-            break;
-    }
-
-}
 
 static void
-pir_name(struct parser_state *p, char *name) {
+pir_name(struct emit_data *data, char *name) {
     fprintf(OUT, " %s ", name);
 }
 
 static void
-pir_sub(struct parser_state *p, char *source, int pos) {
+pir_sub(struct emit_data *data, char *source, int pos) {
     fprintf(OUT, "#character position %d\n", pos);
     fprintf(OUT, ".sub");
 }
 
 static void
-pir_end(struct parser_state *p) {
+pir_end(struct emit_data *data) {
     fprintf(OUT, ".end\n");
 }
 
 static void
-pir_newline(struct parser_state *p) {
+pir_newline(struct emit_data *data) {
     fprintf(OUT, "\n");
 }
 
 static void
-pir_param(struct parser_state *p) {
+pir_param(struct emit_data *data) {
     fprintf(OUT, "  .param ");
 }
 
 static void
-pir_type(struct parser_state *p, char *type) {
+pir_type(struct emit_data *data, char *type) {
     fprintf(OUT, "%s", type);
 }
 
 static void
-pir_sub_flag(struct parser_state *p, int flag) {
+pir_sub_flag(struct emit_data *data, int flag) {
     fprintf(OUT, "%s ", find_keyword(flag));
 }
 
 static void
-pir_expr(struct parser_state *p, char *expr) {
+pir_expr(struct emit_data *data, char *expr) {
     fprintf(OUT, "%s ", expr);
 }
 
 static void
-pir_op(struct parser_state *p, char *op) {
+pir_op(struct emit_data *data, char *op) {
     fprintf(OUT, "  %s ", op);
 }
 
 static void
-pir_comma(struct parser_state *p) {
+pir_comma(struct emit_data *data) {
     fprintf(OUT, ", ");
 }
 
 static void
-pir_list_start(struct parser_state *p) {
+pir_list_start(struct emit_data *data) {
     fprintf(OUT, "(");
 }
 
 static void
-pir_list_end(struct parser_state *p) {
+pir_list_end(struct emit_data *data) {
     fprintf(OUT, ")");
 }
 
 static void
-pir_sub_flag_start(struct parser_state *p) {
+pir_sub_flag_start(struct emit_data *data) {
     fprintf(OUT, "");
 }
 
 
 static void
-pir_sub_flag_end(struct parser_state *p) {
+pir_sub_flag_end(struct emit_data *data) {
     fprintf(OUT, "\n");
 }
 
+static void
+pir_destroy(emit_data *data) {
+    free(data);
+    data = NULL;
+}
+
 /*
 
 =item init_pir_vtable()
@@ -140,11 +114,12 @@
 =cut
 
 */
-pirvtable *
+struct pirvtable *
 init_pir_vtable(void) {
     pirvtable *vtable = new_pirvtable();
 
     /* override the methods that are needed for PIR output */
+    vtable->destroy        = pir_destroy;
     vtable->sub_start      = pir_sub;
     vtable->sub_end        = pir_end;
     vtable->name           = pir_name;
@@ -162,7 +137,11 @@
     vtable->sub_flag_start = pir_sub_flag_start;
     vtable->sub_flag_end   = pir_sub_flag_end;
 
-
+    vtable->data = (emit_data *)malloc(sizeof(emit_data));
+    if (vtable->data == NULL) {
+        fprintf(stderr, "Failed to allocate memory for vtable data\n");
+        exit(1);
+    }
     return vtable;
 }
 

Modified: trunk/compilers/pirc/src/pirparser.c
==============================================================================
--- trunk/compilers/pirc/src/pirparser.c        (original)
+++ trunk/compilers/pirc/src/pirparser.c        Wed Apr  4 01:30:03 2007
@@ -118,8 +118,8 @@
 */
 void
 exit_parser(parser_state *p) {
-    destroy_lexer(p->lexer);
-    p->lexer = NULL;
+    emit_destroy(p); /* call destructor for emit_data */
+    destroy_lexer(p->lexer); /* destroy the lexer */
     free(p);
     p = NULL;
     exit(0);
@@ -1098,19 +1098,28 @@
 const_definition(parser_state *p) {
     switch (p->curtoken) {
         case T_INT:
+            emit_type(p, "int");
             next(p);
             match(p, T_IDENTIFIER);
             match(p, T_ASSIGN);
             match(p, T_INTEGER_CONSTANT);
             break;
         case T_NUM:
+            emit_type(p, "num");
             next(p);
             match(p, T_IDENTIFIER);
             match(p, T_ASSIGN);
             match(p, T_NUMBER_CONSTANT);
             break;
-        case T_STRING: /* both string and PMC have strings as constants */
-        case T_PMC:
+        case T_STRING:
+            emit_type(p, "string");
+            next(p);
+            match(p, T_IDENTIFIER);
+            match(p, T_ASSIGN);
+            match(p, T_STRING_CONSTANT);
+            break;
+        case T_PMC: /* both string and PMC have strings as constants */
+            emit_type(p, "pmc");
             next(p);
             match(p, T_IDENTIFIER);
             match(p, T_ASSIGN);
@@ -1172,6 +1181,7 @@
 static void
 param_flags(parser_state *p) {
     int ok = 1;
+    emit_list_start(p);
     while (ok) {
         /* if the current token is a flag, parse it */
         switch (p->curtoken) {
@@ -1202,6 +1212,7 @@
                 break;
         }
     }
+    emit_list_end(p);
 }
 
 /*
@@ -1575,6 +1586,9 @@
 static void
 instructions(parser_state *p) {
     int ok = 1;
+
+    emit_stmts_start(p); /* open stmts block */
+
     while (ok) {
 
         /* instruction -> {LABEL:} instr */
@@ -1646,7 +1660,7 @@
             case T_PCC_BEGIN_YIELD:
                 long_yield_statement(p);
                 break;
-            case T_LPAREN: /* '(' target_list ')' '=' subcall */
+            case T_LPAREN: /* target_list '=' subcall */
                 multi_result_invocation(p);
                 break;
             case T_PARROT_OP: /* parrot instructions */
@@ -1670,6 +1684,7 @@
                 break;
         }
     }
+    emit_stmts_end(p);  /* close stmts block */
 }
 
 
@@ -1911,9 +1926,7 @@
     sub_flags(p);
     match(p, T_NEWLINE);
     parameters(p);
-    emit_stmts_start(p); /* open stmts block */
     instructions(p);
-    emit_stmts_end(p);  /* close stmts block */
     match(p, T_END);
 
     /* call emit method to close sub*/

Modified: trunk/compilers/pirc/src/pirvtable.c
==============================================================================
--- trunk/compilers/pirc/src/pirvtable.c        (original)
+++ trunk/compilers/pirc/src/pirvtable.c        Wed Apr  4 01:30:03 2007
@@ -15,7 +15,6 @@
 
 */
 #include "pirvtable.h"
-#include "pirparser.h"
 #include <stdlib.h>
 #include <stdio.h>
 #include <malloc.h>
@@ -39,7 +38,7 @@
 
 */
 static void
-not_implemented(struct parser_state *p, ...) {
+not_implemented(struct emit_data *data, ...) {
     /* do nothing */
 }
 
@@ -71,6 +70,7 @@
      * only needs to override the entries that are interesting for that module.
      */
     vtable->initialize     = not_implemented;
+    vtable->destroy        = not_implemented; /* destructor; highly 
recommended to implement! */
     vtable->name           = not_implemented;
     vtable->sub_start      = not_implemented;
     vtable->sub_end        = not_implemented;
@@ -90,11 +90,35 @@
     vtable->sub_flag_start = not_implemented;
     vtable->sub_flag_end   = not_implemented;
 
+    /* set data to NULL, it's initialized in the backend module */
+    vtable->data           = NULL;
+
     return vtable;
 }
 
 /*
 
+=item destroy_pirvtable()
+
+Destructor for the pirvtable. It first calls the custome destructor
+of the emit_data structure, which is private to each of the back-ends
+(so we don't know what memory is allocated for that, only the back-ends
+know).
+
+=cut
+
+*/
+void
+destroy_pirvtable(pirvtable *vtable) {
+    /* call custom destructor of the backend */
+    (*vtable->destroy)(vtable->data);
+    /* free the vtable memory */
+    free(vtable);
+    vtable = NULL;
+}
+
+/*
+
 =back
 
 =cut

Modified: trunk/compilers/pirc/src/pirvtable.h
==============================================================================
--- trunk/compilers/pirc/src/pirvtable.h        (original)
+++ trunk/compilers/pirc/src/pirvtable.h        Wed Apr  4 01:30:03 2007
@@ -3,56 +3,65 @@
 
 struct parser_state;
 
+struct emit_data;
+
 /* vtable that contains function pointers for emit
- * routines.
+ * routines, and a pointer to some data. The definition of the
+ * 'emit_data' structure is left to each back-end separately.
  */
 typedef struct pirvtable {
-    void (* initialize)     (struct parser_state *p);
-    void (* sub_start)      (struct parser_state *p, char *source, int pos);
-    void (* sub_end)        (struct parser_state *p);
-    void (* name)           (struct parser_state *p, char *name);
-    void (* stmts_start)    (struct parser_state *p);
-    void (* stmts_end)      (struct parser_state *p);
-    void (* end)            (struct parser_state *p);
-    void (* param_start)    (struct parser_state *p);
-    void (* param_end)      (struct parser_state *p);
-    void (* type)           (struct parser_state *p, char *type);
-    void (* sub_flag)       (struct parser_state *p, int flag);
-    void (* op_start)       (struct parser_state *p, char *op);
-    void (* op_end)         (struct parser_state *p);
-    void (* expression)     (struct parser_state *p, char *expr);
-    void (* next_expr)      (struct parser_state *p);
-    void (* list_start)     (struct parser_state *p);
-    void (* list_end)       (struct parser_state *p);
-    void (* sub_flag_start) (struct parser_state *p);
-    void (* sub_flag_end)   (struct parser_state *p);
+    struct emit_data        *data;
+    void (* initialize)     (struct emit_data *);
+    void (* destroy)        (struct emit_data *);
+    void (* sub_start)      (struct emit_data *, char *source, int pos);
+    void (* sub_end)        (struct emit_data *);
+    void (* name)           (struct emit_data *, char *name);
+    void (* stmts_start)    (struct emit_data *);
+    void (* stmts_end)      (struct emit_data *);
+    void (* end)            (struct emit_data *);
+    void (* param_start)    (struct emit_data *);
+    void (* param_end)      (struct emit_data *);
+    void (* type)           (struct emit_data *, char *type);
+    void (* sub_flag)       (struct emit_data *, int flag);
+    void (* op_start)       (struct emit_data *, char *op);
+    void (* op_end)         (struct emit_data *);
+    void (* expression)     (struct emit_data *, char *expr);
+    void (* next_expr)      (struct emit_data *);
+    void (* list_start)     (struct emit_data *);
+    void (* list_end)       (struct emit_data *);
+    void (* sub_flag_start) (struct emit_data *);
+    void (* sub_flag_end)   (struct emit_data *);
 
 
 } pirvtable;
 
 /* #defines for cleaner invocation syntax */
-#  define emit_init(P)              (*P->vtable->initialize)    (P)
-#  define emit_sub_start(P,S,L)     (*P->vtable->sub_start)     (P,S,L)
-#  define emit_sub_end(P)           (*P->vtable->sub_end)       (P)
-#  define emit_name(P,N)            (*P->vtable->name)          (P,N)
-#  define emit_stmts_start(P)       (*P->vtable->stmts_start)   (P)
-#  define emit_stmts_end(P)         (*P->vtable->stmts_end)     (P)
-#  define emit_end(P)               (*P->vtable->end)           (P)
-#  define emit_param_start(P)       (*P->vtable->param_start)   (P)
-#  define emit_param_end(P)         (*P->vtable->param_end)     (P)
-#  define emit_type(P,T)            (*P->vtable->type)          (P,T)
-#  define emit_sub_flag(P,F)        (*p->vtable->sub_flag)      (P,F)
-#  define emit_sub_flag_start(P)    (*P->vtable->sub_flag_start)(P)
-#  define emit_sub_flag_end(P)      (*P->vtable->sub_flag_end)  (P)
-#  define emit_op_start(P,O)        (*P->vtable->op_start)      (P,O)
-#  define emit_op_end(P)            (*P->vtable->op_end)        (P)
-#  define emit_expr(P,E)            (*P->vtable->expression)    (P,E)
-#  define emit_next_expr(P)         (*P->vtable->next_expr)     (P)
-#  define emit_list_start(P)        (*P->vtable->list_start)    (P)
-#  define emit_list_end(P)          (*P->vtable->list_end)      (P)
+#  define emit_init(P)             (*P->vtable->initialize)    
(P->vtable->data)
+#  define emit_destroy(P)          (*P->vtable->destroy)       
(P->vtable->data)
+#  define emit_sub_start(P,S,L)    (*P->vtable->sub_start)     
(P->vtable->data, S,L)
+#  define emit_sub_end(P)          (*P->vtable->sub_end)       
(P->vtable->data)
+#  define emit_name(P,N)           (*P->vtable->name)          
(P->vtable->data, N)
+#  define emit_stmts_start(P)      (*P->vtable->stmts_start)   
(P->vtable->data)
+#  define emit_stmts_end(P)        (*P->vtable->stmts_end)     
(P->vtable->data)
+#  define emit_end(P)              (*P->vtable->end)           
(P->vtable->data)
+#  define emit_param_start(P)      (*P->vtable->param_start)   
(P->vtable->data)
+#  define emit_param_end(P)        (*P->vtable->param_end)     
(P->vtable->data)
+#  define emit_type(P,T)           (*P->vtable->type)          
(P->vtable->data, T)
+#  define emit_sub_flag(P,F)       (*p->vtable->sub_flag)      
(P->vtable->data, F)
+#  define emit_sub_flag_start(P)   
(*P->vtable->sub_flag_start)(P->vtable->data)
+#  define emit_sub_flag_end(P)     (*P->vtable->sub_flag_end)  
(P->vtable->data)
+#  define emit_op_start(P,O)       (*P->vtable->op_start)      
(P->vtable->data, O)
+#  define emit_op_end(P)           (*P->vtable->op_end)        
(P->vtable->data)
+#  define emit_expr(P,E)           (*P->vtable->expression)    
(P->vtable->data, E)
+#  define emit_next_expr(P)        (*P->vtable->next_expr)     
(P->vtable->data)
+#  define emit_list_start(P)       (*P->vtable->list_start)    
(P->vtable->data)
+#  define emit_list_end(P)         (*P->vtable->list_end)      
(P->vtable->data)
 
 extern pirvtable *new_pirvtable(void);
 
+extern void destroy_pirvtable(pirvtable *vtable);
+
+
 #endif
 
 /*

Modified: trunk/config/gen/makefiles/pirc.in
==============================================================================
--- trunk/config/gen/makefiles/pirc.in  (original)
+++ trunk/config/gen/makefiles/pirc.in  Wed Apr  4 01:30:03 2007
@@ -43,8 +43,8 @@
   src/jsonout.h
 
 
-pirc: pirmain$(O) pirparser$(O)        pirlexer$(O) pirout$(O) pastout$(O)     
pirvtable$(O) jsonout$(O)
-       $(CC) -o pirc$(EXE)     pirmain$(O)     pirparser$(O) pirlexer$(O) 
pirout$(O) pastout$(O) pirvtable$(O) jsonout$(O)
+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)
 
 pirmain$(O): src/pirmain.c src/pirparser.h
        $(CC) $(CFLAGS) -c src/pirmain.c
@@ -61,6 +61,9 @@
 pastout$(O): src/pastout.c src/pastout.h
        $(CC) $(CFLAGS) -c src/pastout.c
 
+pbcout$(O): src/pbcout.c src/pbcout.h
+       $(CC) $(CFLAGS) -c src/pbcout.c
+
 pirvtable$(O): src/pirvtable.c src/pirvtable.h
        $(CC) $(CFLAGS) -c src/pirvtable.c
 

Reply via email to