Author: kjs
Date: Thu Apr 12 09:03:54 2007
New Revision: 18167

Modified:
   trunk/compilers/pirc/src/jsonout.c
   trunk/compilers/pirc/src/pastout.c
   trunk/compilers/pirc/src/pirlexer.c
   trunk/compilers/pirc/src/pirlexer.h
   trunk/compilers/pirc/src/pirout.c
   trunk/compilers/pirc/src/pirparser.c
   trunk/compilers/pirc/src/pirutil.c
   trunk/compilers/pirc/src/pirvtable.c
   trunk/compilers/pirc/src/pirvtable.h

Log:
compilers/pirc:
* added some more back-end methods
* fix for json output for parameters

Modified: trunk/compilers/pirc/src/jsonout.c
==============================================================================
--- trunk/compilers/pirc/src/jsonout.c  (original)
+++ trunk/compilers/pirc/src/jsonout.c  Thu Apr 12 09:03:54 2007
@@ -193,6 +193,8 @@
 static void
 json_name(emit_data *data, char *name) {
     fprintf(data->file, "%*s{ \"name\" : \"%s\" }\n", data->indent, " ", name);
+    data->need_comma = 1;
+
 }
 
 static void
@@ -206,7 +208,7 @@
 
 static void
 json_param_start(emit_data *data) {
-    fprintf(data->file, "%*s\"parameters\" :\n", data->indent, " ");
+    fprintf(data->file, "%*s{ \"parameters\" :\n", data->indent, " ");
     indent(data);
     fprintf(data->file, "%*s[\n", data->indent, " ");
     indent(data);
@@ -215,6 +217,7 @@
 
 static void
 json_sub_flag_start(emit_data *data) {
+    if (data->need_comma) print_comma(data);
     fprintf(data->file, "%*s{ \"subflags\" :\n", data->indent, " ");
     indent(data);
     fprintf(data->file, "%*s[\n", data->indent, " ");
@@ -384,7 +387,7 @@
     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->param_end        = json_stmts_end;
     vtable->sub_flag_start   = json_sub_flag_start;
     vtable->sub_flag_end     = json_sub_flag_end;
     vtable->list_start       = json_list_start;

Modified: trunk/compilers/pirc/src/pastout.c
==============================================================================
--- trunk/compilers/pirc/src/pastout.c  (original)
+++ trunk/compilers/pirc/src/pastout.c  Thu Apr 12 09:03:54 2007
@@ -35,6 +35,7 @@
 */
 typedef struct emit_data {
     int indent;
+    int index;
     char *outputfile;
     FILE *file;
 
@@ -148,7 +149,7 @@
 */
 static void
 past_stmts(struct emit_data *data) {
-    fprintf(data->file, "%*s[%d] => PMC 'PAST::Stmts'  {\n", data->indent, " 
", 0); /* fix array index */
+    fprintf(data->file, "%*s[%d] => PMC 'PAST::Stmts'  {\n", data->indent, " 
", data->index++);
     indent(data);
 }
 
@@ -226,14 +227,14 @@
 */
 static void
 past_expr(struct emit_data *data, char *expr) {
-    fprintf(data->file, "%*s[%d] => \"%s\"\n", data->indent, " ", 0, expr); /* 
fix index */
+    fprintf(data->file, "%*s[%d] => \"%s\"\n", data->indent, " ", 
data->index++, expr); /* fix index */
 }
 
 /*
 
 =item past_destroy()
 
-
+Destructor, close the outputfile if any, and free the emit_data structure.
 
 =cut
 
@@ -285,6 +286,7 @@
     }
     vtable->data->indent = 0;
     vtable->data->outputfile = outputfile;
+    vtable->data->index = 0;
 
     return vtable;
 }

Modified: trunk/compilers/pirc/src/pirlexer.c
==============================================================================
--- trunk/compilers/pirc/src/pirlexer.c (original)
+++ trunk/compilers/pirc/src/pirlexer.c Thu Apr 12 09:03:54 2007
@@ -1045,14 +1045,11 @@
         if (isspace(c) && (c != '\n')) continue;
 
         /* skip pod */
-        /* FIX
-        while ((c == '=') && is_start_of_line(lexer->curfile)) {
-             c = read_char(lexer->curfile);
-        }
-        XXX */
+
+        /* FIX PARSING OF POD; kinda hard :-( */
+
 
 
-        /* skip comments */
 
 /*
 
@@ -1067,6 +1064,7 @@
 =cut
 
 */
+        /* skip comments */
         if (c == '#') {
             /* eat comments up to but not including newline */
             do {

Modified: trunk/compilers/pirc/src/pirlexer.h
==============================================================================
--- trunk/compilers/pirc/src/pirlexer.h (original)
+++ trunk/compilers/pirc/src/pirlexer.h Thu Apr 12 09:03:54 2007
@@ -143,9 +143,9 @@
     T_HEREDOC_STRING,                   /* "heredoc string",          */
     T_PARROT_OP,                        /* "parrot op",               */
     T_UNICODE,                          /* "unicode:",                */
-    T_ASCII,                                                   /* "ascii:",    
              */
-    T_BINARY,                                              /* "binary:",       
          */
-    T_ISO_8859_1                                               /* 
"iso-8859-1:"              */
+    T_ASCII,                            /* "ascii:",                  */
+    T_BINARY,                           /* "binary:",                 */
+    T_ISO_8859_1                        /* "iso-8859-1:"              */
     /* NULL                                                           */
 } token;
 

Modified: trunk/compilers/pirc/src/pirout.c
==============================================================================
--- trunk/compilers/pirc/src/pirout.c   (original)
+++ trunk/compilers/pirc/src/pirout.c   Thu Apr 12 09:03:54 2007
@@ -15,6 +15,13 @@
 #include <stdlib.h>
 
 
+
+typedef struct target {
+    char *name;
+    struct target *next;
+
+} target;
+
 /* Private declaration of emit_data.
  *
  *
@@ -22,14 +29,53 @@
 typedef struct emit_data {
     char *outputfile;
     FILE *file;
+    int need_comma;
+    target *targets;
 
 } emit_data;
 
 
+#define print_comma(D)  fprintf(data->file, ", ")
+
+
+
 
 
 /*
 
+=over 4
+
+=item new_target()
+
+=cut
+
+*/
+static target *
+new_target(char *name) {
+    target *t = (target *)malloc(sizeof(target));
+    t->name = clone_string(name);
+    t->next = NULL;
+    return t;
+}
+
+/*
+
+=item add_target()
+
+=cut
+
+=back
+
+*/
+
+static void
+add_target(emit_data *data, target *t) {
+    t->next = data->targets;
+    data->targets = t;
+}
+
+/*
+
 =head1 API
 
 =cut
@@ -38,17 +84,21 @@
 
 static void
 pir_name(struct emit_data *data, char *name) {
-    fprintf(data->file, " %s ", name);
+    if (data->need_comma) print_comma(data);
+    fprintf(data->file, "%s", name);
+    data->need_comma = 1;
 }
 
 static void
 pir_sub(struct emit_data *data) {
-    fprintf(data->file, ".sub");
+    fprintf(data->file, "\n.sub ");
+    data->need_comma = 0;
 }
 
 static void
 pir_end(struct emit_data *data) {
     fprintf(data->file, ".end\n");
+    data->need_comma = 0;
 }
 
 static void
@@ -63,7 +113,8 @@
 
 static void
 pir_type(struct emit_data *data, char *type) {
-    fprintf(data->file, "%s", type);
+    fprintf(data->file, "%s ", type);
+    data->need_comma = 0;
 }
 
 static void
@@ -79,17 +130,20 @@
 static void
 pir_op(struct emit_data *data, char *op) {
     fprintf(data->file, "  %s ", op);
+    data->need_comma = 0;
 }
 
 
 static void
 pir_list_start(struct emit_data *data) {
     fprintf(data->file, "(");
+    data->need_comma = 0;
 }
 
 static void
 pir_list_end(struct emit_data *data) {
     fprintf(data->file, ")");
+    data->need_comma = 0;
 }
 
 static void
@@ -116,6 +170,11 @@
 }
 
 static void
+pir_target(emit_data *data, char *target) {
+    add_target(data, new_target(target));
+}
+
+static void
 pir_begin_return(emit_data *data) {
     fprintf(data->file, " ");
 }
@@ -126,6 +185,52 @@
     else data->file = stdout;
 }
 
+/* print the list of targets stored in emit_data. This is done recursively,
+ * and from the end of the list to the front. This is because the items are
+ * added at the front, and the order need to be restored. This also allows
+ * for freeing any resources.
+ */
+static void
+print_target(emit_data *data, target *t) {
+    if (t->next) print_target(data, t->next);
+    fprintf(data->file, "%s", t->name);
+
+    /* name was a cloned string, free it again! */
+    /*
+    free(t->name);
+    t->name = NULL;
+    */
+
+}
+
+static void
+pir_assign(emit_data *data) {
+    target *t = data->targets;
+    /* XXX does not work correctly yet.
+    print_target(data, t);
+    */
+    fprintf(data->file, " = ");
+}
+
+static void
+pir_assign_start(emit_data *data) {
+    fprintf(data->file, "  ");
+}
+
+static void
+pir_assign_end(emit_data *data) {
+    fprintf(data->file, "\n");
+}
+
+static void
+pir_comp_op(emit_data *data, char *op) {
+    fprintf(data->file, " %s ", op);
+}
+
+static void
+pir_bin_op(emit_data *data, char *op) {
+    fprintf(data->file, " %s ", op);
+}
 
 /*
 
@@ -162,6 +267,13 @@
     vtable->list_end       = pir_list_end;
     vtable->sub_flag_start = pir_sub_flag_start;
     vtable->sub_flag_end   = pir_sub_flag_end;
+    vtable->assign         = pir_assign;
+    vtable->assign_start   = pir_assign_start;
+    vtable->assign_end     = pir_assign_end;
+    vtable->target         = pir_target;
+
+    vtable->binary_op      = pir_bin_op;
+    vtable->comparison_op  = pir_comp_op;
 
     vtable->data = (emit_data *)malloc(sizeof(emit_data));
     if (vtable->data == NULL) {
@@ -170,6 +282,7 @@
     }
 
     vtable->data->outputfile = outputfile;
+    vtable->data->targets = NULL;
 
     return vtable;
 }

Modified: trunk/compilers/pirc/src/pirparser.c
==============================================================================
--- trunk/compilers/pirc/src/pirparser.c        (original)
+++ trunk/compilers/pirc/src/pirparser.c        Thu Apr 12 09:03:54 2007
@@ -566,6 +566,9 @@
 static void
 arg_flags(parser_state *p) {
     int ok = 1;
+
+    emit_list_start(p);
+
     while (ok) {
         switch (p->curtoken) {
             case T_FLAT_FLAG:
@@ -590,6 +593,8 @@
                 break;
         }
     }
+
+    emit_list_end(p);
 }
 
 
@@ -764,6 +769,7 @@
         case T_LOG_RSHIFT:
         case T_LSHIFT:
         case T_CONCAT: /* yeah I know, it's not arithmetic */
+            emit_binary_op(p, find_keyword(p->curtoken));
             next(p);
             expression(p);
             break;
@@ -795,16 +801,8 @@
         /* XXX for now, just skip ANYTHING until a newline. This is so we can 
handle
          * "delete obj[bla] etc.
          */
+        emit_expr(p, get_current_token(p->lexer));
         next(p);
-
-        /*
-        expression(p);
-
-        if (p->curtoken == T_COMMA) {
-            next(p);
-        }
-        else break;
-        */
     }
 
     emit_op_end(p);
@@ -833,7 +831,9 @@
 */
 static void
 assignment(parser_state *p) {
+    emit_assign_start(p); /* should this be here?? */
     match(p, T_ASSIGN);
+    emit_assign(p);
 
     switch (p->curtoken) {
         case T_NOT:
@@ -915,6 +915,7 @@
             arith_expression(p);
             break;
     }
+    emit_assign_end(p);
 }
 
 
@@ -1027,7 +1028,7 @@
 */
 static void
 local_id_list(parser_state *p) {
-    emit_expr(p, get_current_token(p->lexer));
+    emit_name(p, get_current_token(p->lexer));
     match(p, T_IDENTIFIER);
 
     /* process the flag, if any */
@@ -1038,7 +1039,7 @@
     while (p->curtoken == T_COMMA) {
         next(p); /* skip comma */
 
-        emit_expr(p, get_current_token(p->lexer));
+        emit_name(p, get_current_token(p->lexer));
         match(p, T_IDENTIFIER);
 
         /* parse optional :unique_reg flag */
@@ -1141,6 +1142,7 @@
     switch (p->curtoken) { /* optional */
         case T_GE: case T_GT: case T_EQ:
         case T_NE: case T_LT: case T_LE:
+            emit_comparison_op(p, find_keyword(p->curtoken));
             next(p); /* skip comparison op */
             expression(p);
             break;
@@ -1530,7 +1532,6 @@
 
     switch (p->curtoken) {
         case T_ASSIGN:   /* target '=' expression */
-
             assignment(p);
             break;
         case T_PLUS_ASSIGN: /* target '+=' simple_expr '\n' (and '-=' etc.) */

Modified: trunk/compilers/pirc/src/pirutil.c
==============================================================================
--- trunk/compilers/pirc/src/pirutil.c  (original)
+++ trunk/compilers/pirc/src/pirutil.c  Thu Apr 12 09:03:54 2007
@@ -98,7 +98,7 @@
 
 
 /* Array holding all parrot ops */
-static char *parrot_ops[] = {
+static char const *parrot_ops[] = {
         "yield",
         "xor",
         "warningson",

Modified: trunk/compilers/pirc/src/pirvtable.c
==============================================================================
--- trunk/compilers/pirc/src/pirvtable.c        (original)
+++ trunk/compilers/pirc/src/pirvtable.c        Thu Apr 12 09:03:54 2007
@@ -98,6 +98,12 @@
     vtable->invokable        = not_implemented;
     vtable->invocation_start = not_implemented;
     vtable->invocation_end   = not_implemented;
+    vtable->assign_start     = not_implemented;
+    vtable->assign_end       = not_implemented;
+    vtable->assign           = not_implemented;
+    vtable->comparison_op    = not_implemented;
+    vtable->binary_op        = not_implemented;
+
 
     /* set data to NULL, it's initialized in the backend module */
     vtable->data = NULL;

Modified: trunk/compilers/pirc/src/pirvtable.h
==============================================================================
--- trunk/compilers/pirc/src/pirvtable.h        (original)
+++ trunk/compilers/pirc/src/pirvtable.h        Thu Apr 12 09:03:54 2007
@@ -4,7 +4,12 @@
 /* predeclaration; the actual definition is left to the back-end(s) */
 struct emit_data;
 
+/* NOTE: when adding a vtable method:
+ 1. add it to the pirvtable structure;
+ 2. add an 'emit_' macro below;
+ 3. initialize the new method to "not_implemented", in pirvtable.c;
 
+*/
 
 /* vtable that contains function pointers for emit
  * routines, and a pointer to some data. The definition of the
@@ -56,6 +61,14 @@
     void (* invocation_start)(struct emit_data *data);
     void (* invocation_end)  (struct emit_data *data);
 
+    /* assignments */
+    void (* assign_start)    (struct emit_data *data);
+    void (* assign)          (struct emit_data *data);
+    void (* assign_end)      (struct emit_data *data);
+
+    void (* binary_op)       (struct emit_data *data, char *op);
+    void (* comparison_op)   (struct emit_data *data, char *op);
+
     /* finalizer (not destructor, that only should be called to free resources 
*/
     void (* end)             (struct emit_data *data);
 
@@ -99,6 +112,13 @@
 #  define emit_invocation_start(P) (*P->vtable->invocation_start) 
(P->vtable->data)
 #  define emit_invocation_end(P)   (*P->vtable->invocation_end)   
(P->vtable->data)
 
+#  define emit_assign_start(P)     (*P->vtable->assign_start)     
(P->vtable->data)
+#  define emit_assign_end(P)       (*P->vtable->assign_end)       
(P->vtable->data)
+#  define emit_assign(P)           (*P->vtable->assign)           
(P->vtable->data)
+
+#  define emit_comparison_op(P,O)  (*P->vtable->comparison_op)    
(P->vtable->data, O)
+#  define emit_binary_op(P,O)      (*P->vtable->binary_op)        
(P->vtable->data, O)
+
 /* constructor */
 extern pirvtable *new_pirvtable(void);
 

Reply via email to