diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-05-28 18:07:32 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-05-28 18:07:32 -0300 |
| commit | 9863223fbf512d903a1677c861e4beb4f8feda4d (patch) | |
| tree | 964f1b36a5a7c9aff238d454fa4bffe88dacbe2d /lua.stx | |
| parent | 9a1948e67d940d988260e738f2a251087835a318 (diff) | |
| download | lua-9863223fbf512d903a1677c861e4beb4f8feda4d.tar.gz lua-9863223fbf512d903a1677c861e4beb4f8feda4d.tar.bz2 lua-9863223fbf512d903a1677c861e4beb4f8feda4d.zip | |
first version of vararg facility (plus new function "call").
Diffstat (limited to 'lua.stx')
| -rw-r--r-- | lua.stx | 81 |
1 files changed, 50 insertions, 31 deletions
| @@ -1,9 +1,10 @@ | |||
| 1 | %{ | 1 | %{ |
| 2 | 2 | ||
| 3 | char *rcs_luastx = "$Id: lua.stx,v 3.35 1996/03/08 12:02:37 roberto Exp roberto $"; | 3 | char *rcs_luastx = "$Id: lua.stx,v 3.36 1996/03/21 16:31:32 roberto Exp roberto $"; |
| 4 | 4 | ||
| 5 | #include <stdio.h> | 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> | 6 | #include <stdlib.h> |
| 7 | #include <string.h> | ||
| 7 | 8 | ||
| 8 | #include "luadebug.h" | 9 | #include "luadebug.h" |
| 9 | #include "mem.h" | 10 | #include "mem.h" |
| @@ -67,49 +68,45 @@ static void yyerror (char *s) | |||
| 67 | lua_error (msg); | 68 | lua_error (msg); |
| 68 | } | 69 | } |
| 69 | 70 | ||
| 71 | static void check_space (int i) | ||
| 72 | { | ||
| 73 | if (pc+i>maxcurr-1) /* 1 byte free to code HALT of main code */ | ||
| 74 | maxcurr = growvector(&basepc, maxcurr, Byte, codeEM, MAX_INT); | ||
| 75 | } | ||
| 76 | |||
| 70 | static void code_byte (Byte c) | 77 | static void code_byte (Byte c) |
| 71 | { | 78 | { |
| 72 | if (pc>maxcurr-2) /* 1 byte free to code HALT of main code */ | 79 | check_space(1); |
| 73 | maxcurr = growvector(&basepc, maxcurr, Byte, codeEM, MAX_INT); | ||
| 74 | basepc[pc++] = c; | 80 | basepc[pc++] = c; |
| 75 | } | 81 | } |
| 76 | 82 | ||
| 77 | static void code_word (Word n) | 83 | static void code_word (Word n) |
| 78 | { | 84 | { |
| 79 | CodeWord code; | 85 | check_space(sizeof(Word)); |
| 80 | code.w = n; | 86 | memcpy(basepc+pc, &n, sizeof(Word)); |
| 81 | code_byte(code.m.c1); | 87 | pc += sizeof(Word); |
| 82 | code_byte(code.m.c2); | ||
| 83 | } | 88 | } |
| 84 | 89 | ||
| 85 | static void code_float (float n) | 90 | static void code_float (real n) |
| 86 | { | 91 | { |
| 87 | CodeFloat code; | 92 | check_space(sizeof(real)); |
| 88 | code.f = n; | 93 | memcpy(basepc+pc, &n, sizeof(real)); |
| 89 | code_byte(code.m.c1); | 94 | pc += sizeof(real); |
| 90 | code_byte(code.m.c2); | ||
| 91 | code_byte(code.m.c3); | ||
| 92 | code_byte(code.m.c4); | ||
| 93 | } | 95 | } |
| 94 | 96 | ||
| 95 | static void code_code (TFunc *tf) | 97 | static void code_code (TFunc *tf) |
| 96 | { | 98 | { |
| 97 | CodeCode code; | 99 | check_space(sizeof(TFunc *)); |
| 98 | code.tf = tf; | 100 | memcpy(basepc+pc, &tf, sizeof(TFunc *)); |
| 99 | code_byte(code.m.c1); | 101 | pc += sizeof(TFunc *); |
| 100 | code_byte(code.m.c2); | ||
| 101 | code_byte(code.m.c3); | ||
| 102 | code_byte(code.m.c4); | ||
| 103 | } | 102 | } |
| 104 | 103 | ||
| 105 | static void code_word_at (Byte *p, int n) | 104 | static void code_word_at (Byte *p, int n) |
| 106 | { | 105 | { |
| 107 | CodeWord code; | 106 | Word w = n; |
| 108 | if ((Word)n != n) | 107 | if (w != n) |
| 109 | yyerror("block too big"); | 108 | yyerror("block too big"); |
| 110 | code.w = (Word)n; | 109 | memcpy(p, &w, sizeof(Word)); |
| 111 | *p++ = code.m.c1; | ||
| 112 | *p++ = code.m.c2; | ||
| 113 | } | 110 | } |
| 114 | 111 | ||
| 115 | static void push_field (Word name) | 112 | static void push_field (Word name) |
| @@ -322,6 +319,19 @@ static void adjust_mult_assign (int vars, Long exps, int temps) | |||
| 322 | lua_codeadjust(temps); | 319 | lua_codeadjust(temps); |
| 323 | } | 320 | } |
| 324 | 321 | ||
| 322 | static int close_parlist (int dots) | ||
| 323 | { | ||
| 324 | if (!dots) | ||
| 325 | lua_codeadjust(0); | ||
| 326 | else | ||
| 327 | { | ||
| 328 | code_byte(VARARGS); | ||
| 329 | code_byte(nlocalvar); | ||
| 330 | add_localvar(luaI_createfixedstring("arg")); | ||
| 331 | } | ||
| 332 | return lua_linenumber; | ||
| 333 | } | ||
| 334 | |||
| 325 | static void storesinglevar (Long v) | 335 | static void storesinglevar (Long v) |
| 326 | { | 336 | { |
| 327 | if (v > 0) /* global var */ | 337 | if (v > 0) /* global var */ |
| @@ -426,6 +436,7 @@ void lua_parse (TFunc *tf) | |||
| 426 | %token RETURN | 436 | %token RETURN |
| 427 | %token LOCAL | 437 | %token LOCAL |
| 428 | %token FUNCTION | 438 | %token FUNCTION |
| 439 | %token DOTS | ||
| 429 | %token <vFloat> NUMBER | 440 | %token <vFloat> NUMBER |
| 430 | %token <vWord> STRING | 441 | %token <vWord> STRING |
| 431 | %token <pTStr> NAME | 442 | %token <pTStr> NAME |
| @@ -440,7 +451,7 @@ void lua_parse (TFunc *tf) | |||
| 440 | %type <vInt> fieldlist, localdeclist, decinit | 451 | %type <vInt> fieldlist, localdeclist, decinit |
| 441 | %type <vInt> ffieldlist, ffieldlist1, semicolonpart | 452 | %type <vInt> ffieldlist, ffieldlist1, semicolonpart |
| 442 | %type <vInt> lfieldlist, lfieldlist1 | 453 | %type <vInt> lfieldlist, lfieldlist1 |
| 443 | %type <vInt> parlist | 454 | %type <vInt> parlist, parlist1, par |
| 444 | %type <vLong> var, singlevar, funcname | 455 | %type <vLong> var, singlevar, funcname |
| 445 | %type <pFunc> body | 456 | %type <pFunc> body |
| 446 | 457 | ||
| @@ -674,13 +685,21 @@ exprlist1 : expr { if ($1 != 0) $$ = $1; else $$ = -1; } | |||
| 674 | } | 685 | } |
| 675 | ; | 686 | ; |
| 676 | 687 | ||
| 677 | parlist : /* empty */ { lua_codeadjust(0); $$ = lua_linenumber; } | 688 | parlist : /* empty */ { $$ = close_parlist(0); } |
| 678 | | parlist1 { lua_codeadjust(0); $$ = lua_linenumber; } | 689 | | parlist1 { $$ = close_parlist($1); } |
| 679 | ; | 690 | ; |
| 680 | 691 | ||
| 681 | parlist1 : NAME { add_localvar($1); } | 692 | parlist1 : par { $$ = $1; } |
| 682 | | parlist1 ',' NAME { add_localvar($3); } | 693 | | parlist1 ',' par |
| 694 | { | ||
| 695 | if ($1) | ||
| 696 | lua_error("invalid parameter list"); | ||
| 697 | $$ = $3; | ||
| 698 | } | ||
| 683 | ; | 699 | ; |
| 700 | |||
| 701 | par : NAME { add_localvar($1); $$ = 0; } | ||
| 702 | | DOTS { $$ = 1; } | ||
| 684 | 703 | ||
| 685 | fieldlist : lfieldlist | 704 | fieldlist : lfieldlist |
| 686 | { flush_list($1/FIELDS_PER_FLUSH, $1%FIELDS_PER_FLUSH); } | 705 | { flush_list($1/FIELDS_PER_FLUSH, $1%FIELDS_PER_FLUSH); } |
