Author: kjs
Date: Fri Mar 30 13:02:41 2007
New Revision: 17861
Added:
trunk/compilers/pirc/src/pirvtable.h (contents, props changed)
Modified:
trunk/compilers/pirc/src/pirmain.c
trunk/compilers/pirc/src/pirout.c
trunk/compilers/pirc/src/pirout.h
trunk/compilers/pirc/src/pirparser.c
trunk/compilers/pirc/src/pirparser.h
trunk/config/gen/makefiles/pirc.in
Log:
compilers/pirc:
* add vtable to parser for flexible output selection
* options: output PIR (incomplete), output PAST (incomplete)
Modified: trunk/compilers/pirc/src/pirmain.c
==============================================================================
--- trunk/compilers/pirc/src/pirmain.c (original)
+++ trunk/compilers/pirc/src/pirmain.c Fri Mar 30 13:02:41 2007
@@ -19,7 +19,7 @@
/* create a new parser, specifying the file name */
- p = new_parser(argv[1]);
+ p = new_parser(argv[1], OUTPUT_PAST);
/* start parsing */
TOP(p);
Modified: trunk/compilers/pirc/src/pirout.c
==============================================================================
--- trunk/compilers/pirc/src/pirout.c (original)
+++ trunk/compilers/pirc/src/pirout.c Fri Mar 30 13:02:41 2007
@@ -2,17 +2,30 @@
#include "pirlexer.h"
#include "pirparser.h"
#include <stdio.h>
+#include <malloc.h>
-/* pirout()
- *
- * Prints the current token from the specified parser.
- * NOT COMPLETE YET, and no fancy output as well...
- */
-void
+/*
+
+=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 */
@@ -42,6 +55,37 @@
}
+
+/*
+
+=item init_pir_vtable()
+
+Creates a vtable for the PIR emitting module, and
+then this vtable is set into the parser_state struct.
+
+=cut
+
+*/
+pirvtable *
+init_pir_vtable(void) {
+ pirvtable *vtable = (pirvtable *)malloc(sizeof(pirvtable));
+
+ vtable->sub_start = pirout;
+ vtable->sub_end = pirout;
+
+ return vtable;
+}
+
+
+
+/*
+
+=back
+
+=cut
+
+*/
+
/*
* Local variables:
* c-file-style: "parrot"
Modified: trunk/compilers/pirc/src/pirout.h
==============================================================================
--- trunk/compilers/pirc/src/pirout.h (original)
+++ trunk/compilers/pirc/src/pirout.h Fri Mar 30 13:02:41 2007
@@ -1,10 +1,10 @@
#ifndef __PIROUT_H
# define __PIROUT_H
+/* predeclare */
+struct pirvtable;
-# include "pirparser.h"
-
-extern void pirout(struct parser_state *parser);
+extern struct pirvtable *init_pir_vtable(void);
#endif
Modified: trunk/compilers/pirc/src/pirparser.c
==============================================================================
--- trunk/compilers/pirc/src/pirparser.c (original)
+++ trunk/compilers/pirc/src/pirparser.c Fri Mar 30 13:02:41 2007
@@ -55,7 +55,10 @@
#include "pirlexer.h"
#include "pirparser.h"
+#include "pirvtable.h"
+
#include "pirout.h" /* for test output */
+#include "pastout.h" /* experimental output of PAST */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
@@ -86,7 +89,8 @@
char *heredoc_ids[10];
unsigned heredoc_index;
unsigned parse_errors;
-
+ pirvtable *vtable;
+
} parser_state;
@@ -96,9 +100,11 @@
/* call next() to get the next token from the lexer */ /* NOTE: it's calling
pirout() */
-/* #define next(P) P->curtoken = next_token(P->lexer) */
+#define next(P) P->curtoken = next_token(P->lexer)
+/*
#define next(P) do { pirout(P); P->curtoken = next_token(P->lexer); } while(0)
+*/
/*
@@ -123,6 +129,8 @@
exit(0);
}
+
+
/*
=item get_parse_errors()
@@ -147,7 +155,7 @@
*/
parser_state *
-new_parser(char const * filename) {
+new_parser(char const * filename, outputtype output) {
parser_state *p = (parser_state *)malloc(sizeof(parser_state));
if (p == NULL) {
@@ -158,6 +166,19 @@
p->curtoken = next_token(p->lexer);
p->parse_errors = 0;
p->heredoc_index = 0;
+
+ switch (output) {
+ case OUTPUT_PIR:
+ p->vtable = init_pir_vtable();
+ break;
+ case OUTPUT_PAST:
+ p->vtable = init_past_vtable();
+ break;
+ default:
+ fprintf(stderr, "Unknown output type specified\n");
+ exit(1);
+ }
+
return p;
}
@@ -1785,13 +1806,20 @@
if (p->curtoken == T_IDENTIFIER) match(p, T_IDENTIFIER);
else stringconstant(p);
-
+
+ /* call emit method */
+ emit_sub_start(p);
+
sub_flags(p);
match(p, T_NEWLINE);
parameters(p);
instructions(p);
match(p, T_END);
+
+ /* call emit method */
+ emit_sub_end(p);
+
}
/*
Modified: trunk/compilers/pirc/src/pirparser.h
==============================================================================
--- trunk/compilers/pirc/src/pirparser.h (original)
+++ trunk/compilers/pirc/src/pirparser.h Fri Mar 30 13:02:41 2007
@@ -2,12 +2,23 @@
# define __PIRPARSER_H
# include "pirlexer.h"
+# include "pirvtable.h"
/* hide internals; definition in pirparser.c */
struct parser_state;
+/* define output type: what kind of semantic routines
+ * should be called in the parser?
+ */
+typedef enum outputtypes {
+ OUTPUT_NONE, /* do nothing */
+ OUTPUT_PIR, /* output PIR */
+ OUTPUT_PAST /* output PAST */
+
+} outputtype;
+
/* parser constructor */
-extern struct parser_state *new_parser(char const * filename);
+extern struct parser_state *new_parser(char const * filename, outputtype type);
/* entry function for the parser */
extern void TOP(struct parser_state *p);
@@ -22,6 +33,8 @@
extern token get_token(struct parser_state *p);
+
+
#endif
/*
Added: trunk/compilers/pirc/src/pirvtable.h
==============================================================================
--- (empty file)
+++ trunk/compilers/pirc/src/pirvtable.h Fri Mar 30 13:02:41 2007
@@ -0,0 +1,31 @@
+#ifndef __VTABLE_H
+# define __VTABLE_H
+
+struct parser_state;
+
+/* vtable that contains function pointers for emit
+ * routines.
+ */
+typedef struct pirvtable {
+
+ void (* sub_start)(struct parser_state *p);
+ void (* sub_end) (struct parser_state *p);
+
+} pirvtable;
+
+/* #defines for cleaner invocation syntax */
+
+# define emit_sub_start(P) (*p->vtable->sub_start)(P)
+# define emit_sub_end(P) (*p->vtable->sub_end) (P)
+
+#endif
+
+
+
+/*
+ * Local variables:
+ * c-file-style: "parrot"
+ * End:
+ * vim: expandtab shiftwidth=4:
+ */
+
\ No newline at end of file
Modified: trunk/config/gen/makefiles/pirc.in
==============================================================================
--- trunk/config/gen/makefiles/pirc.in (original)
+++ trunk/config/gen/makefiles/pirc.in Fri Mar 30 13:02:41 2007
@@ -34,11 +34,13 @@
src/pirlexer.c \
src/pirlexer.h \
src/pirout.c \
- src/pirout.h
+ src/pirout.h \
+ src/pastout.c \
+ src/pastout.h
-pirc: pirmain$(O) pirparser$(O) pirlexer$(O) pirout$(O)
- $(CC) -o pirc$(EXE) pirmain$(O) pirparser$(O) pirlexer$(O) pirout$(O)
+pirc: pirmain$(O) pirparser$(O) pirlexer$(O) pirout$(O) pastout$(O)
+ $(CC) -o pirc$(EXE) pirmain$(O) pirparser$(O) pirlexer$(O) pirout$(O)
pastout$(O)
pirmain$(O): src/pirmain.c src/pirparser.h
$(CC) $(CFLAGS) -c src/pirmain.c
@@ -52,6 +54,9 @@
pirout$(O): src/pirout.c src/pirout.h
$(CC) $(CFLAGS) -c src/pirout.c
+pastout$(O): src/pastout.c src/pastout.h
+ $(CC) $(CFLAGS) -c src/pastout.c
+
# This is a listing of all targets, that are meant to be called by users
help:
@echo ""