diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1994-11-10 15:11:52 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1994-11-10 15:11:52 -0200 |
commit | 9deac27704eee47f858f6b41a386c3198bc49587 (patch) | |
tree | 27f28b2953f4cd9e2ee60a2ae7067fadb21b8a00 | |
parent | d531ccd082a73aa2fda585dfe5edf2749c7e7d13 (diff) | |
download | lua-9deac27704eee47f858f6b41a386c3198bc49587.tar.gz lua-9deac27704eee47f858f6b41a386c3198bc49587.tar.bz2 lua-9deac27704eee47f858f6b41a386c3198bc49587.zip |
fallback list moved from opcode.c to fallback.c
-rw-r--r-- | fallback.c | 42 | ||||
-rw-r--r-- | fallback.h | 17 | ||||
-rw-r--r-- | opcode.c | 74 | ||||
-rw-r--r-- | opcode.h | 3 |
4 files changed, 69 insertions, 67 deletions
@@ -3,7 +3,7 @@ | |||
3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_fallback="$Id: fallback.c,v 1.2 1994/11/08 19:56:39 roberto Exp roberto $"; | 6 | char *rcs_fallback="$Id: fallback.c,v 1.3 1994/11/09 18:12:42 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <stdio.h> | 8 | #include <stdio.h> |
9 | #include <stdlib.h> | 9 | #include <stdlib.h> |
@@ -14,6 +14,46 @@ char *rcs_fallback="$Id: fallback.c,v 1.2 1994/11/08 19:56:39 roberto Exp robert | |||
14 | #include "lua.h" | 14 | #include "lua.h" |
15 | 15 | ||
16 | 16 | ||
17 | /* | ||
18 | ** Warning: This list must be in the same order as the #define's | ||
19 | */ | ||
20 | struct FB luaI_fallBacks[] = { | ||
21 | {"error", {LUA_T_CFUNCTION, luaI_errorFB}}, | ||
22 | {"index", {LUA_T_CFUNCTION, luaI_indexFB}}, | ||
23 | {"gettable", {LUA_T_CFUNCTION, luaI_gettableFB}}, | ||
24 | {"arith", {LUA_T_CFUNCTION, luaI_arithFB}}, | ||
25 | {"order", {LUA_T_CFUNCTION, luaI_orderFB}}, | ||
26 | {"concat", {LUA_T_CFUNCTION, luaI_concatFB}}, | ||
27 | {"unminus", {LUA_T_CFUNCTION, luaI_arithFB}}, | ||
28 | {"settable", {LUA_T_CFUNCTION, luaI_gettableFB}} | ||
29 | }; | ||
30 | |||
31 | #define N_FB (sizeof(luaI_fallBacks)/sizeof(struct FB)) | ||
32 | |||
33 | void luaI_setfallback (void) | ||
34 | { | ||
35 | int i; | ||
36 | char *name = lua_getstring(lua_getparam(1)); | ||
37 | lua_Object func = lua_getparam(2); | ||
38 | if (name == NULL || !(lua_isfunction(func) || lua_iscfunction(func))) | ||
39 | { | ||
40 | lua_pushnil(); | ||
41 | return; | ||
42 | } | ||
43 | for (i=0; i<N_FB; i++) | ||
44 | { | ||
45 | if (strcmp(luaI_fallBacks[i].kind, name) == 0) | ||
46 | { | ||
47 | luaI_pushobject(&luaI_fallBacks[i].function); | ||
48 | luaI_fallBacks[i].function = *luaI_Address(func); | ||
49 | return; | ||
50 | } | ||
51 | } | ||
52 | /* name not found */ | ||
53 | lua_pushnil(); | ||
54 | } | ||
55 | |||
56 | |||
17 | void luaI_errorFB (void) | 57 | void luaI_errorFB (void) |
18 | { | 58 | { |
19 | lua_Object o = lua_getparam(1); | 59 | lua_Object o = lua_getparam(1); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: fallback.h,v 1.1 1994/11/07 15:20:56 roberto Exp roberto $ | 2 | ** $Id: fallback.h,v 1.2 1994/11/08 19:56:39 roberto Exp roberto $ |
3 | */ | 3 | */ |
4 | 4 | ||
5 | #ifndef fallback_h | 5 | #ifndef fallback_h |
@@ -7,6 +7,21 @@ | |||
7 | 7 | ||
8 | #include "opcode.h" | 8 | #include "opcode.h" |
9 | 9 | ||
10 | extern struct FB { | ||
11 | char *kind; | ||
12 | Object function; | ||
13 | } luaI_fallBacks[]; | ||
14 | |||
15 | #define FB_ERROR 0 | ||
16 | #define FB_INDEX 1 | ||
17 | #define FB_GETTABLE 2 | ||
18 | #define FB_ARITH 3 | ||
19 | #define FB_ORDER 4 | ||
20 | #define FB_CONCAT 5 | ||
21 | #define FB_UNMINUS 6 | ||
22 | #define FB_SETTABLE 7 | ||
23 | |||
24 | void luaI_setfallback (void); | ||
10 | void luaI_errorFB (void); | 25 | void luaI_errorFB (void); |
11 | void luaI_indexFB (void); | 26 | void luaI_indexFB (void); |
12 | void luaI_gettableFB (void); | 27 | void luaI_gettableFB (void); |
@@ -3,7 +3,7 @@ | |||
3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_opcode="$Id: opcode.c,v 3.6 1994/11/08 19:56:39 roberto Exp roberto $"; | 6 | char *rcs_opcode="$Id: opcode.c,v 3.7 1994/11/09 18:13:29 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <stdio.h> | 8 | #include <stdio.h> |
9 | #include <stdlib.h> | 9 | #include <stdlib.h> |
@@ -50,6 +50,7 @@ static int lua_execute (Byte *pc, int base); | |||
50 | static void do_call (Object *func, int base, int nResults, int whereRes); | 50 | static void do_call (Object *func, int base, int nResults, int whereRes); |
51 | 51 | ||
52 | 52 | ||
53 | |||
53 | Object *luaI_Address (lua_Object o) | 54 | Object *luaI_Address (lua_Object o) |
54 | { | 55 | { |
55 | return Address(o); | 56 | return Address(o); |
@@ -57,66 +58,13 @@ Object *luaI_Address (lua_Object o) | |||
57 | 58 | ||
58 | 59 | ||
59 | /* | 60 | /* |
60 | ** Fallbacks | ||
61 | */ | ||
62 | |||
63 | static struct FB { | ||
64 | char *kind; | ||
65 | Object function; | ||
66 | } fallBacks[] = { | ||
67 | #define FB_ERROR 0 | ||
68 | {"error", {LUA_T_CFUNCTION, luaI_errorFB}}, | ||
69 | #define FB_INDEX 1 | ||
70 | {"index", {LUA_T_CFUNCTION, luaI_indexFB}}, | ||
71 | #define FB_GETTABLE 2 | ||
72 | {"gettable", {LUA_T_CFUNCTION, luaI_gettableFB}}, | ||
73 | #define FB_ARITH 3 | ||
74 | {"arith", {LUA_T_CFUNCTION, luaI_arithFB}}, | ||
75 | #define FB_ORDER 4 | ||
76 | {"order", {LUA_T_CFUNCTION, luaI_orderFB}}, | ||
77 | #define FB_CONCAT 5 | ||
78 | {"concat", {LUA_T_CFUNCTION, luaI_concatFB}}, | ||
79 | #define FB_UNMINUS 6 | ||
80 | {"unminus", {LUA_T_CFUNCTION, luaI_arithFB}}, | ||
81 | #define FB_SETTABLE 7 | ||
82 | {"settable", {LUA_T_CFUNCTION, luaI_gettableFB}} | ||
83 | }; | ||
84 | |||
85 | #define N_FB (sizeof(fallBacks)/sizeof(struct FB)) | ||
86 | |||
87 | |||
88 | void luaI_setfallback (void) | ||
89 | { | ||
90 | int i; | ||
91 | char *name = lua_getstring(lua_getparam(1)); | ||
92 | lua_Object func = lua_getparam(2); | ||
93 | if (name == NULL || !(lua_isfunction(func) || lua_iscfunction(func))) | ||
94 | { | ||
95 | lua_pushnil(); | ||
96 | return; | ||
97 | } | ||
98 | for (i=0; i<N_FB; i++) | ||
99 | { | ||
100 | if (strcmp(fallBacks[i].kind, name) == 0) | ||
101 | { | ||
102 | luaI_pushobject(&fallBacks[i].function); | ||
103 | fallBacks[i].function = *Address(func); | ||
104 | return; | ||
105 | } | ||
106 | } | ||
107 | /* name not found */ | ||
108 | lua_pushnil(); | ||
109 | } | ||
110 | |||
111 | |||
112 | /* | ||
113 | ** Error messages | 61 | ** Error messages |
114 | */ | 62 | */ |
115 | 63 | ||
116 | static void lua_message (char *s) | 64 | static void lua_message (char *s) |
117 | { | 65 | { |
118 | lua_pushstring(s); | 66 | lua_pushstring(s); |
119 | do_call(&fallBacks[FB_ERROR].function, (top-stack)-1, 0, (top-stack)-1); | 67 | do_call(&luaI_fallBacks[FB_ERROR].function, (top-stack)-1, 0, (top-stack)-1); |
120 | } | 68 | } |
121 | 69 | ||
122 | /* | 70 | /* |
@@ -311,12 +259,12 @@ static void do_call (Object *func, int base, int nResults, int whereRes) | |||
311 | static void pushsubscript (void) | 259 | static void pushsubscript (void) |
312 | { | 260 | { |
313 | if (tag(top-2) != LUA_T_ARRAY) | 261 | if (tag(top-2) != LUA_T_ARRAY) |
314 | do_call(&fallBacks[FB_GETTABLE].function, (top-stack)-2, 1, (top-stack)-2); | 262 | do_call(&luaI_fallBacks[FB_GETTABLE].function, (top-stack)-2, 1, (top-stack)-2); |
315 | else | 263 | else |
316 | { | 264 | { |
317 | Object *h = lua_hashget(avalue(top-2), top-1); | 265 | Object *h = lua_hashget(avalue(top-2), top-1); |
318 | if (h == NULL) | 266 | if (h == NULL) |
319 | do_call(&fallBacks[FB_INDEX].function, (top-stack)-2, 1, (top-stack)-2); | 267 | do_call(&luaI_fallBacks[FB_INDEX].function, (top-stack)-2, 1, (top-stack)-2); |
320 | else | 268 | else |
321 | { | 269 | { |
322 | --top; | 270 | --top; |
@@ -332,7 +280,7 @@ static void pushsubscript (void) | |||
332 | static void storesubscript (void) | 280 | static void storesubscript (void) |
333 | { | 281 | { |
334 | if (tag(top-3) != LUA_T_ARRAY) | 282 | if (tag(top-3) != LUA_T_ARRAY) |
335 | do_call(&fallBacks[FB_SETTABLE].function, (top-stack)-3, 0, (top-stack)-3); | 283 | do_call(&luaI_fallBacks[FB_SETTABLE].function, (top-stack)-3, 0, (top-stack)-3); |
336 | else | 284 | else |
337 | { | 285 | { |
338 | Object *h = lua_hashdefine (avalue(top-3), top-2); | 286 | Object *h = lua_hashdefine (avalue(top-3), top-2); |
@@ -688,7 +636,7 @@ int lua_type (lua_Object o) | |||
688 | static void call_arith (char *op) | 636 | static void call_arith (char *op) |
689 | { | 637 | { |
690 | lua_pushstring(op); | 638 | lua_pushstring(op); |
691 | do_call(&fallBacks[FB_ARITH].function, (top-stack)-3, 1, (top-stack)-3); | 639 | do_call(&luaI_fallBacks[FB_ARITH].function, (top-stack)-3, 1, (top-stack)-3); |
692 | } | 640 | } |
693 | 641 | ||
694 | static void comparison (lua_Type tag_less, lua_Type tag_equal, | 642 | static void comparison (lua_Type tag_less, lua_Type tag_equal, |
@@ -702,7 +650,7 @@ static void comparison (lua_Type tag_less, lua_Type tag_equal, | |||
702 | else if (tostring(l) || tostring(r)) | 650 | else if (tostring(l) || tostring(r)) |
703 | { | 651 | { |
704 | lua_pushstring(op); | 652 | lua_pushstring(op); |
705 | do_call(&fallBacks[FB_ORDER].function, (top-stack)-3, 1, (top-stack)-3); | 653 | do_call(&luaI_fallBacks[FB_ORDER].function, (top-stack)-3, 1, (top-stack)-3); |
706 | return; | 654 | return; |
707 | } | 655 | } |
708 | else | 656 | else |
@@ -824,7 +772,7 @@ static int lua_execute (Byte *pc, int base) | |||
824 | *(top) = *(top-2-n); | 772 | *(top) = *(top-2-n); |
825 | *(top-1) = *(top-3-n); | 773 | *(top-1) = *(top-3-n); |
826 | top += 2; | 774 | top += 2; |
827 | do_call(&fallBacks[FB_SETTABLE].function, (top-stack)-3, 0, (top-stack)-3); | 775 | do_call(&luaI_fallBacks[FB_SETTABLE].function, (top-stack)-3, 0, (top-stack)-3); |
828 | } | 776 | } |
829 | else | 777 | else |
830 | { | 778 | { |
@@ -1016,7 +964,7 @@ static int lua_execute (Byte *pc, int base) | |||
1016 | Object *l = top-2; | 964 | Object *l = top-2; |
1017 | Object *r = top-1; | 965 | Object *r = top-1; |
1018 | if (tostring(r) || tostring(l)) | 966 | if (tostring(r) || tostring(l)) |
1019 | do_call(&fallBacks[FB_CONCAT].function, (top-stack)-2, 1, (top-stack)-2); | 967 | do_call(&luaI_fallBacks[FB_CONCAT].function, (top-stack)-2, 1, (top-stack)-2); |
1020 | else | 968 | else |
1021 | { | 969 | { |
1022 | svalue(l) = lua_createstring (lua_strconc(svalue(l),svalue(r))); | 970 | svalue(l) = lua_createstring (lua_strconc(svalue(l),svalue(r))); |
@@ -1027,7 +975,7 @@ static int lua_execute (Byte *pc, int base) | |||
1027 | 975 | ||
1028 | case MINUSOP: | 976 | case MINUSOP: |
1029 | if (tonumber(top-1)) | 977 | if (tonumber(top-1)) |
1030 | do_call(&fallBacks[FB_UNMINUS].function, (top-stack)-1, 1, (top-stack)-1); | 978 | do_call(&luaI_fallBacks[FB_UNMINUS].function, (top-stack)-1, 1, (top-stack)-1); |
1031 | else | 979 | else |
1032 | nvalue(top-1) = - nvalue(top-1); | 980 | nvalue(top-1) = - nvalue(top-1); |
1033 | break; | 981 | break; |
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | ** TeCGraf - PUC-Rio | 2 | ** TeCGraf - PUC-Rio |
3 | ** $Id: opcode.h,v 3.5 1994/11/07 16:34:44 roberto Exp roberto $ | 3 | ** $Id: opcode.h,v 3.6 1994/11/09 18:10:58 roberto Exp roberto $ |
4 | */ | 4 | */ |
5 | 5 | ||
6 | #ifndef opcode_h | 6 | #ifndef opcode_h |
@@ -160,7 +160,6 @@ char *lua_lasttext (void); /* from "lex.c" module */ | |||
160 | int yylex (void); /* from "lex.c" module */ | 160 | int yylex (void); /* from "lex.c" module */ |
161 | void lua_parse (Byte **code); /* from "lua.stx" module */ | 161 | void lua_parse (Byte **code); /* from "lua.stx" module */ |
162 | void lua_travstack (void (*fn)(Object *)); | 162 | void lua_travstack (void (*fn)(Object *)); |
163 | void luaI_setfallback (void); | ||
164 | Object *luaI_Address (lua_Object o); | 163 | Object *luaI_Address (lua_Object o); |
165 | void luaI_pushobject (Object *o); | 164 | void luaI_pushobject (Object *o); |
166 | 165 | ||