diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1994-11-17 17:43:34 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1994-11-17 17:43:34 -0200 |
commit | a84aa11f71be730d554aa208d2b40ad28f2c9e05 (patch) | |
tree | 1d8a0b99fc8df1f87379e5b2f00d558ac39b9d87 | |
parent | 9bee23fd0550e33b2a3f9c8d1b53506b59407e5c (diff) | |
download | lua-a84aa11f71be730d554aa208d2b40ad28f2c9e05.tar.gz lua-a84aa11f71be730d554aa208d2b40ad28f2c9e05.tar.bz2 lua-a84aa11f71be730d554aa208d2b40ad28f2c9e05.zip |
pow operation is defined in mathlib.c
-rw-r--r-- | mathlib.c | 27 | ||||
-rw-r--r-- | opcode.c | 34 |
2 files changed, 31 insertions, 30 deletions
@@ -3,7 +3,7 @@ | |||
3 | ** Mathematics library to LUA | 3 | ** Mathematics library to LUA |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_mathlib="$Id: mathlib.c,v 1.3 1994/08/15 14:13:44 celes Exp celes $"; | 6 | char *rcs_mathlib="$Id: mathlib.c,v 1.4 1994/10/11 13:06:47 celes Exp roberto $"; |
7 | 7 | ||
8 | #include <stdio.h> /* NULL */ | 8 | #include <stdio.h> /* NULL */ |
9 | #include <math.h> | 9 | #include <math.h> |
@@ -160,16 +160,27 @@ static void math_sqrt (void) | |||
160 | lua_pushnumber (sqrt(d)); | 160 | lua_pushnumber (sqrt(d)); |
161 | } | 161 | } |
162 | 162 | ||
163 | static int old_pow; | ||
164 | |||
163 | static void math_pow (void) | 165 | static void math_pow (void) |
164 | { | 166 | { |
165 | double d1, d2; | ||
166 | lua_Object o1 = lua_getparam (1); | 167 | lua_Object o1 = lua_getparam (1); |
167 | lua_Object o2 = lua_getparam (2); | 168 | lua_Object o2 = lua_getparam (2); |
168 | if (!lua_isnumber(o1) || !lua_isnumber(o2)) | 169 | lua_Object op = lua_getparam(3); |
169 | { lua_error ("incorrect arguments to function `pow'"); return; } | 170 | if (!lua_isnumber(o1) || !lua_isnumber(o2) || *(lua_getstring(op)) != 'p') |
170 | d1 = lua_getnumber(o1); | 171 | { |
171 | d2 = lua_getnumber(o2); | 172 | lua_pushobject(o1); |
172 | lua_pushnumber (pow(d1,d2)); | 173 | lua_pushobject(o2); |
174 | lua_pushobject(op); | ||
175 | if (lua_callfunction(lua_getlocked(old_pow)) != 0) | ||
176 | lua_error(NULL); | ||
177 | } | ||
178 | else | ||
179 | { | ||
180 | double d1 = lua_getnumber(o1); | ||
181 | double d2 = lua_getnumber(o2); | ||
182 | lua_pushnumber (pow(d1,d2)); | ||
183 | } | ||
173 | } | 184 | } |
174 | 185 | ||
175 | static void math_min (void) | 186 | static void math_min (void) |
@@ -292,7 +303,6 @@ void mathlib_open (void) | |||
292 | lua_register ("floor", math_floor); | 303 | lua_register ("floor", math_floor); |
293 | lua_register ("mod", math_mod); | 304 | lua_register ("mod", math_mod); |
294 | lua_register ("sqrt", math_sqrt); | 305 | lua_register ("sqrt", math_sqrt); |
295 | lua_register ("pow", math_pow); | ||
296 | lua_register ("min", math_min); | 306 | lua_register ("min", math_min); |
297 | lua_register ("max", math_max); | 307 | lua_register ("max", math_max); |
298 | lua_register ("log", math_log); | 308 | lua_register ("log", math_log); |
@@ -300,4 +310,5 @@ void mathlib_open (void) | |||
300 | lua_register ("exp", math_exp); | 310 | lua_register ("exp", math_exp); |
301 | lua_register ("deg", math_deg); | 311 | lua_register ("deg", math_deg); |
302 | lua_register ("rad", math_rad); | 312 | lua_register ("rad", math_rad); |
313 | old_pow = lua_lock(lua_setfallback("arith", math_pow)); | ||
303 | } | 314 | } |
@@ -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.14 1994/11/17 13:58:57 roberto Exp roberto $"; | 6 | char *rcs_opcode="$Id: opcode.c,v 3.15 1994/11/17 16:41:42 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <setjmp.h> | 8 | #include <setjmp.h> |
9 | #include <stdio.h> | 9 | #include <stdio.h> |
@@ -69,7 +69,7 @@ static void lua_message (char *s) | |||
69 | */ | 69 | */ |
70 | void lua_error (char *s) | 70 | void lua_error (char *s) |
71 | { | 71 | { |
72 | lua_message(s); | 72 | if (s) lua_message(s); |
73 | if (errorJmp) | 73 | if (errorJmp) |
74 | longjmp(*errorJmp, 1); | 74 | longjmp(*errorJmp, 1); |
75 | else | 75 | else |
@@ -877,19 +877,19 @@ static int lua_execute (Byte *pc, int base) | |||
877 | break; | 877 | break; |
878 | 878 | ||
879 | case LTOP: | 879 | case LTOP: |
880 | comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, "<"); | 880 | comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, "lt"); |
881 | break; | 881 | break; |
882 | 882 | ||
883 | case LEOP: | 883 | case LEOP: |
884 | comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, "<="); | 884 | comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, "le"); |
885 | break; | 885 | break; |
886 | 886 | ||
887 | case GTOP: | 887 | case GTOP: |
888 | comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, ">"); | 888 | comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, "gt"); |
889 | break; | 889 | break; |
890 | 890 | ||
891 | case GEOP: | 891 | case GEOP: |
892 | comparison(LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, ">="); | 892 | comparison(LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, "ge"); |
893 | break; | 893 | break; |
894 | 894 | ||
895 | case ADDOP: | 895 | case ADDOP: |
@@ -897,7 +897,7 @@ static int lua_execute (Byte *pc, int base) | |||
897 | Object *l = top-2; | 897 | Object *l = top-2; |
898 | Object *r = top-1; | 898 | Object *r = top-1; |
899 | if (tonumber(r) || tonumber(l)) | 899 | if (tonumber(r) || tonumber(l)) |
900 | call_arith("+"); | 900 | call_arith("add"); |
901 | else | 901 | else |
902 | { | 902 | { |
903 | nvalue(l) += nvalue(r); | 903 | nvalue(l) += nvalue(r); |
@@ -911,7 +911,7 @@ static int lua_execute (Byte *pc, int base) | |||
911 | Object *l = top-2; | 911 | Object *l = top-2; |
912 | Object *r = top-1; | 912 | Object *r = top-1; |
913 | if (tonumber(r) || tonumber(l)) | 913 | if (tonumber(r) || tonumber(l)) |
914 | call_arith("-"); | 914 | call_arith("sub"); |
915 | else | 915 | else |
916 | { | 916 | { |
917 | nvalue(l) -= nvalue(r); | 917 | nvalue(l) -= nvalue(r); |
@@ -925,7 +925,7 @@ static int lua_execute (Byte *pc, int base) | |||
925 | Object *l = top-2; | 925 | Object *l = top-2; |
926 | Object *r = top-1; | 926 | Object *r = top-1; |
927 | if (tonumber(r) || tonumber(l)) | 927 | if (tonumber(r) || tonumber(l)) |
928 | call_arith("*"); | 928 | call_arith("mul"); |
929 | else | 929 | else |
930 | { | 930 | { |
931 | nvalue(l) *= nvalue(r); | 931 | nvalue(l) *= nvalue(r); |
@@ -939,7 +939,7 @@ static int lua_execute (Byte *pc, int base) | |||
939 | Object *l = top-2; | 939 | Object *l = top-2; |
940 | Object *r = top-1; | 940 | Object *r = top-1; |
941 | if (tonumber(r) || tonumber(l)) | 941 | if (tonumber(r) || tonumber(l)) |
942 | call_arith("/"); | 942 | call_arith("div"); |
943 | else | 943 | else |
944 | { | 944 | { |
945 | nvalue(l) /= nvalue(r); | 945 | nvalue(l) /= nvalue(r); |
@@ -949,18 +949,8 @@ static int lua_execute (Byte *pc, int base) | |||
949 | break; | 949 | break; |
950 | 950 | ||
951 | case POWOP: | 951 | case POWOP: |
952 | { | 952 | call_arith("pow"); |
953 | Object *l = top-2; | 953 | break; |
954 | Object *r = top-1; | ||
955 | if (tonumber(r) || tonumber(l)) | ||
956 | call_arith("^"); | ||
957 | else | ||
958 | { | ||
959 | nvalue(l) = pow(nvalue(l), nvalue(r)); | ||
960 | --top; | ||
961 | } | ||
962 | } | ||
963 | break; | ||
964 | 954 | ||
965 | case CONCOP: | 955 | case CONCOP: |
966 | { | 956 | { |