Author: kjs
Date: Sun Mar 25 07:58:23 2007
New Revision: 17726
Modified:
trunk/compilers/pirc/src/pirlexer.c
trunk/compilers/pirc/src/pirlexer.h
trunk/compilers/pirc/src/pirparser.c
Log:
compilers/pirc:
* renamed token for '.global' to T_GLOBAL_DECL (as it declares a global)
* added T_GLOBAL token for "global" keyword
* added support for "global" statement (global "x" = i, i = global "x")
Modified: trunk/compilers/pirc/src/pirlexer.c
==============================================================================
--- trunk/compilers/pirc/src/pirlexer.c (original)
+++ trunk/compilers/pirc/src/pirlexer.c Sun Mar 25 07:58:23 2007
@@ -63,6 +63,7 @@
*/
char const * dictionary[] = {
+ "global", /* T_GLOBAL, */
"goto", /* T_GOTO, */
"if", /* T_IF, */
"int", /* T_INT, */
@@ -82,7 +83,7 @@
".endm", /* T_ENDM, */
".eom", /* T_EOM, */
".get_results", /* T_GET_RESULTS */
- ".global", /* T_GLOBAL, */
+ ".global", /* T_GLOBAL_DECL, */
".globalconst", /* T_GLOBALCONST */
".HLL", /* T_HLL */
".HLL_map", /* T_HLL_MAP */
@@ -271,7 +272,7 @@
*/
char const *
find_keyword(token t) {
- if ((t > 0) && (t <= MAX_TOKEN)) {
+ if ((t >= 0) && (t <= MAX_TOKEN)) {
return dictionary[t];
}
else {
Modified: trunk/compilers/pirc/src/pirlexer.h
==============================================================================
--- trunk/compilers/pirc/src/pirlexer.h (original)
+++ trunk/compilers/pirc/src/pirlexer.h Sun Mar 25 07:58:23 2007
@@ -2,6 +2,7 @@
#define __PIRLEXER_H
typedef enum tokens {
+ T_GLOBAL, /* "global"
*/
T_GOTO, /* "goto",
*/
T_IF, /* "if", */
T_INT, /* "int", */
@@ -21,7 +22,7 @@
T_ENDM, /* ".endm", */
T_EOM, /* ".eom", */
T_GET_RESULTS, /* ".get_results", */
- T_GLOBAL, /* ".global", */
+ T_GLOBAL_DECL, /* ".global", */
T_GLOBALCONST, /* ".globalconst", */
T_HLL, /* ".HLL", */
T_HLL_MAP, /* ".HLL_map", */
Modified: trunk/compilers/pirc/src/pirparser.c
==============================================================================
--- trunk/compilers/pirc/src/pirparser.c (original)
+++ trunk/compilers/pirc/src/pirparser.c Sun Mar 25 07:58:23 2007
@@ -232,7 +232,7 @@
static token
expression(parser_state *p) {
token exprtok = T_ERROR;
-
+
switch (p->curtoken) {
case T_IDENTIFIER:
case T_INTEGER_CONSTANT:
@@ -253,6 +253,29 @@
return exprtok;
}
+
+
+/*
+
+=item stringconstant()
+
+ strinconstant -> DOUBLE_QUOTED_STRING | SINGLE_QUOTED_STRING
+
+=cut
+
+*/
+static void
+stringconstant(parser_state *p) {
+
+ if (p->curtoken == T_DOUBLE_QUOTED_STRING
+ || p->curtoken == T_SINGLE_QUOTED_STRING) {
+ next(p);
+ }
+ else {
+ syntax_error(p, 1, "string constant expected");
+ }
+}
+
/*
=item target()
@@ -310,26 +333,26 @@
*/
static void
-key(parser_state *p) {
+key(parser_state *p) {
switch (p->curtoken) {
case T_MINUS:
/*
- case T_BXOR:
- case T_NOT:
+ case T_BXOR:
+ case T_NOT:
*/
case T_DOTDOT: /* key -> '..' expr */
next(p);
expression(p);
- break;
+ break;
default: /* key -> expr [ '..' [ expr ] ] */
expression(p);
if (p->curtoken == T_DOTDOT) {
next(p);
if (p->curtoken == T_RBRACKET) return;
- else expression(p);
+ else expression(p);
}
- break;
- }
+ break;
+ }
}
@@ -414,7 +437,7 @@
static void
global_definition(parser_state *p) {
/* global_definition -> '.global' IDENT */
- match(p, T_GLOBAL);
+ match(p, T_GLOBAL_DECL);
match(p, T_IDENTIFIER);
}
@@ -502,7 +525,12 @@
=item assignment()
- assignment -> '=' ( unop expr | expr [binop expr] | target
(keylist|arguments) | heredocstring ) '\n'
+ assignment -> '=' ( unop expr
+ | expr [binop expr]
+ | target (keylist|arguments)
+ | 'global' stringconstant
+ | heredocstring
+ ) '\n'
unop -> '-' | '!' | '~'
@@ -537,7 +565,10 @@
break;
}
break;
-
+ case T_GLOBAL:
+ next(p);
+ stringconstant(p);
+ break;
case T_HEREDOC_ID: { /* parse heredoc string */
char *heredocid = clone_string(get_current_token(p->lexer));
/* read_heredoc() returns a special token */
@@ -711,26 +742,6 @@
}
-/*
-
-=item stringconstant()
-
- strinconstant -> DOUBLE_QUOTED_STRING | SINGLE_QUOTED_STRING
-
-=cut
-
-*/
-static void
-stringconstant(parser_state *p) {
-
- if (p->curtoken == T_DOUBLE_QUOTED_STRING
- || p->curtoken == T_SINGLE_QUOTED_STRING) {
- next(p);
- }
- else {
- syntax_error(p, 1, "string constant expected");
- }
-}
/*
@@ -1264,6 +1275,38 @@
match(p, T_NEWLINE);
}
+/*
+
+=item global_assignment()
+
+Only PMCs can be stored as globals, so only PMC registers and identifiers
+are allowed. Currently no check is done on the type of the identifier
+
+ global_assignment -> 'global' stringconstant '=' (IDENT|PREG) '\n'
+
+=cut
+
+*/
+static void
+global_assignment(parser_state *p) {
+ match(p, T_GLOBAL);
+ stringconstant(p);
+ match(p, T_ASSIGN);
+
+ switch (p->curtoken) {
+ case T_IDENTIFIER:
+ case T_PASM_PREG:
+ case T_PREG:
+ next(p);
+ break;
+ default:
+ syntax_error(p, 1, "PMC object (register or identifier) expected");
+ break;
+ }
+
+ match(p, T_NEWLINE);
+}
+
/*
@@ -1290,6 +1333,7 @@
| long_yield_statement
| NULL var
| get_results_instruction
+ | global_assignment
| '\n'
=cut
@@ -1387,6 +1431,9 @@
case T_GET_RESULTS: /* '.get_results' target_list */
get_results_instruction(p);
break;
+ case T_GLOBAL:
+ global_assignment(p);
+ break;
case T_NEWLINE: /* skip newlines */
next(p);
break;
@@ -1803,7 +1850,7 @@
static void
compilation_unit(parser_state *p) {
switch (p->curtoken) {
- case T_GLOBAL: /* compilation_unit -> global_definition */
+ case T_GLOBAL_DECL: /* compilation_unit -> global_definition */
global_definition(p);
break;
case T_SUB: /* compilation_unit -> sub_definition */