first implementation of centralized global state.

This commit is contained in:
Roberto Ierusalimschy
1997-11-19 15:29:23 -02:00
parent 9cdeb275e7
commit 592a3f289b
25 changed files with 782 additions and 829 deletions

372
llex.c
View File

@@ -1,5 +1,5 @@
/*
** $Id: llex.c,v 1.4 1997/11/04 15:27:53 roberto Exp roberto $
** $Id: llex.c,v 1.5 1997/11/07 15:09:49 roberto Exp roberto $
** Lexical Analizer
** See Copyright Notice in lua.h
*/
@@ -12,35 +12,32 @@
#include "lmem.h"
#include "lobject.h"
#include "lparser.h"
#include "lstate.h"
#include "lstring.h"
#include "lstx.h"
#include "luadebug.h"
#include "lzio.h"
static int current; /* look ahead character */
static ZIO *lex_z;
int luaX_linenumber;
int lua_debug=0;
#define next() (current = zgetc(lex_z))
#define next(LL) (LL->current = zgetc(LL->lex_z))
static struct {
char *name;
int token;
} reserved [] = {
{"and", AND}, {"do", DO}, {"else", ELSE}, {"elseif", ELSEIF},
{"end", END}, {"function", FUNCTION}, {"if", IF}, {"local", LOCAL},
{"nil", NIL}, {"not", NOT}, {"or", OR}, {"repeat", REPEAT},
{"return", RETURN}, {"then", THEN}, {"until", UNTIL}, {"while", WHILE}
};
void luaX_init (void)
{
static struct {
char *name;
int token;
} reserved [] = {
{"and", AND}, {"do", DO}, {"else", ELSE}, {"elseif", ELSEIF},
{"end", END}, {"function", FUNCTION}, {"if", IF}, {"local", LOCAL},
{"nil", NIL}, {"not", NOT}, {"or", OR}, {"repeat", REPEAT},
{"return", RETURN}, {"then", THEN}, {"until", UNTIL}, {"while", WHILE}
};
int i;
for (i=0; i<(sizeof(reserved)/sizeof(reserved[0])); i++) {
TaggedString *ts = luaS_new(reserved[i].name);
@@ -49,47 +46,29 @@ void luaX_init (void)
}
#define MAX_IFS 5
/* "ifstate" keeps the state of each nested $if the lexical is dealing with. */
static struct {
int elsepart; /* true if its in the $else part */
int condition; /* true if $if condition is true */
int skip; /* true if part must be skiped */
} ifstate[MAX_IFS];
static int iflevel; /* level of nested $if's */
static struct textbuff {
char *text;
int tokensize;
int buffsize;
} textbuff;
static void firstline (void)
static void firstline (LexState *LL)
{
int c = zgetc(lex_z);
int c = zgetc(LL->lex_z);
if (c == '#')
while((c=zgetc(lex_z)) != '\n' && c != EOZ) /* skip first line */;
zungetc(lex_z);
while((c=zgetc(LL->lex_z)) != '\n' && c != EOZ) /* skip first line */;
zungetc(LL->lex_z);
}
void luaX_setinput (ZIO *z)
{
current = '\n';
luaX_linenumber = 0;
iflevel = 0;
ifstate[0].skip = 0;
ifstate[0].elsepart = 1; /* to avoid a free $else */
lex_z = z;
firstline();
textbuff.buffsize = 20;
textbuff.text = luaM_buffer(textbuff.buffsize);
LexState *LL = L->lexstate;
LL->current = '\n';
LL->linelasttoken = 0;
LL->lastline = 0;
LL->linenumber = 0;
LL->iflevel = 0;
LL->ifstate[0].skip = 0;
LL->ifstate[0].elsepart = 1; /* to avoid a free $else */
LL->lex_z = z;
firstline(LL);
LL->textbuff.buffsize = 20;
LL->textbuff.text = luaM_buffer(LL->textbuff.buffsize);
}
@@ -102,9 +81,9 @@ void luaX_setinput (ZIO *z)
#define PRAGMASIZE 20
static void skipspace (void)
static void skipspace (LexState *LL)
{
while (current == ' ' || current == '\t') next();
while (LL->current == ' ' || LL->current == '\t') next(LL);
}
@@ -122,49 +101,49 @@ static int checkcond (char *buff)
}
static void readname (char *buff)
static void readname (LexState *LL, char *buff)
{
int i = 0;
skipspace();
while (isalnum(current) || current == '_') {
skipspace(LL);
while (isalnum(LL->current) || LL->current == '_') {
if (i >= PRAGMASIZE) {
buff[PRAGMASIZE] = 0;
luaY_syntaxerror("pragma too long", buff);
}
buff[i++] = current;
next();
buff[i++] = LL->current;
next(LL);
}
buff[i] = 0;
}
static void inclinenumber (void);
static void inclinenumber (LexState *LL);
static void ifskip (void)
static void ifskip (LexState *LL)
{
while (ifstate[iflevel].skip) {
if (current == '\n')
inclinenumber();
else if (current == EOZ)
while (LL->ifstate[LL->iflevel].skip) {
if (LL->current == '\n')
inclinenumber(LL);
else if (LL->current == EOZ)
luaY_syntaxerror("input ends inside a $if", "");
else next();
else next(LL);
}
}
static void inclinenumber (void)
static void inclinenumber (LexState *LL)
{
static char *pragmas [] =
{"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL};
next(); /* skip '\n' */
++luaX_linenumber;
if (current == '$') { /* is a pragma? */
next(LL); /* skip '\n' */
++LL->linenumber;
if (LL->current == '$') { /* is a pragma? */
char buff[PRAGMASIZE+1];
int ifnot = 0;
int skip = ifstate[iflevel].skip;
next(); /* skip $ */
readname(buff);
int skip = LL->ifstate[LL->iflevel].skip;
next(LL); /* skip $ */
readname(LL, buff);
switch (luaO_findstring(buff, pragmas)) {
case 0: /* debug */
if (!skip) lua_debug = 1;
@@ -174,42 +153,42 @@ static void inclinenumber (void)
break;
case 2: /* endinput */
if (!skip) {
current = EOZ;
iflevel = 0; /* to allow $endinput inside a $if */
LL->current = EOZ;
LL->iflevel = 0; /* to allow $endinput inside a $if */
}
break;
case 3: /* end */
if (iflevel-- == 0)
if (LL->iflevel-- == 0)
luaY_syntaxerror("unmatched $end", "$end");
break;
case 4: /* ifnot */
ifnot = 1;
/* go through */
case 5: /* if */
if (iflevel == MAX_IFS-1)
if (LL->iflevel == MAX_IFS-1)
luaY_syntaxerror("too many nested `$ifs'", "$if");
readname(buff);
iflevel++;
ifstate[iflevel].elsepart = 0;
ifstate[iflevel].condition = checkcond(buff) ? !ifnot : ifnot;
ifstate[iflevel].skip = skip || !ifstate[iflevel].condition;
readname(LL, buff);
LL->iflevel++;
LL->ifstate[LL->iflevel].elsepart = 0;
LL->ifstate[LL->iflevel].condition = checkcond(buff) ? !ifnot : ifnot;
LL->ifstate[LL->iflevel].skip = skip || !LL->ifstate[LL->iflevel].condition;
break;
case 6: /* else */
if (ifstate[iflevel].elsepart)
if (LL->ifstate[LL->iflevel].elsepart)
luaY_syntaxerror("unmatched $else", "$else");
ifstate[iflevel].elsepart = 1;
ifstate[iflevel].skip =
ifstate[iflevel-1].skip || ifstate[iflevel].condition;
LL->ifstate[LL->iflevel].elsepart = 1;
LL->ifstate[LL->iflevel].skip = LL->ifstate[LL->iflevel-1].skip ||
LL->ifstate[LL->iflevel].condition;
break;
default:
luaY_syntaxerror("invalid pragma", buff);
}
skipspace();
if (current == '\n') /* pragma must end with a '\n' ... */
inclinenumber();
else if (current != EOZ) /* or eof */
skipspace(LL);
if (LL->current == '\n') /* pragma must end with a '\n' ... */
inclinenumber(LL);
else if (LL->current != EOZ) /* or eof */
luaY_syntaxerror("invalid pragma format", buff);
ifskip();
ifskip(LL);
}
}
@@ -222,162 +201,167 @@ static void inclinenumber (void)
static void save (int c)
static void save (LexState *LL, int c)
{
if (textbuff.tokensize >= textbuff.buffsize)
textbuff.text = luaM_buffer(textbuff.buffsize *= 2);
textbuff.text[textbuff.tokensize++] = c;
if (LL->textbuff.tokensize >= LL->textbuff.buffsize)
LL->textbuff.text = luaM_buffer(LL->textbuff.buffsize *= 2);
LL->textbuff.text[LL->textbuff.tokensize++] = c;
}
char *luaX_lasttoken (void)
{
save(0);
return textbuff.text;
save(L->lexstate, 0);
return L->lexstate->textbuff.text;
}
#define save_and_next() (save(current), next())
#define save_and_next(LL) (save(LL, LL->current), next(LL))
static int read_long_string (void)
static int read_long_string (LexState *LL, YYSTYPE *l)
{
int cont = 0;
while (1) {
switch (current) {
switch (LL->current) {
case EOZ:
save(0);
save(LL, 0);
return WRONGTOKEN;
case '[':
save_and_next();
if (current == '[') {
save_and_next(LL);
if (LL->current == '[') {
cont++;
save_and_next();
save_and_next(LL);
}
continue;
case ']':
save_and_next();
if (current == ']') {
save_and_next(LL);
if (LL->current == ']') {
if (cont == 0) goto endloop;
cont--;
save_and_next();
save_and_next(LL);
}
continue;
case '\n':
save('\n');
inclinenumber();
save(LL, '\n');
inclinenumber(LL);
continue;
default:
save_and_next();
save_and_next(LL);
}
} endloop:
save_and_next(); /* pass the second ']' */
textbuff.text[textbuff.tokensize-2] = 0; /* erases ']]' */
luaY_lval.pTStr = luaS_new(textbuff.text+2);
textbuff.text[textbuff.tokensize-2] = ']'; /* restores ']]' */
save_and_next(LL); /* pass the second ']' */
LL->textbuff.text[LL->textbuff.tokensize-2] = 0; /* erases ']]' */
l->pTStr = luaS_new(LL->textbuff.text+2);
LL->textbuff.text[LL->textbuff.tokensize-2] = ']'; /* restores ']]' */
return STRING;
}
int luaY_lex (void)
/* to avoid warnings; this declaration cannot be public since YYSTYPE
** cannot be visible in llex.h (otherwise there is an error, since
** the parser body redefines it!)
*/
int luaY_lex (YYSTYPE *l);
int luaY_lex (YYSTYPE *l)
{
static int linelasttoken = 0;
LexState *LL = L->lexstate;
double a;
textbuff.tokensize = 0;
LL->textbuff.tokensize = 0;
if (lua_debug)
luaY_codedebugline(linelasttoken);
linelasttoken = luaX_linenumber;
luaY_codedebugline(LL->linelasttoken);
LL->linelasttoken = LL->linenumber;
while (1) {
switch (current) {
switch (LL->current) {
case '\n':
inclinenumber();
linelasttoken = luaX_linenumber;
inclinenumber(LL);
LL->linelasttoken = LL->linenumber;
continue;
case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */
next();
next(LL);
continue;
case '-':
save_and_next();
if (current != '-') return '-';
do { next(); } while (current != '\n' && current != EOZ);
textbuff.tokensize = 0;
save_and_next(LL);
if (LL->current != '-') return '-';
do { next(LL); } while (LL->current != '\n' && LL->current != EOZ);
LL->textbuff.tokensize = 0;
continue;
case '[':
save_and_next();
if (current != '[') return '[';
save_and_next(LL);
if (LL->current != '[') return '[';
else {
save_and_next(); /* pass the second '[' */
return read_long_string();
save_and_next(LL); /* pass the second '[' */
return read_long_string(LL, l);
}
case '=':
save_and_next();
if (current != '=') return '=';
else { save_and_next(); return EQ; }
save_and_next(LL);
if (LL->current != '=') return '=';
else { save_and_next(LL); return EQ; }
case '<':
save_and_next();
if (current != '=') return '<';
else { save_and_next(); return LE; }
save_and_next(LL);
if (LL->current != '=') return '<';
else { save_and_next(LL); return LE; }
case '>':
save_and_next();
if (current != '=') return '>';
else { save_and_next(); return GE; }
save_and_next(LL);
if (LL->current != '=') return '>';
else { save_and_next(LL); return GE; }
case '~':
save_and_next();
if (current != '=') return '~';
else { save_and_next(); return NE; }
save_and_next(LL);
if (LL->current != '=') return '~';
else { save_and_next(LL); return NE; }
case '"':
case '\'': {
int del = current;
save_and_next();
while (current != del) {
switch (current) {
int del = LL->current;
save_and_next(LL);
while (LL->current != del) {
switch (LL->current) {
case EOZ:
case '\n':
save(0);
save(LL, 0);
return WRONGTOKEN;
case '\\':
next(); /* do not save the '\' */
switch (current) {
case 'n': save('\n'); next(); break;
case 't': save('\t'); next(); break;
case 'r': save('\r'); next(); break;
case '\n': save('\n'); inclinenumber(); break;
default : save_and_next(); break;
next(LL); /* do not save the '\' */
switch (LL->current) {
case 'n': save(LL, '\n'); next(LL); break;
case 't': save(LL, '\t'); next(LL); break;
case 'r': save(LL, '\r'); next(LL); break;
case '\n': save(LL, '\n'); inclinenumber(LL); break;
default : save_and_next(LL); break;
}
break;
default:
save_and_next();
save_and_next(LL);
}
}
next(); /* skip delimiter */
save(0);
luaY_lval.pTStr = luaS_new(textbuff.text+1);
textbuff.text[textbuff.tokensize-1] = del; /* restore delimiter */
next(LL); /* skip delimiter */
save(LL, 0);
l->pTStr = luaS_new(LL->textbuff.text+1);
LL->textbuff.text[LL->textbuff.tokensize-1] = del; /* restore delimiter */
return STRING;
}
case '.':
save_and_next();
if (current == '.')
save_and_next(LL);
if (LL->current == '.')
{
save_and_next();
if (current == '.')
save_and_next(LL);
if (LL->current == '.')
{
save_and_next();
save_and_next(LL);
return DOTS; /* ... */
}
else return CONC; /* .. */
}
else if (!isdigit(current)) return '.';
/* current is a digit: goes through to number */
else if (!isdigit(LL->current)) return '.';
/* LL->current is a digit: goes through to number */
a=0.0;
goto fraction;
@@ -385,69 +369,69 @@ int luaY_lex (void)
case '5': case '6': case '7': case '8': case '9':
a=0.0;
do {
a=10.0*a+(current-'0');
save_and_next();
} while (isdigit(current));
if (current == '.') {
save_and_next();
if (current == '.') {
save(0);
a=10.0*a+(LL->current-'0');
save_and_next(LL);
} while (isdigit(LL->current));
if (LL->current == '.') {
save_and_next(LL);
if (LL->current == '.') {
save(LL, 0);
luaY_error(
"ambiguous syntax (decimal point x string concatenation)");
}
}
fraction:
{ double da=0.1;
while (isdigit(current))
while (isdigit(LL->current))
{
a+=(current-'0')*da;
a+=(LL->current-'0')*da;
da/=10.0;
save_and_next();
save_and_next(LL);
}
if (toupper(current) == 'E') {
if (toupper(LL->current) == 'E') {
int e=0;
int neg;
double ea;
save_and_next();
neg=(current=='-');
if (current == '+' || current == '-') save_and_next();
if (!isdigit(current)) {
save(0); return WRONGTOKEN; }
save_and_next(LL);
neg=(LL->current=='-');
if (LL->current == '+' || LL->current == '-') save_and_next(LL);
if (!isdigit(LL->current)) {
save(LL, 0); return WRONGTOKEN; }
do {
e=10.0*e+(current-'0');
save_and_next();
} while (isdigit(current));
e=10.0*e+(LL->current-'0');
save_and_next(LL);
} while (isdigit(LL->current));
for (ea=neg?0.1:10.0; e>0; e>>=1)
{
if (e & 1) a*=ea;
ea*=ea;
}
}
luaY_lval.vReal = a;
l->vReal = a;
return NUMBER;
}
case EOZ:
save(0);
if (iflevel > 0)
save(LL, 0);
if (LL->iflevel > 0)
luaY_error("missing $endif");
return 0;
default:
if (current != '_' && !isalpha(current)) {
save_and_next();
return textbuff.text[0];
if (LL->current != '_' && !isalpha(LL->current)) {
save_and_next(LL);
return LL->textbuff.text[0];
}
else { /* identifier or reserved word */
TaggedString *ts;
do {
save_and_next();
} while (isalnum(current) || current == '_');
save(0);
ts = luaS_new(textbuff.text);
save_and_next(LL);
} while (isalnum(LL->current) || LL->current == '_');
save(LL, 0);
ts = luaS_new(LL->textbuff.text);
if (ts->head.marked > 255)
return ts->head.marked; /* reserved word */
luaY_lval.pTStr = ts;
l->pTStr = ts;
return NAME;
}
}