1994-09-05 23:14:40 +04:00
|
|
|
char *rcs_lex = "$Id: lex.c,v 2.3 1994/08/17 17:41:50 celes Exp celes $";
|
1993-12-23 00:39:15 +03:00
|
|
|
/*$Log: lex.c,v $
|
1994-09-05 23:14:40 +04:00
|
|
|
* Revision 2.3 1994/08/17 17:41:50 celes
|
|
|
|
* Implementacao da macro 'lua_strcmp'
|
|
|
|
*
|
1994-08-17 21:41:50 +04:00
|
|
|
* Revision 2.2 1994/08/05 19:27:41 celes
|
|
|
|
* implementacao de dois buffer de 'yytext' para evitar bug
|
|
|
|
* no look ahead do yacc
|
|
|
|
*
|
1994-08-05 23:27:41 +04:00
|
|
|
* Revision 2.1 1994/04/15 19:00:28 celes
|
|
|
|
* Retirar chamada da funcao lua_findsymbol associada a cada
|
|
|
|
* token NAME. A decisao de chamar lua_findsymbol ou lua_findconstant
|
|
|
|
* fica a cargo do modulo "lua.stx".
|
|
|
|
*
|
1994-04-15 23:00:28 +04:00
|
|
|
* Revision 1.3 1993/12/28 16:42:29 roberto
|
|
|
|
* "include"s de string.h e stdlib.h para evitar warnings
|
|
|
|
*
|
1993-12-28 19:42:29 +03:00
|
|
|
* Revision 1.2 1993/12/22 21:39:15 celes
|
|
|
|
* Tratamento do token $debug e $nodebug
|
|
|
|
*
|
1993-12-23 00:39:15 +03:00
|
|
|
* Revision 1.1 1993/12/22 21:15:16 roberto
|
|
|
|
* Initial revision
|
|
|
|
**/
|
1993-12-23 00:15:16 +03:00
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <math.h>
|
1993-12-28 19:42:29 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1993-12-23 00:15:16 +03:00
|
|
|
|
|
|
|
#include "opcode.h"
|
|
|
|
#include "hash.h"
|
|
|
|
#include "inout.h"
|
|
|
|
#include "table.h"
|
|
|
|
#include "y.tab.h"
|
|
|
|
|
1994-08-17 21:41:50 +04:00
|
|
|
#define lua_strcmp(a,b) (a[0]<b[0]?(-1):(a[0]>b[0]?(1):strcmp(a,b)))
|
|
|
|
|
1993-12-23 00:15:16 +03:00
|
|
|
#define next() { current = input(); }
|
|
|
|
#define save(x) { *yytextLast++ = (x); }
|
|
|
|
#define save_and_next() { save(current); next(); }
|
|
|
|
|
|
|
|
static int current;
|
1994-08-05 23:27:41 +04:00
|
|
|
static char yytext[2][256];
|
1993-12-23 00:15:16 +03:00
|
|
|
static char *yytextLast;
|
1994-08-05 23:27:41 +04:00
|
|
|
static int currentText = 0;
|
1993-12-23 00:15:16 +03:00
|
|
|
|
|
|
|
static Input input;
|
|
|
|
|
|
|
|
void lua_setinput (Input fn)
|
|
|
|
{
|
|
|
|
current = ' ';
|
|
|
|
input = fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *lua_lasttext (void)
|
|
|
|
{
|
|
|
|
*yytextLast = 0;
|
1994-08-05 23:27:41 +04:00
|
|
|
return yytext[currentText];
|
1993-12-23 00:15:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-08-05 23:27:41 +04:00
|
|
|
/* The reserved words must be listed in lexicographic order */
|
1993-12-23 00:15:16 +03:00
|
|
|
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} };
|
|
|
|
|
|
|
|
#define RESERVEDSIZE (sizeof(reserved)/sizeof(reserved[0]))
|
|
|
|
|
|
|
|
|
1994-08-05 23:27:41 +04:00
|
|
|
static int findReserved (char *name)
|
1993-12-23 00:15:16 +03:00
|
|
|
{
|
|
|
|
int l = 0;
|
|
|
|
int h = RESERVEDSIZE - 1;
|
|
|
|
while (l <= h)
|
|
|
|
{
|
|
|
|
int m = (l+h)/2;
|
1994-08-17 21:41:50 +04:00
|
|
|
int comp = lua_strcmp(name, reserved[m].name);
|
1993-12-23 00:15:16 +03:00
|
|
|
if (comp < 0)
|
|
|
|
h = m-1;
|
|
|
|
else if (comp == 0)
|
|
|
|
return reserved[m].token;
|
|
|
|
else
|
|
|
|
l = m+1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int yylex ()
|
|
|
|
{
|
1994-08-05 23:27:41 +04:00
|
|
|
currentText = !currentText;
|
1993-12-23 00:15:16 +03:00
|
|
|
while (1)
|
|
|
|
{
|
1994-08-05 23:27:41 +04:00
|
|
|
yytextLast = yytext[currentText];
|
1993-12-23 00:15:16 +03:00
|
|
|
switch (current)
|
|
|
|
{
|
|
|
|
case '\n': lua_linenumber++;
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
1993-12-23 00:39:15 +03:00
|
|
|
next();
|
1993-12-23 00:15:16 +03:00
|
|
|
continue;
|
1993-12-23 00:39:15 +03:00
|
|
|
|
|
|
|
case '$':
|
|
|
|
next();
|
|
|
|
while (isalnum(current) || current == '_')
|
|
|
|
save_and_next();
|
|
|
|
*yytextLast = 0;
|
1994-08-17 21:41:50 +04:00
|
|
|
if (lua_strcmp(yytext[currentText], "debug") == 0)
|
1993-12-23 00:39:15 +03:00
|
|
|
{
|
|
|
|
yylval.vInt = 1;
|
|
|
|
return DEBUG;
|
|
|
|
}
|
1994-08-17 21:41:50 +04:00
|
|
|
else if (lua_strcmp(yytext[currentText], "nodebug") == 0)
|
1993-12-23 00:39:15 +03:00
|
|
|
{
|
|
|
|
yylval.vInt = 0;
|
|
|
|
return DEBUG;
|
|
|
|
}
|
|
|
|
return WRONGTOKEN;
|
1993-12-23 00:15:16 +03:00
|
|
|
|
|
|
|
case '-':
|
|
|
|
save_and_next();
|
|
|
|
if (current != '-') return '-';
|
1993-12-23 00:39:15 +03:00
|
|
|
do { next(); } while (current != '\n' && current != 0);
|
1993-12-23 00:15:16 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
case '<':
|
|
|
|
save_and_next();
|
|
|
|
if (current != '=') return '<';
|
|
|
|
else { save_and_next(); return LE; }
|
|
|
|
|
|
|
|
case '>':
|
|
|
|
save_and_next();
|
|
|
|
if (current != '=') return '>';
|
|
|
|
else { save_and_next(); return GE; }
|
|
|
|
|
|
|
|
case '~':
|
|
|
|
save_and_next();
|
|
|
|
if (current != '=') return '~';
|
|
|
|
else { save_and_next(); return NE; }
|
|
|
|
|
|
|
|
case '"':
|
|
|
|
case '\'':
|
|
|
|
{
|
|
|
|
int del = current;
|
|
|
|
next(); /* skip the delimiter */
|
|
|
|
while (current != del)
|
|
|
|
{
|
|
|
|
switch (current)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case '\n':
|
|
|
|
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;
|
1994-09-05 23:14:40 +04:00
|
|
|
case '\'': save('\''); next(); break;
|
|
|
|
case '"': save('"'); next(); break;
|
|
|
|
default : save(current); next(); break;
|
1993-12-23 00:15:16 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
save_and_next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
next(); /* skip the delimiter */
|
|
|
|
*yytextLast = 0;
|
1994-08-05 23:27:41 +04:00
|
|
|
yylval.vWord = lua_findconstant (yytext[currentText]);
|
1993-12-23 00:15:16 +03:00
|
|
|
return STRING;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'a': case 'b': case 'c': case 'd': case 'e':
|
|
|
|
case 'f': case 'g': case 'h': case 'i': case 'j':
|
|
|
|
case 'k': case 'l': case 'm': case 'n': case 'o':
|
|
|
|
case 'p': case 'q': case 'r': case 's': case 't':
|
|
|
|
case 'u': case 'v': case 'w': case 'x': case 'y':
|
|
|
|
case 'z':
|
|
|
|
case 'A': case 'B': case 'C': case 'D': case 'E':
|
|
|
|
case 'F': case 'G': case 'H': case 'I': case 'J':
|
|
|
|
case 'K': case 'L': case 'M': case 'N': case 'O':
|
|
|
|
case 'P': case 'Q': case 'R': case 'S': case 'T':
|
|
|
|
case 'U': case 'V': case 'W': case 'X': case 'Y':
|
|
|
|
case 'Z':
|
|
|
|
case '_':
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
do { save_and_next(); } while (isalnum(current) || current == '_');
|
|
|
|
*yytextLast = 0;
|
1994-08-05 23:27:41 +04:00
|
|
|
res = findReserved(yytext[currentText]);
|
1993-12-23 00:15:16 +03:00
|
|
|
if (res) return res;
|
1994-08-05 23:27:41 +04:00
|
|
|
yylval.pChar = yytext[currentText];
|
1993-12-23 00:15:16 +03:00
|
|
|
return NAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
case '.':
|
|
|
|
save_and_next();
|
|
|
|
if (current == '.')
|
|
|
|
{
|
|
|
|
save_and_next();
|
|
|
|
return CONC;
|
|
|
|
}
|
|
|
|
else if (!isdigit(current)) return '.';
|
|
|
|
/* current is a digit: goes through to number */
|
|
|
|
goto fraction;
|
|
|
|
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
|
|
|
|
|
|
|
do { save_and_next(); } while (isdigit(current));
|
|
|
|
if (current == '.') save_and_next();
|
|
|
|
fraction: while (isdigit(current)) save_and_next();
|
|
|
|
if (current == 'e' || current == 'E')
|
|
|
|
{
|
|
|
|
save_and_next();
|
|
|
|
if (current == '+' || current == '-') save_and_next();
|
|
|
|
if (!isdigit(current)) return WRONGTOKEN;
|
|
|
|
do { save_and_next(); } while (isdigit(current));
|
|
|
|
}
|
|
|
|
*yytextLast = 0;
|
1994-08-05 23:27:41 +04:00
|
|
|
yylval.vFloat = atof(yytext[currentText]);
|
1993-12-23 00:15:16 +03:00
|
|
|
return NUMBER;
|
|
|
|
|
1993-12-23 00:39:15 +03:00
|
|
|
default: /* also end of file */
|
1993-12-23 00:15:16 +03:00
|
|
|
{
|
|
|
|
save_and_next();
|
1994-08-05 23:27:41 +04:00
|
|
|
return yytext[currentText][0];
|
1993-12-23 00:15:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|