12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055 |
- %{
- char *rcs_luastx = "$Id: lua.stx,v 2.6 1994/08/03 14:15:46 celes Exp celes $";
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "mm.h"
- #include "opcode.h"
- #include "hash.h"
- #include "inout.h"
- #include "table.h"
- #include "lua.h"
- #define LISTING 0
- #ifndef CODE_BLOCK
- #define CODE_BLOCK 256
- #endif
- static Long maxcode;
- static Long maxmain;
- static Long maxcurr ;
- static Byte *code = NULL;
- static Byte *initcode;
- static Byte *basepc;
- static Long maincode;
- static Long 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 */
- static Word localvar[STACKGAP]; /* 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;
- static int ntemp; /* number of temporary var into stack */
- static int err; /* flag to indicate error */
- /* Internal functions */
- static void code_byte (Byte c)
- {
- if (pc>maxcurr-2) /* 1 byte free to code HALT of main code */
- {
- maxcurr *= 2;
- basepc = (Byte *)realloc(basepc, maxcurr*sizeof(Byte));
- if (basepc == NULL)
- {
- lua_error ("not enough memory");
- err = 1;
- }
- }
- basepc[pc++] = c;
- }
- static void code_word (Word n)
- {
- CodeWord code;
- code.w = n;
- code_byte(code.m.c1);
- code_byte(code.m.c2);
- }
- static void code_float (float n)
- {
- CodeFloat code;
- code.f = n;
- code_byte(code.m.c1);
- code_byte(code.m.c2);
- code_byte(code.m.c3);
- code_byte(code.m.c4);
- }
- static void code_code (Byte *b)
- {
- CodeCode code;
- code.b = b;
- code_byte(code.m.c1);
- code_byte(code.m.c2);
- code_byte(code.m.c3);
- code_byte(code.m.c4);
- }
- static void code_word_at (Byte *p, Word n)
- {
- CodeWord code;
- code.w = n;
- *p++ = code.m.c1;
- *p++ = code.m.c2;
- }
- static void push_field (Word name)
- {
- if (nfields < STACKGAP-1)
- fields[nfields++] = name;
- else
- {
- lua_error ("too many fields in a constructor");
- err = 1;
- }
- }
- 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]);
- ntemp -= n;
- }
- 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
- {
- lua_error ("list constructor too long");
- err = 1;
- }
- code_byte(n);
- ntemp-=n;
- }
- static void incr_ntemp (void)
- {
- if (ntemp+nlocalvar+MAXVAR+1 < STACKGAP)
- ntemp++;
- else
- {
- lua_error ("stack overflow");
- err = 1;
- }
- }
- static void add_nlocalvar (int n)
- {
- if (ntemp+nlocalvar+MAXVAR+n < STACKGAP)
- nlocalvar += n;
- else
- {
- lua_error ("too many local variables or expression too complicate");
- err = 1;
- }
- }
- static void incr_nvarbuffer (void)
- {
- if (nvarbuffer < MAXVAR-1)
- nvarbuffer++;
- else
- {
- lua_error ("variable buffer overflow");
- err = 1;
- }
- }
- 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);
- }
- incr_ntemp();
- }
- static void init_function (void)
- {
- if (code == NULL) /* first function */
- {
- code = (Byte *) calloc(CODE_BLOCK, sizeof(Byte));
- if (code == NULL)
- {
- lua_error("not enough memory");
- err = 1;
- }
- maxcode = CODE_BLOCK;
- }
- }
-
- %}
- %union
- {
- int vInt;
- float vFloat;
- char *pChar;
- Word vWord;
- Long vLong;
- Byte *pByte;
- }
- %start functionlist
- %token WRONGTOKEN
- %token NIL
- %token IF THEN ELSE ELSEIF WHILE DO REPEAT UNTIL END
- %token RETURN
- %token LOCAL
- %token FUNCTION
- %token <vFloat> NUMBER
- %token <vWord> STRING
- %token <pChar> NAME
- %token <vInt> DEBUG
- %type <vLong> PrepJump
- %type <vInt> expr, exprlist, exprlist1, varlist1, typeconstructor
- %type <vInt> fieldlist, localdeclist
- %type <vInt> ffieldlist, ffieldlist1
- %type <vInt> lfieldlist, lfieldlist1
- %type <vInt> functionvalue
- %type <vLong> var, singlevar, objectname
- %left AND OR
- %left '=' NE '>' '<' LE GE
- %left CONC
- %left '+' '-'
- %left '*' '/'
- %left UNARY NOT
- %% /* beginning of rules section */
- functionlist : /* empty */
- | functionlist
- {
- pc=maincode; basepc=initcode; maxcurr=maxmain;
- nlocalvar=0;
- }
- stat sc
- {
- maincode=pc; initcode=basepc; maxmain=maxcurr;
- }
- | functionlist function
- | functionlist method
- | functionlist setdebug
- ;
- function : FUNCTION NAME
- {
- init_function();
- pc=0; basepc=code; maxcurr=maxcode;
- nlocalvar=0;
- $<vWord>$ = lua_findsymbol($2);
- }
- '(' parlist ')'
- {
- if (lua_debug)
- {
- code_byte(SETFUNCTION);
- code_word(lua_nfile-1);
- code_word($<vWord>3);
- }
- lua_codeadjust (0);
- }
- block
- END
- {
- if (lua_debug) code_byte(RESET);
- code_byte(RETCODE); code_byte(nlocalvar);
- s_tag($<vWord>3) = T_FUNCTION;
- s_bvalue($<vWord>3) = calloc (pc, sizeof(Byte));
- if (s_bvalue($<vWord>3) == NULL)
- {
- lua_error("not enough memory");
- err = 1;
- }
- memcpy (s_bvalue($<vWord>3), basepc, pc*sizeof(Byte));
- code = basepc; maxcode=maxcurr;
- #if LISTING
- PrintCode(code,code+pc);
- #endif
- }
- ;
- method : FUNCTION NAME { $<vWord>$ = lua_findsymbol($2); } ':' NAME
- {
- init_function();
- pc=0; basepc=code; maxcurr=maxcode;
- nlocalvar=0;
- localvar[nlocalvar]=lua_findsymbol("self"); /* self param. */
- add_nlocalvar(1);
- $<vWord>$ = lua_findconstant($5);
- }
- '(' parlist ')'
- {
- if (lua_debug)
- {
- code_byte(SETFUNCTION);
- code_word(lua_nfile-1);
- code_word($<vWord>6);
- }
- lua_codeadjust (0);
- }
- block
- END
- {
- Byte *b;
- if (lua_debug) code_byte(RESET);
- code_byte(RETCODE); code_byte(nlocalvar);
- b = calloc (pc, sizeof(Byte));
- if (b == NULL)
- {
- lua_error("not enough memory");
- err = 1;
- }
- memcpy (b, basepc, pc*sizeof(Byte));
- code = basepc; maxcode=maxcurr;
- #if LISTING
- PrintCode(code,code+pc);
- #endif
- /* assign function to table field */
- pc=maincode; basepc=initcode; maxcurr=maxmain;
- nlocalvar=0;
- lua_pushvar($<vWord>3+1);
- code_byte(PUSHSTRING);
- code_word($<vWord>6);
- code_byte(PUSHFUNCTION);
- code_code(b);
- code_byte(STOREINDEXED0);
- maincode=pc; initcode=basepc; maxmain=maxcurr;
- }
- ;
- statlist : /* empty */
- | statlist stat sc
- ;
- stat : {
- ntemp = 0;
- if (lua_debug)
- {
- code_byte(SETLINE); code_word(lua_linenumber);
- }
- }
- stat1
-
- sc : /* empty */ | ';' ;
- stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END
- {
- {
- Long elseinit = $6+sizeof(Word)+1;
- if (pc - elseinit == 0) /* no else */
- {
- pc -= sizeof(Word)+1;
- elseinit = pc;
- }
- else
- {
- basepc[$6] = JMP;
- code_word_at(basepc+$6+1, pc - elseinit);
- }
- basepc[$4] = IFFJMP;
- code_word_at(basepc+$4+1,elseinit-($4+sizeof(Word)+1));
- }
- }
-
- | 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;
- if ($3 == 0 || nvarbuffer != ntemp - $1 * 2)
- lua_codeadjust ($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 { lua_codeadjust (0); }
- | typeconstructor { lua_codeadjust (0); }
- | LOCAL localdeclist decinit { add_nlocalvar($2); lua_codeadjust (0); }
- ;
- elsepart : /* empty */
- | ELSE block
- | ELSEIF expr1 THEN PrepJump block PrepJump elsepart
- {
- {
- Long elseinit = $6+sizeof(Word)+1;
- if (pc - elseinit == 0) /* no else */
- {
- pc -= sizeof(Word)+1;
- elseinit = pc;
- }
- else
- {
- basepc[$6] = JMP;
- code_word_at(basepc+$6+1, pc - elseinit);
- }
- basepc[$4] = IFFJMP;
- code_word_at(basepc+$4+1, elseinit - ($4 + sizeof(Word)+1));
- }
- }
- ;
-
- block : {$<vInt>$ = nlocalvar;} statlist {ntemp = 0;} ret
- {
- if (nlocalvar != $<vInt>1)
- {
- nlocalvar = $<vInt>1;
- lua_codeadjust (0);
- }
- }
- ;
- ret : /* empty */
- | { if (lua_debug){code_byte(SETLINE);code_word(lua_linenumber);}}
- RETURN exprlist sc
- {
- if (lua_debug) code_byte(RESET);
- code_byte(RETCODE); code_byte(nlocalvar);
- }
- ;
- PrepJump : /* empty */
- {
- $$ = pc;
- code_byte(0); /* open space */
- code_word (0);
- }
-
- expr1 : expr { if ($1 == 0) {lua_codeadjust (ntemp+1); incr_ntemp();}}
- ;
-
- expr : '(' expr ')' { $$ = $2; }
- | expr1 '=' expr1 { code_byte(EQOP); $$ = 1; ntemp--;}
- | expr1 '<' expr1 { code_byte(LTOP); $$ = 1; ntemp--;}
- | expr1 '>' expr1 { code_byte(LEOP); code_byte(NOTOP); $$ = 1; ntemp--;}
- | expr1 NE expr1 { code_byte(EQOP); code_byte(NOTOP); $$ = 1; ntemp--;}
- | expr1 LE expr1 { code_byte(LEOP); $$ = 1; ntemp--;}
- | expr1 GE expr1 { code_byte(LTOP); code_byte(NOTOP); $$ = 1; ntemp--;}
- | expr1 '+' expr1 { code_byte(ADDOP); $$ = 1; ntemp--;}
- | expr1 '-' expr1 { code_byte(SUBOP); $$ = 1; ntemp--;}
- | expr1 '*' expr1 { code_byte(MULTOP); $$ = 1; ntemp--;}
- | expr1 '/' expr1 { code_byte(DIVOP); $$ = 1; ntemp--;}
- | expr1 CONC expr1 { code_byte(CONCOP); $$ = 1; ntemp--;}
- | '+' expr1 %prec UNARY { $$ = 1; }
- | '-' expr1 %prec UNARY { code_byte(MINUSOP); $$ = 1;}
- | typeconstructor { $$ = $1; }
- | '@' '(' dimension ')'
- {
- code_byte(CREATEARRAY);
- $$ = 1;
- }
- | var { lua_pushvar ($1); $$ = 1;}
- | NUMBER { code_number($1); $$ = 1; }
- | STRING
- {
- code_byte(PUSHSTRING);
- code_word($1);
- $$ = 1;
- incr_ntemp();
- }
- | NIL {code_byte(PUSHNIL); $$ = 1; incr_ntemp();}
- | functioncall
- {
- $$ = 0;
- if (lua_debug)
- {
- code_byte(SETLINE); code_word(lua_linenumber);
- }
- }
- | NOT expr1 { code_byte(NOTOP); $$ = 1;}
- | expr1 AND PrepJump {code_byte(POP); ntemp--;} expr1
- {
- basepc[$3] = ONFJMP;
- code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
- $$ = 1;
- }
- | expr1 OR PrepJump {code_byte(POP); ntemp--;} expr1
- {
- basepc[$3] = ONTJMP;
- code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
- $$ = 1;
- }
- ;
- typeconstructor: '@'
- {
- code_byte(PUSHWORD);
- $<vLong>$ = pc; code_word(0);
- incr_ntemp();
- code_byte(CREATEARRAY);
- }
- objectname fieldlist
- {
- code_word_at(basepc+$<vLong>2, $4);
- if ($3 < 0) /* there is no function to be called */
- {
- $$ = 1;
- }
- else
- {
- lua_pushvar ($3+1);
- code_byte(PUSHMARK);
- incr_ntemp();
- code_byte(PUSHOBJECT);
- incr_ntemp();
- code_byte(CALLFUNC);
- ntemp -= 4;
- $$ = 0;
- if (lua_debug)
- {
- code_byte(SETLINE); code_word(lua_linenumber);
- }
- }
- }
- ;
- dimension : /* empty */ { code_byte(PUSHNIL); incr_ntemp();}
- | expr1
- ;
-
- functioncall : functionvalue
- {
- code_byte(PUSHMARK); $<vInt>$ = ntemp; incr_ntemp();
- if ($1 != 0) lua_pushvar($1);
- }
- '(' exprlist ')' { code_byte(CALLFUNC); ntemp = $<vInt>2-1;}
- functionvalue : var {lua_pushvar ($1); $$ = 0; }
- | singlevar ':' NAME
- {
- $$ = $1;
- lua_pushvar($1);
- code_byte(PUSHSTRING);
- code_word(lua_findconstant($3));
- incr_ntemp();
- lua_pushvar(0);
- }
- ;
-
- exprlist : /* empty */ { $$ = 1; }
- | exprlist1 { $$ = $1; }
- ;
-
- exprlist1 : expr { $$ = $1; }
- | exprlist1 ',' {if (!$1){lua_codeadjust (ntemp+1); incr_ntemp();}}
- expr {$$ = $4;}
- ;
- parlist : /* empty */
- | parlist1
- ;
-
- parlist1 : NAME
- {
- localvar[nlocalvar]=lua_findsymbol($1);
- add_nlocalvar(1);
- }
- | parlist1 ',' NAME
- {
- localvar[nlocalvar]=lua_findsymbol($3);
- add_nlocalvar(1);
- }
- ;
-
- objectname : /* empty */ {$$=-1;}
- | NAME {$$=lua_findsymbol($1);}
- ;
-
- fieldlist : '{' ffieldlist '}'
- {
- flush_record($2%FIELDS_PER_FLUSH);
- $$ = $2;
- }
- | '[' lfieldlist ']'
- {
- flush_list($2/FIELDS_PER_FLUSH, $2%FIELDS_PER_FLUSH);
- $$ = $2;
- }
- ;
- ffieldlist : /* empty */ { $$ = 0; }
- | ffieldlist1 { $$ = $1; }
- ;
- ffieldlist1 : ffield {$$=1;}
- | ffieldlist1 ',' ffield
- {
- $$=$1+1;
- if ($$%FIELDS_PER_FLUSH == 0) flush_record(FIELDS_PER_FLUSH);
- }
- ;
- ffield : NAME {$<vWord>$ = lua_findconstant($1);} '=' expr1
- {
- push_field($<vWord>2);
- }
- ;
- lfieldlist : /* empty */ { $$ = 0; }
- | lfieldlist1 { $$ = $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;
- varbuffer[nvarbuffer] = $1; incr_nvarbuffer();
- $$ = ($1 == 0) ? 1 : 0;
- }
- | varlist1 ',' var
- {
- varbuffer[nvarbuffer] = $3; incr_nvarbuffer();
- $$ = ($3 == 0) ? $1 + 1 : $1;
- }
- ;
-
- var : singlevar { $$ = $1; }
- | var {lua_pushvar ($1);} '[' expr1 ']'
- {
- $$ = 0; /* indexed variable */
- }
- | var {lua_pushvar ($1);} '.' NAME
- {
- code_byte(PUSHSTRING);
- code_word(lua_findconstant($4)); incr_ntemp();
- $$ = 0; /* indexed variable */
- }
- ;
-
- singlevar : NAME
- {
- Word s = lua_findsymbol($1);
- int local = lua_localname (s);
- if (local == -1) /* global var */
- $$ = s + 1; /* return positive value */
- else
- $$ = -(local+1); /* return negative value */
- }
- ;
-
- localdeclist : NAME {localvar[nlocalvar]=lua_findsymbol($1); $$ = 1;}
- | localdeclist ',' NAME
- {
- localvar[nlocalvar+$1]=lua_findsymbol($3);
- $$ = $1+1;
- }
- ;
-
- decinit : /* empty */
- | '=' exprlist1
- ;
-
- setdebug : DEBUG {lua_debug = $1;}
- %%
- /*
- ** Search a local name and if find return its index. If do not find return -1
- */
- static int lua_localname (Word 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);
- incr_ntemp();
- }
- else if (number < 0) /* local var */
- {
- number = (-number) - 1;
- if (number < 10) code_byte(PUSHLOCAL0 + number);
- else
- {
- code_byte(PUSHLOCAL);
- code_byte(number);
- }
- incr_ntemp();
- }
- else
- {
- code_byte(PUSHINDEXED);
- ntemp--;
- }
- }
- static void lua_codeadjust (int n)
- {
- code_byte(ADJUST);
- code_byte(n + nlocalvar);
- }
- static void lua_codestore (int i)
- {
- if (varbuffer[i] > 0) /* global var */
- {
- code_byte(STOREGLOBAL);
- code_word(varbuffer[i]-1);
- }
- else if (varbuffer[i] < 0) /* local var */
- {
- int number = (-varbuffer[i]) - 1;
- if (number < 10) code_byte(STORELOCAL0 + number);
- else
- {
- code_byte(STORELOCAL);
- code_byte(number);
- }
- }
- 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);
- }
- }
- }
- void yyerror (char *s)
- {
- static char msg[256];
- sprintf (msg,"%s near \"%s\" at line %d in file \"%s\"",
- s, lua_lasttext (), lua_linenumber, lua_filename());
- lua_error (msg);
- err = 1;
- }
- int yywrap (void)
- {
- return 1;
- }
- /*
- ** Parse LUA code and execute global statement.
- ** Return 0 on success or 1 on error.
- */
- int lua_parse (void)
- {
- Byte *init = initcode = (Byte *) calloc(CODE_BLOCK, sizeof(Byte));
- maincode = 0;
- maxmain = CODE_BLOCK;
- if (init == NULL)
- {
- lua_error("not enough memory");
- return 1;
- }
- err = 0;
- if (yyparse () || (err==1)) return 1;
- initcode[maincode++] = HALT;
- init = initcode;
- #if LISTING
- PrintCode(init,init+maincode);
- #endif
- if (lua_execute (init)) return 1;
- free(init);
- return 0;
- }
- #if LISTING
- static void PrintCode (Byte *code, Byte *end)
- {
- Byte *p = code;
- printf ("\n\nCODE\n");
- while (p != end)
- {
- switch ((OpCode)*p)
- {
- case PUSHNIL: printf ("%d PUSHNIL\n", (p++)-code); break;
- case PUSH0: case PUSH1: case PUSH2:
- printf ("%d PUSH%c\n", p-code, *p-PUSH0+'0');
- p++;
- break;
- case PUSHBYTE:
- printf ("%d PUSHBYTE %d\n", p-code, *(++p));
- p++;
- break;
- case PUSHWORD:
- {
- CodeWord c;
- int n = p-code;
- p++;
- get_word(c,p);
- printf ("%d PUSHWORD %d\n", n, c.w);
- }
- break;
- case PUSHFLOAT:
- {
- CodeFloat c;
- int n = p-code;
- p++;
- get_float(c,p);
- printf ("%d PUSHFLOAT %f\n", n, c.f);
- }
- break;
- case PUSHSTRING:
- {
- CodeWord c;
- int n = p-code;
- p++;
- get_word(c,p);
- printf ("%d PUSHSTRING %d\n", n, c.w);
- }
- break;
- case PUSHFUNCTION:
- {
- CodeCode c;
- int n = p-code;
- p++;
- get_code(c,p);
- printf ("%d PUSHFUNCTION %p\n", n, c.b);
- }
- break;
- case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2: case PUSHLOCAL3:
- case PUSHLOCAL4: case PUSHLOCAL5: case PUSHLOCAL6: case PUSHLOCAL7:
- case PUSHLOCAL8: case PUSHLOCAL9:
- printf ("%d PUSHLOCAL%c\n", p-code, *p-PUSHLOCAL0+'0');
- p++;
- break;
- case PUSHLOCAL: printf ("%d PUSHLOCAL %d\n", p-code, *(++p));
- p++;
- break;
- case PUSHGLOBAL:
- {
- CodeWord c;
- int n = p-code;
- p++;
- get_word(c,p);
- printf ("%d PUSHGLOBAL %d\n", n, c.w);
- }
- break;
- case PUSHINDEXED: printf ("%d PUSHINDEXED\n", (p++)-code); break;
- case PUSHMARK: printf ("%d PUSHMARK\n", (p++)-code); break;
- case PUSHOBJECT: printf ("%d PUSHOBJECT\n", (p++)-code); break;
- case STORELOCAL0: case STORELOCAL1: case STORELOCAL2: case STORELOCAL3:
- case STORELOCAL4: case STORELOCAL5: case STORELOCAL6: case STORELOCAL7:
- case STORELOCAL8: case STORELOCAL9:
- printf ("%d STORELOCAL%c\n", p-code, *p-STORELOCAL0+'0');
- p++;
- break;
- case STORELOCAL:
- printf ("%d STORELOCAL %d\n", p-code, *(++p));
- p++;
- break;
- case STOREGLOBAL:
- {
- CodeWord c;
- int n = p-code;
- p++;
- get_word(c,p);
- printf ("%d STOREGLOBAL %d\n", n, c.w);
- }
- break;
- case STOREINDEXED0: printf ("%d STOREINDEXED0\n", (p++)-code); break;
- case STOREINDEXED: printf ("%d STOREINDEXED %d\n", p-code, *(++p));
- p++;
- break;
- case STORELIST0:
- printf("%d STORELIST0 %d\n", p-code, *(++p));
- p++;
- break;
- case STORELIST:
- printf("%d STORELIST %d %d\n", p-code, *(p+1), *(p+2));
- p+=3;
- break;
- case STORERECORD:
- printf("%d STORERECORD %d\n", p-code, *(++p));
- p += *p*sizeof(Word) + 1;
- break;
- case ADJUST:
- printf ("%d ADJUST %d\n", p-code, *(++p));
- p++;
- break;
- case CREATEARRAY: printf ("%d CREATEARRAY\n", (p++)-code); break;
- case EQOP: printf ("%d EQOP\n", (p++)-code); break;
- case LTOP: printf ("%d LTOP\n", (p++)-code); break;
- case LEOP: printf ("%d LEOP\n", (p++)-code); break;
- case ADDOP: printf ("%d ADDOP\n", (p++)-code); break;
- case SUBOP: printf ("%d SUBOP\n", (p++)-code); break;
- case MULTOP: printf ("%d MULTOP\n", (p++)-code); break;
- case DIVOP: printf ("%d DIVOP\n", (p++)-code); break;
- case CONCOP: printf ("%d CONCOP\n", (p++)-code); break;
- case MINUSOP: printf ("%d MINUSOP\n", (p++)-code); break;
- case NOTOP: printf ("%d NOTOP\n", (p++)-code); break;
- case ONTJMP:
- {
- CodeWord c;
- int n = p-code;
- p++;
- get_word(c,p);
- printf ("%d ONTJMP %d\n", n, c.w);
- }
- break;
- case ONFJMP:
- {
- CodeWord c;
- int n = p-code;
- p++;
- get_word(c,p);
- printf ("%d ONFJMP %d\n", n, c.w);
- }
- break;
- case JMP:
- {
- CodeWord c;
- int n = p-code;
- p++;
- get_word(c,p);
- printf ("%d JMP %d\n", n, c.w);
- }
- break;
- case UPJMP:
- {
- CodeWord c;
- int n = p-code;
- p++;
- get_word(c,p);
- printf ("%d UPJMP %d\n", n, c.w);
- }
- break;
- case IFFJMP:
- {
- CodeWord c;
- int n = p-code;
- p++;
- get_word(c,p);
- printf ("%d IFFJMP %d\n", n, c.w);
- }
- break;
- case IFFUPJMP:
- {
- CodeWord c;
- int n = p-code;
- p++;
- get_word(c,p);
- printf ("%d IFFUPJMP %d\n", n, c.w);
- }
- break;
- case POP: printf ("%d POP\n", (p++)-code); break;
- case CALLFUNC: printf ("%d CALLFUNC\n", (p++)-code); break;
- case RETCODE:
- printf ("%d RETCODE %d\n", p-code, *(++p));
- p++;
- break;
- case HALT: printf ("%d HALT\n", (p++)-code); break;
- case SETFUNCTION:
- {
- CodeWord c1, c2;
- int n = p-code;
- p++;
- get_word(c1,p);
- get_word(c2,p);
- printf ("%d SETFUNCTION %d %d\n", n, c1.w, c2.w);
- }
- break;
- case SETLINE:
- {
- CodeWord c;
- int n = p-code;
- p++;
- get_word(c,p);
- printf ("%d SETLINE %d\n", n, c.w);
- }
- break;
- case RESET: printf ("%d RESET\n", (p++)-code); break;
- default: printf ("%d Cannot happen: code %d\n", (p++)-code, *(p-1)); break;
- }
- }
- }
- #endif
|