1993-12-17 21:53:07 +03:00
|
|
|
%{
|
1997-09-16 23:33:21 +04:00
|
|
|
/*
|
|
|
|
** $Id: $
|
|
|
|
** Syntax analizer and code generator
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
1993-12-17 21:53:07 +03:00
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "ldo.h"
|
|
|
|
#include "lfunc.h"
|
|
|
|
#include "lglobal.h"
|
|
|
|
#include "llex.h"
|
|
|
|
#include "lmem.h"
|
|
|
|
#include "lopcodes.h"
|
|
|
|
#include "lparser.h"
|
|
|
|
#include "lstring.h"
|
1993-12-17 21:53:07 +03:00
|
|
|
#include "lua.h"
|
1997-09-16 23:33:21 +04:00
|
|
|
#include "luadebug.h"
|
|
|
|
#include "lzio.h"
|
1993-12-17 21:53:07 +03:00
|
|
|
|
1994-11-23 17:39:52 +03:00
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
/* to avoid warnings generated by yacc */
|
|
|
|
int luaY_parse (void);
|
|
|
|
#define malloc luaM_malloc
|
|
|
|
#define realloc luaM_realloc
|
|
|
|
#define free luaM_free
|
1994-02-13 23:38:20 +03:00
|
|
|
|
1997-07-30 00:38:45 +04:00
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
/* maximum number of local variables */
|
1997-07-30 00:38:45 +04:00
|
|
|
#define MAXLOCALS 32
|
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
/* maximum number of variables in a multiple assignment */
|
|
|
|
#define MAXVAR 32
|
|
|
|
|
|
|
|
/* maximum number of nested functions */
|
|
|
|
#define MAXSTATES 6
|
|
|
|
|
|
|
|
/* maximum number of upvalues */
|
|
|
|
#define MAXUPVALUES 8
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Variable descriptor: if n>0, represents global variable indexed
|
|
|
|
** by (n-1); if n<0, represents local variable index (-n)-1;
|
|
|
|
** if n==0, represents an indexed variable (table and index on top of stack)
|
|
|
|
** Must be long to store negative Word values.
|
|
|
|
*/
|
|
|
|
typedef long vardesc;
|
|
|
|
|
|
|
|
|
1997-07-30 00:38:45 +04:00
|
|
|
/* state needed to generate code for a given function */
|
1997-09-16 23:33:21 +04:00
|
|
|
static struct State {
|
|
|
|
TProtoFunc *f; /* current function header */
|
1997-07-30 00:38:45 +04:00
|
|
|
int pc; /* next position to code */
|
|
|
|
TaggedString *localvar[MAXLOCALS]; /* store local variable names */
|
1997-09-16 23:33:21 +04:00
|
|
|
int stacksize; /* number of values on activation register */
|
|
|
|
int maxstacksize; /* maximum number of values on activation register */
|
1997-07-30 00:38:45 +04:00
|
|
|
int nlocalvar; /* number of active local variables */
|
1997-09-16 23:33:21 +04:00
|
|
|
int nvars; /* number of entries in f->locvars */
|
|
|
|
int maxcode; /* size of f->code */
|
|
|
|
int maxvars; /* size of f->locvars (-1 if no debug information) */
|
|
|
|
int maxconsts; /* size of f->consts */
|
|
|
|
vardesc varbuffer[MAXVAR]; /* variables in an assignment list */
|
|
|
|
vardesc upvalues[MAXUPVALUES]; /* upvalues */
|
|
|
|
} *mainState, *currState;
|
1994-02-13 23:38:20 +03:00
|
|
|
|
1993-12-17 21:53:07 +03:00
|
|
|
|
1994-11-15 00:40:14 +03:00
|
|
|
|
1993-12-17 21:53:07 +03:00
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
void luaY_syntaxerror (char *s, char *token)
|
1995-10-26 17:21:56 +03:00
|
|
|
{
|
1997-09-16 23:33:21 +04:00
|
|
|
if (token[0] == 0)
|
|
|
|
token = "<eof>";
|
|
|
|
luaL_verror("%.100s;\n> last token read: \"%.50s\" at line %d in file %.50s",
|
|
|
|
s, token, luaX_linenumber, mainState->f->fileName->str);
|
1995-10-26 17:21:56 +03:00
|
|
|
}
|
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
|
|
|
|
void luaY_error (char *s)
|
1996-05-29 01:07:32 +04:00
|
|
|
{
|
1997-09-16 23:33:21 +04:00
|
|
|
luaY_syntaxerror(s, luaX_lasttoken());
|
1996-05-29 01:07:32 +04:00
|
|
|
}
|
|
|
|
|
1997-07-31 02:00:50 +04:00
|
|
|
|
1993-12-17 21:53:07 +03:00
|
|
|
static void code_byte (Byte c)
|
|
|
|
{
|
1997-09-16 23:33:21 +04:00
|
|
|
if (currState->pc >= currState->maxcode)
|
|
|
|
currState->maxcode = luaM_growvector(&currState->f->code,
|
|
|
|
currState->maxcode, Byte, codeEM, MAX_INT);
|
1997-07-30 00:38:45 +04:00
|
|
|
currState->f->code[currState->pc++] = c;
|
1994-02-13 23:38:20 +03:00
|
|
|
}
|
|
|
|
|
1997-07-31 02:00:50 +04:00
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
static void deltastack (int delta)
|
|
|
|
{
|
|
|
|
currState->stacksize += delta;
|
|
|
|
if (currState->stacksize > currState->maxstacksize) {
|
|
|
|
if (currState->stacksize > 255)
|
|
|
|
luaY_error("expression too complex");
|
|
|
|
currState->maxstacksize = currState->stacksize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void code_opcode (OpCode op, int delta)
|
|
|
|
{
|
|
|
|
code_byte(op);
|
|
|
|
deltastack(delta);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-07-31 02:00:50 +04:00
|
|
|
static void code_word_at (int pc, int n)
|
1993-12-17 21:53:07 +03:00
|
|
|
{
|
1997-07-31 02:00:50 +04:00
|
|
|
Word w = n;
|
|
|
|
if (w != n)
|
1997-09-16 23:33:21 +04:00
|
|
|
luaY_error("block too big");
|
1997-07-31 02:00:50 +04:00
|
|
|
currState->f->code[pc] = n&0xFF;
|
|
|
|
currState->f->code[pc+1] = n>>8;
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
|
|
|
|
1997-07-31 02:00:50 +04:00
|
|
|
static void code_word (int n)
|
1994-08-05 23:31:09 +04:00
|
|
|
{
|
1997-07-31 02:00:50 +04:00
|
|
|
code_byte(n&0xFF);
|
|
|
|
code_byte(n>>8);
|
1994-08-05 23:31:09 +04:00
|
|
|
}
|
|
|
|
|
1997-07-31 02:00:50 +04:00
|
|
|
static void code_constant (int c)
|
1993-12-17 21:53:07 +03:00
|
|
|
{
|
1997-07-31 02:00:50 +04:00
|
|
|
if (c <= 255) {
|
1997-09-16 23:33:21 +04:00
|
|
|
code_opcode(PUSHCONSTANTB, 1);
|
1997-07-31 02:00:50 +04:00
|
|
|
code_byte(c);
|
|
|
|
}
|
|
|
|
else {
|
1997-09-16 23:33:21 +04:00
|
|
|
code_opcode(PUSHCONSTANT, 1);
|
1997-07-31 02:00:50 +04:00
|
|
|
code_word(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int next_constant (void)
|
|
|
|
{
|
1997-09-16 23:33:21 +04:00
|
|
|
TProtoFunc *f = currState->f;
|
1997-08-01 00:46:59 +04:00
|
|
|
if (f->nconsts >= currState->maxconsts) {
|
1997-07-31 02:00:50 +04:00
|
|
|
currState->maxconsts =
|
1997-09-16 23:33:21 +04:00
|
|
|
luaM_growvector(&f->consts, currState->maxconsts, TObject,
|
1997-08-01 00:46:59 +04:00
|
|
|
constantEM, MAX_WORD);
|
1997-07-31 02:00:50 +04:00
|
|
|
}
|
1997-08-01 00:46:59 +04:00
|
|
|
return f->nconsts++;
|
1997-07-31 02:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int string_constant (TaggedString *s)
|
|
|
|
{
|
1997-09-16 23:33:21 +04:00
|
|
|
TProtoFunc *f = currState->f;
|
1997-07-31 02:00:50 +04:00
|
|
|
int c = s->u.s.constindex;
|
1997-08-01 00:46:59 +04:00
|
|
|
if (!(0 <= c && c < f->nconsts &&
|
|
|
|
ttype(&f->consts[c]) == LUA_T_STRING && tsvalue(&f->consts[c]) == s)) {
|
1997-07-31 02:00:50 +04:00
|
|
|
c = next_constant();
|
1997-08-01 00:46:59 +04:00
|
|
|
ttype(&f->consts[c]) = LUA_T_STRING;
|
|
|
|
tsvalue(&f->consts[c]) = s;
|
1997-07-31 02:00:50 +04:00
|
|
|
s->u.s.constindex = c; /* hint for next time */
|
|
|
|
}
|
|
|
|
return c;
|
1997-07-30 00:38:45 +04:00
|
|
|
}
|
|
|
|
|
1997-07-31 02:00:50 +04:00
|
|
|
|
|
|
|
static void code_string (TaggedString *s)
|
1997-07-30 00:38:45 +04:00
|
|
|
{
|
1997-08-01 00:46:59 +04:00
|
|
|
code_constant(string_constant(s));
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
|
|
|
|
1997-08-01 00:46:59 +04:00
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
#define LIM 13
|
1997-08-01 00:46:59 +04:00
|
|
|
static int real_constant (real r)
|
1997-07-31 02:00:50 +04:00
|
|
|
{
|
1997-08-01 00:46:59 +04:00
|
|
|
/* check whether 'r' has appeared within the last LIM entries */
|
|
|
|
TObject *cnt = currState->f->consts;
|
|
|
|
int c = currState->f->nconsts;
|
|
|
|
int lim = c < LIM ? 0 : c-LIM;
|
|
|
|
while (--c >= lim) {
|
|
|
|
if (ttype(&cnt[c]) == LUA_T_NUMBER && nvalue(&cnt[c]) == r)
|
|
|
|
return c;
|
|
|
|
}
|
1997-09-16 23:33:21 +04:00
|
|
|
/* not found; create a luaM_new entry */
|
1997-08-01 00:46:59 +04:00
|
|
|
c = next_constant();
|
|
|
|
cnt = currState->f->consts; /* 'next_constant' may reallocate this vector */
|
|
|
|
ttype(&cnt[c]) = LUA_T_NUMBER;
|
|
|
|
nvalue(&cnt[c]) = r;
|
|
|
|
return c;
|
1997-07-31 02:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-08-01 00:46:59 +04:00
|
|
|
static void code_number (real f)
|
1997-09-16 23:33:21 +04:00
|
|
|
{
|
1997-07-31 02:00:50 +04:00
|
|
|
Word i;
|
1997-08-01 00:46:59 +04:00
|
|
|
if (f >= 0 && f <= (real)MAX_WORD && (real)(i=(Word)f) == f) {
|
|
|
|
/* f has an (short) integer value */
|
1997-09-16 23:33:21 +04:00
|
|
|
if (i <= 2) code_opcode(PUSH0 + i, 1);
|
1997-08-01 00:46:59 +04:00
|
|
|
else if (i <= 255) {
|
1997-09-16 23:33:21 +04:00
|
|
|
code_opcode(PUSHBYTE, 1);
|
1997-08-01 00:46:59 +04:00
|
|
|
code_byte(i);
|
|
|
|
}
|
|
|
|
else {
|
1997-09-16 23:33:21 +04:00
|
|
|
code_opcode(PUSHWORD, 1);
|
1997-08-01 00:46:59 +04:00
|
|
|
code_word(i);
|
|
|
|
}
|
1997-07-31 02:00:50 +04:00
|
|
|
}
|
|
|
|
else
|
1997-08-01 00:46:59 +04:00
|
|
|
code_constant(real_constant(f));
|
1997-07-31 02:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-02-13 23:38:20 +03:00
|
|
|
static void flush_record (int n)
|
|
|
|
{
|
|
|
|
if (n == 0) return;
|
1997-09-16 23:33:21 +04:00
|
|
|
code_opcode(SETMAP, -2*n);
|
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)
|
1997-09-16 23:33:21 +04:00
|
|
|
code_opcode(SETLIST0, -n);
|
|
|
|
else if (m < 255) {
|
|
|
|
code_opcode(SETLIST, -n);
|
1994-02-13 23:38:20 +03:00
|
|
|
code_byte(m);
|
|
|
|
}
|
1994-08-03 18:15:46 +04:00
|
|
|
else
|
1997-09-16 23:33:21 +04:00
|
|
|
luaY_error("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)
|
1997-09-16 23:33:21 +04:00
|
|
|
currState->maxvars = luaM_growvector(&currState->f->locvars,
|
1997-07-30 00:38:45 +04:00
|
|
|
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
|
1997-09-16 23:33:21 +04:00
|
|
|
luaY_error("too many local variables");
|
|
|
|
luaI_registerlocalvar(name, luaX_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
|
|
|
}
|
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
static void add_varbuffer (vardesc var, int n)
|
1995-04-11 21:56:30 +04:00
|
|
|
{
|
1997-09-16 23:33:21 +04:00
|
|
|
if (n >= MAXVAR)
|
|
|
|
luaY_error("variable buffer overflow");
|
|
|
|
currState->varbuffer[n] = var;
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
static int aux_localname (TaggedString *n, struct State *st)
|
1994-12-27 23:04:29 +03:00
|
|
|
{
|
1997-09-16 23:33:21 +04:00
|
|
|
int i;
|
|
|
|
for (i=st->nlocalvar-1; i >= 0; i--)
|
|
|
|
if (n == st->localvar[i]) return i; /* local var index */
|
|
|
|
return -1; /* not found */
|
1994-12-27 23:04:29 +03:00
|
|
|
}
|
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
|
|
|
|
static vardesc singlevar (TaggedString *n, struct State *st)
|
|
|
|
{
|
|
|
|
int i = aux_localname(n, st);
|
|
|
|
if (i == -1) { /* check shadowing */
|
|
|
|
int l;
|
|
|
|
for (l=1; l<=(st-mainState); l++)
|
|
|
|
if (aux_localname(n, st-l) >= 0)
|
|
|
|
luaY_syntaxerror("cannot access a variable in outer scope", n->str);
|
|
|
|
return luaG_findsymbol(n)+1; /* positive value */
|
1994-12-27 23:04:29 +03:00
|
|
|
}
|
1997-09-16 23:33:21 +04:00
|
|
|
else return -(i+1); /* negative value */
|
1994-12-27 23:04:29 +03:00
|
|
|
}
|
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
|
|
|
|
static int indexupvalue (TaggedString *n)
|
1994-12-27 23:04:29 +03:00
|
|
|
{
|
1997-09-16 23:33:21 +04:00
|
|
|
vardesc v = singlevar(n, currState-1);
|
|
|
|
int i;
|
|
|
|
for (i=0; i<currState->f->nupvalues; i++) {
|
|
|
|
if (currState->upvalues[i] == v)
|
|
|
|
return i;
|
1997-07-30 00:38:45 +04:00
|
|
|
}
|
1997-09-16 23:33:21 +04:00
|
|
|
/* new one */
|
|
|
|
if (++currState->f->nupvalues > MAXUPVALUES)
|
|
|
|
luaY_error("too many upvalues in a single function");
|
|
|
|
currState->upvalues[i] = v; /* i = currState->f->nupvalues - 1 */
|
|
|
|
return i;
|
1994-12-27 23:04:29 +03:00
|
|
|
}
|
|
|
|
|
1995-10-25 16:05:51 +03:00
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
static void pushupvalue (TaggedString *n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if (currState == mainState)
|
|
|
|
luaY_error("cannot access upvalue in main");
|
|
|
|
if (aux_localname(n, currState) >= 0)
|
|
|
|
luaY_syntaxerror("cannot access an upvalue in current scope", n->str);
|
|
|
|
i = indexupvalue(n);
|
|
|
|
if (i == 0)
|
|
|
|
code_opcode(PUSHUPVALUE0, 1);
|
|
|
|
else {
|
|
|
|
code_opcode(PUSHUPVALUE, 1);
|
|
|
|
code_byte(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-07-30 00:38:45 +04:00
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
void luaY_codedebugline (int line)
|
1994-12-27 23:04:29 +03:00
|
|
|
{
|
1995-10-25 16:05:51 +03:00
|
|
|
static int lastline = 0;
|
1997-09-16 23:33:21 +04:00
|
|
|
if (lua_debug && line != lastline) {
|
|
|
|
code_opcode(SETLINE, 0);
|
1995-10-25 16:05:51 +03:00
|
|
|
code_word(line);
|
|
|
|
lastline = line;
|
1994-12-27 23:04:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
|
|
|
|
static void adjuststack (int n)
|
|
|
|
{
|
|
|
|
if (n > 0) {
|
|
|
|
code_opcode(POPS, -n);
|
|
|
|
code_byte(n);
|
|
|
|
}
|
|
|
|
else if (n < 0) {
|
|
|
|
if (n == -1)
|
|
|
|
code_opcode(PUSHNIL, 1);
|
|
|
|
else {
|
|
|
|
code_opcode(PUSHNILS, -n);
|
|
|
|
code_byte(-n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static long 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;
|
1997-09-16 23:33:21 +04:00
|
|
|
if (i != MULT_RET)
|
|
|
|
deltastack(i);
|
1995-10-25 16:05:51 +03:00
|
|
|
return temp+i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
|
|
|
|
static void adjust_mult_assign (int vars, long exps)
|
1995-10-25 16:05:51 +03:00
|
|
|
{
|
1997-07-30 00:38:45 +04:00
|
|
|
if (exps > 0) { /* must correct function call */
|
1997-09-16 23:33:21 +04:00
|
|
|
int diff = currState->f->code[exps] - vars;
|
|
|
|
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);
|
1997-09-16 23:33:21 +04:00
|
|
|
adjuststack(diff);
|
1994-12-27 23:04:29 +03:00
|
|
|
}
|
|
|
|
}
|
1997-09-16 23:33:21 +04:00
|
|
|
else adjuststack((-exps)-vars);
|
1994-12-27 23:04:29 +03:00
|
|
|
}
|
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
|
|
|
|
static void code_args (int dots)
|
1996-05-29 01:07:32 +04:00
|
|
|
{
|
1997-09-16 23:33:21 +04:00
|
|
|
if (!dots) {
|
|
|
|
code_opcode(ARGS, currState->nlocalvar);
|
|
|
|
code_byte(currState->nlocalvar);
|
|
|
|
}
|
1997-07-30 00:38:45 +04:00
|
|
|
else {
|
1997-09-16 23:33:21 +04:00
|
|
|
code_opcode(VARARGS, currState->nlocalvar+1);
|
1997-07-30 00:38:45 +04:00
|
|
|
code_byte(currState->nlocalvar);
|
1997-09-16 23:33:21 +04:00
|
|
|
add_localvar(luaS_new("arg"));
|
1996-05-29 01:07:32 +04:00
|
|
|
}
|
1997-09-16 23:33:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void lua_pushvar (vardesc number)
|
|
|
|
{
|
|
|
|
if (number > 0) { /* global var */
|
|
|
|
code_opcode(PUSHGLOBAL, 1);
|
|
|
|
code_word(number-1);
|
|
|
|
}
|
|
|
|
else if (number < 0) { /* local var */
|
|
|
|
number = (-number) - 1;
|
|
|
|
if (number < 10)
|
|
|
|
code_opcode(PUSHLOCAL0 + number, 1);
|
|
|
|
else {
|
|
|
|
code_opcode(PUSHLOCAL, 1);
|
|
|
|
code_byte(number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
code_opcode(PUSHTABLE, -1);
|
1994-12-27 23:04:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
|
|
|
|
static void storevar (vardesc number)
|
|
|
|
{
|
|
|
|
if (number == 0) /* indexed var */
|
|
|
|
code_opcode(SETTABLE0, -3);
|
|
|
|
else if (number > 0) { /* global var */
|
|
|
|
code_opcode(SETGLOBAL, -1);
|
|
|
|
code_word(number-1);
|
|
|
|
}
|
|
|
|
else { /* number < 0 - local var */
|
|
|
|
number = (-number) - 1;
|
|
|
|
if (number < 10)
|
|
|
|
code_opcode(SETLOCAL0 + number, -1);
|
|
|
|
else {
|
|
|
|
code_opcode(SETLOCAL, -1);
|
|
|
|
code_byte(number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* returns how many elements are left as 'garbage' on the stack */
|
|
|
|
static int lua_codestore (int i, int left)
|
1994-12-27 23:04:29 +03:00
|
|
|
{
|
1997-09-16 23:33:21 +04:00
|
|
|
if (currState->varbuffer[i] != 0 || /* global or local var or */
|
|
|
|
left+i == 0) { /* indexed var without values in between */
|
|
|
|
storevar(currState->varbuffer[i]);
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
else { /* indexed var with values in between*/
|
|
|
|
code_opcode(SETTABLE, -1);
|
|
|
|
code_byte(left+i); /* number of elements between table/index and value */
|
|
|
|
return left+2; /* table/index are not poped, since they are not on top */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void codeIf (int thenAdd, int elseAdd)
|
|
|
|
{
|
|
|
|
int elseinit = elseAdd+sizeof(Word)+1;
|
|
|
|
if (currState->pc == elseinit) { /* no else part */
|
1997-07-30 00:38:45 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
static void codereturn (void)
|
1997-07-31 02:00:50 +04:00
|
|
|
{
|
1997-09-16 23:33:21 +04:00
|
|
|
code_opcode(RETCODE, 0);
|
|
|
|
code_byte(currState->nlocalvar);
|
|
|
|
currState->stacksize = currState->nlocalvar;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void func_onstack (TProtoFunc *f)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int nupvalues = (currState+1)->f->nupvalues;
|
|
|
|
int c = next_constant();
|
|
|
|
ttype(&currState->f->consts[c]) = LUA_T_PROTO;
|
|
|
|
currState->f->consts[c].value.tf = (currState+1)->f;
|
|
|
|
for (i=0; i<nupvalues; i++)
|
|
|
|
lua_pushvar((currState+1)->upvalues[i]);
|
|
|
|
code_constant(c);
|
|
|
|
code_opcode(CLOSURE, -nupvalues);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void init_state (TaggedString *filename)
|
|
|
|
{
|
|
|
|
TProtoFunc *f = luaF_newproto();
|
|
|
|
currState->stacksize = 0;
|
|
|
|
currState->maxstacksize = 0;
|
1997-07-31 02:00:50 +04:00
|
|
|
currState->nlocalvar = 0;
|
|
|
|
currState->f = f;
|
1997-09-16 23:33:21 +04:00
|
|
|
f->fileName = filename;
|
1997-07-31 02:00:50 +04:00
|
|
|
currState->pc = 0;
|
1997-09-16 23:33:21 +04:00
|
|
|
currState->maxcode = 0;
|
|
|
|
f->code = NULL;
|
1997-07-31 02:00:50 +04:00
|
|
|
currState->maxconsts = 0;
|
|
|
|
if (lua_debug) {
|
|
|
|
currState->nvars = 0;
|
|
|
|
currState->maxvars = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
currState->maxvars = -1; /* flag no debug information */
|
1997-09-16 23:33:21 +04:00
|
|
|
code_byte(0); /* to be filled with stacksize */
|
1997-07-31 02:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
static void init_func (void)
|
1997-07-31 02:00:50 +04:00
|
|
|
{
|
1997-09-16 23:33:21 +04:00
|
|
|
if (currState-mainState >= MAXSTATES-1)
|
|
|
|
luaY_error("too many nested functions");
|
|
|
|
currState++;
|
|
|
|
init_state(mainState->f->fileName);
|
|
|
|
luaY_codedebugline(luaX_linenumber);
|
|
|
|
currState->f->lineDefined = luaX_linenumber;
|
1997-07-31 02:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
static TProtoFunc *close_func (void)
|
1997-07-31 02:00:50 +04:00
|
|
|
{
|
1997-09-16 23:33:21 +04:00
|
|
|
TProtoFunc *f = currState->f;
|
|
|
|
code_opcode(ENDCODE, 0);
|
|
|
|
f->code[0] = currState->maxstacksize;
|
|
|
|
f->code = luaM_reallocvector(f->code, currState->pc, Byte);
|
|
|
|
f->consts = luaM_reallocvector(f->consts, f->nconsts, TObject);
|
1997-07-31 02:00:50 +04:00
|
|
|
if (currState->maxvars != -1) { /* debug information? */
|
|
|
|
luaI_registerlocalvar(NULL, -1); /* flag end of vector */
|
1997-09-16 23:33:21 +04:00
|
|
|
f->locvars = luaM_reallocvector(f->locvars, currState->nvars, LocVar);
|
1997-07-31 02:00:50 +04:00
|
|
|
}
|
1997-09-16 23:33:21 +04:00
|
|
|
currState--;
|
|
|
|
return f;
|
1997-07-31 02:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-12-27 23:04:29 +03:00
|
|
|
/*
|
|
|
|
** Parse LUA code.
|
|
|
|
*/
|
1997-09-16 23:33:21 +04:00
|
|
|
TProtoFunc *luaY_parser (ZIO *z, char *chunkname)
|
1994-12-27 23:04:29 +03:00
|
|
|
{
|
1997-09-16 23:33:21 +04:00
|
|
|
struct State state[MAXSTATES];
|
|
|
|
currState = mainState = &state[0];
|
|
|
|
luaX_setinput(z);
|
|
|
|
init_state(luaS_new(chunkname));
|
|
|
|
if (luaY_parse ()) lua_error("parse error");
|
|
|
|
return 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
|
|
|
%}
|
|
|
|
|
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
%union
|
1993-12-17 21:53:07 +03:00
|
|
|
{
|
1997-09-16 23:33:21 +04:00
|
|
|
int vInt;
|
|
|
|
real vReal;
|
|
|
|
char *pChar;
|
|
|
|
long vLong;
|
|
|
|
TaggedString *pTStr;
|
|
|
|
TProtoFunc *pFunc;
|
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
|
1997-08-01 00:46:59 +04:00
|
|
|
%token <vReal> NUMBER
|
1997-07-31 02:00:50 +04:00
|
|
|
%token <pTStr> NAME STRING
|
1993-12-17 21:53:07 +03:00
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
%type <vInt> PrepJump, PrepJumpPop
|
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
|
1997-09-16 23:33:21 +04:00
|
|
|
%type <vInt> parlist1, par
|
|
|
|
%type <vLong> var, singlevar, funcname /* vardesc */
|
|
|
|
%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 */
|
|
|
|
|
1994-08-05 23:31:09 +04:00
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
chunk : statlist ret
|
|
|
|
;
|
1994-11-15 00:40:14 +03:00
|
|
|
|
1993-12-17 21:53:07 +03:00
|
|
|
statlist : /* empty */
|
1997-09-16 23:33:21 +04:00
|
|
|
| statlist stat sc { if (currState->stacksize != currState->nlocalvar)
|
|
|
|
{ luaY_error("contagem"); exit(1); }}
|
1993-12-17 21:53:07 +03:00
|
|
|
;
|
|
|
|
|
|
|
|
sc : /* empty */ | ';' ;
|
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
stat : IF expr1 THEN PrepJumpPop block PrepJump elsepart END
|
1994-11-17 21:59:06 +03:00
|
|
|
{ codeIf($4, $6); }
|
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
| WHILE {$<vInt>$=currState->pc;} expr1 DO PrepJumpPop 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;
|
1997-09-16 23:33:21 +04:00
|
|
|
code_word_at($7+1, currState->pc - ($<vInt>2));
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
1997-09-16 23:33:21 +04:00
|
|
|
|
|
|
|
| REPEAT {$<vInt>$=currState->pc;} block UNTIL expr1 PrepJumpPop
|
1993-12-17 21:53:07 +03:00
|
|
|
{
|
1997-07-30 00:38:45 +04:00
|
|
|
currState->f->code[$6] = IFFUPJMP;
|
1997-09-16 23:33:21 +04:00
|
|
|
code_word_at($6+1, currState->pc - ($<vInt>2));
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
| varlist1 '=' exprlist1
|
1997-09-16 23:33:21 +04:00
|
|
|
{{
|
|
|
|
int i;
|
|
|
|
int left = 0;
|
|
|
|
adjust_mult_assign($1, $3);
|
|
|
|
for (i=$1-1; i>=0; i--)
|
|
|
|
left = lua_codestore(i, left);
|
|
|
|
adjuststack(left); /* remove eventual 'garbage' left on stack */
|
|
|
|
}}
|
|
|
|
| functioncall
|
1994-11-17 21:59:06 +03:00
|
|
|
| LOCAL localdeclist decinit
|
1997-09-16 23:33:21 +04:00
|
|
|
{
|
|
|
|
currState->nlocalvar += $2;
|
|
|
|
adjust_mult_assign($2, $3);
|
|
|
|
}
|
|
|
|
| FUNCTION funcname body
|
|
|
|
{
|
|
|
|
func_onstack($3);
|
|
|
|
storevar($2);
|
|
|
|
}
|
1993-12-17 21:53:07 +03:00
|
|
|
;
|
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
block : {$<vInt>$ = currState->nlocalvar;} chunk
|
|
|
|
{
|
|
|
|
adjuststack(currState->nlocalvar - $<vInt>1);
|
|
|
|
for (; currState->nlocalvar > $<vInt>1; currState->nlocalvar--)
|
|
|
|
luaI_unregisterlocalvar(luaX_linenumber);
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
funcname : var { init_func(); $$ = $1; }
|
|
|
|
| varexp ':' NAME
|
|
|
|
{
|
|
|
|
code_string($3);
|
|
|
|
$$ = 0; /* indexed variable */
|
|
|
|
init_func();
|
|
|
|
add_localvar(luaS_new("self"));
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
body : '(' parlist ')' chunk END { $$ = close_func(); }
|
|
|
|
;
|
|
|
|
|
1993-12-17 21:53:07 +03:00
|
|
|
elsepart : /* empty */
|
|
|
|
| ELSE block
|
1997-09-16 23:33:21 +04:00
|
|
|
| ELSEIF expr1 THEN PrepJumpPop block PrepJump elsepart
|
1994-11-17 21:59:06 +03:00
|
|
|
{ codeIf($4, $6); }
|
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-09-16 23:33:21 +04:00
|
|
|
{
|
|
|
|
$$ = currState->pc;
|
|
|
|
code_opcode(0, 0); /* open space */
|
|
|
|
code_word(0);
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
1996-07-24 18:38:12 +04:00
|
|
|
;
|
1997-09-16 23:33:21 +04:00
|
|
|
|
|
|
|
PrepJumpPop : PrepJump { $$ = $1; deltastack(-1); /* pop condition */ }
|
|
|
|
;
|
|
|
|
|
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; }
|
1997-09-16 23:33:21 +04:00
|
|
|
| expr1 EQ expr1 { code_opcode(EQOP, -1); $$ = 0; }
|
|
|
|
| expr1 '<' expr1 { code_opcode(LTOP, -1); $$ = 0; }
|
|
|
|
| expr1 '>' expr1 { code_opcode(GTOP, -1); $$ = 0; }
|
|
|
|
| expr1 NE expr1 { code_opcode(NEQOP, -1); $$ = 0; }
|
|
|
|
| expr1 LE expr1 { code_opcode(LEOP, -1); $$ = 0; }
|
|
|
|
| expr1 GE expr1 { code_opcode(GEOP, -1); $$ = 0; }
|
|
|
|
| expr1 '+' expr1 { code_opcode(ADDOP, -1); $$ = 0; }
|
|
|
|
| expr1 '-' expr1 { code_opcode(SUBOP, -1); $$ = 0; }
|
|
|
|
| expr1 '*' expr1 { code_opcode(MULTOP, -1); $$ = 0; }
|
|
|
|
| expr1 '/' expr1 { code_opcode(DIVOP, -1); $$ = 0; }
|
|
|
|
| expr1 '^' expr1 { code_opcode(POWOP, -1); $$ = 0; }
|
|
|
|
| expr1 CONC expr1 { code_opcode(CONCOP, -1); $$ = 0; }
|
|
|
|
| '-' expr1 %prec UNARY { code_opcode(MINUSOP, 0); $$ = 0;}
|
1995-10-25 16:05:51 +03:00
|
|
|
| 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
|
|
|
}
|
1997-09-16 23:33:21 +04:00
|
|
|
| NIL {code_opcode(PUSHNIL, 1); $$ = 0; }
|
1995-10-25 16:05:51 +03:00
|
|
|
| functioncall { $$ = $1; }
|
1997-09-16 23:33:21 +04:00
|
|
|
| NOT expr1 { code_opcode(NOTOP, 0); $$ = 0;}
|
|
|
|
| expr1 AND PrepJumpPop expr1
|
|
|
|
{
|
1997-07-30 00:38:45 +04:00
|
|
|
code_shortcircuit($3, ONFJMP);
|
|
|
|
$$ = 0;
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
1997-09-16 23:33:21 +04:00
|
|
|
| expr1 OR PrepJumpPop expr1
|
|
|
|
{
|
1997-07-30 00:38:45 +04:00
|
|
|
code_shortcircuit($3, ONTJMP);
|
|
|
|
$$ = 0;
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
1997-09-16 23:33:21 +04:00
|
|
|
| FUNCTION { init_func(); } body { func_onstack($3); $$ = 0; }
|
1993-12-17 21:53:07 +03:00
|
|
|
;
|
|
|
|
|
1994-10-11 16:02:39 +03:00
|
|
|
table :
|
|
|
|
{
|
1997-09-16 23:33:21 +04:00
|
|
|
code_opcode(CREATEARRAY, 1);
|
|
|
|
$<vInt>$ = currState->pc; code_word(0);
|
1994-10-11 16:02:39 +03:00
|
|
|
}
|
|
|
|
'{' fieldlist '}'
|
|
|
|
{
|
1997-09-16 23:33:21 +04:00
|
|
|
code_word_at($<vInt>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
|
|
|
{
|
1997-09-16 23:33:21 +04:00
|
|
|
code_opcode(CALLFUNC, -($1+$2+1)); /* ajdust counts results */
|
1995-10-25 16:05:51 +03:00
|
|
|
code_byte($1+$2);
|
1997-07-30 00:38:45 +04:00
|
|
|
$$ = currState->pc;
|
1997-09-16 23:33:21 +04:00
|
|
|
code_byte(0); /* may be adjusted by other rules */
|
1995-10-25 16:05:51 +03:00
|
|
|
}
|
1994-10-17 22:05:32 +03:00
|
|
|
;
|
1994-11-02 23:30:53 +03:00
|
|
|
|
|
|
|
funcvalue : varexp { $$ = 0; }
|
1997-09-16 23:33:21 +04:00
|
|
|
| varexp ':' NAME
|
|
|
|
{
|
|
|
|
code_opcode(PUSHSELF, 1);
|
1997-07-31 02:00:50 +04:00
|
|
|
code_word(string_constant($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; }
|
1997-09-16 23:33:21 +04:00
|
|
|
| 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
|
|
|
|
{
|
1997-09-16 23:33:21 +04:00
|
|
|
currState->f->code[$4] = $<vLong>3; /* store list length */
|
1995-10-25 16:05:51 +03:00
|
|
|
$$ = $4;
|
|
|
|
}
|
1994-11-02 23:30:53 +03:00
|
|
|
}
|
1993-12-17 21:53:07 +03:00
|
|
|
;
|
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
parlist : /* empty */ { code_args(0); }
|
|
|
|
| parlist1 { code_args($1); }
|
1993-12-17 21:53:07 +03:00
|
|
|
;
|
|
|
|
|
1996-05-29 01:07:32 +04:00
|
|
|
parlist1 : par { $$ = $1; }
|
|
|
|
| parlist1 ',' par
|
|
|
|
{
|
|
|
|
if ($1)
|
1997-09-16 23:33:21 +04:00
|
|
|
luaY_error("invalid parameter list");
|
1996-05-29 01:07:32 +04:00
|
|
|
$$ = $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
|
1997-09-16 23:33:21 +04:00
|
|
|
{ flush_list($1/LFIELDS_PER_FLUSH, $1%LFIELDS_PER_FLUSH); }
|
1995-06-08 23:47:28 +04:00
|
|
|
semicolonpart
|
|
|
|
{ $$ = $1+$3; }
|
1994-10-11 16:02:39 +03:00
|
|
|
| ffieldlist1 lastcomma
|
1997-09-16 23:33:21 +04:00
|
|
|
{ $$ = $1; flush_record($1%RFIELDS_PER_FLUSH); }
|
1993-12-17 21:53:07 +03:00
|
|
|
;
|
|
|
|
|
1995-06-08 23:47:28 +04:00
|
|
|
semicolonpart : /* empty */
|
|
|
|
{ $$ = 0; }
|
|
|
|
| ';' ffieldlist
|
1997-09-16 23:33:21 +04:00
|
|
|
{ $$ = $2; flush_record($2%RFIELDS_PER_FLUSH); }
|
1995-06-08 23:47:28 +04:00
|
|
|
;
|
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
lastcomma : /* empty */
|
1994-10-11 16:02:39 +03:00
|
|
|
| ','
|
|
|
|
;
|
1993-12-17 21:53:07 +03:00
|
|
|
|
1995-06-08 23:47:28 +04:00
|
|
|
ffieldlist : /* empty */ { $$ = 0; }
|
|
|
|
| ffieldlist1 lastcomma { $$ = $1; }
|
1997-09-16 23:33:21 +04:00
|
|
|
;
|
1995-06-08 23:47:28 +04:00
|
|
|
|
1993-12-17 21:53:07 +03:00
|
|
|
ffieldlist1 : ffield {$$=1;}
|
1994-02-13 23:38:20 +03:00
|
|
|
| ffieldlist1 ',' ffield
|
|
|
|
{
|
|
|
|
$$=$1+1;
|
1997-09-16 23:33:21 +04:00
|
|
|
if ($$%RFIELDS_PER_FLUSH == 0)
|
|
|
|
flush_record(RFIELDS_PER_FLUSH);
|
1994-02-13 23:38:20 +03:00
|
|
|
}
|
1997-09-16 23:33:21 +04:00
|
|
|
;
|
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 ']'
|
1997-07-31 02:00:50 +04:00
|
|
|
| NAME { code_string($1); }
|
1997-03-06 20:30:55 +03:00
|
|
|
;
|
|
|
|
|
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;
|
1997-09-16 23:33:21 +04:00
|
|
|
if ($$%LFIELDS_PER_FLUSH == 0)
|
|
|
|
flush_list($$/LFIELDS_PER_FLUSH - 1, LFIELDS_PER_FLUSH);
|
1994-02-13 23:38:20 +03:00
|
|
|
}
|
1993-12-17 21:53:07 +03:00
|
|
|
;
|
|
|
|
|
|
|
|
varlist1 : var
|
|
|
|
{
|
1997-09-16 23:33:21 +04:00
|
|
|
$$ = 1;
|
|
|
|
add_varbuffer($1, 0);
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
|
|
|
| varlist1 ',' var
|
1997-09-16 23:33:21 +04:00
|
|
|
{
|
|
|
|
add_varbuffer($3, $1);
|
|
|
|
$$ = $1+1;
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
|
|
|
;
|
|
|
|
|
1994-08-05 23:31:09 +04:00
|
|
|
var : singlevar { $$ = $1; }
|
1997-09-16 23:33:21 +04: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-07-31 02:00:50 +04:00
|
|
|
code_string($3);
|
1997-03-06 20:30:55 +03:00
|
|
|
$$ = 0; /* indexed variable */
|
1993-12-17 21:53:07 +03:00
|
|
|
}
|
|
|
|
;
|
|
|
|
|
1997-09-16 23:33:21 +04:00
|
|
|
singlevar : NAME { $$ = singlevar($1, currState); }
|
1994-08-05 23:31:09 +04:00
|
|
|
;
|
1994-10-17 22:05:32 +03:00
|
|
|
|
|
|
|
varexp : var { lua_pushvar($1); }
|
1997-09-16 23:33:21 +04:00
|
|
|
| '%' NAME { pushupvalue($2); }
|
1994-10-17 22:05:32 +03:00
|
|
|
;
|
1997-09-16 23:33:21 +04:00
|
|
|
|
1996-01-23 20:50:29 +03:00
|
|
|
localdeclist : NAME {store_localvar($1, 0); $$ = 1;}
|
1997-09-16 23:33:21 +04:00
|
|
|
| localdeclist ',' NAME
|
1994-04-15 23:02:04 +04:00
|
|
|
{
|
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
|
|
|
;
|
1997-09-16 23:33:21 +04:00
|
|
|
|
1993-12-17 21:53:07 +03:00
|
|
|
%%
|