1993-12-17 21:53:07 +03:00
|
|
|
%{
|
|
|
|
|
1997-07-30 00:38:45 +04:00
|
|
|
char *rcs_luastx = "$Id: lua.stx,v 3.47 1997/06/19 17:46:12 roberto Exp roberto $";
|
1993-12-17 21:53:07 +03:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1996-05-29 01:07:32 +04:00
|
|
|
#include <string.h>
|
1993-12-17 21:53:07 +03:00
|
|
|
|
1996-02-07 21:10:27 +03:00
|
|
|
#include "luadebug.h"
|
1997-03-31 18:19:01 +04:00
|
|
|
#include "luamem.h"
|
1996-02-13 20:30:39 +03:00
|
|
|
#include "lex.h"
|
1993-12-17 21:53:07 +03:00
|
|
|
#include "opcode.h"
|
|
|
|
#include "hash.h"
|
|
|
|
#include "inout.h"
|
1994-11-15 00:40:14 +03:00
|
|
|
#include "tree.h"
|
1993-12-17 21:53:07 +03:00
|
|
|
#include "table.h"
|
|
|
|
#include "lua.h"
|
1995-10-04 20:13:02 +03:00
|
|
|
#include "func.h"
|
1993-12-17 21:53:07 +03:00
|
|
|
|
1994-11-23 17:39:52 +03:00
|
|
|
/* to avoid warnings generated by yacc */
|
|
|
|
int yyparse (void);
|
|
|
|
#define malloc luaI_malloc
|
|
|
|
#define realloc luaI_realloc
|
|
|
|
#define free luaI_free
|
|
|
|
|
1994-11-09 21:07:38 +03:00
|
|
|
#ifndef LISTING
|
1994-11-17 22:09:46 +03:00
|
|
|
#define LISTING 0
|
1994-11-09 21:07:38 +03:00
|
|
|
#endif
|
1994-02-13 23:38:20 +03:00
|
|
|
|
1994-07-20 01:27:18 +04:00
|
|
|
#ifndef CODE_BLOCK
|
1997-07-30 00:38:45 +04:00
|
|
|
#define CODE_BLOCK 1000
|
1993-12-17 21:53:07 +03:00
|
|
|
#endif
|
1997-07-30 00:38:45 +04:00
|
|
|
|
|
|
|
#define MAXLOCALS 32
|
|
|
|
|
|
|
|
/* state needed to generate code for a given function */
|
|
|
|
struct State {
|
|
|
|
TFunc *f; /* current function header */
|
|
|
|
int codesize;
|
|
|
|
int pc; /* next position to code */
|
|
|
|
TaggedString *localvar[MAXLOCALS]; /* store local variable names */
|
|
|
|
int nlocalvar; /* number of active local variables */
|
|
|
|
int nvars; /* total number of local variables (for debugging information) */
|
|
|
|
int maxvars; /* = -1 if no debug information */
|
|
|
|
} stateMain, stateFunc, *currState;
|
1993-12-17 21:53:07 +03:00
|
|
|
|
1995-10-25 16:05:51 +03:00
|
|
|
|
1993-12-17 21:53:07 +03:00
|
|
|
#define MAXVAR 32
|
1994-12-21 00:20:36 +03:00
|
|
|
static Long varbuffer[MAXVAR]; /* variables in an assignment list;
|
1994-02-13 23:38:20 +03:00
|
|
|
it's long to store negative Word values */
|
|
|
|
static int nvarbuffer=0; /* number of variables at a list */
|
|
|
|
|
1993-12-17 21:53:07 +03:00
|
|
|
|
1994-02-13 23:38:20 +03:00
|
|
|
#define MAXFIELDS FIELDS_PER_FLUSH*2
|
1993-12-17 21:53:07 +03:00
|
|
|
|
1996-02-07 21:10:27 +03:00
|
|
|
int lua_debug = 0;
|
1994-11-15 00:40:14 +03:00
|
|
|
|
1993-12-17 21:53:07 +03:00
|
|
|
/* Internal functions */
|
|
|
|
|
1995-10-26 17:21:56 +03:00
|
|
|
static void yyerror (char *s)
|
|
|
|
{
|
1996-11-08 15:49:35 +03:00
|
|
|
luaI_syntaxerror(s);
|
1995-10-26 17:21:56 +03:00
|
|
|
}
|
|
|
|
|
1996-05-29 01:07:32 +04:00
|
|
|
static void check_space (int i)
|
|
|
|
{
|
1997-07-30 00:38:45 +04:00
|
|
|
if (currState->pc+i >= currState->codesize)
|
|
|
|
currState->codesize = growvector(&currState->f->code, currState->codesize,
|
|
|
|
Byte, codeEM, MAX_INT);
|
1996-05-29 01:07:32 +04:00
|
|
|
}
|
|
|
|
|
1993-12-17 21:53:07 +03:00
|
|
|
static void code_byte (Byte c)
|
|
|
|
{
|
1997-07-30 00:38:45 +04:00
|
|
|
check_space(1);
|
|
|
|
currState->f->code[currState->pc++] = c;
|
1994-02-13 23:38:20 +03:00
|
|
|
}
|
|
|
|
|
1996-05-29 01:07:32 +04:00
|
|
|
static void code_float (real n)
|
1993-12-17 21:53:07 +03:00
|
|
|
{
|
1996-05-29 01:07:32 +04:00
|
|
|
check_space(sizeof(real));
|
1997-07-30 00:38:45 +04:00
|
|
|
memcpy(currState->f->code+currState->pc, &n, sizeof(real));
|
|
|
|
currState->pc += sizeof(real);
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
|
|
|
|
1995-10-04 20:13:02 +03:00
|
|
|
static void code_code (TFunc *tf)
|
1994-08-05 23:31:09 +04:00
|
|
|
{
|
1996-05-29 01:07:32 +04:00
|
|
|
check_space(sizeof(TFunc *));
|
1997-07-30 00:38:45 +04:00
|
|
|
memcpy(currState->f->code+currState->pc, &tf, sizeof(TFunc *));
|
|
|
|
currState->pc += sizeof(TFunc *);
|
1994-08-05 23:31:09 +04:00
|
|
|
}
|
|
|
|
|
1997-07-30 00:38:45 +04:00
|
|
|
static void code_word_at (int pc, int n)
|
1993-12-17 21:53:07 +03:00
|
|
|
{
|
1996-05-29 01:07:32 +04:00
|
|
|
Word w = n;
|
|
|
|
if (w != n)
|
|
|
|
yyerror("block too big");
|
1997-07-30 00:38:45 +04:00
|
|
|
memcpy(currState->f->code+pc, &w, sizeof(Word));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void code_word (Word n)
|
|
|
|
{
|
|
|
|
check_space(sizeof(Word));
|
|
|
|
memcpy(currState->f->code+currState->pc, &n, sizeof(Word));
|
|
|
|
currState->pc += sizeof(Word);
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
|
|
|
|
1994-02-13 23:38:20 +03:00
|
|
|
static void flush_record (int n)
|
|
|
|
{
|
|
|
|
if (n == 0) return;
|
1997-03-06 20:30:55 +03:00
|
|
|
code_byte(STOREMAP);
|
1994-02-13 23:38:20 +03:00
|
|
|
code_byte(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void flush_list (int m, int n)
|
|
|
|
{
|
|
|
|
if (n == 0) return;
|
|
|
|
if (m == 0)
|
|
|
|
code_byte(STORELIST0);
|
|
|
|
else
|
1994-08-03 18:15:46 +04:00
|
|
|
if (m < 255)
|
1994-02-13 23:38:20 +03:00
|
|
|
{
|
|
|
|
code_byte(STORELIST);
|
|
|
|
code_byte(m);
|
|
|
|
}
|
1994-08-03 18:15:46 +04:00
|
|
|
else
|
1996-02-05 16:26:01 +03:00
|
|
|
yyerror ("list constructor too long");
|
1994-02-13 23:38:20 +03:00
|
|
|
code_byte(n);
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
|
|
|
|
1997-07-30 00:38:45 +04:00
|
|
|
|
|
|
|
static void luaI_registerlocalvar (TaggedString *varname, int line)
|
|
|
|
{
|
|
|
|
if (currState->maxvars != -1) { /* debug information? */
|
|
|
|
if (currState->nvars >= currState->maxvars)
|
|
|
|
currState->maxvars = growvector(&currState->f->locvars,
|
|
|
|
currState->maxvars, LocVar, "", MAX_WORD);
|
|
|
|
currState->f->locvars[currState->nvars].varname = varname;
|
|
|
|
currState->f->locvars[currState->nvars].line = line;
|
|
|
|
currState->nvars++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void luaI_unregisterlocalvar (int line)
|
|
|
|
{
|
|
|
|
luaI_registerlocalvar(NULL, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-02-12 21:32:40 +03:00
|
|
|
static void store_localvar (TaggedString *name, int n)
|
1993-12-17 21:53:07 +03:00
|
|
|
{
|
1997-07-30 00:38:45 +04:00
|
|
|
if (currState->nlocalvar+n < MAXLOCALS)
|
|
|
|
currState->localvar[currState->nlocalvar+n] = name;
|
|
|
|
else
|
|
|
|
yyerror ("too many local variables");
|
|
|
|
luaI_registerlocalvar(name, lua_linenumber);
|
1996-02-07 21:10:27 +03:00
|
|
|
}
|
|
|
|
|
1996-02-12 21:32:40 +03:00
|
|
|
static void add_localvar (TaggedString *name)
|
1996-02-07 21:10:27 +03:00
|
|
|
{
|
|
|
|
store_localvar(name, 0);
|
1997-07-30 00:38:45 +04:00
|
|
|
currState->nlocalvar++;
|
1995-04-11 21:56:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void add_varbuffer (Long var)
|
|
|
|
{
|
|
|
|
if (nvarbuffer < MAXVAR)
|
|
|
|
varbuffer[nvarbuffer++] = var;
|
1993-12-17 21:53:07 +03:00
|
|
|
else
|
1996-02-05 16:26:01 +03:00
|
|
|
yyerror ("variable buffer overflow");
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
|
|
|
|
1997-03-06 20:30:55 +03:00
|
|
|
static void code_string (Word w)
|
|
|
|
{
|
|
|
|
code_byte(PUSHSTRING);
|
|
|
|
code_word(w);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void code_constant (TaggedString *s)
|
|
|
|
{
|
|
|
|
code_string(luaI_findconstant(s));
|
|
|
|
}
|
|
|
|
|
1993-12-17 21:53:07 +03:00
|
|
|
static void code_number (float f)
|
1994-07-20 01:27:18 +04:00
|
|
|
{
|
1997-01-31 17:27:11 +03:00
|
|
|
Word i;
|
|
|
|
if (f >= 0 && f <= (float)MAX_WORD && (float)(i=(Word)f) == f) {
|
|
|
|
/* f has an (short) integer value */
|
1993-12-17 21:53:07 +03:00
|
|
|
if (i <= 2) code_byte(PUSH0 + i);
|
|
|
|
else if (i <= 255)
|
|
|
|
{
|
|
|
|
code_byte(PUSHBYTE);
|
|
|
|
code_byte(i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
code_byte(PUSHWORD);
|
|
|
|
code_word(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
code_byte(PUSHFLOAT);
|
|
|
|
code_float(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-12-27 23:04:29 +03:00
|
|
|
/*
|
|
|
|
** Search a local name and if find return its index. If do not find return -1
|
|
|
|
*/
|
1996-02-12 21:32:40 +03:00
|
|
|
static int lua_localname (TaggedString *n)
|
1994-12-27 23:04:29 +03:00
|
|
|
{
|
|
|
|
int i;
|
1997-07-30 00:38:45 +04:00
|
|
|
for (i=currState->nlocalvar-1; i >= 0; i--)
|
|
|
|
if (n == currState->localvar[i]) return i; /* local var */
|
|
|
|
return -1; /* global var */
|
1994-12-27 23:04:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Push a variable given a number. If number is positive, push global variable
|
|
|
|
** indexed by (number -1). If negative, push local indexed by ABS(number)-1.
|
|
|
|
** Otherwise, if zero, push indexed variable (record).
|
|
|
|
*/
|
|
|
|
static void lua_pushvar (Long number)
|
|
|
|
{
|
|
|
|
if (number > 0) /* global var */
|
|
|
|
{
|
|
|
|
code_byte(PUSHGLOBAL);
|
|
|
|
code_word(number-1);
|
|
|
|
}
|
|
|
|
else if (number < 0) /* local var */
|
|
|
|
{
|
|
|
|
number = (-number) - 1;
|
|
|
|
if (number < 10) code_byte(PUSHLOCAL0 + number);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
code_byte(PUSHLOCAL);
|
|
|
|
code_byte(number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
code_byte(PUSHINDEXED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lua_codeadjust (int n)
|
|
|
|
{
|
1997-07-30 00:38:45 +04:00
|
|
|
n += currState->nlocalvar;
|
|
|
|
if (n == 0)
|
|
|
|
code_byte(ADJUST0);
|
|
|
|
else {
|
|
|
|
code_byte(ADJUST);
|
|
|
|
code_byte(n);
|
|
|
|
}
|
1994-12-27 23:04:29 +03:00
|
|
|
}
|
|
|
|
|
1995-10-25 16:05:51 +03:00
|
|
|
|
1997-07-30 00:38:45 +04:00
|
|
|
static void init_state (TFunc *f)
|
1995-10-25 16:05:51 +03:00
|
|
|
{
|
1997-07-30 00:38:45 +04:00
|
|
|
luaI_initTFunc(f);
|
|
|
|
currState->nlocalvar = 0;
|
|
|
|
currState->f = f;
|
|
|
|
currState->pc = 0;
|
|
|
|
currState->codesize = CODE_BLOCK;
|
|
|
|
f->code = newvector(CODE_BLOCK, Byte);
|
|
|
|
if (lua_debug) {
|
|
|
|
currState->nvars = 0;
|
|
|
|
currState->maxvars = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
currState->maxvars = -1; /* flag no debug information */
|
1994-12-27 23:04:29 +03:00
|
|
|
}
|
|
|
|
|
1997-07-30 00:38:45 +04:00
|
|
|
|
1995-10-26 17:21:56 +03:00
|
|
|
static void init_func (void)
|
|
|
|
{
|
1997-07-30 00:38:45 +04:00
|
|
|
currState = &stateFunc;
|
|
|
|
init_state(new(TFunc));
|
1995-10-26 17:21:56 +03:00
|
|
|
luaI_codedebugline(lua_linenumber);
|
|
|
|
}
|
|
|
|
|
1997-07-30 00:38:45 +04:00
|
|
|
|
1994-12-27 23:04:29 +03:00
|
|
|
static void codereturn (void)
|
|
|
|
{
|
1997-07-30 00:38:45 +04:00
|
|
|
if (currState->nlocalvar == 0)
|
1994-12-27 23:04:29 +03:00
|
|
|
code_byte(RETCODE0);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
code_byte(RETCODE);
|
1997-07-30 00:38:45 +04:00
|
|
|
code_byte(currState->nlocalvar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void close_func (void)
|
|
|
|
{
|
|
|
|
codereturn();
|
|
|
|
code_byte(ENDCODE);
|
|
|
|
currState->f->code = shrinkvector(currState->f->code, currState->pc, Byte);
|
|
|
|
if (currState->maxvars != -1) { /* debug information? */
|
|
|
|
luaI_registerlocalvar(NULL, -1); /* flag end of vector */
|
|
|
|
currState->f->locvars = shrinkvector(currState->f->locvars,
|
|
|
|
currState->nvars, LocVar);
|
1994-12-27 23:04:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-07-30 00:38:45 +04:00
|
|
|
|
1995-10-25 16:05:51 +03:00
|
|
|
void luaI_codedebugline (int line)
|
1994-12-27 23:04:29 +03:00
|
|
|
{
|
1995-10-25 16:05:51 +03:00
|
|
|
static int lastline = 0;
|
|
|
|
if (lua_debug && line != lastline)
|
1994-12-27 23:04:29 +03:00
|
|
|
{
|
|
|
|
code_byte(SETLINE);
|
1995-10-25 16:05:51 +03:00
|
|
|
code_word(line);
|
|
|
|
lastline = line;
|
1994-12-27 23:04:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-10-25 16:05:51 +03:00
|
|
|
static int adjust_functioncall (Long exp, int i)
|
1994-12-27 23:04:29 +03:00
|
|
|
{
|
1995-10-25 16:05:51 +03:00
|
|
|
if (exp <= 0)
|
|
|
|
return -exp; /* exp is -list length */
|
1997-07-30 00:38:45 +04:00
|
|
|
else {
|
|
|
|
int temp = currState->f->code[exp];
|
|
|
|
currState->f->code[exp] = i;
|
1995-10-25 16:05:51 +03:00
|
|
|
return temp+i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void adjust_mult_assign (int vars, Long exps, int temps)
|
|
|
|
{
|
1997-07-30 00:38:45 +04:00
|
|
|
if (exps > 0) { /* must correct function call */
|
|
|
|
int diff = vars - currState->f->code[exps];
|
1995-10-25 16:05:51 +03:00
|
|
|
if (diff >= 0)
|
|
|
|
adjust_functioncall(exps, diff);
|
1997-07-30 00:38:45 +04:00
|
|
|
else {
|
1995-10-25 16:05:51 +03:00
|
|
|
adjust_functioncall(exps, 0);
|
1994-12-27 23:04:29 +03:00
|
|
|
lua_codeadjust(temps);
|
|
|
|
}
|
|
|
|
}
|
1995-10-25 16:05:51 +03:00
|
|
|
else if (vars != -exps)
|
1994-12-27 23:04:29 +03:00
|
|
|
lua_codeadjust(temps);
|
|
|
|
}
|
|
|
|
|
1996-05-29 01:07:32 +04:00
|
|
|
static int close_parlist (int dots)
|
|
|
|
{
|
|
|
|
if (!dots)
|
|
|
|
lua_codeadjust(0);
|
1997-07-30 00:38:45 +04:00
|
|
|
else {
|
1996-05-29 01:07:32 +04:00
|
|
|
code_byte(VARARGS);
|
1997-07-30 00:38:45 +04:00
|
|
|
code_byte(currState->nlocalvar);
|
1996-05-29 01:07:32 +04:00
|
|
|
add_localvar(luaI_createfixedstring("arg"));
|
|
|
|
}
|
|
|
|
return lua_linenumber;
|
|
|
|
}
|
|
|
|
|
1997-07-30 00:38:45 +04:00
|
|
|
|
1995-10-26 17:21:56 +03:00
|
|
|
static void storesinglevar (Long v)
|
1994-12-27 23:04:29 +03:00
|
|
|
{
|
1995-10-26 17:21:56 +03:00
|
|
|
if (v > 0) /* global var */
|
1994-12-27 23:04:29 +03:00
|
|
|
{
|
1995-10-26 17:21:56 +03:00
|
|
|
code_byte(STOREGLOBAL);
|
|
|
|
code_word(v-1);
|
1994-12-27 23:04:29 +03:00
|
|
|
}
|
1995-10-26 17:21:56 +03:00
|
|
|
else if (v < 0) /* local var */
|
1994-12-27 23:04:29 +03:00
|
|
|
{
|
1995-10-26 17:21:56 +03:00
|
|
|
int number = (-v) - 1;
|
|
|
|
if (number < 10) code_byte(STORELOCAL0 + number);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
code_byte(STORELOCAL);
|
|
|
|
code_byte(number);
|
|
|
|
}
|
1994-12-27 23:04:29 +03:00
|
|
|
}
|
1995-10-26 17:21:56 +03:00
|
|
|
else
|
|
|
|
code_byte(STOREINDEXED0);
|
|
|
|
}
|
|
|
|
|
1997-07-30 00:38:45 +04:00
|
|
|
|
1995-10-26 17:21:56 +03:00
|
|
|
static void lua_codestore (int i)
|
|
|
|
{
|
|
|
|
if (varbuffer[i] != 0) /* global or local var */
|
|
|
|
storesinglevar(varbuffer[i]);
|
1994-12-27 23:04:29 +03:00
|
|
|
else /* indexed var */
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
int upper=0; /* number of indexed variables upper */
|
|
|
|
int param; /* number of itens until indexed expression */
|
|
|
|
for (j=i+1; j <nvarbuffer; j++)
|
|
|
|
if (varbuffer[j] == 0) upper++;
|
|
|
|
param = upper*2 + i;
|
|
|
|
if (param == 0)
|
|
|
|
code_byte(STOREINDEXED0);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
code_byte(STOREINDEXED);
|
|
|
|
code_byte(param);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void codeIf (Long thenAdd, Long elseAdd)
|
|
|
|
{
|
|
|
|
Long elseinit = elseAdd+sizeof(Word)+1;
|
1997-07-30 00:38:45 +04:00
|
|
|
if (currState->pc == elseinit) { /* no else */
|
|
|
|
currState->pc -= sizeof(Word)+1;
|
|
|
|
elseinit = currState->pc;
|
1994-12-27 23:04:29 +03:00
|
|
|
}
|
1997-07-30 00:38:45 +04:00
|
|
|
else {
|
|
|
|
currState->f->code[elseAdd] = JMP;
|
|
|
|
code_word_at(elseAdd+1, currState->pc-elseinit);
|
1994-12-27 23:04:29 +03:00
|
|
|
}
|
1997-07-30 00:38:45 +04:00
|
|
|
currState->f->code[thenAdd] = IFFJMP;
|
|
|
|
code_word_at(thenAdd+1, elseinit-(thenAdd+sizeof(Word)+1));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void code_shortcircuit (int pc, Byte jmp)
|
|
|
|
{
|
|
|
|
currState->f->code[pc] = jmp;
|
|
|
|
code_word_at(pc+1, currState->pc - (pc + sizeof(Word)+1));
|
1994-12-27 23:04:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Parse LUA code.
|
|
|
|
*/
|
1995-10-04 20:13:02 +03:00
|
|
|
void lua_parse (TFunc *tf)
|
1994-12-27 23:04:29 +03:00
|
|
|
{
|
1997-07-30 00:38:45 +04:00
|
|
|
currState = &stateMain;
|
|
|
|
init_state(tf);
|
|
|
|
if (yyparse ()) lua_error("parse error");
|
|
|
|
currState = &stateMain;
|
|
|
|
close_func();
|
1994-12-27 23:04:29 +03:00
|
|
|
}
|
|
|
|
|
1994-08-05 23:31:09 +04:00
|
|
|
|
1993-12-17 21:53:07 +03:00
|
|
|
%}
|
|
|
|
|
|
|
|
|
|
|
|
%union
|
|
|
|
{
|
|
|
|
int vInt;
|
|
|
|
float vFloat;
|
1994-04-15 23:02:04 +04:00
|
|
|
char *pChar;
|
1993-12-17 21:53:07 +03:00
|
|
|
Word vWord;
|
1994-07-20 01:27:18 +04:00
|
|
|
Long vLong;
|
1995-10-04 20:13:02 +03:00
|
|
|
TFunc *pFunc;
|
1996-02-12 21:32:40 +03:00
|
|
|
TaggedString *pTStr;
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
|
|
|
|
1996-09-24 21:29:50 +04:00
|
|
|
%start chunk
|
1993-12-17 21:53:07 +03:00
|
|
|
|
1993-12-23 00:19:23 +03:00
|
|
|
%token WRONGTOKEN
|
1993-12-17 21:53:07 +03:00
|
|
|
%token NIL
|
|
|
|
%token IF THEN ELSE ELSEIF WHILE DO REPEAT UNTIL END
|
|
|
|
%token RETURN
|
|
|
|
%token LOCAL
|
1994-08-05 23:31:09 +04:00
|
|
|
%token FUNCTION
|
1996-05-29 01:07:32 +04:00
|
|
|
%token DOTS
|
1993-12-17 21:53:07 +03:00
|
|
|
%token <vFloat> NUMBER
|
1994-11-15 00:40:14 +03:00
|
|
|
%token <vWord> STRING
|
1996-02-12 21:32:40 +03:00
|
|
|
%token <pTStr> NAME
|
1993-12-17 21:53:07 +03:00
|
|
|
|
1994-07-20 01:27:18 +04:00
|
|
|
%type <vLong> PrepJump
|
1995-10-25 16:05:51 +03:00
|
|
|
%type <vLong> exprlist, exprlist1 /* if > 0, points to function return
|
|
|
|
counter (which has list length); if <= 0, -list lenght */
|
|
|
|
%type <vLong> functioncall, expr /* if != 0, points to function return
|
|
|
|
counter */
|
|
|
|
%type <vInt> varlist1, funcParams, funcvalue
|
1994-11-02 23:30:53 +03:00
|
|
|
%type <vInt> fieldlist, localdeclist, decinit
|
1995-06-08 23:47:28 +04:00
|
|
|
%type <vInt> ffieldlist, ffieldlist1, semicolonpart
|
|
|
|
%type <vInt> lfieldlist, lfieldlist1
|
1996-05-29 01:07:32 +04:00
|
|
|
%type <vInt> parlist, parlist1, par
|
1995-10-26 17:21:56 +03:00
|
|
|
%type <vLong> var, singlevar, funcname
|
1995-10-04 20:13:02 +03:00
|
|
|
%type <pFunc> body
|
1993-12-17 21:53:07 +03:00
|
|
|
|
|
|
|
%left AND OR
|
1994-10-11 16:02:39 +03:00
|
|
|
%left EQ NE '>' '<' LE GE
|
1993-12-17 21:53:07 +03:00
|
|
|
%left CONC
|
|
|
|
%left '+' '-'
|
|
|
|
%left '*' '/'
|
|
|
|
%left UNARY NOT
|
1994-10-17 22:05:32 +03:00
|
|
|
%right '^'
|
1993-12-17 21:53:07 +03:00
|
|
|
|
|
|
|
|
|
|
|
%% /* beginning of rules section */
|
|
|
|
|
1997-02-13 19:18:39 +03:00
|
|
|
chunk : chunklist ret ;
|
1993-12-17 21:53:07 +03:00
|
|
|
|
1996-09-24 21:29:50 +04:00
|
|
|
chunklist : /* empty */
|
1996-09-26 01:52:00 +04:00
|
|
|
| chunklist stat sc
|
1996-09-24 21:29:50 +04:00
|
|
|
| chunklist function
|
|
|
|
;
|
1994-08-05 23:31:09 +04:00
|
|
|
|
1995-10-26 17:21:56 +03:00
|
|
|
function : FUNCTION funcname body
|
1993-12-17 21:53:07 +03:00
|
|
|
{
|
1995-10-25 17:33:25 +03:00
|
|
|
code_byte(PUSHFUNCTION);
|
|
|
|
code_code($3);
|
1995-10-26 17:21:56 +03:00
|
|
|
storesinglevar($2);
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
|
|
|
;
|
|
|
|
|
1995-10-26 17:21:56 +03:00
|
|
|
funcname : var { $$ =$1; init_func(); }
|
|
|
|
| varexp ':' NAME
|
1995-10-25 16:05:51 +03:00
|
|
|
{
|
1997-03-06 20:30:55 +03:00
|
|
|
code_constant($3);
|
1995-10-26 17:21:56 +03:00
|
|
|
$$ = 0; /* indexed variable */
|
|
|
|
init_func();
|
1996-02-27 00:00:27 +03:00
|
|
|
add_localvar(luaI_createfixedstring("self"));
|
1995-10-25 16:05:51 +03:00
|
|
|
}
|
1995-10-25 17:33:25 +03:00
|
|
|
;
|
|
|
|
|
1994-11-15 00:40:14 +03:00
|
|
|
body : '(' parlist ')' block END
|
|
|
|
{
|
1997-07-30 00:38:45 +04:00
|
|
|
close_func();
|
|
|
|
$$ = currState->f;
|
1995-10-26 17:21:56 +03:00
|
|
|
$$->lineDefined = $2;
|
1997-07-30 00:38:45 +04:00
|
|
|
currState = &stateMain; /* change back to main code */
|
1994-11-15 00:40:14 +03:00
|
|
|
}
|
|
|
|
;
|
|
|
|
|
1993-12-17 21:53:07 +03:00
|
|
|
statlist : /* empty */
|
|
|
|
| statlist stat sc
|
|
|
|
;
|
|
|
|
|
|
|
|
sc : /* empty */ | ';' ;
|
|
|
|
|
1995-10-25 16:05:51 +03:00
|
|
|
stat : IF expr1 THEN PrepJump block PrepJump elsepart END
|
1994-11-17 21:59:06 +03:00
|
|
|
{ codeIf($4, $6); }
|
|
|
|
|
1997-07-30 00:38:45 +04:00
|
|
|
| WHILE {$<vLong>$=currState->pc;} expr1 DO PrepJump block PrepJump END
|
1993-12-17 21:53:07 +03:00
|
|
|
{
|
1997-07-30 00:38:45 +04:00
|
|
|
currState->f->code[$5] = IFFJMP;
|
|
|
|
code_word_at($5+1, currState->pc - ($5+sizeof(Word)+1));
|
|
|
|
currState->f->code[$7] = UPJMP;
|
|
|
|
code_word_at($7+1, currState->pc - ($<vLong>2));
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
|
|
|
|
1997-07-30 00:38:45 +04:00
|
|
|
| REPEAT {$<vLong>$=currState->pc;} block UNTIL expr1 PrepJump
|
1993-12-17 21:53:07 +03:00
|
|
|
{
|
1997-07-30 00:38:45 +04:00
|
|
|
currState->f->code[$6] = IFFUPJMP;
|
|
|
|
code_word_at($6+1, currState->pc - ($<vLong>2));
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
| varlist1 '=' exprlist1
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int i;
|
1994-11-02 23:30:53 +03:00
|
|
|
adjust_mult_assign(nvarbuffer, $3, $1 * 2 + nvarbuffer);
|
1993-12-17 21:53:07 +03:00
|
|
|
for (i=nvarbuffer-1; i>=0; i--)
|
1997-07-30 00:38:45 +04:00
|
|
|
lua_codestore(i);
|
1993-12-17 21:53:07 +03:00
|
|
|
if ($1 > 1 || ($1 == 1 && varbuffer[0] != 0))
|
1997-07-30 00:38:45 +04:00
|
|
|
lua_codeadjust(0);
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
|
|
|
}
|
1997-01-15 19:11:37 +03:00
|
|
|
| functioncall {;}
|
1994-11-17 21:59:06 +03:00
|
|
|
| LOCAL localdeclist decinit
|
1997-07-30 00:38:45 +04:00
|
|
|
{ currState->nlocalvar += $2;
|
1994-11-02 23:30:53 +03:00
|
|
|
adjust_mult_assign($2, $3, 0);
|
1995-04-11 21:56:30 +04:00
|
|
|
}
|
1993-12-17 21:53:07 +03:00
|
|
|
;
|
|
|
|
|
|
|
|
elsepart : /* empty */
|
|
|
|
| ELSE block
|
1995-10-25 16:05:51 +03:00
|
|
|
| ELSEIF expr1 THEN PrepJump block PrepJump elsepart
|
1994-11-17 21:59:06 +03:00
|
|
|
{ codeIf($4, $6); }
|
1993-12-17 21:53:07 +03:00
|
|
|
;
|
|
|
|
|
1997-07-30 00:38:45 +04:00
|
|
|
block : {$<vInt>$ = currState->nlocalvar;} statlist ret
|
1993-12-17 21:53:07 +03:00
|
|
|
{
|
1997-07-30 00:38:45 +04:00
|
|
|
if (currState->nlocalvar != $<vInt>1) {
|
|
|
|
for (; currState->nlocalvar > $<vInt>1; currState->nlocalvar--)
|
|
|
|
luaI_unregisterlocalvar(lua_linenumber);
|
|
|
|
lua_codeadjust(0);
|
|
|
|
}
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
ret : /* empty */
|
1995-10-25 16:05:51 +03:00
|
|
|
| RETURN exprlist sc
|
1994-11-17 21:59:06 +03:00
|
|
|
{
|
1995-10-25 16:05:51 +03:00
|
|
|
adjust_functioncall($2, MULT_RET);
|
1994-11-02 23:30:53 +03:00
|
|
|
codereturn();
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
PrepJump : /* empty */
|
|
|
|
{
|
1997-07-30 00:38:45 +04:00
|
|
|
$$ = currState->pc;
|
1993-12-17 21:53:07 +03:00
|
|
|
code_byte(0); /* open space */
|
1997-07-30 00:38:45 +04:00
|
|
|
code_word(0);
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
1996-07-24 18:38:12 +04:00
|
|
|
;
|
1993-12-17 21:53:07 +03:00
|
|
|
|
1995-10-25 17:33:25 +03:00
|
|
|
expr1 : expr { adjust_functioncall($1, 1); }
|
1993-12-17 21:53:07 +03:00
|
|
|
;
|
|
|
|
|
1994-10-17 22:05:32 +03:00
|
|
|
expr : '(' expr ')' { $$ = $2; }
|
1995-10-25 16:05:51 +03:00
|
|
|
| expr1 EQ expr1 { code_byte(EQOP); $$ = 0; }
|
|
|
|
| expr1 '<' expr1 { code_byte(LTOP); $$ = 0; }
|
|
|
|
| expr1 '>' expr1 { code_byte(GTOP); $$ = 0; }
|
|
|
|
| expr1 NE expr1 { code_byte(EQOP); code_byte(NOTOP); $$ = 0; }
|
|
|
|
| expr1 LE expr1 { code_byte(LEOP); $$ = 0; }
|
|
|
|
| expr1 GE expr1 { code_byte(GEOP); $$ = 0; }
|
|
|
|
| expr1 '+' expr1 { code_byte(ADDOP); $$ = 0; }
|
|
|
|
| expr1 '-' expr1 { code_byte(SUBOP); $$ = 0; }
|
|
|
|
| expr1 '*' expr1 { code_byte(MULTOP); $$ = 0; }
|
|
|
|
| expr1 '/' expr1 { code_byte(DIVOP); $$ = 0; }
|
|
|
|
| expr1 '^' expr1 { code_byte(POWOP); $$ = 0; }
|
|
|
|
| expr1 CONC expr1 { code_byte(CONCOP); $$ = 0; }
|
|
|
|
| '-' expr1 %prec UNARY { code_byte(MINUSOP); $$ = 0;}
|
|
|
|
| table { $$ = 0; }
|
|
|
|
| varexp { $$ = 0;}
|
|
|
|
| NUMBER { code_number($1); $$ = 0; }
|
1994-10-17 22:05:32 +03:00
|
|
|
| STRING
|
1993-12-17 21:53:07 +03:00
|
|
|
{
|
1997-03-06 20:30:55 +03:00
|
|
|
code_string($1);
|
|
|
|
$$ = 0;
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
1995-10-25 16:05:51 +03:00
|
|
|
| NIL {code_byte(PUSHNIL); $$ = 0; }
|
|
|
|
| functioncall { $$ = $1; }
|
|
|
|
| NOT expr1 { code_byte(NOTOP); $$ = 0;}
|
1994-11-02 23:30:53 +03:00
|
|
|
| expr1 AND PrepJump {code_byte(POP); } expr1
|
1993-12-17 21:53:07 +03:00
|
|
|
{
|
1997-07-30 00:38:45 +04:00
|
|
|
code_shortcircuit($3, ONFJMP);
|
|
|
|
$$ = 0;
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
1994-11-02 23:30:53 +03:00
|
|
|
| expr1 OR PrepJump {code_byte(POP); } expr1
|
1993-12-17 21:53:07 +03:00
|
|
|
{
|
1997-07-30 00:38:45 +04:00
|
|
|
code_shortcircuit($3, ONTJMP);
|
|
|
|
$$ = 0;
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
|
|
|
;
|
|
|
|
|
1994-10-11 16:02:39 +03:00
|
|
|
table :
|
|
|
|
{
|
|
|
|
code_byte(CREATEARRAY);
|
1997-07-30 00:38:45 +04:00
|
|
|
$<vLong>$ = currState->pc; code_word(0);
|
1994-10-11 16:02:39 +03:00
|
|
|
}
|
|
|
|
'{' fieldlist '}'
|
|
|
|
{
|
1997-07-30 00:38:45 +04:00
|
|
|
code_word_at($<vLong>1, $3);
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
|
|
|
;
|
|
|
|
|
1994-11-02 23:30:53 +03:00
|
|
|
functioncall : funcvalue funcParams
|
1995-10-25 16:05:51 +03:00
|
|
|
{
|
|
|
|
code_byte(CALLFUNC);
|
|
|
|
code_byte($1+$2);
|
1997-07-30 00:38:45 +04:00
|
|
|
$$ = currState->pc;
|
1995-10-25 16:05:51 +03:00
|
|
|
code_byte(0); /* may be modified by other rules */
|
|
|
|
}
|
1994-10-17 22:05:32 +03:00
|
|
|
;
|
1994-11-02 23:30:53 +03:00
|
|
|
|
|
|
|
funcvalue : varexp { $$ = 0; }
|
1994-10-17 22:05:32 +03:00
|
|
|
| varexp ':' NAME
|
|
|
|
{
|
1994-11-02 23:30:53 +03:00
|
|
|
code_byte(PUSHSELF);
|
1994-12-06 17:27:18 +03:00
|
|
|
code_word(luaI_findconstant($3));
|
1994-11-02 23:30:53 +03:00
|
|
|
$$ = 1;
|
1994-10-17 22:05:32 +03:00
|
|
|
}
|
1994-10-11 16:02:39 +03:00
|
|
|
;
|
1994-11-02 23:30:53 +03:00
|
|
|
|
1994-10-17 22:05:32 +03:00
|
|
|
funcParams : '(' exprlist ')'
|
1995-10-25 16:05:51 +03:00
|
|
|
{ $$ = adjust_functioncall($2, 1); }
|
1994-11-02 23:30:53 +03:00
|
|
|
| table { $$ = 1; }
|
1994-10-17 22:05:32 +03:00
|
|
|
;
|
1994-11-02 23:30:53 +03:00
|
|
|
|
|
|
|
exprlist : /* empty */ { $$ = 0; }
|
1993-12-17 21:53:07 +03:00
|
|
|
| exprlist1 { $$ = $1; }
|
|
|
|
;
|
|
|
|
|
1995-10-25 16:05:51 +03:00
|
|
|
exprlist1 : expr { if ($1 != 0) $$ = $1; else $$ = -1; }
|
|
|
|
| exprlist1 ',' { $<vLong>$ = adjust_functioncall($1, 1); } expr
|
1994-11-02 23:30:53 +03:00
|
|
|
{
|
1995-10-25 16:05:51 +03:00
|
|
|
if ($4 == 0) $$ = -($<vLong>3 + 1); /* -length */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
adjust_functioncall($4, $<vLong>3);
|
|
|
|
$$ = $4;
|
|
|
|
}
|
1994-11-02 23:30:53 +03:00
|
|
|
}
|
1993-12-17 21:53:07 +03:00
|
|
|
;
|
|
|
|
|
1996-05-29 01:07:32 +04:00
|
|
|
parlist : /* empty */ { $$ = close_parlist(0); }
|
|
|
|
| parlist1 { $$ = close_parlist($1); }
|
1993-12-17 21:53:07 +03:00
|
|
|
;
|
|
|
|
|
1996-05-29 01:07:32 +04:00
|
|
|
parlist1 : par { $$ = $1; }
|
|
|
|
| parlist1 ',' par
|
|
|
|
{
|
|
|
|
if ($1)
|
|
|
|
lua_error("invalid parameter list");
|
|
|
|
$$ = $3;
|
|
|
|
}
|
1993-12-17 21:53:07 +03:00
|
|
|
;
|
1996-05-29 01:07:32 +04:00
|
|
|
|
|
|
|
par : NAME { add_localvar($1); $$ = 0; }
|
|
|
|
| DOTS { $$ = 1; }
|
1996-07-24 18:38:12 +04:00
|
|
|
;
|
1993-12-17 21:53:07 +03:00
|
|
|
|
1995-06-08 23:47:28 +04:00
|
|
|
fieldlist : lfieldlist
|
|
|
|
{ flush_list($1/FIELDS_PER_FLUSH, $1%FIELDS_PER_FLUSH); }
|
|
|
|
semicolonpart
|
|
|
|
{ $$ = $1+$3; }
|
1994-10-11 16:02:39 +03:00
|
|
|
| ffieldlist1 lastcomma
|
|
|
|
{ $$ = $1; flush_record($1%FIELDS_PER_FLUSH); }
|
1993-12-17 21:53:07 +03:00
|
|
|
;
|
|
|
|
|
1995-06-08 23:47:28 +04:00
|
|
|
semicolonpart : /* empty */
|
|
|
|
{ $$ = 0; }
|
|
|
|
| ';' ffieldlist
|
|
|
|
{ $$ = $2; flush_record($2%FIELDS_PER_FLUSH); }
|
|
|
|
;
|
|
|
|
|
1994-10-11 16:02:39 +03:00
|
|
|
lastcomma : /* empty */
|
|
|
|
| ','
|
|
|
|
;
|
1993-12-17 21:53:07 +03:00
|
|
|
|
1995-06-08 23:47:28 +04:00
|
|
|
ffieldlist : /* empty */ { $$ = 0; }
|
|
|
|
| ffieldlist1 lastcomma { $$ = $1; }
|
|
|
|
;
|
|
|
|
|
1993-12-17 21:53:07 +03:00
|
|
|
ffieldlist1 : ffield {$$=1;}
|
1994-02-13 23:38:20 +03:00
|
|
|
| ffieldlist1 ',' ffield
|
|
|
|
{
|
|
|
|
$$=$1+1;
|
|
|
|
if ($$%FIELDS_PER_FLUSH == 0) flush_record(FIELDS_PER_FLUSH);
|
|
|
|
}
|
1993-12-17 21:53:07 +03:00
|
|
|
;
|
|
|
|
|
1997-03-06 20:30:55 +03:00
|
|
|
ffield : ffieldkey '=' expr1
|
1993-12-17 21:53:07 +03:00
|
|
|
;
|
|
|
|
|
1997-03-06 20:30:55 +03:00
|
|
|
ffieldkey : '[' expr1 ']'
|
|
|
|
| NAME { code_constant($1); }
|
|
|
|
;
|
|
|
|
|
1995-06-08 23:47:28 +04:00
|
|
|
lfieldlist : /* empty */ { $$ = 0; }
|
|
|
|
| lfieldlist1 lastcomma { $$ = $1; }
|
|
|
|
;
|
|
|
|
|
1994-02-13 23:38:20 +03:00
|
|
|
lfieldlist1 : expr1 {$$=1;}
|
|
|
|
| lfieldlist1 ',' expr1
|
|
|
|
{
|
|
|
|
$$=$1+1;
|
|
|
|
if ($$%FIELDS_PER_FLUSH == 0)
|
|
|
|
flush_list($$/FIELDS_PER_FLUSH - 1, FIELDS_PER_FLUSH);
|
|
|
|
}
|
1993-12-17 21:53:07 +03:00
|
|
|
;
|
|
|
|
|
|
|
|
varlist1 : var
|
|
|
|
{
|
|
|
|
nvarbuffer = 0;
|
1995-04-11 21:56:30 +04:00
|
|
|
add_varbuffer($1);
|
1993-12-17 21:53:07 +03:00
|
|
|
$$ = ($1 == 0) ? 1 : 0;
|
|
|
|
}
|
|
|
|
| varlist1 ',' var
|
|
|
|
{
|
1995-04-11 21:56:30 +04:00
|
|
|
add_varbuffer($3);
|
1993-12-17 21:53:07 +03:00
|
|
|
$$ = ($3 == 0) ? $1 + 1 : $1;
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
1994-08-05 23:31:09 +04:00
|
|
|
var : singlevar { $$ = $1; }
|
1994-10-17 22:05:32 +03:00
|
|
|
| varexp '[' expr1 ']'
|
1993-12-17 21:53:07 +03:00
|
|
|
{
|
|
|
|
$$ = 0; /* indexed variable */
|
|
|
|
}
|
1994-10-17 22:05:32 +03:00
|
|
|
| varexp '.' NAME
|
1993-12-17 21:53:07 +03:00
|
|
|
{
|
1997-03-06 20:30:55 +03:00
|
|
|
code_constant($3);
|
|
|
|
$$ = 0; /* indexed variable */
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
|
|
|
;
|
|
|
|
|
1994-08-05 23:31:09 +04:00
|
|
|
singlevar : NAME
|
|
|
|
{
|
1996-01-23 20:50:29 +03:00
|
|
|
int local = lua_localname($1);
|
1994-08-05 23:31:09 +04:00
|
|
|
if (local == -1) /* global var */
|
1996-01-23 20:50:29 +03:00
|
|
|
$$ = luaI_findsymbol($1)+1; /* return positive value */
|
1994-08-05 23:31:09 +04:00
|
|
|
else
|
|
|
|
$$ = -(local+1); /* return negative value */
|
|
|
|
}
|
|
|
|
;
|
1994-10-17 22:05:32 +03:00
|
|
|
|
|
|
|
varexp : var { lua_pushvar($1); }
|
|
|
|
;
|
1994-08-05 23:31:09 +04:00
|
|
|
|
1996-01-23 20:50:29 +03:00
|
|
|
localdeclist : NAME {store_localvar($1, 0); $$ = 1;}
|
1994-04-15 23:02:04 +04:00
|
|
|
| localdeclist ',' NAME
|
|
|
|
{
|
1996-01-23 20:50:29 +03:00
|
|
|
store_localvar($3, $1);
|
1994-04-15 23:02:04 +04:00
|
|
|
$$ = $1+1;
|
|
|
|
}
|
1993-12-17 21:53:07 +03:00
|
|
|
;
|
|
|
|
|
1994-11-02 23:30:53 +03:00
|
|
|
decinit : /* empty */ { $$ = 0; }
|
|
|
|
| '=' exprlist1 { $$ = $2; }
|
1993-12-17 21:53:07 +03:00
|
|
|
;
|
|
|
|
|
|
|
|
%%
|