Author: kjs
Date: Sat Mar 24 06:58:20 2007
New Revision: 17710

Modified:
   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/pirparser.h
   trunk/config/gen/makefiles/pirc.in

Log:
compilers/pirc:
* added simple PIR output function for testing
* added token comments to parser/lexer

Modified: trunk/compilers/pirc/src/pirlexer.c
==============================================================================
--- trunk/compilers/pirc/src/pirlexer.c (original)
+++ trunk/compilers/pirc/src/pirlexer.c Sat Mar 24 06:58:20 2007
@@ -121,46 +121,46 @@
     ")",                        /* T_RPAREN,                */
     "[",                        /* T_LBRACKET               */
     "]",                        /* T_RBRACKET               */
-    "end of file",              /* descriptions of misc. tokens. Please note 
that these may NOT be  */
-    "not found",                /* single words, because then they might be 
considered "keywords"   */
-    "PASM PREG",                /* So, always use multiple words or 'strange' 
tokens like ' and "   */
-    "PASM NREG",                /*                          */
-    "PASM_IREG",                /*                          */
-    "PASM_SREG",                /*                          */
-    "PREG",                     /*                          */
-    "SREG",                     /*                          */
-    "NREG",                     /*                          */
-    "IREG",                     /*                          */
-    "macro identifier",         /*                          */
-    "macro label",              /*                          */
-    "type specifier",           /*                          */
-    "'identifier'",             /*                          */
-    "int constant",             /*                          */
-    "num constant",             /*                          */
-    "label identifier",         /*                          */
-    "'\\n'",                    /*                          */
-    "=",                        /*                          */
-    "\"string\"",               /*                          */
-    "'string'",                 /*                          */
-    "'literal'",                /*                          */
-    "invocant id",              /*                          */
-    "'number'",                 /*                          */
-    "'error'",                  /*                          */
-    "**",                       /*                          */
-    "**=",                      /*                          */
-    "*=",                       /*                          */
-    "+=",                       /*                          */
-    "-=",                       /*                          */
-    "'register'",               /*                          */
-    "/=",                       /*                          */
-    "%=",                       /*                          */
-    "&=",                       /*                          */
-    "|=",                       /*                          */
-    ">>=",                      /*                          */
-    ">>>=",                     /*                          */
-    "heredoc id",               /*                          */
-    "heredoc string",           /*                          */
-    "parrot op",                /*                          */
+    "end of file",              /* T_EOF                    */
+    "not found",                /* T_NOT_FOUND              */
+    "PASM PREG",                /* T_PASM_PREG              */             
+    "PASM NREG",                /* T_PASM_NREG              */
+    "PASM_IREG",                /* T_PASM_IREG,             */
+    "PASM_SREG",                /* T_PASM_SREG,             */
+    "PREG",                     /* T_PREG,                  */
+    "SREG",                     /* T_SREG,                  */
+    "NREG",                     /* T_NREG,                  */
+    "IREG",                     /* T_IREG,                  */
+    "macro identifier",         /* T_MACRO_IDENT,           */
+    "macro label",              /* T_MACRO_LABEL,           */
+    "type specifier",           /* T_TYPE,                  */
+    "'identifier'",             /* T_IDENTIFIER,            */
+    "int constant",             /* T_INTEGER_CONSTANT,      */
+    "num constant",             /* T_NUMBER_CONSTANT,       */
+    "label identifier",         /* T_LABEL,                 */
+    "'\\n'",                    /* T_NEWLINE,               */
+    "=",                        /* T_ASSIGN,                */
+    "\"string\"",               /* T_DOUBLE_QUOTED_STRING,  */
+    "'string'",                 /* T_SINGLE_QUOTED_STRING,  */
+    "'literal'",                /* T_LITERAL,               */
+    "invocant id",              /* T_INVOCANT_IDENT,        */
+    "'number'",                 /* T_NUMBER,                */
+    "'error'",                  /* T_ERROR,                 */
+    "**",                       /* T_POWER,                 */
+    "**=",                      /* T_POWER_ASSIGN,          */
+    "*=",                       /* T_MULTIPLY_ASSIGN,       */
+    "+=",                       /* T_PLUS_ASSIGN,           */
+    "-=",                       /* T_MINUS_ASSIGN,          */
+    "'register'",               /* T_REGISTER,              */
+    "/=",                       /* T_DIVIDE_ASSIGN,         */
+    "%=",                       /* T_MODULO_ASSIGN,         */
+    "&=",                       /* T_BAND_ASSIGN,           */
+    "|=",                       /* T_BOR_ASSIGN,            */
+    ">>=",                      /* T_RRSHIFT_ASSIGN,        */
+    ">>>=",                     /* T_RSHIFT_ASSIGN,         */
+    "heredoc id",               /* T_HEREDOC_ID,            */
+    "heredoc string",           /* T_HEREDOC_STRING,        */
+    "parrot op",                /* T_PARROT_OP              */
     NULL                        /*                          */
 };
 
@@ -212,11 +212,13 @@
  */
 char const *
 find_keyword(token t) {
-    if ((t > 0) && (t < MAX_TOKEN)) {
+    if ((t > 0) && (t <= MAX_TOKEN)) {
         return dictionary[t];
     }
     else {
         fprintf(stderr, "FATAL: invalid token in find_keyword()\n");
+        fprintf(stderr, "No entry for token %d\n", t);
+        if (t-1 <= MAX_TOKEN) fprintf(stderr, "Previous token: %s\n", 
dictionary[t - 1]);
         return "ERROR";
     }
 }

Modified: trunk/compilers/pirc/src/pirlexer.h
==============================================================================
--- trunk/compilers/pirc/src/pirlexer.h (original)
+++ trunk/compilers/pirc/src/pirlexer.h Sat Mar 24 06:58:20 2007
@@ -2,142 +2,142 @@
 #define __PIRLEXER_H
 
 typedef enum tokens {
-        T_GOTO,
-        T_IF,
-        T_INT,
-        T_N_OPERATORS,
-        T_NULL,
-        T_NUM,
-        T_PMC,
-        T_STRING,
-        T_UNLESS,
-
-        T_ARG,
-        T_CONST,
-        T_CONSTANT,
-        T_EMIT,
-        T_END,
-        T_ENDNAMESPACE,
-        T_ENDM,
-        T_EOM,
-        T_GET_RESULTS,
-        T_GLOBAL,
-        T_GLOBALCONST,
-        T_HLL,
-        T_HLL_MAP,
-        T_INCLUDE,
-        T_INVOCANT,
-        T_LEX,
-        T_LOADLIB,
-        T_LOCAL,
-        T_MACRO,
-        T_METH_CALL,
-        T_NAMESPACE,
-        T_NCI_CALL,
-        T_PARAM,
-        T_PCC_BEGIN,
-        T_PCC_BEGIN_RETURN,
-        T_PCC_BEGIN_YIELD,
-        T_PCC_CALL,
-        T_PCC_END,
-        T_PCC_END_RETURN,
-        T_PCC_END_YIELD,
-        T_PCC_SUB,
-        T_PRAGMA,
-        T_RESULT,
-        T_RETURN,
-        T_SUB,
-        T_SYM,
-        T_YIELD,
-
-        T_ANON_FLAG,
-        T_IMMEDIATE_FLAG,
-        T_INIT_FLAG,
-        T_LEX_FLAG,
-        T_LOAD_FLAG,
-        T_MAIN_FLAG,
-        T_MULTI_FLAG,
-        T_OUTER_FLAG,
-        T_POSTCOMP_FLAG,
-        T_VTABLE_FLAG,
-        T_NAMED_FLAG,
-        T_OPT_FLAG_FLAG,
-        T_OPTIONAL_FLAG,
-        T_SLURPY_FLAG,
-        T_FLAT_FLAG,
-        T_UNIQUE_REG_FLAG,
-
-        T_PLUS,
-        T_MINUS,
-        T_DIVIDE,
-        T_MODULO,
-        T_MULTIPLY,
-        T_CONCAT,
-        T_DOTDOT,
-        T_BAND,
-        T_AND,
-        T_BOR,
-        T_OR,
-        T_XOR,
-        T_HUH,
-        T_RSHIFT,
-        T_LSHIFT,
-        T_RRSHIFT,
-        T_GT,
-        T_LT,
-        T_GE,
-        T_LE,
-        T_EQ,
-        T_NE,
-        T_ARROW,
-        T_PTR,
-        T_COMMA,
-        T_SEMICOLON,
-        T_LPAREN,
-        T_RPAREN,
-        T_LBRACKET,
-        T_RBRACKET,
-        T_EOF,
-        T_NOT_FOUND,
-        T_PASM_PREG,
-        T_PASM_NREG,
-        T_PASM_IREG,
-        T_PASM_SREG,
-        T_PREG,
-        T_SREG,
-        T_NREG,
-        T_IREG,
-        T_MACRO_IDENT,
-        T_MACRO_LABEL,
-        T_TYPE,
-        T_IDENTIFIER,
-        T_INTEGER_CONSTANT,
-        T_NUMBER_CONSTANT,
-        T_LABEL,
-        T_NEWLINE,
-        T_ASSIGN,
-        T_DOUBLE_QUOTED_STRING,
-        T_SINGLE_QUOTED_STRING,
-        T_LITERAL,
-        T_INVOCANT_IDENT,
-        T_NUMBER,
-        T_ERROR,
-        T_POWER,
-        T_POWER_ASSIGN,
-        T_MULTIPLY_ASSIGN,
-        T_PLUS_ASSIGN,
-        T_MINUS_ASSIGN,
-        T_REGISTER,
-        T_DIVIDE_ASSIGN,
-        T_MODULO_ASSIGN,
-        T_BAND_ASSIGN,
-        T_BOR_ASSIGN,
-        T_RRSHIFT_ASSIGN,
-        T_RSHIFT_ASSIGN,
-        T_HEREDOC_ID,
-        T_HEREDOC_STRING,
-        T_PARROT_OP
-
+        T_GOTO,                                                /* "goto",      
              */
+        T_IF,                               /* "if",                      */
+        T_INT,                              /* "int",                     */
+        T_N_OPERATORS,                      /* "n_operators",             */
+        T_NULL,                             /* "null",                    */
+        T_NUM,                              /* "num",                     */
+        T_PMC,                              /* "pmc",                     */
+        T_STRING,                           /* "string",                  */
+        T_UNLESS,                           /* "unless",                  */
+                                            /* NULL,                      */
+        T_ARG,                              /* ".arg",                    */
+        T_CONST,                            /* ".const",                  */
+        T_CONSTANT,                         /* ".constant",               */
+        T_EMIT,                             /* ".emit",                   */
+        T_END,                              /* ".end",                    */
+        T_ENDNAMESPACE,                     /* ".endnamespace",           */
+        T_ENDM,                             /* ".endm",                   */
+        T_EOM,                              /* ".eom",                    */
+        T_GET_RESULTS,                      /* ".get_results",            */
+        T_GLOBAL,                           /* ".global",                 */
+        T_GLOBALCONST,                      /* ".globalconst",            */
+        T_HLL,                              /* ".HLL",                    */
+        T_HLL_MAP,                          /* ".HLL_map",                */
+        T_INCLUDE,                          /* ".include",                */
+        T_INVOCANT,                         /* ".invocant",               */
+        T_LEX,                              /* ".lex",                    */
+        T_LOADLIB,                          /* ".loadlib",                */
+        T_LOCAL,                            /* ".local",                  */
+        T_MACRO,                            /* ".macro",                  */
+        T_METH_CALL,                        /* ".meth_call",              */
+        T_NAMESPACE,                        /* ".namespace",              */
+        T_NCI_CALL,                         /* ".nci_call",               */
+        T_PARAM,                            /* ".param",                  */
+        T_PCC_BEGIN,                        /* ".pcc_begin",              */
+        T_PCC_BEGIN_RETURN,                 /* ".pcc_begin_return",       */
+        T_PCC_BEGIN_YIELD,                  /* ".pcc_begin_yield",        */
+        T_PCC_CALL,                         /* ".pcc_call",               */
+        T_PCC_END,                          /* ".pcc_end",                */
+        T_PCC_END_RETURN,                   /* ".pcc_end_return",         */
+        T_PCC_END_YIELD,                    /* ".pcc_end_yield",          */
+        T_PCC_SUB,                          /* ".pcc_sub",                */
+        T_PRAGMA,                           /* ".pragma",                 */
+        T_RESULT,                           /* ".result",                 */
+        T_RETURN,                           /* ".return",                 */
+        T_SUB,                              /* ".sub",                    */
+        T_SYM,                              /* ".sym",                    */
+        T_YIELD,                            /* ".yield",                  */
+                                            /* NULL,                      */
+        T_ANON_FLAG,                        /* ":anon",                   */
+        T_IMMEDIATE_FLAG,                   /* ":immediate",              */
+        T_INIT_FLAG,                        /* ":init",                   */
+        T_LEX_FLAG,                         /* ":lex",                    */
+        T_LOAD_FLAG,                        /* ":load",                   */
+        T_MAIN_FLAG,                        /* ":main",                   */
+        T_MULTI_FLAG,                       /* ":multi",                  */
+        T_OUTER_FLAG,                       /* ":outer",                  */
+        T_POSTCOMP_FLAG,                    /* ":postcomp",               */
+        T_VTABLE_FLAG,                      /* ":vtable",                 */
+        T_NAMED_FLAG,                       /* ":named",                  */
+        T_OPT_FLAG_FLAG,                    /* ":opt_flag",               */
+        T_OPTIONAL_FLAG,                    /* ":optional",               */
+        T_SLURPY_FLAG,                      /* ":slurpy",                 */
+        T_FLAT_FLAG,                        /* ":flat",                   */
+        T_UNIQUE_REG_FLAG,                  /* ":unique_reg",             */
+                                            /*   NULL,                    */
+        T_PLUS,                             /* "+",                       */
+        T_MINUS,                            /* "-",                       */
+        T_DIVIDE,                           /* "/",                       */
+        T_MODULO,                           /* "%",                       */
+        T_MULTIPLY,                         /* "*",                       */
+        T_CONCAT,                           /* ".",                       */
+        T_DOTDOT,                           /* "..",                      */
+        T_BAND,                             /* "&",                       */
+        T_AND,                              /* "&&",                      */
+        T_BOR,                              /* "|",                       */
+        T_OR,                               /* "||",                      */
+        T_XOR,                              /* "~",                       */
+        T_HUH,                              /* "~~",                      */
+        T_RSHIFT,                           /* ">>",                      */
+        T_LSHIFT,                           /* "<<",                      */
+        T_RRSHIFT,                          /* ">>>",                     */
+        T_GT,                               /* ">",                       */
+        T_LT,                               /* "<",                       */
+        T_GE,                               /* ">=",                      */
+        T_LE,                               /* "<=",                      */
+        T_EQ,                               /* "==",                      */
+        T_NE,                               /* "!=",                      */
+        T_ARROW,                            /* "=>",                      */
+        T_PTR,                              /* "->",                      */
+        T_COMMA,                            /* ",",                       */
+        T_SEMICOLON,                        /* ";",                       */
+        T_LPAREN,                           /* "(",                       */
+        T_RPAREN,                           /* ")",                       */
+        T_LBRACKET,                         /* "[",                       */
+        T_RBRACKET,                         /* "]",                       */
+        T_EOF,                              /* "end of file",             */
+        T_NOT_FOUND,                        /* "not found",               */
+        T_PASM_PREG,                        /* "PASM PREG",               */
+        T_PASM_NREG,                        /* "PASM NREG",               */
+        T_PASM_IREG,                        /* "PASM_IREG",               */
+        T_PASM_SREG,                        /* "PASM_SREG",               */
+        T_PREG,                             /* "PREG",                    */
+        T_SREG,                             /* "SREG",                    */
+        T_NREG,                             /* "NREG",                    */
+        T_IREG,                             /* "IREG",                    */
+        T_MACRO_IDENT,                      /* "macro identifier",        */
+        T_MACRO_LABEL,                      /* "macro label",             */
+        T_TYPE,                             /* "type specifier",          */
+        T_IDENTIFIER,                       /* "'identifier'",            */
+        T_INTEGER_CONSTANT,                 /* "int constant",            */
+        T_NUMBER_CONSTANT,                  /* "num constant",            */
+        T_LABEL,                            /* "label identifier",        */
+        T_NEWLINE,                          /* "'\\n'",                   */
+        T_ASSIGN,                           /* "=",                       */
+        T_DOUBLE_QUOTED_STRING,             /* "\"string\"",              */
+        T_SINGLE_QUOTED_STRING,             /* "'string'",                */
+        T_LITERAL,                          /* "'literal'",               */
+        T_INVOCANT_IDENT,                   /* "invocant id",             */
+        T_NUMBER,                           /* "'number'",                */
+        T_ERROR,                            /* "'error'",                 */
+        T_POWER,                            /* "**",                      */
+        T_POWER_ASSIGN,                     /* "**=",                     */
+        T_MULTIPLY_ASSIGN,                  /* "*=",                      */
+        T_PLUS_ASSIGN,                      /* "+=",                      */
+        T_MINUS_ASSIGN,                     /* "-=",                      */
+        T_REGISTER,                         /* "'register'",              */
+        T_DIVIDE_ASSIGN,                    /* "/=",                      */
+        T_MODULO_ASSIGN,                    /* "%=",                      */
+        T_BAND_ASSIGN,                      /* "&=",                      */
+        T_BOR_ASSIGN,                       /* "|=",                      */
+        T_RRSHIFT_ASSIGN,                   /* ">>=",                     */
+        T_RSHIFT_ASSIGN,                    /* ">>>=",                    */
+        T_HEREDOC_ID,                       /* "heredoc id",              */
+        T_HEREDOC_STRING,                   /* "heredoc string",          */
+        T_PARROT_OP                         /* "parrot op",               */
+                                            /* NULL                       */
 } token;
 
 /* Make sure MAX_TOKEN is the last enum value from enum token {} */

Modified: trunk/compilers/pirc/src/pirmain.c
==============================================================================
--- trunk/compilers/pirc/src/pirmain.c  (original)
+++ trunk/compilers/pirc/src/pirmain.c  Sat Mar 24 06:58:20 2007
@@ -16,6 +16,7 @@
         fprintf(stderr, "usage: %s <file>\n", argv[0]);
         exit(1);
     }
+    
 
     /* create a new parser, specifying the file name */
     p = new_parser(argv[1]);
@@ -24,8 +25,8 @@
     TOP(p);
 
     /* check for errors */
-    if (get_parse_errors(p) == 0) printf("parse successful\n");
-    else fprintf(stderr, "There were %d errors.\n", get_parse_errors(p));
+    if (get_parse_errors(p)) 
+        fprintf(stderr, "There were %d errors.\n", get_parse_errors(p));    
 
     /* clean up and exit */
     exit_parser(p);

Modified: trunk/compilers/pirc/src/pirparser.c
==============================================================================
--- trunk/compilers/pirc/src/pirparser.c        (original)
+++ trunk/compilers/pirc/src/pirparser.c        Sat Mar 24 06:58:20 2007
@@ -1,6 +1,8 @@
 #include "pirlexer.h"
 #include "pirparser.h"
 
+#include "pirout.h" /* for test output */
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <assert.h>
@@ -27,8 +29,8 @@
 
 
 
-/* call next() to get the next token from the lexer */
-#define next(P) P->curtoken = next_token(P->lexer)
+/* call next() to get the next token from the lexer */ /* NOTE: it's calling 
pirout() */
+#define next(P) do { pirout(P); P->curtoken = next_token(P->lexer); } while(0)
 
 /* prototypes */
 static void target(parser_state *p);
@@ -92,7 +94,7 @@
  */
 static void
 match(parser_state *p, token expected) {
-    if (p->curtoken == expected) { /* if all is fine, get next token */
+    if (p->curtoken == expected) { /* if all is fine, get next token */        
               
         next(p);
     }
     else {
@@ -100,9 +102,8 @@
             syntax_error(p, 3 , "expected ", find_keyword(expected), " but got 
end of file");
             exit_parser(p);     /* no use to continue when having read end of 
file */
         }
-        else { /* 'normal' error; not end of file yet */
-            char *curtoken = get_current_token(p->lexer);
-            syntax_error(p, 5, "expected ", find_keyword(expected), " but got 
'", curtoken, "'");
+        else { /* 'normal' error; not end of file yet */            
+            syntax_error(p, 4, "expected ", find_keyword(expected), " but got: 
", find_keyword(p->curtoken));
 
             /* Try to reduce errors; skip tokens up to ".end" and
              * try to continue with the next subroutine, if any
@@ -121,6 +122,14 @@
 }
 
 
+struct lexer_state const *
+get_lexer(parser_state *p) {
+    return p->lexer;
+}
+
+token get_token(parser_state *p) {
+    return p->curtoken;   
+}
 
 
 
@@ -1083,7 +1092,7 @@
     /* emit_block -> '.emit' '\n' {pasm_instruction} '.eom' */
     match(p, T_EMIT);
     match(p, T_NEWLINE);
-
+    
     while (p->curtoken == T_PARROT_OP) {
         next(p);
         while (p->curtoken != T_NEWLINE) {
@@ -1093,7 +1102,8 @@
         }
         match(p, T_NEWLINE);
     }
-
+    if (p->curtoken == T_NEWLINE) next(p);
+    
     match(p, T_EOM);
 }
 
@@ -1288,9 +1298,9 @@
         syntax_error(p, 3, "end of file expected in file '", 
get_current_file(p->lexer), "'\n");
 
     }
-    else {
-        fprintf(stderr, "TOP: end of file '%s'\n", get_current_file(p->lexer));
-    }
+    //else {
+    //    fprintf(stderr, "TOP: end of file '%s'\n", 
get_current_file(p->lexer));
+    //}
 
 }
 

Modified: trunk/compilers/pirc/src/pirparser.h
==============================================================================
--- trunk/compilers/pirc/src/pirparser.h        (original)
+++ trunk/compilers/pirc/src/pirparser.h        Sat Mar 24 06:58:20 2007
@@ -18,5 +18,9 @@
 /* get number of parse errors */
 extern int get_parse_errors(struct parser_state *p);
 
+extern struct lexer_state const *get_lexer(struct parser_state *p);
+
+extern token get_token(struct parser_state *p);
+
 #endif
 

Modified: trunk/config/gen/makefiles/pirc.in
==============================================================================
--- trunk/config/gen/makefiles/pirc.in  (original)
+++ trunk/config/gen/makefiles/pirc.in  Sat Mar 24 06:58:20 2007
@@ -32,12 +32,13 @@
   src/pirparser.c \
   src/pirparser.h \
   src/pirlexer.c \
-  src/pirlexer.h
-  
+  src/pirlexer.h \
+  src/pirout.c \
+  src/pirout.h
 
 
-pirc: pirmain$(O) pirparser$(O) pirlexer$(O) 
-       $(CC) -o pirc$(EXE) pirmain$(O) pirparser$(O) pirlexer$(O)
+pirc: pirmain$(O) pirparser$(O) pirlexer$(O) pirout$(O)
+       $(CC) -o pirc$(EXE) pirmain$(O) pirparser$(O) pirlexer$(O) pirout$(O)
 
 pirmain$(O): src/pirmain.c src/pirparser.h
        $(CC) $(CFLAGS) -c src/pirmain.c
@@ -48,6 +49,8 @@
 pirlexer$(O): src/pirlexer.c src/pirlexer.h
        $(CC) $(CFLAGS) -c src/pirlexer.c
        
+pirout$(O): src/pirout.c src/pirout.h
+       $(CC) $(CFLAGS) -c src/pirout.c
 
 # This is a listing of all targets, that are meant to be called by users
 help:

Reply via email to