1998-05-27 21:08:34 +08:00
|
|
|
/*
|
1999-02-09 02:54:19 +08:00
|
|
|
** $Id: lparser.c,v 1.17 1999/02/08 17:07:59 roberto Exp roberto $
|
1998-05-27 21:08:34 +08:00
|
|
|
** LL(1) Parser and code generator for Lua
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
1999-01-22 02:38:39 +08:00
|
|
|
#include <string.h>
|
1998-05-27 21:08:34 +08:00
|
|
|
|
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "ldo.h"
|
|
|
|
#include "lfunc.h"
|
|
|
|
#include "llex.h"
|
|
|
|
#include "lmem.h"
|
|
|
|
#include "lopcodes.h"
|
|
|
|
#include "lparser.h"
|
|
|
|
#include "lstate.h"
|
|
|
|
#include "lstring.h"
|
|
|
|
#include "lua.h"
|
|
|
|
#include "luadebug.h"
|
|
|
|
#include "lzio.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* for limit numbers in error messages */
|
|
|
|
#define MES_LIM(x) "(limit=" x ")"
|
|
|
|
|
|
|
|
|
|
|
|
/* size of a "normal" jump instruction: OpCode + 1 byte */
|
|
|
|
#define JMPSIZE 2
|
|
|
|
|
|
|
|
/* maximum number of local variables */
|
|
|
|
#define MAXLOCALS 32
|
|
|
|
#define SMAXLOCALS "32"
|
|
|
|
|
|
|
|
|
|
|
|
/* maximum number of upvalues */
|
|
|
|
#define MAXUPVALUES 16
|
|
|
|
#define SMAXUPVALUES "16"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Variable descriptor:
|
|
|
|
** must include a "exp" option because LL(1) cannot distinguish
|
|
|
|
** between variables, upvalues and function calls on first sight.
|
|
|
|
** VGLOBAL: info is constant index of global name
|
|
|
|
** VLOCAL: info is stack index
|
|
|
|
** VDOT: info is constant index of index name
|
|
|
|
** VEXP: info is pc index of "nparam" of function call (or 0 if exp is closed)
|
|
|
|
*/
|
|
|
|
typedef enum {VGLOBAL, VLOCAL, VDOT, VINDEXED, VEXP} varkind;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
varkind k;
|
|
|
|
int info;
|
|
|
|
} vardesc;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Expression List descriptor:
|
|
|
|
** tells number of expressions in the list,
|
|
|
|
** and, if last expression is open (a function call),
|
|
|
|
** where is its pc index of "nparam"
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
int n;
|
|
|
|
int pc; /* 0 if last expression is closed */
|
|
|
|
} listdesc;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Constructors descriptor:
|
|
|
|
** "n" indicates number of elements, and "k" signals whether
|
|
|
|
** it is a list constructor (k = 0) or a record constructor (k = 1)
|
|
|
|
** or empty (k = ';' or '}')
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
int n;
|
|
|
|
int k;
|
|
|
|
} constdesc;
|
|
|
|
|
|
|
|
|
|
|
|
/* state needed to generate code for a given function */
|
|
|
|
typedef struct FuncState {
|
|
|
|
TProtoFunc *f; /* current function header */
|
|
|
|
struct FuncState *prev; /* enclosuring function */
|
|
|
|
int pc; /* next position to code */
|
|
|
|
int stacksize; /* number of values on activation register */
|
|
|
|
int maxstacksize; /* maximum number of values on activation register */
|
|
|
|
int nlocalvar; /* number of active local variables */
|
|
|
|
int nupvalues; /* number of upvalues */
|
|
|
|
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 */
|
|
|
|
int lastsetline; /* line where last SETLINE was issued */
|
|
|
|
vardesc upvalues[MAXUPVALUES]; /* upvalues */
|
|
|
|
TaggedString *localvar[MAXLOCALS]; /* store local variable names */
|
|
|
|
} FuncState;
|
|
|
|
|
|
|
|
|
1999-02-05 00:36:16 +08:00
|
|
|
/*
|
|
|
|
** prototypes for non-terminal functions
|
|
|
|
*/
|
1999-02-09 02:54:19 +08:00
|
|
|
static int assignment (LexState *ls, vardesc *v, int nvars, OpCode *codes);
|
1998-05-27 21:08:34 +08:00
|
|
|
static int cond (LexState *ls);
|
|
|
|
static int funcname (LexState *ls, vardesc *v);
|
|
|
|
static int funcparams (LexState *ls, int slf);
|
|
|
|
static int listfields (LexState *ls);
|
|
|
|
static int localnamelist (LexState *ls);
|
|
|
|
static int optional (LexState *ls, int c);
|
|
|
|
static int recfields (LexState *ls);
|
|
|
|
static int stat (LexState *ls);
|
|
|
|
static void block (LexState *ls);
|
|
|
|
static void body (LexState *ls, int needself, int line);
|
|
|
|
static void chunk (LexState *ls);
|
|
|
|
static void constructor (LexState *ls);
|
|
|
|
static void decinit (LexState *ls, listdesc *d);
|
1998-07-07 06:07:51 +08:00
|
|
|
static void exp0 (LexState *ls, vardesc *v);
|
1999-02-03 03:41:17 +08:00
|
|
|
static void Gexp (LexState *ls, vardesc *v);
|
1998-05-27 21:08:34 +08:00
|
|
|
static void exp1 (LexState *ls);
|
|
|
|
static void exp2 (LexState *ls, vardesc *v);
|
|
|
|
static void explist (LexState *ls, listdesc *e);
|
|
|
|
static void explist1 (LexState *ls, listdesc *e);
|
1999-02-02 02:52:05 +08:00
|
|
|
static void ifpart (LexState *ls, int isexp, int line);
|
1998-05-27 21:08:34 +08:00
|
|
|
static void parlist (LexState *ls);
|
|
|
|
static void part (LexState *ls, constdesc *cd);
|
|
|
|
static void recfield (LexState *ls);
|
|
|
|
static void ret (LexState *ls);
|
|
|
|
static void statlist (LexState *ls);
|
|
|
|
static void var_or_func (LexState *ls, vardesc *v);
|
|
|
|
static void var_or_func_tail (LexState *ls, vardesc *v);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void check_pc (FuncState *fs, int n) {
|
|
|
|
if (fs->pc+n > fs->maxcode)
|
|
|
|
fs->maxcode = luaM_growvector(&fs->f->code, fs->maxcode,
|
|
|
|
Byte, codeEM, MAX_INT);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void code_byte (FuncState *fs, Byte c) {
|
|
|
|
check_pc(fs, 1);
|
|
|
|
fs->f->code[fs->pc++] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void deltastack (LexState *ls, int delta) {
|
|
|
|
FuncState *fs = ls->fs;
|
|
|
|
fs->stacksize += delta;
|
|
|
|
if (fs->stacksize > fs->maxstacksize) {
|
1999-02-05 00:36:16 +08:00
|
|
|
if (fs->stacksize > MAX_BYTE)
|
1998-05-27 21:08:34 +08:00
|
|
|
luaX_error(ls, "function/expression too complex");
|
|
|
|
fs->maxstacksize = fs->stacksize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-02-05 00:36:16 +08:00
|
|
|
static void code_oparg_at (LexState *ls, int pc, OpCode op,
|
|
|
|
int arg, int delta) {
|
1998-05-27 21:08:34 +08:00
|
|
|
Byte *code = ls->fs->f->code;
|
|
|
|
deltastack(ls, delta);
|
1999-02-05 00:36:16 +08:00
|
|
|
if (arg <= MAX_BYTE) {
|
1998-12-28 21:44:54 +08:00
|
|
|
code[pc] = (Byte)op;
|
|
|
|
code[pc+1] = (Byte)arg;
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
|
|
|
else if (arg <= MAX_WORD) {
|
1999-02-05 00:36:16 +08:00
|
|
|
code[pc] = (Byte)(op-1); /* opcode for word argument */
|
1998-12-28 21:44:54 +08:00
|
|
|
code[pc+1] = (Byte)(arg>>8);
|
|
|
|
code[pc+2] = (Byte)(arg&0xFF);
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
1999-02-05 00:36:16 +08:00
|
|
|
else if (arg <= MAX_ARG) {
|
|
|
|
code[pc] = (Byte)LONGARG;
|
|
|
|
code[pc+1] = (Byte)(arg>>16);
|
|
|
|
code_oparg_at(ls, pc+2, op, arg&0xFFFF, 0);
|
|
|
|
}
|
|
|
|
else luaX_error(ls, "code too long");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int codesize (int arg) {
|
|
|
|
if (arg <= MAX_BYTE) return 2; /* opcode + 1 byte */
|
|
|
|
else if (arg <= MAX_WORD) return 3; /* opcode + 1 word */
|
|
|
|
else return 2+codesize(arg&0xFFFF); /* LONGARG + 1 byte + original opcode */
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-02-03 01:57:49 +08:00
|
|
|
static int fix_opcode (LexState *ls, int pc, OpCode op, int arg) {
|
1999-02-05 00:36:16 +08:00
|
|
|
int tomove = codesize(arg)-2;
|
|
|
|
if (tomove > 0) { /* need to open space? */
|
|
|
|
FuncState *fs = ls->fs;
|
|
|
|
TProtoFunc *f = fs->f;
|
|
|
|
check_pc(fs, tomove);
|
|
|
|
luaO_memup(f->code+pc+tomove, f->code+pc, fs->pc-pc);
|
|
|
|
fs->pc += tomove;
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
1999-02-05 00:36:16 +08:00
|
|
|
code_oparg_at(ls, pc, op, arg, 0);
|
|
|
|
return tomove;
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
|
|
|
|
1999-02-03 01:57:49 +08:00
|
|
|
|
|
|
|
static void code_oparg (LexState *ls, OpCode op, int arg, int delta) {
|
1999-02-05 00:36:16 +08:00
|
|
|
int size = codesize(arg);
|
|
|
|
check_pc(ls->fs, size);
|
|
|
|
code_oparg_at(ls, ls->fs->pc, op, arg, delta);
|
|
|
|
ls->fs->pc += size;
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void code_opcode (LexState *ls, OpCode op, int delta) {
|
|
|
|
deltastack(ls, delta);
|
1998-12-28 21:44:54 +08:00
|
|
|
code_byte(ls->fs, (Byte)op);
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void code_constant (LexState *ls, int c) {
|
1999-02-03 01:57:49 +08:00
|
|
|
code_oparg(ls, PUSHCONSTANT, c, 1);
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int next_constant (FuncState *fs) {
|
|
|
|
TProtoFunc *f = fs->f;
|
1999-02-05 00:36:16 +08:00
|
|
|
if (f->nconsts >= fs->maxconsts)
|
1998-05-27 21:08:34 +08:00
|
|
|
fs->maxconsts = luaM_growvector(&f->consts, fs->maxconsts, TObject,
|
1999-02-05 00:36:16 +08:00
|
|
|
constantEM, MAX_ARG);
|
1998-05-27 21:08:34 +08:00
|
|
|
return f->nconsts++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int string_constant (FuncState *fs, TaggedString *s) {
|
|
|
|
TProtoFunc *f = fs->f;
|
|
|
|
int c = s->constindex;
|
|
|
|
if (!(c < f->nconsts &&
|
|
|
|
ttype(&f->consts[c]) == LUA_T_STRING && tsvalue(&f->consts[c]) == s)) {
|
|
|
|
c = next_constant(fs);
|
|
|
|
ttype(&f->consts[c]) = LUA_T_STRING;
|
|
|
|
tsvalue(&f->consts[c]) = s;
|
|
|
|
s->constindex = c; /* hint for next time */
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void code_string (LexState *ls, TaggedString *s) {
|
|
|
|
code_constant(ls, string_constant(ls->fs, s));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define LIM 20
|
|
|
|
static int real_constant (FuncState *fs, real r) {
|
|
|
|
/* check whether 'r' has appeared within the last LIM entries */
|
|
|
|
TObject *cnt = fs->f->consts;
|
|
|
|
int c = fs->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;
|
|
|
|
}
|
1999-02-05 00:36:16 +08:00
|
|
|
/* not found; create a new entry */
|
1998-05-27 21:08:34 +08:00
|
|
|
c = next_constant(fs);
|
1999-02-05 00:36:16 +08:00
|
|
|
cnt = fs->f->consts; /* 'next_constant' may have reallocated this vector */
|
1998-05-27 21:08:34 +08:00
|
|
|
ttype(&cnt[c]) = LUA_T_NUMBER;
|
|
|
|
nvalue(&cnt[c]) = r;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void code_number (LexState *ls, real f) {
|
1999-02-05 00:36:16 +08:00
|
|
|
if (-NUMOFFSET <= f && f <= (real)(MAX_WORD-NUMOFFSET) &&
|
|
|
|
(int)f == f) /* f+NUMOFFSET has a short integer value? */
|
|
|
|
code_oparg(ls, PUSHNUMBER, (int)f+NUMOFFSET, 1);
|
1998-05-27 21:08:34 +08:00
|
|
|
else
|
|
|
|
code_constant(ls, real_constant(ls->fs, f));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void flush_record (LexState *ls, int n) {
|
|
|
|
if (n > 0)
|
1999-02-03 01:57:49 +08:00
|
|
|
code_oparg(ls, SETMAP, n-1, -2*n);
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void flush_list (LexState *ls, int m, int n) {
|
1999-02-05 00:36:16 +08:00
|
|
|
if (n > 0) {
|
|
|
|
code_oparg(ls, SETLIST, m, -n);
|
|
|
|
code_byte(ls->fs, (Byte)n);
|
|
|
|
}
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void luaI_registerlocalvar (FuncState *fs, TaggedString *varname,
|
|
|
|
int line) {
|
|
|
|
if (fs->maxvars != -1) { /* debug information? */
|
|
|
|
TProtoFunc *f = fs->f;
|
1999-02-03 01:57:49 +08:00
|
|
|
if (fs->nvars+1 > fs->maxvars)
|
|
|
|
fs->maxvars = luaM_growvector(&f->locvars, fs->maxvars+1,
|
1999-02-05 00:36:16 +08:00
|
|
|
LocVar, "", MAX_INT);
|
1998-05-27 21:08:34 +08:00
|
|
|
f->locvars[fs->nvars].varname = varname;
|
|
|
|
f->locvars[fs->nvars].line = line;
|
|
|
|
fs->nvars++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void luaI_unregisterlocalvar (FuncState *fs, int line) {
|
|
|
|
luaI_registerlocalvar(fs, NULL, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void store_localvar (LexState *ls, TaggedString *name, int n) {
|
|
|
|
FuncState *fs = ls->fs;
|
1999-02-05 00:36:16 +08:00
|
|
|
if (fs->nlocalvar+n >= MAXLOCALS)
|
1998-05-27 21:08:34 +08:00
|
|
|
luaX_error(ls, "too many local variables " MES_LIM(SMAXLOCALS));
|
1999-02-05 00:36:16 +08:00
|
|
|
fs->localvar[fs->nlocalvar+n] = name;
|
1998-05-27 21:08:34 +08:00
|
|
|
luaI_registerlocalvar(fs, name, ls->linenumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void add_localvar (LexState *ls, TaggedString *name) {
|
|
|
|
store_localvar(ls, name, 0);
|
|
|
|
ls->fs->nlocalvar++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int aux_localname (FuncState *fs, TaggedString *n) {
|
|
|
|
int i;
|
|
|
|
for (i=fs->nlocalvar-1; i >= 0; i--)
|
|
|
|
if (n == fs->localvar[i]) return i; /* local var index */
|
|
|
|
return -1; /* not found */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void singlevar (LexState *ls, TaggedString *n, vardesc *var, int prev) {
|
|
|
|
FuncState *fs = prev ? ls->fs->prev : ls->fs;
|
|
|
|
int i = aux_localname(fs, n);
|
1999-02-05 00:36:16 +08:00
|
|
|
if (i >= 0) { /* local value? */
|
1998-05-27 21:08:34 +08:00
|
|
|
var->k = VLOCAL;
|
|
|
|
var->info = i;
|
|
|
|
}
|
1999-02-05 00:36:16 +08:00
|
|
|
else {
|
1998-05-27 21:08:34 +08:00
|
|
|
FuncState *level = fs;
|
1999-02-05 00:36:16 +08:00
|
|
|
while ((level = level->prev) != NULL) /* check shadowing */
|
1998-05-27 21:08:34 +08:00
|
|
|
if (aux_localname(level, n) >= 0)
|
|
|
|
luaX_syntaxerror(ls, "cannot access a variable in outer scope", n->str);
|
|
|
|
var->k = VGLOBAL;
|
|
|
|
var->info = string_constant(fs, n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int indexupvalue (LexState *ls, TaggedString *n) {
|
|
|
|
FuncState *fs = ls->fs;
|
|
|
|
vardesc v;
|
|
|
|
int i;
|
|
|
|
singlevar(ls, n, &v, 1);
|
|
|
|
for (i=0; i<fs->nupvalues; i++) {
|
|
|
|
if (fs->upvalues[i].k == v.k && fs->upvalues[i].info == v.info)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
/* new one */
|
|
|
|
if (++(fs->nupvalues) > MAXUPVALUES)
|
|
|
|
luaX_error(ls, "too many upvalues in a single function "
|
|
|
|
MES_LIM(SMAXUPVALUES));
|
|
|
|
fs->upvalues[i] = v; /* i = fs->nupvalues - 1 */
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void pushupvalue (LexState *ls, TaggedString *n) {
|
|
|
|
if (ls->fs->prev == NULL)
|
|
|
|
luaX_syntaxerror(ls, "cannot access upvalue in main", n->str);
|
|
|
|
if (aux_localname(ls->fs, n) >= 0)
|
|
|
|
luaX_syntaxerror(ls, "cannot access an upvalue in current scope", n->str);
|
1999-02-05 00:36:16 +08:00
|
|
|
code_oparg(ls, PUSHUPVALUE, indexupvalue(ls, n), 1);
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void check_debugline (LexState *ls) {
|
1999-02-05 01:47:59 +08:00
|
|
|
if (L->debug && ls->linenumber != ls->fs->lastsetline) {
|
1999-02-03 01:57:49 +08:00
|
|
|
code_oparg(ls, SETLINE, ls->linenumber, 0);
|
1998-05-27 21:08:34 +08:00
|
|
|
ls->fs->lastsetline = ls->linenumber;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void adjuststack (LexState *ls, int n) {
|
|
|
|
if (n > 0)
|
1999-02-09 02:54:19 +08:00
|
|
|
code_oparg(ls, POP, n, -n);
|
1998-05-27 21:08:34 +08:00
|
|
|
else if (n < 0)
|
1999-02-03 01:57:49 +08:00
|
|
|
code_oparg(ls, PUSHNIL, (-n)-1, -n);
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void close_exp (LexState *ls, int pc, int nresults) {
|
|
|
|
if (pc > 0) { /* expression is an open function call */
|
|
|
|
Byte *code = ls->fs->f->code;
|
1999-02-05 00:36:16 +08:00
|
|
|
code[pc-1] = nresults; /* set nresults */
|
1998-05-27 21:08:34 +08:00
|
|
|
if (nresults != MULT_RET)
|
1999-02-05 00:36:16 +08:00
|
|
|
deltastack(ls, nresults); /* push results */
|
|
|
|
deltastack(ls, -(code[pc]+1)); /* pop params (at code[pc]) and function */
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void adjust_mult_assign (LexState *ls, int nvars, listdesc *d) {
|
|
|
|
int diff = d->n - nvars;
|
|
|
|
if (d->pc == 0) { /* list is closed */
|
|
|
|
/* push or pop eventual difference between list lengths */
|
|
|
|
adjuststack(ls, diff);
|
|
|
|
}
|
|
|
|
else { /* must correct function call */
|
|
|
|
diff--; /* do not count function call itself */
|
1999-02-05 00:36:16 +08:00
|
|
|
if (diff <= 0) { /* more variables than values? */
|
1998-05-27 21:08:34 +08:00
|
|
|
/* function call must provide extra values */
|
|
|
|
close_exp(ls, d->pc, -diff);
|
|
|
|
}
|
|
|
|
else { /* more values than variables */
|
|
|
|
close_exp(ls, d->pc, 0); /* call should provide no value */
|
|
|
|
adjuststack(ls, diff); /* pop eventual extra values */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void code_args (LexState *ls, int nparams, int dots) {
|
|
|
|
FuncState *fs = ls->fs;
|
|
|
|
fs->nlocalvar += nparams; /* "self" may already be there */
|
|
|
|
nparams = fs->nlocalvar;
|
|
|
|
if (!dots) {
|
1998-12-28 21:44:54 +08:00
|
|
|
fs->f->code[1] = (Byte)nparams; /* fill-in arg information */
|
1998-05-27 21:08:34 +08:00
|
|
|
deltastack(ls, nparams);
|
|
|
|
}
|
|
|
|
else {
|
1998-12-28 21:44:54 +08:00
|
|
|
fs->f->code[1] = (Byte)(nparams+ZEROVARARG);
|
1998-05-27 21:08:34 +08:00
|
|
|
deltastack(ls, nparams+1);
|
|
|
|
add_localvar(ls, luaS_new("arg"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-02-03 03:41:17 +08:00
|
|
|
static void unloaddot (LexState *ls, vardesc *v) {
|
|
|
|
/* dotted variables <a.x> must be stored like regular indexed vars <a["x"]> */
|
|
|
|
if (v->k == VDOT) {
|
|
|
|
code_constant(ls, v->info);
|
|
|
|
v->k = VINDEXED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-05-27 21:08:34 +08:00
|
|
|
static void lua_pushvar (LexState *ls, vardesc *var) {
|
|
|
|
switch (var->k) {
|
|
|
|
case VLOCAL:
|
1999-02-03 01:57:49 +08:00
|
|
|
code_oparg(ls, PUSHLOCAL, var->info, 1);
|
1998-05-27 21:08:34 +08:00
|
|
|
break;
|
|
|
|
case VGLOBAL:
|
1999-02-03 01:57:49 +08:00
|
|
|
code_oparg(ls, GETGLOBAL, var->info, 1);
|
1998-05-27 21:08:34 +08:00
|
|
|
break;
|
|
|
|
case VDOT:
|
1999-02-03 01:57:49 +08:00
|
|
|
code_oparg(ls, GETDOTTED, var->info, 0);
|
1998-05-27 21:08:34 +08:00
|
|
|
break;
|
|
|
|
case VINDEXED:
|
|
|
|
code_opcode(ls, GETTABLE, -1);
|
|
|
|
break;
|
|
|
|
case VEXP:
|
|
|
|
close_exp(ls, var->info, 1); /* function must return 1 value */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
var->k = VEXP;
|
|
|
|
var->info = 0; /* now this is a closed expression */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-02-09 02:54:19 +08:00
|
|
|
/* to be used by "storevar" and assignment */
|
|
|
|
static OpCode set_pop[] = {SETLOCAL, SETGLOBAL, SETTABLEPOP, SETTABLE};
|
|
|
|
static OpCode set_dup[] = {SETLOCALDUP, SETGLOBALDUP, SETTABPPDUP,
|
|
|
|
SETTABLEDUP};
|
|
|
|
|
|
|
|
|
|
|
|
static void storevar (LexState *ls, vardesc *var, OpCode *codes) {
|
1998-05-27 21:08:34 +08:00
|
|
|
switch (var->k) {
|
|
|
|
case VLOCAL:
|
1999-02-03 03:41:17 +08:00
|
|
|
code_oparg(ls, codes[0], var->info, -1);
|
1998-05-27 21:08:34 +08:00
|
|
|
break;
|
|
|
|
case VGLOBAL:
|
1999-02-03 03:41:17 +08:00
|
|
|
code_oparg(ls, codes[1], var->info, -1);
|
1998-05-27 21:08:34 +08:00
|
|
|
break;
|
|
|
|
case VINDEXED:
|
1999-02-03 03:41:17 +08:00
|
|
|
code_opcode(ls, codes[2], -3);
|
1998-05-27 21:08:34 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LUA_INTERNALERROR("invalid var kind to store");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int fix_jump (LexState *ls, int pc, OpCode op, int n) {
|
|
|
|
/* jump is relative to position following jump instruction */
|
1999-02-03 01:57:49 +08:00
|
|
|
return fix_opcode(ls, pc, op, n-(pc+JMPSIZE));
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void fix_upjmp (LexState *ls, OpCode op, int pos) {
|
|
|
|
int delta = ls->fs->pc+JMPSIZE - pos; /* jump is relative */
|
1999-02-05 00:36:16 +08:00
|
|
|
code_oparg(ls, op, delta+(codesize(delta)-2), 0);
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void codeIf (LexState *ls, int thenAdd, int elseAdd) {
|
|
|
|
FuncState *fs = ls->fs;
|
|
|
|
int elseinit = elseAdd+JMPSIZE;
|
1999-02-05 00:36:16 +08:00
|
|
|
if (fs->pc == elseinit) { /* no else part? */
|
1998-05-27 21:08:34 +08:00
|
|
|
fs->pc -= JMPSIZE;
|
|
|
|
elseinit = fs->pc;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
elseinit += fix_jump(ls, elseAdd, JMP, fs->pc);
|
|
|
|
fix_jump(ls, thenAdd, IFFJMP, elseinit);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void func_onstack (LexState *ls, FuncState *func) {
|
|
|
|
FuncState *fs = ls->fs;
|
|
|
|
int i;
|
|
|
|
int c = next_constant(fs);
|
|
|
|
ttype(&fs->f->consts[c]) = LUA_T_PROTO;
|
|
|
|
fs->f->consts[c].value.tf = func->f;
|
|
|
|
if (func->nupvalues == 0)
|
|
|
|
code_constant(ls, c);
|
|
|
|
else {
|
|
|
|
for (i=0; i<func->nupvalues; i++)
|
|
|
|
lua_pushvar(ls, &func->upvalues[i]);
|
1999-02-03 01:57:49 +08:00
|
|
|
code_oparg(ls, CLOSURE, c, -func->nupvalues+1);
|
1998-12-28 21:44:54 +08:00
|
|
|
code_byte(fs, (Byte)func->nupvalues);
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void init_state (LexState *ls, FuncState *fs, TaggedString *filename) {
|
|
|
|
TProtoFunc *f = luaF_newproto();
|
|
|
|
fs->prev = ls->fs; /* linked list of funcstates */
|
|
|
|
ls->fs = fs;
|
|
|
|
fs->stacksize = 0;
|
|
|
|
fs->maxstacksize = 0;
|
|
|
|
fs->nlocalvar = 0;
|
|
|
|
fs->nupvalues = 0;
|
|
|
|
fs->lastsetline = 0;
|
|
|
|
fs->f = f;
|
|
|
|
f->fileName = filename;
|
|
|
|
fs->pc = 0;
|
|
|
|
fs->maxcode = 0;
|
|
|
|
f->code = NULL;
|
|
|
|
fs->maxconsts = 0;
|
1999-02-05 01:47:59 +08:00
|
|
|
if (L->debug)
|
1998-05-27 21:08:34 +08:00
|
|
|
fs->nvars = fs->maxvars = 0;
|
|
|
|
else
|
|
|
|
fs->maxvars = -1; /* flag no debug information */
|
1999-02-05 00:36:16 +08:00
|
|
|
code_byte(fs, 0); /* to be filled with maxstacksize */
|
1998-05-27 21:08:34 +08:00
|
|
|
code_byte(fs, 0); /* to be filled with arg information */
|
1999-01-15 19:38:33 +08:00
|
|
|
/* push function (to avoid GC) */
|
|
|
|
tfvalue(L->stack.top) = f; ttype(L->stack.top) = LUA_T_PROTO;
|
|
|
|
incr_top;
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void close_func (LexState *ls) {
|
|
|
|
FuncState *fs = ls->fs;
|
|
|
|
TProtoFunc *f = fs->f;
|
|
|
|
code_opcode(ls, ENDCODE, 0);
|
1998-12-28 21:44:54 +08:00
|
|
|
f->code[0] = (Byte)fs->maxstacksize;
|
1998-05-27 21:08:34 +08:00
|
|
|
f->code = luaM_reallocvector(f->code, fs->pc, Byte);
|
|
|
|
f->consts = luaM_reallocvector(f->consts, f->nconsts, TObject);
|
|
|
|
if (fs->maxvars != -1) { /* debug information? */
|
|
|
|
luaI_registerlocalvar(fs, NULL, -1); /* flag end of vector */
|
|
|
|
f->locvars = luaM_reallocvector(f->locvars, fs->nvars, LocVar);
|
|
|
|
}
|
|
|
|
ls->fs = fs->prev;
|
1999-01-15 19:38:33 +08:00
|
|
|
L->stack.top--; /* pop function */
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int expfollow [] = {ELSE, ELSEIF, THEN, IF, WHILE, REPEAT, DO, NAME,
|
|
|
|
LOCAL, FUNCTION, END, UNTIL, RETURN, ')', ']', '}', ';', EOS, ',', 0};
|
|
|
|
|
1999-02-05 00:36:16 +08:00
|
|
|
|
1998-05-27 21:08:34 +08:00
|
|
|
static int is_in (int tok, int *toks) {
|
1999-02-05 00:36:16 +08:00
|
|
|
int *t;
|
|
|
|
for (t=toks; *t; t++)
|
|
|
|
if (*t == tok) return t-toks;
|
1998-05-27 21:08:34 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void next (LexState *ls) {
|
|
|
|
ls->token = luaX_lex(ls);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void error_expected (LexState *ls, int token) {
|
|
|
|
char buff[100], t[TOKEN_LEN];
|
1998-07-25 02:02:38 +08:00
|
|
|
luaX_token2str(token, t);
|
1998-05-27 21:08:34 +08:00
|
|
|
sprintf(buff, "`%s' expected", t);
|
|
|
|
luaX_error(ls, buff);
|
|
|
|
}
|
|
|
|
|
1998-12-23 22:06:57 +08:00
|
|
|
|
|
|
|
static void error_unexpected (LexState *ls) {
|
|
|
|
luaX_error(ls, "unexpected token");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-05-27 21:08:34 +08:00
|
|
|
static void error_unmatched (LexState *ls, int what, int who, int where) {
|
|
|
|
if (where == ls->linenumber)
|
|
|
|
error_expected(ls, what);
|
|
|
|
else {
|
|
|
|
char buff[100];
|
|
|
|
char t_what[TOKEN_LEN], t_who[TOKEN_LEN];
|
1998-07-25 02:02:38 +08:00
|
|
|
luaX_token2str(what, t_what);
|
|
|
|
luaX_token2str(who, t_who);
|
1998-05-27 21:08:34 +08:00
|
|
|
sprintf(buff, "`%s' expected (to close `%s' at line %d)",
|
|
|
|
t_what, t_who, where);
|
|
|
|
luaX_error(ls, buff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void check (LexState *ls, int c) {
|
|
|
|
if (ls->token != c)
|
|
|
|
error_expected(ls, c);
|
|
|
|
next(ls);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void check_match (LexState *ls, int what, int who, int where) {
|
|
|
|
if (ls->token != what)
|
|
|
|
error_unmatched(ls, what, who, where);
|
|
|
|
check_debugline(ls); /* to 'mark' the 'what' */
|
|
|
|
next(ls);
|
|
|
|
}
|
|
|
|
|
1999-01-15 19:38:33 +08:00
|
|
|
static int checkname (LexState *ls) {
|
|
|
|
int sc;
|
1998-05-27 21:08:34 +08:00
|
|
|
if (ls->token != NAME)
|
|
|
|
luaX_error(ls, "`NAME' expected");
|
1999-01-15 19:38:33 +08:00
|
|
|
sc = string_constant(ls->fs, ls->seminfo.ts);
|
1998-05-27 21:08:34 +08:00
|
|
|
next(ls);
|
1999-01-15 19:38:33 +08:00
|
|
|
return sc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static TaggedString *str_checkname (LexState *ls) {
|
1999-02-05 00:36:16 +08:00
|
|
|
return tsvalue(&ls->fs->f->consts[checkname(ls)]);
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int optional (LexState *ls, int c) {
|
|
|
|
if (ls->token == c) {
|
|
|
|
next(ls);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TProtoFunc *luaY_parser (ZIO *z) {
|
|
|
|
struct LexState lexstate;
|
|
|
|
struct FuncState funcstate;
|
|
|
|
luaX_setinput(&lexstate, z);
|
|
|
|
init_state(&lexstate, &funcstate, luaS_new(zname(z)));
|
|
|
|
next(&lexstate); /* read first token */
|
|
|
|
chunk(&lexstate);
|
|
|
|
if (lexstate.token != EOS)
|
|
|
|
luaX_error(&lexstate, "<eof> expected");
|
|
|
|
close_func(&lexstate);
|
|
|
|
return funcstate.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*============================================================*/
|
|
|
|
/* GRAMAR RULES */
|
|
|
|
/*============================================================*/
|
|
|
|
|
|
|
|
static void chunk (LexState *ls) {
|
|
|
|
/* chunk -> statlist ret */
|
|
|
|
statlist(ls);
|
|
|
|
ret(ls);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void statlist (LexState *ls) {
|
|
|
|
/* statlist -> { stat [;] } */
|
|
|
|
while (stat(ls)) {
|
|
|
|
LUA_ASSERT(ls->fs->stacksize == ls->fs->nlocalvar,
|
|
|
|
"stack size != # local vars");
|
|
|
|
optional(ls, ';');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stat (LexState *ls) {
|
|
|
|
int line = ls->linenumber; /* may be needed for error messages */
|
|
|
|
FuncState *fs = ls->fs;
|
|
|
|
switch (ls->token) {
|
1999-02-02 02:52:05 +08:00
|
|
|
case IF: /* stat -> IF ifpart END */
|
|
|
|
ifpart(ls, 0, line);
|
1998-05-27 21:08:34 +08:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
case WHILE: { /* stat -> WHILE cond DO block END */
|
|
|
|
TProtoFunc *f = fs->f;
|
|
|
|
int while_init = fs->pc;
|
|
|
|
int cond_end, cond_size;
|
|
|
|
next(ls);
|
|
|
|
cond_end = cond(ls);
|
|
|
|
check(ls, DO);
|
|
|
|
block(ls);
|
|
|
|
check_match(ls, END, WHILE, line);
|
|
|
|
cond_size = cond_end-while_init;
|
|
|
|
check_pc(fs, cond_size);
|
|
|
|
memcpy(f->code+fs->pc, f->code+while_init, cond_size);
|
|
|
|
luaO_memdown(f->code+while_init, f->code+cond_end, fs->pc-while_init);
|
|
|
|
while_init += JMPSIZE + fix_jump(ls, while_init, JMP, fs->pc-cond_size);
|
|
|
|
fix_upjmp(ls, IFTUPJMP, while_init);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DO: { /* stat -> DO block END */
|
|
|
|
next(ls);
|
|
|
|
block(ls);
|
|
|
|
check_match(ls, END, DO, line);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
case REPEAT: { /* stat -> REPEAT block UNTIL exp1 */
|
|
|
|
int repeat_init = fs->pc;
|
|
|
|
next(ls);
|
|
|
|
block(ls);
|
|
|
|
check_match(ls, UNTIL, REPEAT, line);
|
|
|
|
exp1(ls);
|
|
|
|
fix_upjmp(ls, IFFUPJMP, repeat_init);
|
|
|
|
deltastack(ls, -1); /* pops condition */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
case FUNCTION: { /* stat -> FUNCTION funcname body */
|
|
|
|
int needself;
|
|
|
|
vardesc v;
|
|
|
|
if (ls->fs->prev) /* inside other function? */
|
|
|
|
return 0;
|
|
|
|
check_debugline(ls);
|
|
|
|
next(ls);
|
|
|
|
needself = funcname(ls, &v);
|
|
|
|
body(ls, needself, line);
|
1999-02-09 02:54:19 +08:00
|
|
|
storevar(ls, &v, set_pop);
|
1998-05-27 21:08:34 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
case LOCAL: { /* stat -> LOCAL localnamelist decinit */
|
|
|
|
listdesc d;
|
|
|
|
int nvars;
|
|
|
|
check_debugline(ls);
|
|
|
|
next(ls);
|
|
|
|
nvars = localnamelist(ls);
|
|
|
|
decinit(ls, &d);
|
|
|
|
ls->fs->nlocalvar += nvars;
|
|
|
|
adjust_mult_assign(ls, nvars, &d);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
case NAME: case '%': { /* stat -> func | ['%'] NAME assignment */
|
|
|
|
vardesc v;
|
|
|
|
check_debugline(ls);
|
|
|
|
var_or_func(ls, &v);
|
|
|
|
if (v.k == VEXP) { /* stat -> func */
|
|
|
|
if (v.info == 0) /* is just an upper value? */
|
|
|
|
luaX_error(ls, "syntax error");
|
|
|
|
close_exp(ls, v.info, 0);
|
|
|
|
}
|
1999-02-09 02:54:19 +08:00
|
|
|
else { /* stat -> ['%'] NAME assignment */
|
|
|
|
int left = assignment(ls, &v, 1, set_pop);
|
1998-05-27 21:08:34 +08:00
|
|
|
adjuststack(ls, left); /* remove eventual 'garbage' left on stack */
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
case RETURN: case ';': case ELSE: case ELSEIF:
|
|
|
|
case END: case UNTIL: case EOS: /* 'stat' follow */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
1998-12-23 22:06:57 +08:00
|
|
|
error_unexpected(ls);
|
1998-05-27 21:08:34 +08:00
|
|
|
return 0; /* to avoid warnings */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int SaveWord (LexState *ls) {
|
|
|
|
int res = ls->fs->pc;
|
|
|
|
check_pc(ls->fs, JMPSIZE);
|
|
|
|
ls->fs->pc += JMPSIZE; /* open space */
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int SaveWordPop (LexState *ls) {
|
|
|
|
deltastack(ls, -1); /* pop condition */
|
|
|
|
return SaveWord(ls);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cond (LexState *ls) {
|
|
|
|
/* cond -> exp1 */
|
|
|
|
exp1(ls);
|
|
|
|
return SaveWordPop(ls);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void block (LexState *ls) {
|
|
|
|
/* block -> chunk */
|
|
|
|
FuncState *fs = ls->fs;
|
|
|
|
int nlocalvar = fs->nlocalvar;
|
|
|
|
chunk(ls);
|
|
|
|
adjuststack(ls, fs->nlocalvar - nlocalvar);
|
|
|
|
for (; fs->nlocalvar > nlocalvar; fs->nlocalvar--)
|
|
|
|
luaI_unregisterlocalvar(fs, ls->linenumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int funcname (LexState *ls, vardesc *v) {
|
|
|
|
/* funcname -> NAME [':' NAME | '.' NAME] */
|
|
|
|
int needself = 0;
|
1999-01-15 19:38:33 +08:00
|
|
|
singlevar(ls, str_checkname(ls), v, 0);
|
1998-05-27 21:08:34 +08:00
|
|
|
if (ls->token == ':' || ls->token == '.') {
|
|
|
|
needself = (ls->token == ':');
|
|
|
|
next(ls);
|
|
|
|
lua_pushvar(ls, v);
|
1999-01-15 19:38:33 +08:00
|
|
|
code_constant(ls, checkname(ls));
|
1998-05-27 21:08:34 +08:00
|
|
|
v->k = VINDEXED;
|
|
|
|
}
|
|
|
|
return needself;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void body (LexState *ls, int needself, int line) {
|
|
|
|
/* body -> '(' parlist ')' chunk END */
|
|
|
|
FuncState newfs;
|
|
|
|
init_state(ls, &newfs, ls->fs->f->fileName);
|
|
|
|
newfs.f->lineDefined = line;
|
|
|
|
check(ls, '(');
|
|
|
|
if (needself)
|
|
|
|
add_localvar(ls, luaS_new("self"));
|
|
|
|
parlist(ls);
|
|
|
|
check(ls, ')');
|
|
|
|
chunk(ls);
|
|
|
|
check_match(ls, END, FUNCTION, line);
|
|
|
|
close_func(ls);
|
|
|
|
func_onstack(ls, &newfs);
|
|
|
|
}
|
|
|
|
|
1999-02-02 02:52:05 +08:00
|
|
|
|
|
|
|
static void ifpart (LexState *ls, int isexp, int line) {
|
1998-05-27 21:08:34 +08:00
|
|
|
/* ifpart -> cond THEN block [ELSE block | ELSEIF ifpart] */
|
1999-02-02 02:52:05 +08:00
|
|
|
/* ifpart -> cond THEN exp [ELSE exp | ELSEIF ifpart] */
|
|
|
|
int c;
|
1998-05-27 21:08:34 +08:00
|
|
|
int e;
|
1999-02-02 02:52:05 +08:00
|
|
|
next(ls); /* skip IF or ELSEIF */
|
|
|
|
c = cond(ls);
|
1998-05-27 21:08:34 +08:00
|
|
|
check(ls, THEN);
|
1999-02-02 21:47:31 +08:00
|
|
|
if (isexp) {
|
|
|
|
exp1(ls);
|
1999-02-05 00:36:16 +08:00
|
|
|
deltastack(ls, -1); /* only 'then' x-or 'else' will stay on the stack */
|
1999-02-02 21:47:31 +08:00
|
|
|
}
|
1999-02-02 02:52:05 +08:00
|
|
|
else block(ls);
|
1998-05-27 21:08:34 +08:00
|
|
|
e = SaveWord(ls);
|
1999-02-02 02:52:05 +08:00
|
|
|
if (ls->token == ELSEIF)
|
|
|
|
ifpart(ls, isexp, line);
|
|
|
|
else {
|
|
|
|
int elsepart = optional(ls, ELSE);
|
|
|
|
if (!isexp) {
|
|
|
|
if (elsepart) block(ls);
|
|
|
|
}
|
|
|
|
else { /* is exp */
|
|
|
|
if (elsepart) exp1(ls);
|
1999-02-03 01:57:49 +08:00
|
|
|
else adjuststack(ls, -1); /* empty else exp -> push nil */
|
1999-02-02 02:52:05 +08:00
|
|
|
}
|
|
|
|
check_match(ls, END, IF, line);
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
|
|
|
codeIf(ls, c, e);
|
|
|
|
}
|
|
|
|
|
1999-02-02 02:52:05 +08:00
|
|
|
|
1998-05-27 21:08:34 +08:00
|
|
|
static void ret (LexState *ls) {
|
|
|
|
/* ret -> [RETURN explist sc] */
|
1999-02-05 00:36:16 +08:00
|
|
|
check_debugline(ls);
|
|
|
|
if (optional(ls, RETURN)) {
|
1998-05-27 21:08:34 +08:00
|
|
|
listdesc e;
|
|
|
|
explist(ls, &e);
|
|
|
|
close_exp(ls, e.pc, MULT_RET);
|
1999-02-03 01:57:49 +08:00
|
|
|
code_oparg(ls, RETCODE, ls->fs->nlocalvar, 0);
|
1998-05-27 21:08:34 +08:00
|
|
|
ls->fs->stacksize = ls->fs->nlocalvar; /* removes all temp values */
|
|
|
|
optional(ls, ';');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** For parsing expressions, we use a classic stack with priorities.
|
|
|
|
** Each binary operator is represented by its index in "binop" + FIRSTBIN
|
|
|
|
** (EQ=2, NE=3, ... '^'=13). The unary NOT is 0 and UNMINUS is 1.
|
|
|
|
*/
|
|
|
|
|
1999-02-05 00:36:16 +08:00
|
|
|
#define INDNOT 0
|
|
|
|
#define INDMINUS 1
|
|
|
|
|
1998-05-27 21:08:34 +08:00
|
|
|
/* code of first binary operator */
|
|
|
|
#define FIRSTBIN 2
|
|
|
|
|
|
|
|
/* code for power operator (last operator)
|
|
|
|
** '^' needs special treatment because it is right associative
|
|
|
|
*/
|
|
|
|
#define POW 13
|
|
|
|
|
|
|
|
static int binop [] = {EQ, NE, '>', '<', LE, GE, CONC,
|
|
|
|
'+', '-', '*', '/', '^', 0};
|
|
|
|
|
|
|
|
static int priority [POW+1] = {5, 5, 1, 1, 1, 1, 1, 1, 2, 3, 3, 4, 4, 6};
|
|
|
|
|
|
|
|
static OpCode opcodes [POW+1] = {NOTOP, MINUSOP, EQOP, NEQOP, GTOP, LTOP,
|
|
|
|
LEOP, GEOP, CONCOP, ADDOP, SUBOP, MULTOP, DIVOP, POWOP};
|
|
|
|
|
1999-02-05 00:36:16 +08:00
|
|
|
#define MAXOPS 20 /* op's stack size */
|
1998-05-27 21:08:34 +08:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int ops[MAXOPS];
|
|
|
|
int top;
|
|
|
|
} stack_op;
|
|
|
|
|
|
|
|
|
|
|
|
static void exp1 (LexState *ls) {
|
|
|
|
vardesc v;
|
1998-07-07 06:07:51 +08:00
|
|
|
exp0(ls, &v);
|
1998-05-27 21:08:34 +08:00
|
|
|
lua_pushvar(ls, &v);
|
|
|
|
if (is_in(ls->token, expfollow) < 0)
|
|
|
|
luaX_error(ls, "ill formed expression");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-07-07 06:07:51 +08:00
|
|
|
static void exp0 (LexState *ls, vardesc *v) {
|
1999-02-05 00:36:16 +08:00
|
|
|
/* exp0 -> exp2 {(AND | OR) exp2} */
|
1998-05-27 21:08:34 +08:00
|
|
|
exp2(ls, v);
|
|
|
|
while (ls->token == AND || ls->token == OR) {
|
1999-02-05 00:36:16 +08:00
|
|
|
int op = (ls->token == AND) ? ONFJMP : ONTJMP;
|
1998-05-27 21:08:34 +08:00
|
|
|
int pc;
|
|
|
|
lua_pushvar(ls, v);
|
|
|
|
next(ls);
|
|
|
|
pc = SaveWordPop(ls);
|
|
|
|
exp2(ls, v);
|
|
|
|
lua_pushvar(ls, v);
|
1999-02-05 00:36:16 +08:00
|
|
|
fix_jump(ls, pc, op, ls->fs->pc);
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-02-03 03:41:17 +08:00
|
|
|
static void Gexp (LexState *ls, vardesc *v) {
|
1999-02-09 02:54:19 +08:00
|
|
|
/* Gexp -> exp0 | assignment */
|
1999-02-03 03:41:17 +08:00
|
|
|
exp0(ls, v);
|
1999-02-09 02:54:19 +08:00
|
|
|
if (v->k != VEXP && (ls->token == '=' || ls->token == ',')) {
|
|
|
|
int left = assignment(ls, v, 1, set_dup);
|
1999-02-03 03:41:17 +08:00
|
|
|
deltastack(ls, 1); /* DUP operations push an extra value */
|
1999-02-09 02:54:19 +08:00
|
|
|
if (left > 0)
|
|
|
|
code_oparg(ls, POPDUP, left, -left);
|
1999-02-05 00:36:16 +08:00
|
|
|
v->k = VEXP; v->info = 0; /* this expression is closed now */
|
1999-02-03 03:41:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-05-27 21:08:34 +08:00
|
|
|
static void push (LexState *ls, stack_op *s, int op) {
|
1999-02-05 00:36:16 +08:00
|
|
|
if (s->top >= MAXOPS)
|
1998-05-27 21:08:34 +08:00
|
|
|
luaX_error(ls, "expression too complex");
|
|
|
|
s->ops[s->top++] = op;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void pop_to (LexState *ls, stack_op *s, int prio) {
|
|
|
|
int op;
|
|
|
|
while (s->top > 0 && priority[(op=s->ops[s->top-1])] >= prio) {
|
|
|
|
code_opcode(ls, opcodes[op], op<FIRSTBIN?0:-1);
|
|
|
|
s->top--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-29 21:48:58 +08:00
|
|
|
static void simpleexp (LexState *ls, vardesc *v, stack_op *s) {
|
1998-05-27 21:08:34 +08:00
|
|
|
check_debugline(ls);
|
|
|
|
switch (ls->token) {
|
1999-01-29 21:48:58 +08:00
|
|
|
case NUMBER: { /* simpleexp -> NUMBER */
|
|
|
|
real r = ls->seminfo.r;
|
1998-05-27 21:08:34 +08:00
|
|
|
next(ls);
|
1999-02-05 00:36:16 +08:00
|
|
|
/* dirty trick: check whether it is a -NUMBER not followed by '^' */
|
|
|
|
/* (because the priority of '^' is closer than '-'...) */
|
1999-01-29 21:48:58 +08:00
|
|
|
if (s->top > 0 && s->ops[s->top-1] == INDMINUS && ls->token != '^') {
|
1999-02-05 00:36:16 +08:00
|
|
|
s->top--; /* remove '-' from stack */
|
1999-01-29 21:48:58 +08:00
|
|
|
r = -r;
|
|
|
|
}
|
|
|
|
code_number(ls, r);
|
1998-05-27 21:08:34 +08:00
|
|
|
break;
|
1999-01-29 21:48:58 +08:00
|
|
|
}
|
1998-05-27 21:08:34 +08:00
|
|
|
|
|
|
|
case STRING: /* simpleexp -> STRING */
|
1999-02-05 00:36:16 +08:00
|
|
|
code_string(ls, ls->seminfo.ts); /* must use 'seminfo' before "next" */
|
1998-05-27 21:08:34 +08:00
|
|
|
next(ls);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NIL: /* simpleexp -> NIL */
|
|
|
|
adjuststack(ls, -1);
|
|
|
|
next(ls);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '{': /* simpleexp -> constructor */
|
|
|
|
constructor(ls);
|
|
|
|
break;
|
|
|
|
|
1999-02-02 02:52:05 +08:00
|
|
|
case FUNCTION: /* simpleexp -> FUNCTION body */
|
1998-05-27 21:08:34 +08:00
|
|
|
next(ls);
|
1999-02-02 02:52:05 +08:00
|
|
|
body(ls, 0, ls->linenumber);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IF: /* simpleexp -> IF ifpart END */
|
|
|
|
ifpart(ls, 1, ls->linenumber);
|
1998-05-27 21:08:34 +08:00
|
|
|
break;
|
|
|
|
|
1999-02-03 03:41:17 +08:00
|
|
|
case '(': /* simpleexp -> '(' Gexp ')' */
|
1999-01-29 21:48:58 +08:00
|
|
|
next(ls);
|
1999-02-03 03:41:17 +08:00
|
|
|
Gexp(ls, v);
|
1999-01-29 21:48:58 +08:00
|
|
|
check(ls, ')');
|
|
|
|
return;
|
|
|
|
|
1998-05-27 21:08:34 +08:00
|
|
|
case NAME: case '%':
|
|
|
|
var_or_func(ls, v);
|
1999-01-29 21:48:58 +08:00
|
|
|
return;
|
1998-05-27 21:08:34 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
luaX_error(ls, "<expression> expected");
|
1999-01-29 21:48:58 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
v->k = VEXP; v->info = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-02-05 00:36:16 +08:00
|
|
|
static void prefixexp (LexState *ls, vardesc *v, stack_op *s) {
|
|
|
|
/* prefixexp -> {NOT | '-'} simpleexp */
|
|
|
|
while (ls->token == NOT || ls->token == '-') {
|
|
|
|
push(ls, s, (ls->token==NOT)?INDNOT:INDMINUS);
|
|
|
|
next(ls);
|
|
|
|
}
|
|
|
|
simpleexp(ls, v, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-01-29 21:48:58 +08:00
|
|
|
static void exp2 (LexState *ls, vardesc *v) {
|
|
|
|
stack_op s;
|
|
|
|
int op;
|
|
|
|
s.top = 0;
|
1999-02-05 00:36:16 +08:00
|
|
|
prefixexp(ls, v, &s);
|
1999-01-29 21:48:58 +08:00
|
|
|
while ((op = is_in(ls->token, binop)) >= 0) {
|
|
|
|
op += FIRSTBIN;
|
|
|
|
lua_pushvar(ls, v);
|
|
|
|
/* '^' is right associative, so must 'simulate' a higher priority */
|
|
|
|
pop_to(ls, &s, (op == POW)?priority[op]+1:priority[op]);
|
|
|
|
push(ls, &s, op);
|
|
|
|
next(ls);
|
1999-02-05 00:36:16 +08:00
|
|
|
prefixexp(ls, v, &s);
|
1999-01-29 21:48:58 +08:00
|
|
|
lua_pushvar(ls, v);
|
|
|
|
}
|
|
|
|
if (s.top > 0) {
|
|
|
|
lua_pushvar(ls, v);
|
|
|
|
pop_to(ls, &s, 0);
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-29 21:48:58 +08:00
|
|
|
|
1998-05-27 21:08:34 +08:00
|
|
|
static void var_or_func (LexState *ls, vardesc *v) {
|
|
|
|
/* var_or_func -> ['%'] NAME var_or_func_tail */
|
|
|
|
if (optional(ls, '%')) { /* upvalue? */
|
1999-01-15 19:38:33 +08:00
|
|
|
pushupvalue(ls, str_checkname(ls));
|
1998-05-27 21:08:34 +08:00
|
|
|
v->k = VEXP;
|
|
|
|
v->info = 0; /* closed expression */
|
|
|
|
}
|
|
|
|
else /* variable name */
|
1999-01-15 19:38:33 +08:00
|
|
|
singlevar(ls, str_checkname(ls), v, 0);
|
1998-05-27 21:08:34 +08:00
|
|
|
var_or_func_tail(ls, v);
|
|
|
|
}
|
|
|
|
|
1999-02-05 00:36:16 +08:00
|
|
|
|
1998-05-27 21:08:34 +08:00
|
|
|
static void var_or_func_tail (LexState *ls, vardesc *v) {
|
|
|
|
for (;;) {
|
|
|
|
switch (ls->token) {
|
|
|
|
case '.': /* var_or_func_tail -> '.' NAME */
|
|
|
|
next(ls);
|
|
|
|
lua_pushvar(ls, v); /* 'v' must be on stack */
|
|
|
|
v->k = VDOT;
|
1999-01-15 19:38:33 +08:00
|
|
|
v->info = checkname(ls);
|
1998-05-27 21:08:34 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '[': /* var_or_func_tail -> '[' exp1 ']' */
|
|
|
|
next(ls);
|
|
|
|
lua_pushvar(ls, v); /* 'v' must be on stack */
|
|
|
|
exp1(ls);
|
|
|
|
check(ls, ']');
|
|
|
|
v->k = VINDEXED;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ':': /* var_or_func_tail -> ':' NAME funcparams */
|
|
|
|
next(ls);
|
|
|
|
lua_pushvar(ls, v); /* 'v' must be on stack */
|
1999-02-03 01:57:49 +08:00
|
|
|
code_oparg(ls, PUSHSELF, checkname(ls), 1);
|
1998-05-27 21:08:34 +08:00
|
|
|
v->k = VEXP;
|
|
|
|
v->info = funcparams(ls, 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '(': case STRING: case '{': /* var_or_func_tail -> funcparams */
|
|
|
|
lua_pushvar(ls, v); /* 'v' must be on stack */
|
|
|
|
v->k = VEXP;
|
|
|
|
v->info = funcparams(ls, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: return; /* should be follow... */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int funcparams (LexState *ls, int slf) {
|
|
|
|
FuncState *fs = ls->fs;
|
1999-02-05 00:36:16 +08:00
|
|
|
int nparams = 1; /* in cases STRING and constructor */
|
1998-05-27 21:08:34 +08:00
|
|
|
switch (ls->token) {
|
|
|
|
case '(': { /* funcparams -> '(' explist ')' */
|
1999-02-05 00:36:16 +08:00
|
|
|
int line = ls->linenumber;
|
1998-05-27 21:08:34 +08:00
|
|
|
listdesc e;
|
|
|
|
next(ls);
|
|
|
|
explist(ls, &e);
|
1999-02-05 00:36:16 +08:00
|
|
|
check_match(ls, ')', '(', line);
|
1998-05-27 21:08:34 +08:00
|
|
|
close_exp(ls, e.pc, 1);
|
|
|
|
nparams = e.n;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case '{': /* funcparams -> constructor */
|
|
|
|
constructor(ls);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STRING: /* funcparams -> STRING */
|
1999-02-05 00:36:16 +08:00
|
|
|
code_string(ls, ls->seminfo.ts); /* must use 'seminfo' before "next" */
|
1998-05-27 21:08:34 +08:00
|
|
|
next(ls);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
luaX_error(ls, "function arguments expected");
|
|
|
|
break;
|
|
|
|
}
|
1999-02-05 00:36:16 +08:00
|
|
|
code_byte(fs, CALLFUNC);
|
|
|
|
code_byte(fs, 0); /* save space for nresult */
|
1998-12-28 21:44:54 +08:00
|
|
|
code_byte(fs, (Byte)(nparams+slf));
|
1998-05-27 21:08:34 +08:00
|
|
|
return fs->pc-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void explist (LexState *ls, listdesc *d) {
|
|
|
|
switch (ls->token) {
|
|
|
|
case ELSE: case ELSEIF: case END: case UNTIL:
|
|
|
|
case EOS: case ';': case ')':
|
|
|
|
d->pc = 0;
|
|
|
|
d->n = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
explist1(ls, d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void explist1 (LexState *ls, listdesc *d) {
|
|
|
|
vardesc v;
|
1998-07-07 06:07:51 +08:00
|
|
|
exp0(ls, &v);
|
1998-05-27 21:08:34 +08:00
|
|
|
d->n = 1;
|
|
|
|
while (ls->token == ',') {
|
|
|
|
d->n++;
|
|
|
|
lua_pushvar(ls, &v);
|
|
|
|
next(ls);
|
1998-07-07 06:07:51 +08:00
|
|
|
exp0(ls, &v);
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
|
|
|
if (v.k == VEXP)
|
|
|
|
d->pc = v.info;
|
|
|
|
else {
|
|
|
|
lua_pushvar(ls, &v);
|
|
|
|
d->pc = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parlist (LexState *ls) {
|
|
|
|
int nparams = 0;
|
|
|
|
int dots = 0;
|
|
|
|
switch (ls->token) {
|
|
|
|
case DOTS: /* parlist -> DOTS */
|
|
|
|
next(ls);
|
|
|
|
dots = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NAME: /* parlist, tailparlist -> NAME [',' tailparlist] */
|
|
|
|
init:
|
1999-01-15 19:38:33 +08:00
|
|
|
store_localvar(ls, str_checkname(ls), nparams++);
|
1998-05-27 21:08:34 +08:00
|
|
|
if (ls->token == ',') {
|
|
|
|
next(ls);
|
|
|
|
switch (ls->token) {
|
|
|
|
case DOTS: /* tailparlist -> DOTS */
|
|
|
|
next(ls);
|
|
|
|
dots = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NAME: /* tailparlist -> NAME [',' tailparlist] */
|
|
|
|
goto init;
|
|
|
|
|
|
|
|
default: luaX_error(ls, "NAME or `...' expected");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ')': break; /* parlist -> empty */
|
|
|
|
|
|
|
|
default: luaX_error(ls, "NAME or `...' expected");
|
|
|
|
}
|
|
|
|
code_args(ls, nparams, dots);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int localnamelist (LexState *ls) {
|
|
|
|
/* localnamelist -> NAME {',' NAME} */
|
|
|
|
int i = 1;
|
1999-01-15 19:38:33 +08:00
|
|
|
store_localvar(ls, str_checkname(ls), 0);
|
1998-05-27 21:08:34 +08:00
|
|
|
while (ls->token == ',') {
|
|
|
|
next(ls);
|
1999-01-15 19:38:33 +08:00
|
|
|
store_localvar(ls, str_checkname(ls), i++);
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void decinit (LexState *ls, listdesc *d) {
|
|
|
|
/* decinit -> ['=' explist1] */
|
|
|
|
if (ls->token == '=') {
|
|
|
|
next(ls);
|
|
|
|
explist1(ls, d);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
d->n = 0;
|
|
|
|
d->pc = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-02-03 03:41:17 +08:00
|
|
|
|
1999-02-09 02:54:19 +08:00
|
|
|
static int assignment (LexState *ls, vardesc *v, int nvars, OpCode *codes) {
|
1998-05-27 21:08:34 +08:00
|
|
|
int left = 0;
|
1999-02-03 03:41:17 +08:00
|
|
|
unloaddot(ls, v);
|
1998-05-27 21:08:34 +08:00
|
|
|
if (ls->token == ',') { /* assignment -> ',' NAME assignment */
|
|
|
|
vardesc nv;
|
|
|
|
next(ls);
|
|
|
|
var_or_func(ls, &nv);
|
|
|
|
if (nv.k == VEXP)
|
|
|
|
luaX_error(ls, "syntax error");
|
1999-02-09 02:54:19 +08:00
|
|
|
left = assignment(ls, &nv, nvars+1, set_pop);
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
|
|
|
else { /* assignment -> '=' explist1 */
|
|
|
|
listdesc d;
|
|
|
|
check(ls, '=');
|
|
|
|
explist1(ls, &d);
|
|
|
|
adjust_mult_assign(ls, nvars, &d);
|
|
|
|
}
|
|
|
|
if (v->k != VINDEXED || left+(nvars-1) == 0) {
|
|
|
|
/* global/local var or indexed var without values in between */
|
1999-02-09 02:54:19 +08:00
|
|
|
storevar(ls, v, codes);
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
|
|
|
else { /* indexed var with values in between*/
|
1999-02-09 02:54:19 +08:00
|
|
|
code_oparg(ls, codes[3], left+(nvars-1), -1);
|
1998-05-27 21:08:34 +08:00
|
|
|
left += 2; /* table/index are not popped, because they aren't on top */
|
|
|
|
}
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void constructor (LexState *ls) {
|
|
|
|
/* constructor -> '{' part [';' part] '}' */
|
|
|
|
int line = ls->linenumber;
|
|
|
|
int pc = SaveWord(ls);
|
|
|
|
int nelems;
|
|
|
|
constdesc cd;
|
|
|
|
deltastack(ls, 1);
|
|
|
|
check(ls, '{');
|
|
|
|
part(ls, &cd);
|
|
|
|
nelems = cd.n;
|
|
|
|
if (ls->token == ';') {
|
|
|
|
constdesc other_cd;
|
|
|
|
next(ls);
|
|
|
|
part(ls, &other_cd);
|
|
|
|
if (cd.k == other_cd.k) /* repeated parts? */
|
|
|
|
luaX_error(ls, "invalid constructor syntax");
|
|
|
|
nelems += other_cd.n;
|
|
|
|
}
|
|
|
|
check_match(ls, '}', '{', line);
|
1999-02-03 01:57:49 +08:00
|
|
|
fix_opcode(ls, pc, CREATEARRAY, nelems);
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void part (LexState *ls, constdesc *cd) {
|
|
|
|
switch (ls->token) {
|
|
|
|
case ';': case '}': /* part -> empty */
|
|
|
|
cd->n = 0;
|
|
|
|
cd->k = ls->token;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case NAME: {
|
|
|
|
vardesc v;
|
1998-07-07 06:07:51 +08:00
|
|
|
exp0(ls, &v);
|
1998-05-27 21:08:34 +08:00
|
|
|
if (ls->token == '=') {
|
|
|
|
switch (v.k) {
|
|
|
|
case VGLOBAL:
|
|
|
|
code_constant(ls, v.info);
|
|
|
|
break;
|
|
|
|
case VLOCAL:
|
|
|
|
code_string(ls, ls->fs->localvar[v.info]);
|
|
|
|
break;
|
|
|
|
default:
|
1998-12-23 22:06:57 +08:00
|
|
|
error_unexpected(ls);
|
1998-05-27 21:08:34 +08:00
|
|
|
}
|
|
|
|
next(ls);
|
|
|
|
exp1(ls);
|
|
|
|
cd->n = recfields(ls);
|
|
|
|
cd->k = 1; /* record */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lua_pushvar(ls, &v);
|
|
|
|
cd->n = listfields(ls);
|
|
|
|
cd->k = 0; /* list */
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case '[': /* part -> recfield recfields */
|
|
|
|
recfield(ls);
|
|
|
|
cd->n = recfields(ls);
|
|
|
|
cd->k = 1; /* record */
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* part -> exp1 listfields */
|
|
|
|
exp1(ls);
|
|
|
|
cd->n = listfields(ls);
|
|
|
|
cd->k = 0; /* list */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int recfields (LexState *ls) {
|
|
|
|
/* recfields -> { ',' recfield } [','] */
|
|
|
|
int n = 1; /* one has been read before */
|
|
|
|
while (ls->token == ',') {
|
|
|
|
next(ls);
|
|
|
|
if (ls->token == ';' || ls->token == '}')
|
|
|
|
break;
|
|
|
|
recfield(ls);
|
|
|
|
n++;
|
|
|
|
if (n%RFIELDS_PER_FLUSH == 0)
|
|
|
|
flush_record(ls, RFIELDS_PER_FLUSH);
|
|
|
|
}
|
|
|
|
flush_record(ls, n%RFIELDS_PER_FLUSH);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int listfields (LexState *ls) {
|
|
|
|
/* listfields -> { ',' exp1 } [','] */
|
|
|
|
int n = 1; /* one has been read before */
|
|
|
|
while (ls->token == ',') {
|
|
|
|
next(ls);
|
|
|
|
if (ls->token == ';' || ls->token == '}')
|
|
|
|
break;
|
|
|
|
exp1(ls);
|
|
|
|
n++;
|
|
|
|
if (n%LFIELDS_PER_FLUSH == 0)
|
|
|
|
flush_list(ls, n/LFIELDS_PER_FLUSH - 1, LFIELDS_PER_FLUSH);
|
|
|
|
}
|
|
|
|
flush_list(ls, n/LFIELDS_PER_FLUSH, n%LFIELDS_PER_FLUSH);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void recfield (LexState *ls) {
|
|
|
|
/* recfield -> (NAME | '['exp1']') = exp1 */
|
|
|
|
switch (ls->token) {
|
|
|
|
case NAME:
|
1999-01-15 19:38:33 +08:00
|
|
|
code_constant(ls, checkname(ls));
|
1998-05-27 21:08:34 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '[':
|
|
|
|
next(ls);
|
|
|
|
exp1(ls);
|
|
|
|
check(ls, ']');
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: luaX_error(ls, "NAME or `[' expected");
|
|
|
|
}
|
|
|
|
check(ls, '=');
|
|
|
|
exp1(ls);
|
|
|
|
}
|
|
|
|
|