summaryrefslogtreecommitdiff
path: root/lua.stx
blob: 6032b99332a4944698e4cbff50a98a08f375e074 (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
%{

char *rcs_luastx = "$Id: lua.stx,v 3.37 1996/05/28 21:07:32 roberto Exp roberto $";

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

#include "luadebug.h"
#include "mem.h"
#include "lex.h"
#include "opcode.h"
#include "hash.h"
#include "inout.h"
#include "tree.h"
#include "table.h"
#include "lua.h"
#include "func.h"

/* to avoid warnings generated by yacc */
int yyparse (void);
#define malloc luaI_malloc
#define realloc luaI_realloc
#define free luaI_free

#ifndef LISTING
#define LISTING 0
#endif

#ifndef CODE_BLOCK
#define CODE_BLOCK 256
#endif
static int   maxcode;
static int   maxmain;
static int   maxcurr;
static Byte  *funcCode = NULL;
static Byte **initcode;
static Byte  *basepc;
static int   maincode;
static int   pc;


#define MAXVAR 32
static Long    varbuffer[MAXVAR];    /* variables in an assignment list;
				it's long to store negative Word values */
static int     nvarbuffer=0;	     /* number of variables at a list */

#define MAXLOCALS 32
static TaggedString *localvar[MAXLOCALS];  /* store local variable names */
static int     nlocalvar=0;	     /* number of local variables */

#define MAXFIELDS FIELDS_PER_FLUSH*2
static Word    fields[MAXFIELDS];     /* fieldnames to be flushed */
static int     nfields=0;

int lua_debug = 0;

/* Internal functions */

static void yyerror (char *s)
{
 char msg[256];
 char *token = lua_lasttext();
 if (token[0] == 0)
   token = "<eof>";
 sprintf (msg,"%s; last token read: \"%s\" at line %d in file `%s'",
          s, token, lua_linenumber, lua_parsedfile);
 lua_error (msg);
}

static void check_space (int i)
{
  if (pc+i>maxcurr-1)  /* 1 byte free to code HALT of main code */
    maxcurr = growvector(&basepc, maxcurr, Byte, codeEM, MAX_INT);
}

static void code_byte (Byte c)
{
 check_space(1);
 basepc[pc++] = c;
}

static void code_word (Word n)
{
  check_space(sizeof(Word));
  memcpy(basepc+pc, &n, sizeof(Word));
  pc += sizeof(Word);
}

static void code_float (real n)
{
  check_space(sizeof(real));
  memcpy(basepc+pc, &n, sizeof(real));
  pc += sizeof(real);
}

static void code_code (TFunc *tf)
{
  check_space(sizeof(TFunc *));
  memcpy(basepc+pc, &tf, sizeof(TFunc *));
  pc += sizeof(TFunc *);
}

static void code_word_at (Byte *p, int n)
{
  Word w = n;
  if (w != n)
    yyerror("block too big");
  memcpy(p, &w, sizeof(Word));
}

static void push_field (Word name)
{
  if (nfields < MAXFIELDS)
    fields[nfields++] = name;
  else
   yyerror ("too many fields in nested constructors");
}

static void flush_record (int n)
{
  int i;
  if (n == 0) return;
  code_byte(STORERECORD);
  code_byte(n);
  for (i=0; i<n; i++)
    code_word(fields[--nfields]);
}

static void flush_list (int m, int n)
{
  if (n == 0) return;
  if (m == 0)
    code_byte(STORELIST0); 
  else
  if (m < 255)
  {
    code_byte(STORELIST);
    code_byte(m);
  }
  else
   yyerror ("list constructor too long");
  code_byte(n);
}

static void store_localvar (TaggedString *name, int n)
{
 if (nlocalvar+n < MAXLOCALS)
  localvar[nlocalvar+n] = name;
 else
  yyerror ("too many local variables");
 if (lua_debug)
   luaI_registerlocalvar(name, lua_linenumber);
}

static void add_localvar (TaggedString *name)
{
  store_localvar(name, 0);
  nlocalvar++;
}

static void add_varbuffer (Long var)
{
 if (nvarbuffer < MAXVAR)
  varbuffer[nvarbuffer++] = var;
 else
  yyerror ("variable buffer overflow");
}

static void code_number (float f)
{ 
  Word i = (Word)f;
  if (f == (float)i)  /* f has an (short) integer value */
  {
   if (i <= 2) code_byte(PUSH0 + i);
   else if (i <= 255)
   {
    code_byte(PUSHBYTE);
    code_byte(i);
   }
   else
   {
    code_byte(PUSHWORD);
    code_word(i);
   }
  }
  else
  {
   code_byte(PUSHFLOAT);
   code_float(f);
  }
}

/*
** Search a local name and if find return its index. If do not find return -1
*/
static int lua_localname (TaggedString *n)
{
 int i;
 for (i=nlocalvar-1; i >= 0; i--)
  if (n == localvar[i]) return i;	/* local var */
 return -1;		        /* global var */
}

/*
** Push a variable given a number. If number is positive, push global variable
** indexed by (number -1). If negative, push local indexed by ABS(number)-1.
** Otherwise, if zero, push indexed variable (record).
*/
static void lua_pushvar (Long number)
{ 
 if (number > 0)	/* global var */
 {
  code_byte(PUSHGLOBAL);
  code_word(number-1);
 }
 else if (number < 0)	/* local var */
 {
  number = (-number) - 1;
  if (number < 10) code_byte(PUSHLOCAL0 + number);
  else
  {
   code_byte(PUSHLOCAL);
   code_byte(number);
  }
 }
 else
 {
  code_byte(PUSHINDEXED);
 }
}

static void lua_codeadjust (int n)
{
 if (n+nlocalvar == 0)
   code_byte(ADJUST0);
 else
 {
   code_byte(ADJUST);
   code_byte(n+nlocalvar);
 }
}

static void change2main (void)
{
  /* (re)store main values */
  pc=maincode; basepc=*initcode; maxcurr=maxmain;
  nlocalvar=0;
}

static void savemain (void)
{
  /* save main values */
  maincode=pc; *initcode=basepc; maxmain=maxcurr;
}

static void init_func (void)
{
  if (funcCode == NULL)	/* first function */
  {
   funcCode = newvector(CODE_BLOCK, Byte);
   maxcode = CODE_BLOCK;
  }
  savemain();  /* save main values */
  /* set func values */
  pc=0; basepc=funcCode; maxcurr=maxcode; 
  nlocalvar = 0;
  luaI_codedebugline(lua_linenumber);
}

static void codereturn (void)
{
  if (nlocalvar == 0)
    code_byte(RETCODE0);
  else
  {
    code_byte(RETCODE);
    code_byte(nlocalvar);
  }
}

void luaI_codedebugline (int line)
{
  static int lastline = 0;
  if (lua_debug && line != lastline)
  {
    code_byte(SETLINE);
    code_word(line);
    lastline = line;
  }
}

static int adjust_functioncall (Long exp, int i)
{
  if (exp <= 0)
    return -exp; /* exp is -list length */
  else
  {
    int temp = basepc[exp];
    basepc[exp] = i;
    return temp+i;
  }
}

static void adjust_mult_assign (int vars, Long exps, int temps)
{
  if (exps > 0)
  { /* must correct function call */
    int diff = vars - basepc[exps];
    if (diff >= 0)
      adjust_functioncall(exps, diff);
    else
    {
      adjust_functioncall(exps, 0);
      lua_codeadjust(temps);
    }
  }
  else if (vars != -exps)
    lua_codeadjust(temps);
}

static int close_parlist (int dots)
{
  if (!dots)
    lua_codeadjust(0);
  else
  {
    code_byte(VARARGS);
    code_byte(nlocalvar);
    add_localvar(luaI_createfixedstring("arg"));
  }
  return lua_linenumber;
}

static void storesinglevar (Long v)
{
 if (v > 0)		/* global var */
 {
   code_byte(STOREGLOBAL);
   code_word(v-1);
 }
 else if (v < 0)      /* local var */
 {
   int number = (-v) - 1;
   if (number < 10) code_byte(STORELOCAL0 + number);
   else
   {
     code_byte(STORELOCAL);
     code_byte(number);
   }
 }
 else 
   code_byte(STOREINDEXED0);
}

static void lua_codestore (int i)
{
 if (varbuffer[i] != 0)  /* global or local var */
  storesinglevar(varbuffer[i]);
 else				  /* indexed var */
 {
  int j;
  int upper=0;     	/* number of indexed variables upper */
  int param;		/* number of itens until indexed expression */
  for (j=i+1; j <nvarbuffer; j++)
   if (varbuffer[j] == 0) upper++;
  param = upper*2 + i;
  if (param == 0)
   code_byte(STOREINDEXED0);
  else
  {
   code_byte(STOREINDEXED);
   code_byte(param);
  }
 }
}

static void codeIf (Long thenAdd, Long elseAdd)
{
  Long elseinit = elseAdd+sizeof(Word)+1;
  if (pc == elseinit)		/* no else */
  {
    pc -= sizeof(Word)+1;
    elseinit = pc;
  }
  else
  {
    basepc[elseAdd] = JMP;
    code_word_at(basepc+elseAdd+1, pc-elseinit);
  }
  basepc[thenAdd] = IFFJMP;
  code_word_at(basepc+thenAdd+1,elseinit-(thenAdd+sizeof(Word)+1));
}


/*
** Parse LUA code.
*/
void lua_parse (TFunc *tf)
{
 initcode = &(tf->code);
 *initcode = newvector(CODE_BLOCK, Byte);
 maincode = 0; 
 maxmain = CODE_BLOCK;
 change2main();
 if (yyparse ()) lua_error("parse error");
 savemain();
 (*initcode)[maincode++] = RETCODE0;
 tf->size = maincode;
#if LISTING
{ static void PrintCode (Byte *c, Byte *end);
 PrintCode(*initcode,*initcode+maincode); }
#endif
}


%}


%union 
{
 int   vInt;
 float vFloat;
 char *pChar;
 Word  vWord;
 Long  vLong;
 TFunc *pFunc;
 TaggedString *pTStr;
}

%start functionlist

%token WRONGTOKEN
%token NIL
%token IF THEN ELSE ELSEIF WHILE DO REPEAT UNTIL END
%token RETURN
%token LOCAL
%token FUNCTION
%token DOTS
%token <vFloat> NUMBER
%token <vWord>  STRING
%token <pTStr>  NAME 
%token <vInt>   DEBUG

%type <vLong> PrepJump
%type <vLong> exprlist, exprlist1  /* if > 0, points to function return
	counter (which has list length); if <= 0, -list lenght */
%type <vLong> functioncall, expr  /* if != 0, points to function return
					 counter */
%type <vInt>  varlist1, funcParams, funcvalue
%type <vInt>  fieldlist, localdeclist, decinit
%type <vInt>  ffieldlist, ffieldlist1, semicolonpart
%type <vInt>  lfieldlist, lfieldlist1
%type <vInt>  parlist, parlist1, par
%type <vLong> var, singlevar, funcname
%type <pFunc> body

%left AND OR
%left EQ NE '>' '<' LE GE
%left CONC
%left '+' '-'
%left '*' '/'
%left UNARY NOT
%right '^'


%% /* beginning of rules section */


functionlist : /* empty */
	     | functionlist globalstat
	     | functionlist function
	     ;

globalstat   : stat sc
	     | setdebug
	     ;

function     : FUNCTION funcname body 	 
	       { 
		code_byte(PUSHFUNCTION);
		code_code($3);
		storesinglevar($2);
	       }
	       ;

funcname	: var { $$ =$1; init_func(); }
		| varexp ':' NAME
	{
	  code_byte(PUSHSTRING);
	  code_word(luaI_findconstant($3));
	  $$ = 0;  /* indexed variable */
	  init_func();
	  add_localvar(luaI_createfixedstring("self"));
	}
		;

body :  '(' parlist ')' block END
	{
          codereturn();
	  $$ = new(TFunc);
          luaI_initTFunc($$);
	  $$->size = pc;
	  $$->code = newvector(pc, Byte);
	  $$->fileName = lua_parsedfile;
	  $$->lineDefined = $2;
	  memcpy($$->code, basepc, pc*sizeof(Byte));
          if (lua_debug)
            luaI_closelocalvars($$);
	  /* save func values */
	  funcCode = basepc; maxcode=maxcurr;
#if LISTING
                PrintCode(funcCode,funcCode+pc);
#endif
	  change2main();  /* change back to main code */
	}
		;

statlist : /* empty */
	 | statlist stat sc
	 ;

sc	 : /* empty */ | ';' ;

stat   : IF expr1 THEN PrepJump block PrepJump elsepart END
	{ codeIf($4, $6); }

       | WHILE {$<vLong>$=pc;} expr1 DO PrepJump block PrepJump END
       {
        basepc[$5] = IFFJMP;
	code_word_at(basepc+$5+1, pc - ($5 + sizeof(Word)+1));
        basepc[$7] = UPJMP;
	code_word_at(basepc+$7+1, pc - ($<vLong>2));
       }
     
       | REPEAT {$<vLong>$=pc;} block UNTIL expr1 PrepJump
       {
        basepc[$6] = IFFUPJMP;
	code_word_at(basepc+$6+1, pc - ($<vLong>2));
       }

       | varlist1 '=' exprlist1
       {
        {
         int i;
         adjust_mult_assign(nvarbuffer, $3, $1 * 2 + nvarbuffer);
	 for (i=nvarbuffer-1; i>=0; i--)
	  lua_codestore (i);
	 if ($1 > 1 || ($1 == 1 && varbuffer[0] != 0))
	  lua_codeadjust (0);
	}
       } 
       | functioncall
       | LOCAL localdeclist decinit
	{ nlocalvar += $2;
	  adjust_mult_assign($2, $3, 0);
	}
       ;

elsepart : /* empty */
	 | ELSE block
         | ELSEIF expr1 THEN PrepJump block PrepJump elsepart
	{ codeIf($4, $6); }
         ;
     
block    : {$<vInt>$ = nlocalvar;} statlist ret 
         {
	  if (nlocalvar != $<vInt>1)
	  {
           if (lua_debug)
             for (; nlocalvar > $<vInt>1; nlocalvar--)
               luaI_unregisterlocalvar(lua_linenumber);
           else
             nlocalvar = $<vInt>1;
	   lua_codeadjust (0);
	  }
         }
         ;

ret	: /* empty */
        | RETURN exprlist sc
          {
	   adjust_functioncall($2, MULT_RET);
           codereturn();
          }
	;

PrepJump : /* empty */
	 { 
	  $$ = pc;
	  code_byte(0);		/* open space */
	  code_word (0);
         }
	 ;
	   
expr1	 : expr { adjust_functioncall($1, 1); }
	 ;
				
expr :  '(' expr ')'  { $$ = $2; }
     |  expr1 EQ  expr1	{ code_byte(EQOP);   $$ = 0; }
     |	expr1 '<' expr1	{ code_byte(LTOP);   $$ = 0; }
     |	expr1 '>' expr1	{ code_byte(GTOP);   $$ = 0; }
     |	expr1 NE  expr1	{ code_byte(EQOP); code_byte(NOTOP); $$ = 0; }
     |	expr1 LE  expr1	{ code_byte(LEOP);   $$ = 0; }
     |	expr1 GE  expr1	{ code_byte(GEOP);   $$ = 0; }
     |	expr1 '+' expr1 { code_byte(ADDOP);  $$ = 0; }
     |	expr1 '-' expr1 { code_byte(SUBOP);  $$ = 0; }
     |	expr1 '*' expr1 { code_byte(MULTOP); $$ = 0; }
     |	expr1 '/' expr1 { code_byte(DIVOP);  $$ = 0; }
     |	expr1 '^' expr1 { code_byte(POWOP);  $$ = 0; }
     |	expr1 CONC expr1 { code_byte(CONCOP);  $$ = 0; }
     |	'-' expr1 %prec UNARY	{ code_byte(MINUSOP); $$ = 0;}
     | table { $$ = 0; }
     |  varexp          { $$ = 0;}
     |  NUMBER          { code_number($1); $$ = 0; }
     |  STRING
     {
      code_byte(PUSHSTRING);
      code_word($1);
      $$ = 0;
     }
     |	NIL		{code_byte(PUSHNIL); $$ = 0; }
     |  functioncall    { $$ = $1; }
     |	NOT expr1	{ code_byte(NOTOP);  $$ = 0;}
     |	expr1 AND PrepJump {code_byte(POP); } expr1
     { 
      basepc[$3] = ONFJMP;
      code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
      $$ = 0;
     }
     |	expr1 OR PrepJump {code_byte(POP); } expr1	
     { 
      basepc[$3] = ONTJMP;
      code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
      $$ = 0;
     }
     ;

table :
     {
      code_byte(CREATEARRAY);
      $<vLong>$ = pc; code_word(0);
     }
      '{' fieldlist '}'
     {
      code_word_at(basepc+$<vLong>1, $3);
     }
         ;

functioncall : funcvalue funcParams
	{
	  code_byte(CALLFUNC);
	  code_byte($1+$2);
	  $$ = pc;
	  code_byte(0);  /* may be modified by other rules */
	}
	     ;

funcvalue    : varexp { $$ = 0; }
	     | varexp ':' NAME 
	     { 
               code_byte(PUSHSELF); 
	       code_word(luaI_findconstant($3));
               $$ = 1;
	     }
	     ;

funcParams :	'(' exprlist ')'
	{ $$ = adjust_functioncall($2, 1); }
	|	table  { $$ = 1; }
	;

exprlist  :	/* empty */		{ $$ = 0; }
	  |	exprlist1		{ $$ = $1; }
	  ;
		
exprlist1 :  expr	{ if ($1 != 0) $$ = $1; else $$ = -1; }
	  |  exprlist1 ',' { $<vLong>$ = adjust_functioncall($1, 1); } expr 
	{
	  if ($4 == 0) $$ = -($<vLong>3 + 1);  /* -length */
	  else
	  {
	    adjust_functioncall($4, $<vLong>3);
	    $$ = $4;
	  }
	}
	  ;

parlist  :	/* empty */ { $$ = close_parlist(0); }
	  |	parlist1    { $$ = close_parlist($1); }
	  ;
		
parlist1 :	par		  { $$ = $1; }
	  |	parlist1 ',' par
	{
	  if ($1)
            lua_error("invalid parameter list");
          $$ = $3;
	}
	  ;

par : NAME	{ add_localvar($1); $$ = 0; }
    | DOTS	{ $$ = 1; }
    ;
		
fieldlist  : lfieldlist
  	     { flush_list($1/FIELDS_PER_FLUSH, $1%FIELDS_PER_FLUSH); }
	     semicolonpart
	     { $$ = $1+$3; }
	   | ffieldlist1 lastcomma
	     { $$ = $1; flush_record($1%FIELDS_PER_FLUSH); }
	   ;

semicolonpart : /* empty */
	        { $$ = 0; }
	      | ';' ffieldlist
	        { $$ = $2; flush_record($2%FIELDS_PER_FLUSH); }
	      ;

lastcomma  : /* empty */ 
	   | ','
	   ;

ffieldlist  : /* empty */ { $$ = 0; }
            | ffieldlist1 lastcomma { $$ = $1; }
            ;   

ffieldlist1 : ffield			{$$=1;}
	   | ffieldlist1 ',' ffield	
		{
		  $$=$1+1;
		  if ($$%FIELDS_PER_FLUSH == 0) flush_record(FIELDS_PER_FLUSH);
		}
	   ; 

ffield      : NAME '=' expr1 
	      { 
	       push_field(luaI_findconstant($1));
	      }
           ;

lfieldlist  : /* empty */ { $$ = 0; }
	    | lfieldlist1 lastcomma { $$ = $1; }
	    ;

lfieldlist1 : expr1  {$$=1;}
	    | lfieldlist1 ',' expr1
		{
		  $$=$1+1;
		  if ($$%FIELDS_PER_FLUSH == 0) 
		    flush_list($$/FIELDS_PER_FLUSH - 1, FIELDS_PER_FLUSH);
		}
            ;

varlist1  :	var			
	  {
	   nvarbuffer = 0; 
	   add_varbuffer($1);
	   $$ = ($1 == 0) ? 1 : 0;
	  }
	  |	varlist1 ',' var	
	  { 
	   add_varbuffer($3);
	   $$ = ($3 == 0) ? $1 + 1 : $1;
	  }
	  ;
		
var	  :	singlevar { $$ = $1; }
	  |	varexp '[' expr1 ']' 
	  {
	   $$ = 0;		/* indexed variable */
	  }
	  |	varexp '.' NAME
	  {
	   code_byte(PUSHSTRING);
	   code_word(luaI_findconstant($3));
	   $$ = 0;		/* indexed variable */
	  }
	  ;
		
singlevar :	NAME
	  {
	   int local = lua_localname($1);
	   if (local == -1)	/* global var */
	    $$ = luaI_findsymbol($1)+1;  /* return positive value */
           else
	    $$ = -(local+1);		/* return negative value */
	  }
	  ;

varexp	: var { lua_pushvar($1); }
	;
	  
localdeclist  : NAME {store_localvar($1, 0); $$ = 1;}
     	  | localdeclist ',' NAME 
	    {
	     store_localvar($3, $1);
	     $$ = $1+1;
	    }
	  ;
		
decinit	  : /* empty */  { $$ = 0; }
	  | '=' exprlist1 { $$ = $2; }
	  ;
	  
setdebug  : DEBUG  { lua_debug = $1; }
	  ;

%%