reorganization of `stat' function.
This commit is contained in:
114
lparser.c
114
lparser.c
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lparser.c,v 1.39 1999/08/16 20:52:00 roberto Exp roberto $
|
** $Id: lparser.c,v 1.40 1999/09/02 13:13:22 roberto Exp roberto $
|
||||||
** LL(1) Parser and code generator for Lua
|
** LL(1) Parser and code generator for Lua
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -8,18 +8,15 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "lauxlib.h"
|
|
||||||
#include "ldo.h"
|
#include "ldo.h"
|
||||||
#include "lfunc.h"
|
#include "lfunc.h"
|
||||||
#include "llex.h"
|
#include "llex.h"
|
||||||
#include "lmem.h"
|
#include "lmem.h"
|
||||||
|
#include "lobject.h"
|
||||||
#include "lopcodes.h"
|
#include "lopcodes.h"
|
||||||
#include "lparser.h"
|
#include "lparser.h"
|
||||||
#include "lstate.h"
|
#include "lstate.h"
|
||||||
#include "lstring.h"
|
#include "lstring.h"
|
||||||
#include "lua.h"
|
|
||||||
#include "luadebug.h"
|
|
||||||
#include "lzio.h"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -136,7 +133,6 @@ static void parlist (LexState *ls);
|
|||||||
static void part (LexState *ls, constdesc *cd);
|
static void part (LexState *ls, constdesc *cd);
|
||||||
static void recfield (LexState *ls);
|
static void recfield (LexState *ls);
|
||||||
static void ret (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 (LexState *ls, vardesc *v);
|
||||||
static void var_or_func_tail (LexState *ls, vardesc *v);
|
static void var_or_func_tail (LexState *ls, vardesc *v);
|
||||||
|
|
||||||
@@ -699,30 +695,21 @@ TProtoFunc *luaY_parser (ZIO *z) {
|
|||||||
/* GRAMAR RULES */
|
/* GRAMAR RULES */
|
||||||
/*============================================================*/
|
/*============================================================*/
|
||||||
|
|
||||||
static void chunk (LexState *ls) {
|
|
||||||
/* chunk -> statlist ret */
|
|
||||||
statlist(ls);
|
|
||||||
ret(ls);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void statlist (LexState *ls) {
|
static void chunk (LexState *ls) {
|
||||||
/* statlist -> { stat [;] } */
|
/* chunk -> { stat [;] } ret */
|
||||||
while (stat(ls)) {
|
while (stat(ls)) {
|
||||||
LUA_ASSERT(ls->fs->stacksize == ls->fs->nlocalvar,
|
LUA_ASSERT(ls->fs->stacksize == ls->fs->nlocalvar,
|
||||||
"stack size != # local vars");
|
"stack size != # local vars");
|
||||||
optional(ls, ';');
|
optional(ls, ';');
|
||||||
}
|
}
|
||||||
|
ret(ls); /* optional return */
|
||||||
}
|
}
|
||||||
|
|
||||||
static int stat (LexState *ls) {
|
|
||||||
int line = ls->linenumber; /* may be needed for error messages */
|
|
||||||
FuncState *fs = ls->fs;
|
|
||||||
switch (ls->token) {
|
|
||||||
case IF: /* stat -> IF ifpart END */
|
|
||||||
ifpart(ls, line);
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
case WHILE: { /* stat -> WHILE cond DO block END */
|
static void whilestat (LexState *ls, int line) {
|
||||||
|
/* whilestat -> WHILE cond DO block END */
|
||||||
|
FuncState *fs = ls->fs;
|
||||||
TProtoFunc *f = fs->f;
|
TProtoFunc *f = fs->f;
|
||||||
int while_init = fs->pc;
|
int while_init = fs->pc;
|
||||||
int cond_end, cond_size;
|
int cond_end, cond_size;
|
||||||
@@ -737,17 +724,12 @@ static int stat (LexState *ls) {
|
|||||||
luaO_memdown(f->code+while_init, f->code+cond_end, fs->pc-while_init);
|
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);
|
while_init += JMPSIZE + fix_jump(ls, while_init, JMP, fs->pc-cond_size);
|
||||||
fix_upjmp(ls, IFTUPJMP, while_init);
|
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 */
|
static void repeatstat (LexState *ls, int line) {
|
||||||
|
/* repeatstat -> REPEAT block UNTIL exp1 */
|
||||||
|
FuncState *fs = ls->fs;
|
||||||
int repeat_init = fs->pc;
|
int repeat_init = fs->pc;
|
||||||
next(ls);
|
next(ls);
|
||||||
block(ls);
|
block(ls);
|
||||||
@@ -755,10 +737,26 @@ static int stat (LexState *ls) {
|
|||||||
exp1(ls);
|
exp1(ls);
|
||||||
fix_upjmp(ls, IFFUPJMP, repeat_init);
|
fix_upjmp(ls, IFFUPJMP, repeat_init);
|
||||||
deltastack(ls, -1); /* pops condition */
|
deltastack(ls, -1); /* pops condition */
|
||||||
return 1;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
case FUNCTION: { /* stat -> FUNCTION funcname body */
|
|
||||||
|
static void localstat (LexState *ls) {
|
||||||
|
/* stat -> LOCAL localnamelist decinit */
|
||||||
|
FuncState *fs = ls->fs;
|
||||||
|
listdesc d;
|
||||||
|
int nvars;
|
||||||
|
check_debugline(ls);
|
||||||
|
next(ls);
|
||||||
|
nvars = localnamelist(ls);
|
||||||
|
decinit(ls, &d);
|
||||||
|
fs->nlocalvar += nvars;
|
||||||
|
correctvarlines(ls, nvars); /* vars will be alive only after decinit */
|
||||||
|
adjust_mult_assign(ls, nvars, &d);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int funcstat (LexState *ls, int line) {
|
||||||
|
/* funcstat -> FUNCTION funcname body */
|
||||||
int needself;
|
int needself;
|
||||||
vardesc v;
|
vardesc v;
|
||||||
if (ls->fs->prev) /* inside other function? */
|
if (ls->fs->prev) /* inside other function? */
|
||||||
@@ -769,22 +767,11 @@ static int stat (LexState *ls) {
|
|||||||
body(ls, needself, line);
|
body(ls, needself, line);
|
||||||
storevar(ls, &v);
|
storevar(ls, &v);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
case LOCAL: { /* stat -> LOCAL localnamelist decinit */
|
|
||||||
listdesc d;
|
|
||||||
int nvars;
|
|
||||||
check_debugline(ls);
|
|
||||||
next(ls);
|
|
||||||
nvars = localnamelist(ls);
|
|
||||||
decinit(ls, &d);
|
|
||||||
fs->nlocalvar += nvars;
|
|
||||||
correctvarlines(ls, nvars); /* vars will be alive only after decinit */
|
|
||||||
adjust_mult_assign(ls, nvars, &d);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
case NAME: case '%': { /* stat -> func | ['%'] NAME assignment */
|
static void namestat (LexState *ls) {
|
||||||
|
/* stat -> func | ['%'] NAME assignment */
|
||||||
vardesc v;
|
vardesc v;
|
||||||
check_debugline(ls);
|
check_debugline(ls);
|
||||||
var_or_func(ls, &v);
|
var_or_func(ls, &v);
|
||||||
@@ -797,9 +784,42 @@ static int stat (LexState *ls) {
|
|||||||
int left = assignment(ls, &v, 1);
|
int left = assignment(ls, &v, 1);
|
||||||
adjuststack(ls, left); /* remove eventual garbage left on stack */
|
adjuststack(ls, left); /* remove eventual garbage left on stack */
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int stat (LexState *ls) {
|
||||||
|
int line = ls->linenumber; /* may be needed for error messages */
|
||||||
|
switch (ls->token) {
|
||||||
|
case IF: /* stat -> IF ifpart END */
|
||||||
|
ifpart(ls, line);
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
case WHILE: /* stat -> whilestat */
|
||||||
|
whilestat(ls, line);
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
case DO: { /* stat -> DO block END */
|
||||||
|
next(ls);
|
||||||
|
block(ls);
|
||||||
|
check_match(ls, END, DO, line);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case REPEAT: /* stat -> repeatstat */
|
||||||
|
repeatstat(ls, line);
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
case FUNCTION: /* stat -> funcstat */
|
||||||
|
return funcstat(ls, line);
|
||||||
|
|
||||||
|
case LOCAL: /* stat -> localstat */
|
||||||
|
localstat(ls);
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
case NAME: case '%': /* stat -> namestat */
|
||||||
|
namestat(ls);
|
||||||
|
return 1;
|
||||||
|
|
||||||
case RETURN: case ';': case ELSE: case ELSEIF:
|
case RETURN: case ';': case ELSE: case ELSEIF:
|
||||||
case END: case UNTIL: case EOS: /* 'stat' follow */
|
case END: case UNTIL: case EOS: /* 'stat' follow */
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user