3 char *rcs_luastx = "$Id: lua.stx,v 3.47 1997/06/19 17:46:12 roberto Exp $";
20 /* to avoid warnings generated by yacc */
22 #define malloc luaI_malloc
23 #define realloc luaI_realloc
24 #define free luaI_free
31 #define CODE_BLOCK 256
36 static Byte *funcCode = NULL;
37 static Byte **initcode;
44 static Long varbuffer[MAXVAR]; /* variables in an assignment list;
45 it's long to store negative Word values */
46 static int nvarbuffer=0; /* number of variables at a list */
49 static TaggedString *localvar[MAXLOCALS]; /* store local variable names */
50 static int nlocalvar=0; /* number of local variables */
52 #define MAXFIELDS FIELDS_PER_FLUSH*2
56 /* Internal functions */
58 static void yyerror (char *s)
63 static void check_space (int i)
65 if (pc+i>maxcurr-1) /* 1 byte free to code HALT of main code */
66 maxcurr = growvector(&basepc, maxcurr, Byte, codeEM, MAX_INT);
69 static void code_byte (Byte c)
75 static void code_word (Word n)
77 check_space(sizeof(Word));
78 memcpy(basepc+pc, &n, sizeof(Word));
82 static void code_float (real n)
84 check_space(sizeof(real));
85 memcpy(basepc+pc, &n, sizeof(real));
89 static void code_code (TFunc *tf)
91 check_space(sizeof(TFunc *));
92 memcpy(basepc+pc, &tf, sizeof(TFunc *));
93 pc += sizeof(TFunc *);
96 static void code_word_at (Byte *p, int n)
100 yyerror("block too big");
101 memcpy(p, &w, sizeof(Word));
104 static void flush_record (int n)
111 static void flush_list (int m, int n)
115 code_byte(STORELIST0);
119 code_byte(STORELIST);
123 yyerror ("list constructor too long");
127 static void store_localvar (TaggedString *name, int n)
129 if (nlocalvar+n < MAXLOCALS)
130 localvar[nlocalvar+n] = name;
132 yyerror ("too many local variables");
134 luaI_registerlocalvar(name, lua_linenumber);
137 static void add_localvar (TaggedString *name)
139 store_localvar(name, 0);
143 static void add_varbuffer (Long var)
145 if (nvarbuffer < MAXVAR)
146 varbuffer[nvarbuffer++] = var;
148 yyerror ("variable buffer overflow");
151 static void code_string (Word w)
153 code_byte(PUSHSTRING);
157 static void code_constant (TaggedString *s)
159 code_string(luaI_findconstant(s));
162 static void code_number (float f)
165 if (f >= 0 && f <= (float)MAX_WORD && (float)(i=(Word)f) == f) {
166 /* f has an (short) integer value */
167 if (i <= 2) code_byte(PUSH0 + i);
181 code_byte(PUSHFLOAT);
187 ** Search a local name and if find return its index. If do not find return -1
189 static int lua_localname (TaggedString *n)
192 for (i=nlocalvar-1; i >= 0; i--)
193 if (n == localvar[i]) return i; /* local var */
194 return -1; /* global var */
198 ** Push a variable given a number. If number is positive, push global variable
199 ** indexed by (number -1). If negative, push local indexed by ABS(number)-1.
200 ** Otherwise, if zero, push indexed variable (record).
202 static void lua_pushvar (Long number)
204 if (number > 0) /* global var */
206 code_byte(PUSHGLOBAL);
209 else if (number < 0) /* local var */
211 number = (-number) - 1;
212 if (number < 10) code_byte(PUSHLOCAL0 + number);
215 code_byte(PUSHLOCAL);
221 code_byte(PUSHINDEXED);
225 static void lua_codeadjust (int n)
227 if (n+nlocalvar == 0)
232 code_byte(n+nlocalvar);
236 static void change2main (void)
238 /* (re)store main values */
239 pc=maincode; basepc=*initcode; maxcurr=maxmain;
243 static void savemain (void)
245 /* save main values */
246 maincode=pc; *initcode=basepc; maxmain=maxcurr;
249 static void init_func (void)
251 if (funcCode == NULL) /* first function */
253 funcCode = newvector(CODE_BLOCK, Byte);
254 maxcode = CODE_BLOCK;
256 savemain(); /* save main values */
257 /* set func values */
258 pc=0; basepc=funcCode; maxcurr=maxcode;
260 luaI_codedebugline(lua_linenumber);
263 static void codereturn (void)
270 code_byte(nlocalvar);
274 void luaI_codedebugline (int line)
276 static int lastline = 0;
277 if (lua_debug && line != lastline)
285 static int adjust_functioncall (Long exp, int i)
288 return -exp; /* exp is -list length */
291 int temp = basepc[exp];
297 static void adjust_mult_assign (int vars, Long exps, int temps)
300 { /* must correct function call */
301 int diff = vars - basepc[exps];
303 adjust_functioncall(exps, diff);
306 adjust_functioncall(exps, 0);
307 lua_codeadjust(temps);
310 else if (vars != -exps)
311 lua_codeadjust(temps);
314 static int close_parlist (int dots)
321 code_byte(nlocalvar);
322 add_localvar(luaI_createfixedstring("arg"));
324 return lua_linenumber;
327 static void storesinglevar (Long v)
329 if (v > 0) /* global var */
331 code_byte(STOREGLOBAL);
334 else if (v < 0) /* local var */
336 int number = (-v) - 1;
337 if (number < 10) code_byte(STORELOCAL0 + number);
340 code_byte(STORELOCAL);
345 code_byte(STOREINDEXED0);
348 static void lua_codestore (int i)
350 if (varbuffer[i] != 0) /* global or local var */
351 storesinglevar(varbuffer[i]);
352 else /* indexed var */
355 int upper=0; /* number of indexed variables upper */
356 int param; /* number of itens until indexed expression */
357 for (j=i+1; j <nvarbuffer; j++)
358 if (varbuffer[j] == 0) upper++;
361 code_byte(STOREINDEXED0);
364 code_byte(STOREINDEXED);
370 static void codeIf (Long thenAdd, Long elseAdd)
372 Long elseinit = elseAdd+sizeof(Word)+1;
373 if (pc == elseinit) /* no else */
375 pc -= sizeof(Word)+1;
380 basepc[elseAdd] = JMP;
381 code_word_at(basepc+elseAdd+1, pc-elseinit);
383 basepc[thenAdd] = IFFJMP;
384 code_word_at(basepc+thenAdd+1,elseinit-(thenAdd+sizeof(Word)+1));
391 void lua_parse (TFunc *tf)
393 initcode = &(tf->code);
394 *initcode = newvector(CODE_BLOCK, Byte);
396 maxmain = CODE_BLOCK;
398 if (yyparse ()) lua_error("parse error");
400 (*initcode)[maincode++] = RETCODE0;
403 { static void PrintCode (Byte *c, Byte *end);
404 PrintCode(*initcode,*initcode+maincode); }
427 %token IF THEN ELSE ELSEIF WHILE DO REPEAT UNTIL END
432 %token <vFloat> NUMBER
433 %token <vWord> STRING
436 %type <vLong> PrepJump
437 %type <vLong> exprlist, exprlist1 /* if > 0, points to function return
438 counter (which has list length); if <= 0, -list lenght */
439 %type <vLong> functioncall, expr /* if != 0, points to function return
441 %type <vInt> varlist1, funcParams, funcvalue
442 %type <vInt> fieldlist, localdeclist, decinit
443 %type <vInt> ffieldlist, ffieldlist1, semicolonpart
444 %type <vInt> lfieldlist, lfieldlist1
445 %type <vInt> parlist, parlist1, par
446 %type <vLong> var, singlevar, funcname
450 %left EQ NE '>' '<' LE GE
458 %% /* beginning of rules section */
460 chunk : chunklist ret ;
462 chunklist : /* empty */
467 function : FUNCTION funcname body
469 code_byte(PUSHFUNCTION);
475 funcname : var { $$ =$1; init_func(); }
479 $$ = 0; /* indexed variable */
481 add_localvar(luaI_createfixedstring("self"));
485 body : '(' parlist ')' block END
491 $$->code = newvector(pc, Byte);
492 $$->lineDefined = $2;
493 memcpy($$->code, basepc, pc*sizeof(Byte));
495 luaI_closelocalvars($$);
496 /* save func values */
497 funcCode = basepc; maxcode=maxcurr;
499 PrintCode(funcCode,funcCode+pc);
501 change2main(); /* change back to main code */
505 statlist : /* empty */
509 sc : /* empty */ | ';' ;
511 stat : IF expr1 THEN PrepJump block PrepJump elsepart END
514 | WHILE {$<vLong>$=pc;} expr1 DO PrepJump block PrepJump END
517 code_word_at(basepc+$5+1, pc - ($5 + sizeof(Word)+1));
519 code_word_at(basepc+$7+1, pc - ($<vLong>2));
522 | REPEAT {$<vLong>$=pc;} block UNTIL expr1 PrepJump
524 basepc[$6] = IFFUPJMP;
525 code_word_at(basepc+$6+1, pc - ($<vLong>2));
528 | varlist1 '=' exprlist1
532 adjust_mult_assign(nvarbuffer, $3, $1 * 2 + nvarbuffer);
533 for (i=nvarbuffer-1; i>=0; i--)
535 if ($1 > 1 || ($1 == 1 && varbuffer[0] != 0))
540 | LOCAL localdeclist decinit
542 adjust_mult_assign($2, $3, 0);
546 elsepart : /* empty */
548 | ELSEIF expr1 THEN PrepJump block PrepJump elsepart
552 block : {$<vInt>$ = nlocalvar;} statlist ret
554 if (nlocalvar != $<vInt>1)
557 for (; nlocalvar > $<vInt>1; nlocalvar--)
558 luaI_unregisterlocalvar(lua_linenumber);
560 nlocalvar = $<vInt>1;
569 adjust_functioncall($2, MULT_RET);
574 PrepJump : /* empty */
577 code_byte(0); /* open space */
582 expr1 : expr { adjust_functioncall($1, 1); }
585 expr : '(' expr ')' { $$ = $2; }
586 | expr1 EQ expr1 { code_byte(EQOP); $$ = 0; }
587 | expr1 '<' expr1 { code_byte(LTOP); $$ = 0; }
588 | expr1 '>' expr1 { code_byte(GTOP); $$ = 0; }
589 | expr1 NE expr1 { code_byte(EQOP); code_byte(NOTOP); $$ = 0; }
590 | expr1 LE expr1 { code_byte(LEOP); $$ = 0; }
591 | expr1 GE expr1 { code_byte(GEOP); $$ = 0; }
592 | expr1 '+' expr1 { code_byte(ADDOP); $$ = 0; }
593 | expr1 '-' expr1 { code_byte(SUBOP); $$ = 0; }
594 | expr1 '*' expr1 { code_byte(MULTOP); $$ = 0; }
595 | expr1 '/' expr1 { code_byte(DIVOP); $$ = 0; }
596 | expr1 '^' expr1 { code_byte(POWOP); $$ = 0; }
597 | expr1 CONC expr1 { code_byte(CONCOP); $$ = 0; }
598 | '-' expr1 %prec UNARY { code_byte(MINUSOP); $$ = 0;}
601 | NUMBER { code_number($1); $$ = 0; }
607 | NIL {code_byte(PUSHNIL); $$ = 0; }
608 | functioncall { $$ = $1; }
609 | NOT expr1 { code_byte(NOTOP); $$ = 0;}
610 | expr1 AND PrepJump {code_byte(POP); } expr1
613 code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
616 | expr1 OR PrepJump {code_byte(POP); } expr1
619 code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
626 code_byte(CREATEARRAY);
627 $<vLong>$ = pc; code_word(0);
631 code_word_at(basepc+$<vLong>1, $3);
635 functioncall : funcvalue funcParams
640 code_byte(0); /* may be modified by other rules */
644 funcvalue : varexp { $$ = 0; }
648 code_word(luaI_findconstant($3));
653 funcParams : '(' exprlist ')'
654 { $$ = adjust_functioncall($2, 1); }
658 exprlist : /* empty */ { $$ = 0; }
659 | exprlist1 { $$ = $1; }
662 exprlist1 : expr { if ($1 != 0) $$ = $1; else $$ = -1; }
663 | exprlist1 ',' { $<vLong>$ = adjust_functioncall($1, 1); } expr
665 if ($4 == 0) $$ = -($<vLong>3 + 1); /* -length */
668 adjust_functioncall($4, $<vLong>3);
674 parlist : /* empty */ { $$ = close_parlist(0); }
675 | parlist1 { $$ = close_parlist($1); }
678 parlist1 : par { $$ = $1; }
682 lua_error("invalid parameter list");
687 par : NAME { add_localvar($1); $$ = 0; }
691 fieldlist : lfieldlist
692 { flush_list($1/FIELDS_PER_FLUSH, $1%FIELDS_PER_FLUSH); }
695 | ffieldlist1 lastcomma
696 { $$ = $1; flush_record($1%FIELDS_PER_FLUSH); }
699 semicolonpart : /* empty */
702 { $$ = $2; flush_record($2%FIELDS_PER_FLUSH); }
705 lastcomma : /* empty */
709 ffieldlist : /* empty */ { $$ = 0; }
710 | ffieldlist1 lastcomma { $$ = $1; }
713 ffieldlist1 : ffield {$$=1;}
714 | ffieldlist1 ',' ffield
717 if ($$%FIELDS_PER_FLUSH == 0) flush_record(FIELDS_PER_FLUSH);
721 ffield : ffieldkey '=' expr1
724 ffieldkey : '[' expr1 ']'
725 | NAME { code_constant($1); }
728 lfieldlist : /* empty */ { $$ = 0; }
729 | lfieldlist1 lastcomma { $$ = $1; }
732 lfieldlist1 : expr1 {$$=1;}
733 | lfieldlist1 ',' expr1
736 if ($$%FIELDS_PER_FLUSH == 0)
737 flush_list($$/FIELDS_PER_FLUSH - 1, FIELDS_PER_FLUSH);
745 $$ = ($1 == 0) ? 1 : 0;
750 $$ = ($3 == 0) ? $1 + 1 : $1;
754 var : singlevar { $$ = $1; }
755 | varexp '[' expr1 ']'
757 $$ = 0; /* indexed variable */
762 $$ = 0; /* indexed variable */
768 int local = lua_localname($1);
769 if (local == -1) /* global var */
770 $$ = luaI_findsymbol($1)+1; /* return positive value */
772 $$ = -(local+1); /* return negative value */
776 varexp : var { lua_pushvar($1); }
779 localdeclist : NAME {store_localvar($1, 0); $$ = 1;}
780 | localdeclist ',' NAME
782 store_localvar($3, $1);
787 decinit : /* empty */ { $$ = 0; }
788 | '=' exprlist1 { $$ = $2; }