aboutsummaryrefslogtreecommitdiff
path: root/lparser.c
blob: c185b50602b7dfe89f10777b8768ba50507b864d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
/*
** $Id: lparser.c,v 1.58 2000/02/11 16:52:54 roberto Exp roberto $
** LL(1) Parser and code generator for Lua
** See Copyright Notice in lua.h
*/


#include <stdio.h>
#include <string.h>

#include "ldo.h"
#include "lfunc.h"
#include "llex.h"
#include "lmem.h"
#include "lobject.h"
#include "lopcodes.h"
#include "lparser.h"
#include "lstate.h"
#include "lstring.h"



/* maximum number of local variables */
#ifndef MAXLOCALS
#define MAXLOCALS 200  /* arbitrary limit (<MAXARG_B) */
#endif


/* maximum number of upvalues */
#ifndef MAXUPVALUES
#define MAXUPVALUES 32  /* arbitrary limit (<MAXARG_B) */
#endif


/* maximum number of variables in the left side of an assignment */
#ifndef MAXVARSLH
#define MAXVARSLH	100  /* arbitrary limit (<MAXARG_B) */
#endif


/* maximum number of parameters in a function */
#ifndef MAXPARAMS
#define MAXPARAMS	100  /* arbitrary limit (<MAXLOCALS) */
#endif


/*
** Variable descriptor:
** must include an `exp' option because LL(1) cannot distinguish
** between variables, upvalues and function calls on first sight.
*/
typedef enum {
  VGLOBAL,  /* info is constant index of global name */
  VLOCAL,   /* info is stack index */
  VDOT,     /* info is constant index of index name */
  VINDEXED, /* no info (table and index are on the stack) */
  VEXP      /* info is pc index of a call (or 0 if exp is closed) */
} varkind;

typedef struct vardesc {
  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 the call pc index.
*/
typedef struct listdesc {
  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 constdesc {
  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;  /* enclosing function */
  int pc;  /* next position to code */
  int stacksize;  /* 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 (-1 if no debug information) */
  int lastsetline;  /* line where last SETLINE was issued */
  vardesc upvalues[MAXUPVALUES];  /* upvalues */
  TaggedString *localvar[MAXLOCALS];  /* store local variable names */
} FuncState;


/*
** prototypes for recursive non-terminal functions
*/
static void body (LexState *ls, int needself, int line);
static void chunk (LexState *ls);
static void constructor (LexState *ls);
static void expr (LexState *ls, vardesc *v);
static void exp1 (LexState *ls);



static void luaY_error (LexState *ls, const char *msg) {
  luaX_error(ls, msg, ls->token);
}


static void checklimit (LexState *ls, int val, int limit, const char *msg) {
  if (val > limit) {
    char buff[100];
    sprintf(buff, "too many %.50s (limit=%d)", msg, limit);
    luaY_error(ls, buff);
  }
}


static int code_instruction (LexState *ls, Instruction i) {
  FuncState *fs = ls->fs;
  luaM_growvector(ls->L, fs->f->code, fs->pc, 1, Instruction, codeEM, MAXARG_S);
  fs->f->code[fs->pc] = i;
  return fs->pc++;
}


static void fix_jump (LexState *ls, int pc, int dest) {
  Instruction *jmp = &ls->fs->f->code[pc];
  /* jump is relative to position following jump instruction */
  *jmp = SETARG_S(*jmp, dest-(pc+1));
}


static void deltastack (LexState *ls, int delta) {
  FuncState *fs = ls->fs;
  fs->stacksize += delta;
  if (delta > 0 && fs->stacksize > fs->f->maxstacksize) {
    fs->f->maxstacksize = fs->stacksize;
  }
}


static int aux_code (LexState *ls, OpCode op, Instruction i, int delta) {
  deltastack(ls, delta);
  return code_instruction(ls, SET_OPCODE(i, op));
}


static int code_0 (LexState *ls, OpCode op, int delta) {
  return aux_code(ls, op, 0, delta);
}



static int code_U (LexState *ls, OpCode op, int u, int delta) {
  Instruction i = SETARG_U(0, u);
  return aux_code(ls, op, i, delta);
}


static int code_S (LexState *ls, OpCode op, int s, int delta) {
  Instruction i = SETARG_S(0, s);
  return aux_code(ls, op, i, delta);
}


static int code_AB (LexState *ls, OpCode op, int a, int b, int delta) {
  Instruction i = SETARG_A(0, a);
  i = SETARG_B(i, b);
  return aux_code(ls, op, i, delta);
}


static void code_kstr (LexState *ls, int c) {
  code_U(ls, PUSHSTRING, c, 1);
}


static void assertglobal (LexState *ls, int index) {
  luaS_assertglobal(ls->L, ls->fs->f->kstr[index]);
}


static int string_constant (LexState *ls, FuncState *fs, TaggedString *s) {
  TProtoFunc *f = fs->f;
  int c = s->constindex;
  if (c >= f->nkstr || f->kstr[c] != s) {
    luaM_growvector(ls->L, f->kstr, f->nkstr, 1, TaggedString *,
                    constantEM, MAXARG_U);
    c = f->nkstr++;
    f->kstr[c] = s;
    s->constindex = c;  /* hint for next time */
  }
  return c;
}


static void code_string (LexState *ls, TaggedString *s) {
  code_kstr(ls, string_constant(ls, ls->fs, s));
}


#define LIM 20
static int real_constant (LexState *ls, real r) {
  /* check whether `r' has appeared within the last LIM entries */
  TProtoFunc *f = ls->fs->f;
  int c = f->nknum;
  int lim = c < LIM ? 0 : c-LIM;
  while (--c >= lim)
    if (f->knum[c] == r) return c;
  /* not found; create a new entry */
  luaM_growvector(ls->L, f->knum, f->nknum, 1, real, constantEM, MAXARG_U);
  c = f->nknum++;
  f->knum[c] = r;
  return c;
}


static void code_number (LexState *ls, real f) {
  if ((real)(-MAXARG_S) <= f && f <= (real)MAXARG_S && (int)f == f)
    code_S(ls, PUSHINT, (int)f, 1);  /* f has a short integer value */
  else
    code_U(ls, PUSHNUMBER, real_constant(ls, f), 1);
}


static void luaI_registerlocalvar (LexState *ls, TaggedString *varname,
                                   int line) {
  FuncState *fs = ls->fs;
  if (fs->nvars != -1) {  /* debug information? */
    TProtoFunc *f = fs->f;
    luaM_growvector(ls->L, f->locvars, fs->nvars, 1, LocVar, "", MAX_INT);
    f->locvars[fs->nvars].varname = varname;
    f->locvars[fs->nvars].line = line;
    fs->nvars++;
  }
}


static void luaI_unregisterlocalvar (LexState *ls, int line) {
  luaI_registerlocalvar(ls, NULL, line);
}


static void store_localvar (LexState *ls, TaggedString *name, int n) {
  FuncState *fs = ls->fs;
  checklimit(ls, fs->nlocalvar+n+1, MAXLOCALS, "local variables");
  fs->localvar[fs->nlocalvar+n] = name;
}


static void adjustlocalvars (LexState *ls, int nvars, int line) {
  FuncState *fs = ls->fs;
  int i;
  fs->nlocalvar += nvars;
  for (i=fs->nlocalvar-nvars; i<fs->nlocalvar; i++)
    luaI_registerlocalvar(ls, fs->localvar[i], line);
}


static void add_localvar (LexState *ls, TaggedString *name) {
  store_localvar(ls, name, 0);
  adjustlocalvars(ls, 1, 0);
}


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);
  if (i >= 0) {  /* local value? */
    var->k = VLOCAL;
    var->info = i;
  }
  else {
    FuncState *level = fs;
    while ((level = level->prev) != NULL)  /* check shadowing */
      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(ls, 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 */
  ++(fs->nupvalues);
  checklimit(ls, fs->nupvalues, MAXUPVALUES, "upvalues");
  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);
  code_U(ls, PUSHUPVALUE, indexupvalue(ls, n), 1);
}



static void check_debugline (LexState *ls) {
  if (ls->L->debug && ls->linenumber != ls->fs->lastsetline) {
    code_U(ls, SETLINE, ls->linenumber, 0);
    ls->fs->lastsetline = ls->linenumber;
  }
}


static void adjuststack (LexState *ls, int n) {
  if (n > 0)
    code_U(ls, POP, n, -n);
  else if (n < 0)
    code_U(ls, PUSHNIL, (-n)-1, -n);
}


static void close_exp (LexState *ls, int pc, int nresults) {
  if (pc > 0) {  /* expression is an open function call? */
    Instruction *i = &ls->fs->f->code[pc];
    *i = SETARG_B(*i, nresults);  /* set nresults */
    if (nresults != MULT_RET)
      deltastack(ls, nresults);  /* push results */
  }
}


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 */
    if (diff <= 0) {  /* more variables than values? */
      /* 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;
  adjustlocalvars(ls, nparams, 0);
  checklimit(ls, fs->nlocalvar, MAXPARAMS, "parameters");
  nparams = fs->nlocalvar;  /* `self' could be there already */
  fs->f->numparams = nparams;
  fs->f->is_vararg = dots;
  if (!dots)
    deltastack(ls, nparams);
  else {
    deltastack(ls, nparams+1);
    add_localvar(ls, luaS_newfixed(ls->L, "arg"));
  }
}


static void unloaddot (LexState *ls, vardesc *v) {
  /* dotted variables <a.x> must be stored as regular indexed vars <a["x"]> */
  if (v->k == VDOT) {
    code_kstr(ls, v->info);
    v->k = VINDEXED;
  }
}


static void lua_pushvar (LexState *ls, vardesc *var) {
  switch (var->k) {
    case VLOCAL:
      code_U(ls, PUSHLOCAL, var->info, 1);
      break;
    case VGLOBAL:
      code_U(ls, GETGLOBAL, var->info, 1);
      assertglobal(ls, var->info);  /* make sure that there is a global */
      break;
    case VDOT:
      code_U(ls, GETDOTTED, var->info, 0);
      break;
    case VINDEXED:
      code_0(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 */
}


static void storevar (LexState *ls, const vardesc *var) {
  switch (var->k) {
    case VLOCAL:
      code_U(ls, SETLOCAL, var->info, -1);
      break;
    case VGLOBAL:
      code_U(ls, SETGLOBAL, var->info, -1);
      assertglobal(ls, var->info);  /* make sure that there is a global */
      break;
    case VINDEXED:
      code_0(ls, SETTABLEPOP, -3);
      break;
    default:
      LUA_INTERNALERROR(ls->L, "invalid var kind to store");
  }
}


static void func_onstack (LexState *ls, FuncState *func) {
  TProtoFunc *f = ls->fs->f;
  int i;
  for (i=0; i<func->nupvalues; i++)
    lua_pushvar(ls, &func->upvalues[i]);
  luaM_growvector(ls->L, f->kproto, f->nkproto, 1, TProtoFunc *,
                  constantEM, MAXARG_A);
  f->kproto[f->nkproto++] = func->f;
  deltastack(ls, 1);  /* CLOSURE puts one extra element (before popping) */
  code_AB(ls, CLOSURE, f->nkproto-1, func->nupvalues, -func->nupvalues);
}


static void init_state (LexState *ls, FuncState *fs, TaggedString *source) {
  lua_State *L = ls->L;
  TProtoFunc *f = luaF_newproto(ls->L);
  fs->prev = ls->fs;  /* linked list of funcstates */
  ls->fs = fs;
  fs->stacksize = 0;
  fs->nlocalvar = 0;
  fs->nupvalues = 0;
  fs->lastsetline = 0;
  fs->f = f;
  f->source = source;
  fs->pc = 0;
  f->code = NULL;
  f->maxstacksize = 0;
  f->numparams = 0;  /* default for main chunk */
  f->is_vararg = 0;  /* default for main chunk */
  fs->nvars = (L->debug) ? 0 : -1;  /* flag no debug information? */
  /* push function (to avoid GC) */
  tfvalue(L->top) = f;
  ttype(L->top) = LUA_T_LPROTO;
  incr_top;
}


static void close_func (LexState *ls) {
  FuncState *fs = ls->fs;
  TProtoFunc *f = fs->f;
  code_0(ls, ENDCODE, 0);
  luaM_reallocvector(ls->L, f->code, fs->pc, Instruction);
  luaM_reallocvector(ls->L, f->kstr, f->nkstr, TaggedString *);
  luaM_reallocvector(ls->L, f->knum, f->nknum, real);
  luaM_reallocvector(ls->L, f->kproto, f->nkproto, TProtoFunc *);
  if (fs->nvars != -1) {  /* debug information? */
    luaI_registerlocalvar(ls, NULL, -1);  /* flag end of vector */
    luaM_reallocvector(ls->L, f->locvars, fs->nvars, LocVar);
  }
  ls->fs = fs->prev;
  ls->L->top--;  /* pop function */
}


static void next (LexState *ls) {
  ls->token = luaX_lex(ls);
}


static void error_expected (LexState *ls, int token) {
  char buff[100], t[TOKEN_LEN];
  luaX_token2str(token, t);
  sprintf(buff, "`%.20s' expected", t);
  luaY_error(ls, buff);
}


static void error_unexpected (LexState *ls) {
  luaY_error(ls, "unexpected token");
}


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];
    luaX_token2str(what, t_what);
    luaX_token2str(who, t_who);
    sprintf(buff, "`%.20s' expected (to close `%.20s' at line %d)",
            t_what, t_who, where);
    luaY_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);
}

static int checkname (LexState *ls) {
  int sc;
  if (ls->token != NAME)
    luaY_error(ls, "<name> expected");
  sc = string_constant(ls, ls->fs, ls->seminfo.ts);
  next(ls);
  return sc;
}


static TaggedString *str_checkname (LexState *ls) {
  int i = checkname(ls);  /* this call may realloc `f->consts' */
  return ls->fs->f->kstr[i];
}


static int optional (LexState *ls, int c) {
  if (ls->token == c) {
    next(ls);
    return 1;
  }
  else return 0;
}


TProtoFunc *luaY_parser (lua_State *L, ZIO *z) {
  struct LexState lexstate;
  struct FuncState funcstate;
  luaX_setinput(L, &lexstate, z);
  init_state(&lexstate, &funcstate, luaS_new(L, zname(z)));
  next(&lexstate);  /* read first token */
  chunk(&lexstate);
  if (lexstate.token != EOS)
    luaY_error(&lexstate, "<eof> expected");
  close_func(&lexstate);
  return funcstate.f;
}



/*============================================================*/
/* GRAMAR RULES */
/*============================================================*/


static void explist1 (LexState *ls, listdesc *d) {
  vardesc v;
  expr(ls, &v);
  d->n = 1;
  while (ls->token == ',') {
    d->n++;
    lua_pushvar(ls, &v);
    next(ls);
    expr(ls, &v);
  }
  if (v.k == VEXP)
    d->pc = v.info;
  else {
    lua_pushvar(ls, &v);
    d->pc = 0;
  }
}


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 int funcparams (LexState *ls, int slf) {
  FuncState *fs = ls->fs;
  int slevel = fs->stacksize - slf - 1;  /* where is func in the stack */
  switch (ls->token) {
    case '(': {  /* funcparams -> '(' explist ')' */
      int line = ls->linenumber;
      listdesc e;
      next(ls);
      explist(ls, &e);
      check_match(ls, ')', '(', line);
      close_exp(ls, e.pc, MULT_RET);  /* close 1 for old semantics */
      break;
    }

    case '{':  /* funcparams -> constructor */
      constructor(ls);
      break;

    case STRING:  /* funcparams -> STRING */
      code_string(ls, ls->seminfo.ts);  /* must use 'seminfo' before `next' */
      next(ls);
      break;

    default:
      luaY_error(ls, "function arguments expected");
      break;
  }
  fs->stacksize = slevel;  /* call will remove func and params */
  return code_AB(ls, CALL, slevel, 0, 0);
}


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;
        v->info = checkname(ls);
        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 */
        int name;
        next(ls);
        name = checkname(ls);
        lua_pushvar(ls, v);  /* `v' must be on stack */
        code_U(ls, PUSHSELF, name, 1);
        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 void var_or_func (LexState *ls, vardesc *v) {
  /* var_or_func -> ['%'] NAME var_or_func_tail */
  if (optional(ls, '%')) {  /* upvalue? */
    pushupvalue(ls, str_checkname(ls));
    v->k = VEXP;
    v->info = 0;  /* closed expression */
  }
  else  /* variable name */
    singlevar(ls, str_checkname(ls), v, 0);
  var_or_func_tail(ls, v);
}



/*
** {======================================================================
** Rules for Constructors
** =======================================================================
*/

static void recfield (LexState *ls) {
  /* recfield -> (NAME | '['exp1']') = exp1 */
  switch (ls->token) {
    case NAME:
      code_kstr(ls, checkname(ls));
      break;

    case '[':
      next(ls);
      exp1(ls);
      check(ls, ']');
      break;

    default: luaY_error(ls, "<name> or `[' expected");
  }
  check(ls, '=');
  exp1(ls);
}


static int recfields (LexState *ls) {
  /* recfields -> { ',' recfield } [','] */
  int n = 1;  /* one has been read before */
  int mod_n = 1;  /* mod_n == n%RFIELDS_PER_FLUSH */
  while (ls->token == ',') {
    next(ls);
    if (ls->token == ';' || ls->token == '}')
      break;
    recfield(ls);
    n++;
    if (++mod_n == RFIELDS_PER_FLUSH) {
      code_U(ls, SETMAP, RFIELDS_PER_FLUSH-1, -2*RFIELDS_PER_FLUSH);
      mod_n = 0;
    }
  }
  if (mod_n)
    code_U(ls, SETMAP, mod_n-1, -2*mod_n);
  return n;
}


static int listfields (LexState *ls) {
  /* listfields -> { ',' exp1 } [','] */
  int n = 1;  /* one has been read before */
  int mod_n = 1;  /* mod_n == n%LFIELDS_PER_FLUSH */
  while (ls->token == ',') {
    next(ls);
    if (ls->token == ';' || ls->token == '}')
      break;
    exp1(ls);
    n++;
    checklimit(ls, n, MAXARG_A*LFIELDS_PER_FLUSH,
               "items in a list initializer");
    if (++mod_n == LFIELDS_PER_FLUSH) {
      code_AB(ls, SETLIST, n/LFIELDS_PER_FLUSH - 1, LFIELDS_PER_FLUSH-1,
              -LFIELDS_PER_FLUSH);
      mod_n = 0;
    }
  }
  if (mod_n > 0)
    code_AB(ls, SETLIST, n/LFIELDS_PER_FLUSH, mod_n-1, -mod_n);
  return n;
}



static void constructor_part (LexState *ls, constdesc *cd) {
  switch (ls->token) {
    case ';': case '}':  /* constructor_part -> empty */
      cd->n = 0;
      cd->k = ls->token;
      return;

    case NAME: {
      vardesc v;
      expr(ls, &v);
      if (ls->token == '=') {
        switch (v.k) {
          case VGLOBAL:
            code_kstr(ls, v.info);
            break;
          case VLOCAL:
            code_string(ls, ls->fs->localvar[v.info]);
            break;
          default:
            error_unexpected(ls);
        }
        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 '[':  /* constructor_part -> recfield recfields */
      recfield(ls);
      cd->n = recfields(ls);
      cd->k = 1;  /* record */
      break;

    default:  /* constructor_part -> exp1 listfields */
      exp1(ls);
      cd->n = listfields(ls);
      cd->k = 0;  /* list */
      break;
  }
}


static void constructor (LexState *ls) {
  /* constructor -> '{' constructor_part [';' constructor_part] '}' */
  int line = ls->linenumber;
  int pc = code_U(ls, CREATETABLE, 0, 1);
  int nelems;
  constdesc cd;
  check(ls, '{');
  constructor_part(ls, &cd);
  nelems = cd.n;
  if (ls->token == ';') {
    constdesc other_cd;
    next(ls);
    constructor_part(ls, &other_cd);
    if (cd.k == other_cd.k)  /* repeated parts? */
      luaY_error(ls, "invalid constructor syntax");
    nelems += other_cd.n;
  }
  check_match(ls, '}', '{', line);
  /* set initial table size */
  ls->fs->f->code[pc] = SETARG_U(ls->fs->f->code[pc], nelems);
}

/* }====================================================================== */




/*
** {======================================================================
** For parsing expressions, we use a classic stack with priorities.
** Each binary operator is represented by an index: EQ=2, NE=3, ... '^'=13.
** The unary NOT is 0 and UNMINUS is 1.
** =======================================================================
*/

#define INDNOT		0
#define INDMINUS	1

/* 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 const int priority [POW+1] =  {5, 5, 1, 1, 1, 1, 1, 1, 2, 3, 3, 4, 4, 6};

static const OpCode opcodes [POW+1] = {NOTOP, MINUSOP, EQOP, NEQOP, GTOP,
  LTOP, LEOP, GEOP, CONCOP, ADDOP, SUBOP, MULTOP, DIVOP, POWOP};

#define MAXOPS	20  /* op's stack size (arbitrary limit) */

typedef struct stack_op {
  int ops[MAXOPS];
  int top;
} stack_op;


/*
** returns the index of a binary operator
*/
static int binop (int op) {
  switch (op) {
    case EQ: return FIRSTBIN;
    case  NE: return FIRSTBIN+1;
    case  '>': return FIRSTBIN+2;
    case  '<': return FIRSTBIN+3;
    case  LE: return FIRSTBIN+4;
    case  GE: return FIRSTBIN+5;
    case  CONC: return FIRSTBIN+6;
    case  '+': return FIRSTBIN+7;
    case  '-': return FIRSTBIN+8;
    case  '*': return FIRSTBIN+9;
    case  '/': return FIRSTBIN+10;
    case  '^': return FIRSTBIN+11;
    default: return -1;
  }
}


static void push (LexState *ls, stack_op *s, int op) {
  if (s->top >= MAXOPS)
    luaY_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_0(ls, opcodes[op], op<FIRSTBIN?0:-1);
    s->top--;
  }
}

static void simpleexp (LexState *ls, vardesc *v, stack_op *s) {
  check_debugline(ls);
  switch (ls->token) {
    case NUMBER: {  /* simpleexp -> NUMBER */
      real r = ls->seminfo.r;
      next(ls);
      /* dirty trick: check whether it is a -NUMBER not followed by '^'   */
      /* (because the priority of '^' is higher than the priority of '-') */
      if (s->top > 0 && s->ops[s->top-1] == INDMINUS && ls->token != '^') {
        s->top--;  /* remove '-' from stack */
        r = -r;
      }
      code_number(ls, r);
      break;
    }

    case STRING:  /* simpleexp -> STRING */
      code_string(ls, ls->seminfo.ts);  /* must use 'seminfo' before `next' */
      next(ls);
      break;

    case NIL:  /* simpleexp -> NIL */
      adjuststack(ls, -1);
      next(ls);
      break;

    case '{':  /* simpleexp -> constructor */
      constructor(ls);
      break;

    case FUNCTION:  /* simpleexp -> FUNCTION body */
      next(ls);
      body(ls, 0, ls->linenumber);
      break;

    case '(':  /* simpleexp -> '(' expr ')' */
      next(ls);
      expr(ls, v);
      check(ls, ')');
      return;

    case NAME: case '%':
      var_or_func(ls, v);
      return;

    default:
      luaY_error(ls, "<expression> expected");
      return;
  }
  v->k = VEXP; v->info = 0;
}


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);
}


static void arith_exp (LexState *ls, vardesc *v) {
  stack_op s;
  int op;
  s.top = 0;
  prefixexp(ls, v, &s);
  while ((op = binop(ls->token)) >= 0) {
    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);
    prefixexp(ls, v, &s);
    lua_pushvar(ls, v);
  }
  if (s.top > 0) {
    lua_pushvar(ls, v);
    pop_to(ls, &s, 0);
  }
}


static void exp1 (LexState *ls) {
  vardesc v;
  expr(ls, &v);
  lua_pushvar(ls, &v);
}


static void expr (LexState *ls, vardesc *v) {
  /* expr -> arith_exp {(AND | OR) arith_exp} */
  arith_exp(ls, v);
  while (ls->token == AND || ls->token == OR) {
    OpCode op = (ls->token == AND) ? ONFJMP : ONTJMP;
    int pc;
    lua_pushvar(ls, v);
    next(ls);
    pc = code_S(ls, op, 0, -1);
    arith_exp(ls, v);
    lua_pushvar(ls, v);
    fix_jump(ls, pc, ls->fs->pc);
  }
}


/* }==================================================================== */


/*
** {======================================================================
** Rules for Statements
** =======================================================================
*/


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(ls, fs->lastsetline);
}


static int assignment (LexState *ls, vardesc *v, int nvars) {
  int left = 0;
  checklimit(ls, nvars, MAXVARSLH, "variables in a multiple assignment");
  unloaddot(ls, v);
  if (ls->token == ',') {  /* assignment -> ',' NAME assignment */
    vardesc nv;
    next(ls);
    var_or_func(ls, &nv);
    if (nv.k == VEXP)
      luaY_error(ls, "syntax error");
    left = assignment(ls, &nv, nvars+1);
  }
  else {  /* assignment -> '=' explist1 */
    listdesc d;
    if (ls->token != '=')
      error_unexpected(ls);
    next(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 */
    storevar(ls, v);
  }
  else {  /* indexed var with values in between*/
    code_U(ls, SETTABLE, left+(nvars-1), -1);
    left += 2;  /* table&index are not popped, because they aren't on top */
  }
  return left;
}


static void whilestat (LexState *ls, int line) {
  /* whilestat -> WHILE exp1 DO block END */
  FuncState *fs = ls->fs;
  int while_init = fs->pc;
  int j1;
  next(ls);
  exp1(ls);
  j1 = code_U(ls, IFFJMP, 0, -1);  /* jump to exit loop */
  check(ls, DO);
  block(ls);
  check_match(ls, END, WHILE, line);
  fix_jump(ls, code_U(ls, JMP, 0, 0), while_init);  /* jump to keep loop */
  fix_jump(ls, j1, fs->pc);
}


static void repeatstat (LexState *ls, int line) {
  /* repeatstat -> REPEAT block UNTIL exp1 */
  FuncState *fs = ls->fs;
  int repeat_init = fs->pc;
  next(ls);
  block(ls);
  check_match(ls, UNTIL, REPEAT, line);
  exp1(ls);
  fix_jump(ls, code_U(ls, IFFJMP, 0, -1), repeat_init);
}


static int localnamelist (LexState *ls) {
  /* localnamelist -> NAME {',' NAME} */
  int i = 1;
  store_localvar(ls, str_checkname(ls), 0);
  while (ls->token == ',') {
    next(ls);
    store_localvar(ls, str_checkname(ls), i++);
  }
  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;
  }
}


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);
  adjustlocalvars(ls, nvars, fs->lastsetline);
  adjust_mult_assign(ls, nvars, &d);
}


static int funcname (LexState *ls, vardesc *v) {
  /* funcname -> NAME [':' NAME | '.' NAME] */
  int needself = 0;
  singlevar(ls, str_checkname(ls), v, 0);
  if (ls->token == ':' || ls->token == '.') {
    needself = (ls->token == ':');
    next(ls);
    lua_pushvar(ls, v);
    code_kstr(ls, checkname(ls));
    v->k = VINDEXED;
  }
  return needself;
}


static int funcstat (LexState *ls, int line) {
  /* funcstat -> 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);
  storevar(ls, &v);
  return 1;
}


static void namestat (LexState *ls) {
  /* 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? */
      luaY_error(ls, "syntax error");
    close_exp(ls, v.info, 0);
  }
  else {  /* stat -> ['%'] NAME assignment */
    int left = assignment(ls, &v, 1);
    adjuststack(ls, left);  /* remove eventual garbage left on stack */
  }
}


static void ifpart (LexState *ls, int line) {
  /* ifpart -> cond THEN block [ELSE block | ELSEIF ifpart] */
  FuncState *fs = ls->fs;
  int c;
  int je;
  next(ls);  /* skip IF or ELSEIF */
  exp1(ls);  /* cond */
  c = code_U(ls, IFFJMP, 0, -1);  /* jump `then' if `cond' is false */
  check(ls, THEN);
  block(ls);  /* `then' part */
  je = code_U(ls, JMP, 0, 0);  /* jump `else' part after `then' */
  if (ls->token == ELSEIF)
    ifpart(ls, line);
  else {
    if (optional(ls, ELSE))
      block(ls);  /* `else' part */
    check_match(ls, END, IF, line);
  }
  if (fs->pc == je+1) {  /* `else' part empty? */
    fs->pc--;  /* remove last jump */
    je--;  /* first jump will be smaller */
  }
  else
    fix_jump(ls, je, fs->pc);  /* fix last jump */
  fix_jump(ls, c, je+1);  /* fix first jump to beginning of `else' part */
}


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;
    }

    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 END: case UNTIL: case EOS:  /* 'stat' follow */
      return 0;

    default:
      error_unexpected(ls);
      return 0;  /* to avoid warnings */
  }
}


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:
      store_localvar(ls, str_checkname(ls), nparams++);
      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: luaY_error(ls, "<name> or `...' expected");
        }
      }
      break;

    case ')': break;  /* parlist -> empty */

    default: luaY_error(ls, "<name> or `...' expected");
  }
  code_args(ls, nparams, dots);
}


static void body (LexState *ls, int needself, int line) {
  /* body ->  '(' parlist ')' chunk END */
  FuncState new_fs;
  init_state(ls, &new_fs, ls->fs->f->source);
  new_fs.f->lineDefined = line;
  check(ls, '(');
  if (needself)
    add_localvar(ls, luaS_newfixed(ls->L, "self"));
  parlist(ls);
  check(ls, ')');
  chunk(ls);
  check_match(ls, END, FUNCTION, line);
  close_func(ls);
  func_onstack(ls, &new_fs);
}


static void ret (LexState *ls) {
  /* ret -> [RETURN explist sc] */
  if (ls->token == RETURN) {
    listdesc e;
    check_debugline(ls);
    next(ls);
    explist(ls, &e); 
    if (e.pc > 0) {  /* expression is an open function call? */
      Instruction *i = &ls->fs->f->code[e.pc];
      *i = SET_OPCODE(*i, TAILCALL);  /* instead of a conventional CALL */
      *i = SETARG_B(*i, ls->fs->nlocalvar);
    }
    else
      code_U(ls, RETCODE, ls->fs->nlocalvar, 0);
    ls->fs->stacksize = ls->fs->nlocalvar;  /* removes all temp values */
    optional(ls, ';');
  }
}

/* }====================================================================== */


static void chunk (LexState *ls) {
  /* chunk -> { stat [;] } ret */
  while (stat(ls)) {
    LUA_ASSERT(ls->L, ls->fs->stacksize == ls->fs->nlocalvar,
               "stack size != # local vars");
    optional(ls, ';');
  }
  ret(ls);  /* optional return */
}