Author: kjs
Date: Tue Apr  3 09:28:52 2007
New Revision: 17958

Added:
   trunk/compilers/pirc/src/jsonout.c
   trunk/compilers/pirc/src/jsonout.h
Modified:
   trunk/compilers/pirc/src/pirlexer.c
   trunk/compilers/pirc/src/pirmain.c
   trunk/compilers/pirc/src/pirparser.c
   trunk/config/gen/makefiles/pirc.in

Log:
compilers/pirc:
* add option and start of impl. of json output.

Added: trunk/compilers/pirc/src/jsonout.c
==============================================================================
--- (empty file)
+++ trunk/compilers/pirc/src/jsonout.c  Tue Apr  3 09:28:52 2007
@@ -0,0 +1,45 @@
+/*
+
+=head1 NAME
+
+jsonout.c - JSON backend for PIRC
+
+=cut
+
+*/
+#include "jsonout.h"
+#include "pirvtable.h"
+#include "pirparser.h"
+#include <stdio.h>
+
+#define OUT  stderr
+
+static void
+json_init(struct parser_state *p) {
+    fprintf(OUT, "{\n");
+}
+
+
+static void
+json_end(struct parser_state *p) {
+    fprintf(OUT, "}\n");
+}
+
+
+static void
+json_sub_start(struct parser_state *p, char *name, int pos) {
+
+}
+
+
+pirvtable *
+init_json_vtable(void) {
+    pirvtable *vtable = new_pirvtable();
+
+    vtable->initialize = json_init;
+    vtable->sub_start  = json_sub_start;
+    vtable->end        = json_end;
+
+    return vtable;
+}
+

Added: trunk/compilers/pirc/src/jsonout.h
==============================================================================
--- (empty file)
+++ trunk/compilers/pirc/src/jsonout.h  Tue Apr  3 09:28:52 2007
@@ -0,0 +1,9 @@
+#ifndef __JSONOUT_H
+#  define __JSONOUT_H
+
+/* predeclare */
+struct pirvtable;
+
+extern struct pirvtable *init_json_vtable(void);
+
+#endif

Modified: trunk/compilers/pirc/src/pirlexer.c
==============================================================================
--- trunk/compilers/pirc/src/pirlexer.c (original)
+++ trunk/compilers/pirc/src/pirlexer.c Tue Apr  3 09:28:52 2007
@@ -14,10 +14,6 @@
 
 =item *
 
-Remove limit of 128 characters for identifier length
-
-=item *
-
 Optimize small functions (using #define to inline) and optimize by 'smarter'
 implementation (where appropiate). I'm doing a lot of stuff in read_char(),
 which might slow down things.
@@ -54,7 +50,8 @@
 #define EOF_MARKER          -1
 /* number of characters being displayed in syntax errors */
 #define ERROR_CONTEXT_SIZE  30
-
+/* max. token length is 128 for now. */
+#define MAX_ID_LENGTH       128
 
 /*
 
@@ -848,6 +845,8 @@
     return t; /* return either T_ENDM or T_EOF */
 }
 
+
+
 /*
 
 =item new_lexer()
@@ -865,8 +864,8 @@
         exit(1);
     }
 
-    /* max. token length is 128 for now. XXX this should be fixed later */
-    lexer->token_chars = (char *)calloc(128, sizeof(char));
+    
+    lexer->token_chars = (char *)calloc(MAX_ID_LENGTH, sizeof(char));
     lexer->charptr = lexer->token_chars;
     lexer->curfile = NULL;
 

Modified: trunk/compilers/pirc/src/pirmain.c
==============================================================================
--- trunk/compilers/pirc/src/pirmain.c  (original)
+++ trunk/compilers/pirc/src/pirmain.c  Tue Apr  3 09:28:52 2007
@@ -13,6 +13,7 @@
 #include "pirout.h"    /* for PIR output  */
 #include "pastout.h"   /* for PAST output */
 #include "pirvtable.h" /* vtable api      */
+#include "jsonout.h"   /* for json output */
 
 
 
@@ -31,7 +32,8 @@
 typedef enum outputtypes {
         OUTPUT_NONE,    /* do nothing  */
         OUTPUT_PIR,     /* output PIR  */
-        OUTPUT_PAST     /* output PAST */
+        OUTPUT_PAST,    /* output PAST */
+        OUTPUT_JSON     /* output JSON */
 /* future: OUTPUT_PBC  for outputting PBC files */
 
 } outputtype;
@@ -57,6 +59,7 @@
     fprintf(stderr, "\t-v  verbose (not implemented yet)\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, "\n");
 }
 
@@ -104,6 +107,9 @@
             case 'r':
                 output = OUTPUT_PIR;
                 break;
+            case 'j':
+                output = OUTPUT_JSON;
+                break;
             default:
                 fprintf(stderr, "Unknown option: '%c'\n", opt);
                 print_help();
@@ -116,7 +122,7 @@
 
 
     if (argv[0] == NULL) {
-        fprintf(stderr, "No file specified\n");
+        fprintf(stderr, "No file specified. Try -h for help.\n");
         exit(1);
     }
 
@@ -132,6 +138,9 @@
         case OUTPUT_PAST:
             vtable = init_past_vtable();
             break;
+        case OUTPUT_JSON:
+            vtable = init_json_vtable();
+            break;
         default:
             fprintf(stderr, "Unknown output type specified\n");
             exit(1);

Modified: trunk/compilers/pirc/src/pirparser.c
==============================================================================
--- trunk/compilers/pirc/src/pirparser.c        (original)
+++ trunk/compilers/pirc/src/pirparser.c        Tue Apr  3 09:28:52 2007
@@ -9,7 +9,6 @@
 =over 4
 
 =item *
-
 Remove limit of 10 heredoc arguments
 
 =item *
@@ -110,7 +109,7 @@
 
 =over 4
 
-=item void exit_parser()
+=item * void exit_parser()
 
 Clean up and exit the program normally.
 
@@ -130,7 +129,7 @@
 
 /*
 
-=item get_parse_errors()
+=item * get_parse_errors()
 
 return the number of parse errors.
 
@@ -144,7 +143,7 @@
 
 /*
 
-=item new_parser()
+=item * new_parser()
 
 constructor for a parser_state object. The specified filename
 is parsed. The semantic actions in vtable are called at certain
@@ -174,7 +173,7 @@
 
 /*
 
-=item struct lexer_state const *get_lexer()
+=item * struct lexer_state const *get_lexer()
 
 returns the specified parser's lexer
 
@@ -188,7 +187,7 @@
 
 /*
 
-=item token get_token()
+=item * token get_token()
 
 returns the specified parser's current token
 
@@ -208,7 +207,7 @@
 
 =over 4
 
-=item static void syntax_error()
+=item * static void syntax_error()
 
 Handle all syntax error through this function.
 numargs is the number of variable arguments.
@@ -249,7 +248,7 @@
 
 /*
 
-=item static void match()
+=item * static void match()
 
 checks whether the current token is the same
 as the expected token. If so, all is ok, and the
@@ -313,7 +312,7 @@
 
 =over 4
 
-=item
+=item *
 
   expression -> ( IDENTIFIER | INTC | NUMC | STRINGC | register )
 
@@ -353,7 +352,7 @@
 
 /*
 
-=item
+=item *
 
   string_value -> SREG | PASM_SREG | STRINGC
 
@@ -378,7 +377,7 @@
 
 /*
 
-=item
+=item *
 
   method -> IDENTIFIER | STRINGC
 
@@ -395,7 +394,7 @@
 
 /*
 
-=item
+=item *
 
   target   -> register | IDENTIFIER
 
@@ -418,7 +417,7 @@
 
 /*
 
-=item
+=item *
 
   type -> 'int' | 'num' | 'pmc' | 'string'
 
@@ -452,7 +451,7 @@
 
 /*
 
-=item
+=item *
 
   key -> '-' expression | '..' expression | expression [ '..' [ expression ] ]
 
@@ -481,7 +480,7 @@
 
 /*
 
-=item
+=item *
 
   keylist -> '[' key { (';'|',') key } ']'
 
@@ -501,7 +500,7 @@
 
 /*
 
-=item
+=item *
 
   argument -> HEREDOCID | expression | STRINGC '=>' expression
 
@@ -529,7 +528,7 @@
 
 /*
 
-=item
+=item *
 
   argument_list -> argument { ',' argument }
 
@@ -550,7 +549,7 @@
 
 /*
 
-=item
+=item *
 
   arguments         -> '(' [argument_list] ')' heredoc_arguments
 
@@ -593,7 +592,7 @@
 
 /*
 
-=item
+=item *
 
   methodcall -> INVOCANT_IDENT method arguments
 
@@ -610,7 +609,7 @@
 
 /*
 
-=item
+=item *
 
 
 
@@ -655,7 +654,7 @@
 
 /*
 
-=item
+=item *
 
   parrot_instruction -> PARROT_OP [ expression {',' expression } ] '\n'
 
@@ -679,15 +678,15 @@
         }
         else break;
     }
-
-    emit_op_end(p);
+    
     match(p, T_NEWLINE);
+    emit_op_end(p);
 }
 
 
 /*
 
-=item
+=item *
 
   assignment -> '=' ( unop expression
                     | expression arith_expr
@@ -776,7 +775,7 @@
 
 /*
 
-=item
+=item *
 
   return_statement -> '.return' ( arguments | target ['->' method] arguments | 
methodcall ) '\n'
 
@@ -809,7 +808,7 @@
 
 /*
 
-=item
+=item *
 
   yield_statement -> '.yield' arguments '\n'
 
@@ -827,7 +826,7 @@
 
 /*
 
-=item
+=item *
 
   close_ns -> '.endnamespace' IDENTIFIER '\n'
 
@@ -846,7 +845,7 @@
 
 /*
 
-=item
+=item *
 
   open_ns -> '.namespace' IDENTIFIER '\n'
 
@@ -866,7 +865,7 @@
 
 /*
 
-=item
+=item *
 
   local_id_list -> IDENTIFIER [':unique_reg'] { ',' IDENTIFIER [':unique_reg'] 
}
 
@@ -896,7 +895,7 @@
 
 /*
 
-=item
+=item *
 
   declaration_list -> type local_id_list '\n'
 
@@ -914,7 +913,7 @@
 
 /*
 
-=item
+=item *
 
   sym_declaration -> '.sym' declaration_list
 
@@ -930,7 +929,7 @@
 
 /*
 
-=item
+=item *
 
   local_declaration -> '.local' declaration_list
 
@@ -948,7 +947,7 @@
 
 /*
 
-=item
+=item *
 
   lex_declaration -> '.lex' STRINGC ',' target '\n'
 
@@ -957,18 +956,22 @@
 */
 static void
 lex_declaration(parser_state *p) {
+    emit_op_start(p, ".lex");
     match(p, T_LEX);
+    emit_expr(p, get_current_token(p->lexer));
     match(p, T_STRING_CONSTANT);
+    emit_next_expr(p);
     match(p, T_COMMA);
     target(p);
     match(p, T_NEWLINE);
+    emit_op_end(p);
 }
 
 
 
 /*
 
-=item
+=item *
 
   conditional_expression -> expression [cond_op expression]
 
@@ -997,7 +1000,7 @@
 
 /*
 
-=item
+=item *
 
   jump_statement -> 'goto' IDENTIFIER '\n'
 
@@ -1015,7 +1018,7 @@
 
 /*
 
-=item
+=item *
 
   goto_statement -> jump_statement
 
@@ -1031,7 +1034,7 @@
 
 /*
 
-=item
+=item *
 
   unless_statement -> 'unless' (['null'] expression | conditional_expression) 
jump_statement
 
@@ -1056,7 +1059,7 @@
 
 /*
 
-=item
+=item *
 
   if_statement -> 'if' (['null'] expression | conditional_expression) 
jump_statement
 
@@ -1081,7 +1084,7 @@
 
 /*
 
-=item
+=item *
 
   const_definition -> 'int' IDENTIFIER '=' INTC
                     | 'num' IDENTIFIER '=' NUMC
@@ -1124,7 +1127,7 @@
 
 /*
 
-=item
+=item *
 
   arg_flags -> ':flat' | ':named' [ '(' STRINGC ')' ]
 
@@ -1155,7 +1158,7 @@
 
 /*
 
-=item
+=item *
 
   param_flags -> ':slurpy'
                | ':named'['(' STRINGC ')']
@@ -1203,7 +1206,7 @@
 
 /*
 
-=item
+=item *
 
   long-invocation -> '.pcc_begin' '\n'
                      { '.arg' expression arg_flags }
@@ -1279,7 +1282,7 @@
 
 /*
 
-=item
+=item *
 
   long_return_statement -> '.pcc_begin_return' '\n'
                            { '.return' expression '\n' }
@@ -1305,7 +1308,7 @@
 
 /*
 
-=item
+=item *
 
   long_yield_statement -> '.pcc_begin_yield' '\n'
                           { '.yield' expression '\n' }
@@ -1332,7 +1335,7 @@
 
 /*
 
-=item
+=item *
 
   target_statement -> target ( '=' assignment
                              | augmented_op expression
@@ -1397,7 +1400,7 @@
 
 /*
 
-=item
+=item *
 
   target_list -> '(' target param_flags {',' target param_flags } ')'
 
@@ -1418,7 +1421,7 @@
 
 /*
 
-=item
+=item *
 
   multi-result-invocation -> target_list '=' (subcall | methodcall)
 
@@ -1450,7 +1453,7 @@
 
 /*
 
-=item
+=item *
 
   macro_expansion -> MACRO_IDENT [ '(' [ expression { ',' expression } ')' ] 
'\n'
 
@@ -1481,7 +1484,7 @@
 
 /*
 
-=item
+=item *
 
   get_results_instr -> '.get_results' target_list '\n'
 
@@ -1501,7 +1504,7 @@
 
 /*
 
-=item
+=item *
 
   global_assignment -> 'global' string_value '=' (IDENTIFIER|PREG) '\n'
 
@@ -1538,7 +1541,7 @@
 
 /*
 
-=item
+=item *
 
   instructions -> {instruction}
 
@@ -1672,7 +1675,7 @@
 
 /*
 
-=item
+=item *
 
   global_definition -> '.global' IDENTIFIER
 
@@ -1687,7 +1690,7 @@
 
 /*
 
-=item
+=item *
 
   multi-type-list -> '(' [multi-type {',' multi-type } ] ')'
 
@@ -1733,7 +1736,7 @@
 
 /*
 
-=item
+=item *
 
   sub_flags -> [sub_flag { [','] sub_flag } ]
 
@@ -1811,7 +1814,7 @@
 
 /*
 
-=item
+=item *
 
   parameters -> { '.param' (register | type IDENTIFIER) [param_flag] '\n' }
 
@@ -1866,7 +1869,7 @@
 
 /*
 
-=item
+=item *
 
   sub_definition -> '.sub' (IDENTIFIER | STRINGC) subflags '\n' parameters 
instructions '.end'
 
@@ -1905,7 +1908,7 @@
 
 /*
 
-=item
+=item *
 
   emit_block -> '.emit' '\n' {parrot_instruction} ['\n'] '.eom'
 
@@ -1929,7 +1932,7 @@
 
 /*
 
-=item
+=item *
 
   macro_parameters -> [ '(' [ id {',' id} ] ')' ]
 
@@ -1953,7 +1956,7 @@
 
 /*
 
-=item
+=item *
 
   '.macro' IDENTIFIER parameters '\n' macro_body '.endm'
 
@@ -1979,7 +1982,7 @@
 
 /*
 
-=item
+=item *
 
   include -> '.include' STRINGC
 
@@ -2013,7 +2016,7 @@
 
 /*
 
-=item
+=item *
 
   pragma -> '.pragma' 'n_operators' INTC
 
@@ -2030,7 +2033,7 @@
 
 /*
 
-=item
+=item *
 
   hll_specifier -> '.HLL' STRINGC ',' STRINGC
 
@@ -2047,7 +2050,7 @@
 
 /*
 
-=item
+=item *
 
   hll_mapping -> '.HLL_map' INTC ',' INTC
 
@@ -2063,7 +2066,7 @@
 
 /*
 
-=item
+=item *
 
   namespace_declaration -> '.namespace' [ '[' STRINGC { (','|';') STRINGC ']' ]
 
@@ -2087,7 +2090,7 @@
 
 /*
 
-=item
+=item *
 
   loadlib -> '.loadlib' STRINGC
 
@@ -2102,7 +2105,7 @@
 
 /*
 
-=item
+=item *
 
   compilation_unit -> global_definition
                     | sub_definition
@@ -2166,7 +2169,7 @@
 
 /*
 
-=item
+=item *
 
   program -> {'\n'} compilation_unit { '\n' compilation_unit }
 
@@ -2192,7 +2195,7 @@
 
 /*
 
-=item
+=item *
 
   TOP -> program EOF
 

Modified: trunk/config/gen/makefiles/pirc.in
==============================================================================
--- trunk/config/gen/makefiles/pirc.in  (original)
+++ trunk/config/gen/makefiles/pirc.in  Tue Apr  3 09:28:52 2007
@@ -38,11 +38,13 @@
   src/pastout.c        \
   src/pastout.h        \
   src/pirvtable.c \
-  src/pirvtable.h
+  src/pirvtable.h \
+  src/jsonout.c \
+  src/jsonout.h
 
 
-pirc: pirmain$(O) pirparser$(O)        pirlexer$(O) pirout$(O) pastout$(O)     
pirvtable$(O)
-       $(CC) -o pirc$(EXE)     pirmain$(O)     pirparser$(O) pirlexer$(O) 
pirout$(O) pastout$(O) pirvtable$(O)
+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)
 
 pirmain$(O): src/pirmain.c src/pirparser.h
        $(CC) $(CFLAGS) -c src/pirmain.c
@@ -62,6 +64,9 @@
 pirvtable$(O): src/pirvtable.c src/pirvtable.h
        $(CC) $(CFLAGS) -c src/pirvtable.c
 
+jsonout$(O): src/jsonout.c src/jsonout.h
+       $(CC) $(CFLAGS) -c src/jsonout.c
+
 
 
 # This is a    listing of all targets, that are meant to be called     by users

Reply via email to