Author: fperrad
Date: Thu Feb 9 00:26:19 2006
New Revision: 11478
Modified:
trunk/languages/lua/lib/luabasic.pir
trunk/languages/lua/lib/luapir.pir
trunk/languages/lua/pmc/luaboolean.pmc
trunk/languages/lua/pmc/luafunction.pmc
trunk/languages/lua/pmc/luanil.pmc
trunk/languages/lua/pmc/luanumber.pmc
trunk/languages/lua/pmc/luastring.pmc
trunk/languages/lua/pmc/luatable.pmc
trunk/languages/lua/pmc/luathread.pmc
trunk/languages/lua/pmc/luauserdata.pmc
trunk/languages/lua/t/basic.t
trunk/languages/lua/t/pmc/boolean.t
trunk/languages/lua/t/pmc/function.t
trunk/languages/lua/t/pmc/nil.t
trunk/languages/lua/t/pmc/number.t
trunk/languages/lua/t/pmc/string.t
trunk/languages/lua/t/pmc/table.t
trunk/languages/lua/t/pmc/thread.t
trunk/languages/lua/t/pmc/userdata.t
Log:
Lua :
- add methods tonumber & tostring at each Lua PMC
Modified: trunk/languages/lua/lib/luabasic.pir
==============================================================================
--- trunk/languages/lua/lib/luabasic.pir (original)
+++ trunk/languages/lua/lib/luabasic.pir Thu Feb 9 00:26:19 2006
@@ -521,7 +521,7 @@ the C<tostring> function to convert them
intended for formatted output, but only as a quick way to show a value,
typically for debugging. For formatted output, use C<format>.
-STILL INCOMPLETE.
+STILL INCOMPLETE (see tostring).
=cut
@@ -529,8 +529,6 @@ STILL INCOMPLETE.
.param pmc argv :slurpy
.local int argc
.local int i
- .local pmc curr
-# .local string str
argc = argv
i = 0
L1:
@@ -538,10 +536,9 @@ L1:
if i == 0 goto L2
print "\t"
L2:
- curr = argv[i]
- print curr
-# str = tostring(curr)
-# print str
+ $P0 = argv[i]
+ $P1 = $P0."tostring"()
+ print $P1
i = i + 1
goto L1
L3:
@@ -707,38 +704,27 @@ with �Z� representing 35. In base 10 (th
decimal part, as well as an optional exponent part. In other bases, only
unsigned integers are accepted.
-STILL INCOMPLETE.
-
=cut
.sub _lua_tonumber :anon
.param pmc e
.param pmc base :optional
.local pmc ret
- $I0 = optint(base, 10)
- unless $I0 == 10 goto L0
checkany(e)
- $I1 = isa e, "LuaNumber"
- unless $I1 goto L1
- ret = clone e
+ $I0 = optint(base, 10)
+ unless $I0 == 10 goto L1
+ ret = e."tonumber"()
.return (ret)
L1:
- $I1 = isa e, "LuaString"
- unless $I1 goto L2
- $S0 = e
-# print $S0
-# print "\n"
- $N0 = $S0
-# print $N0
-# print "\n"
- new ret, .LuaNumber
- ret = $N0
- .return (ret)
+ $P0 = checkstring(e)
+ unless 2 <= $I0 goto L2
+ unless $I0 <= 36 goto L2
+ goto L3
L2:
- new ret, .LuaNil
+ argerror("base out of range")
+L3:
+ ret = $P0."tobase"($I0)
.return (ret)
-L0:
- not_implemented()
.end
=item C<tostring (e)>
@@ -750,7 +736,7 @@ If the metatable of e has a C<"__tostrin
corresponding value with C<e> as argument, and uses the result of the call
as its result.
-STILL INCOMPLETE.
+STILL INCOMPLETE (see tostring in luatable.pmc & luauserdata.pmc).
=cut
@@ -758,14 +744,7 @@ STILL INCOMPLETE.
.param pmc e
.local pmc ret
checkany(e)
- # TODO: __tostring
- $I1 = isa e, "LuaString"
- unless $I1 goto L1
- .return (e)
-L1:
- $S0 = e
- new ret, .LuaString
- ret = $S0
+ ret = e."tostring"()
.return (ret)
.end
Modified: trunk/languages/lua/lib/luapir.pir
==============================================================================
--- trunk/languages/lua/lib/luapir.pir (original)
+++ trunk/languages/lua/lib/luapir.pir Thu Feb 9 00:26:19 2006
@@ -49,11 +49,17 @@ L1:
$S0 = "no value"
if_null arg, L0
$S0 = typeof arg
-# print $S0
-# print "\n"
- if $S0 == "nil" goto L0
+ $I0 = isa arg, "LuaNumber"
+ unless $I0 goto L1
val = arg
- # TODO
+ .return (val)
+L1:
+ $I0 = isa arg, "LuaString"
+ unless $I0 goto L0
+ $P0 = arg."tonumber"()
+ $I0 = isa $P0, "LuaNumber"
+ unless $I0 goto L0
+ val = $P0
.return (val)
L0:
tag_error($S0, "number")
@@ -66,10 +72,21 @@ L0:
.sub checkstring
.param pmc arg
- .local string val
+ .local pmc val
+ $S0 = "no value"
+ if_null arg, L0
+ $S0 = typeof arg
+ $I0 = isa arg, "LuaString"
+ unless $I0 goto L1
val = arg
- # TODO
+ .return (val)
+L1:
+ $I0 = isa arg, "LuaNumber"
+ unless $I0 goto L0
+ val = arg."tostring"()
.return (val)
+L0:
+ tag_error($S0, "string")
.end
@@ -80,9 +97,9 @@ L0:
.sub checktype
.param pmc arg
.param string type
- $S0 = "no value"
+ $S0 = "no value"
if_null arg, L0
- $S0 = typeof arg
+ $S0 = typeof arg
if $S0 != type goto L0
.return ()
L0:
Modified: trunk/languages/lua/pmc/luaboolean.pmc
==============================================================================
--- trunk/languages/lua/pmc/luaboolean.pmc (original)
+++ trunk/languages/lua/pmc/luaboolean.pmc Thu Feb 9 00:26:19 2006
@@ -26,6 +26,8 @@ The value is stored as an Integer.
static STRING *false_string;
static STRING *true_string;
static STRING *luaboolean_name;
+static INTVAL dynpmc_LuaNil;
+static INTVAL dynpmc_LuaString;
pmclass LuaBoolean
@@ -40,9 +42,31 @@ pmclass LuaBoolean
*/
void class_init() {
if (pass) {
+ PMC *meth;
+
false_string = const_string(INTERP, "false");
true_string = const_string(INTERP, "true");
luaboolean_name = const_string(INTERP, "boolean");
+ dynpmc_LuaNil = pmc_type(INTERP,
+ string_from_const_cstring(INTERP, "LuaNil", 0));
+ dynpmc_LuaString = pmc_type(INTERP,
+ string_from_const_cstring(INTERP, "LuaString", 0));
+
+ /* namespace hack */
+ meth = Parrot_find_global(INTERP,
+ const_string(INTERP, "LuaBoolean"),
+ const_string(INTERP, "tostring"));
+ Parrot_store_global(INTERP,
+ const_string(INTERP, "boolean"),
+ const_string(INTERP, "tostring"),
+ meth);
+ meth = Parrot_find_global(INTERP,
+ const_string(INTERP, "LuaBoolean"),
+ const_string(INTERP, "tonumber"));
+ Parrot_store_global(INTERP,
+ const_string(INTERP, "boolean"),
+ const_string(INTERP, "tonumber"),
+ meth);
}
}
@@ -75,6 +99,43 @@ Return the string "true" or "false".
return false_string;
}
+/*
+
+=back
+
+=head2 Specific Methods
+
+=over 4
+
+=item C<PMC* tonumber()>
+
+=cut
+
+*/
+ METHOD PMC* tonumber() {
+ PMC *retval;
+
+ retval = pmc_new(INTERP, dynpmc_LuaNil);
+ return retval;
+ }
+
+/*
+
+=item C<PMC* tostring()>
+
+=cut
+
+*/
+ METHOD PMC* tostring() {
+ PMC *retval;
+ STRING *rep;
+
+ retval = pmc_new(INTERP, dynpmc_LuaString);
+ rep = VTABLE_get_string(INTERP, SELF);
+ PMC_str_val(retval) = rep;
+ return retval;
+ }
+
}
/*
Modified: trunk/languages/lua/pmc/luafunction.pmc
==============================================================================
--- trunk/languages/lua/pmc/luafunction.pmc (original)
+++ trunk/languages/lua/pmc/luafunction.pmc Thu Feb 9 00:26:19 2006
@@ -22,6 +22,9 @@ of the Lua C<Function> type.
#include "parrot/parrot.h"
static STRING *luafunction_name;
+static INTVAL dynpmc_LuaNil;
+static INTVAL dynpmc_LuaString;
+
pmclass LuaFunction
extends Sub
@@ -34,7 +37,29 @@ pmclass LuaFunction
*/
void class_init() {
if (pass) {
+ PMC *meth;
+
luafunction_name = const_string(INTERP, "function");
+ dynpmc_LuaNil = pmc_type(INTERP,
+ string_from_const_cstring(INTERP, "LuaNil", 0));
+ dynpmc_LuaString = pmc_type(INTERP,
+ string_from_const_cstring(INTERP, "LuaString", 0));
+
+ /* namespace hack */
+ meth = Parrot_find_global(INTERP,
+ const_string(INTERP, "LuaFunction"),
+ const_string(INTERP, "tostring"));
+ Parrot_store_global(INTERP,
+ const_string(INTERP, "function"),
+ const_string(INTERP, "tostring"),
+ meth);
+ meth = Parrot_find_global(INTERP,
+ const_string(INTERP, "LuaFunction"),
+ const_string(INTERP, "tonumber"));
+ Parrot_store_global(INTERP,
+ const_string(INTERP, "function"),
+ const_string(INTERP, "tonumber"),
+ meth);
}
}
@@ -95,6 +120,43 @@ Return always false.
return dest;
}
+/*
+
+=back
+
+=head2 Specific Methods
+
+=over 4
+
+=item C<PMC* tonumber()>
+
+=cut
+
+*/
+ METHOD PMC* tonumber() {
+ PMC *retval;
+
+ retval = pmc_new(INTERP, dynpmc_LuaNil);
+ return retval;
+ }
+
+/*
+
+=item C<PMC* tostring()>
+
+=cut
+
+*/
+ METHOD PMC* tostring() {
+ PMC *retval;
+ STRING *rep;
+
+ retval = pmc_new(INTERP, dynpmc_LuaString);
+ rep = VTABLE_get_string(INTERP, SELF);
+ PMC_str_val(retval) = rep;
+ return retval;
+ }
+
}
/*
Modified: trunk/languages/lua/pmc/luanil.pmc
==============================================================================
--- trunk/languages/lua/pmc/luanil.pmc (original)
+++ trunk/languages/lua/pmc/luanil.pmc Thu Feb 9 00:26:19 2006
@@ -27,6 +27,7 @@ troublesome (if not possible?).
#include "parrot/parrot.h"
static STRING *string_representation;
+static INTVAL dynpmc_LuaNil;
static INTVAL dynpmc_LuaNumber;
static INTVAL dynpmc_LuaString;
static INTVAL dynpmc_LuaBoolean;
@@ -41,13 +42,33 @@ pmclass LuaNil
*/
void class_init() {
if (pass) {
+ PMC *meth;
+
string_representation = const_string(INTERP, "nil");
+ dynpmc_LuaNil = pmc_type(INTERP,
+ string_from_const_cstring(INTERP, "LuaNil", 0));
dynpmc_LuaNumber = pmc_type(INTERP,
string_from_const_cstring(INTERP, "LuaNumber", 0));
dynpmc_LuaString = pmc_type(INTERP,
string_from_const_cstring(INTERP, "LuaString", 0));
dynpmc_LuaBoolean = pmc_type(INTERP,
string_from_const_cstring(INTERP, "LuaBoolean", 0));
+
+ /* namespace hack */
+ meth = Parrot_find_global(INTERP,
+ const_string(INTERP, "LuaNil"),
+ const_string(INTERP, "tostring"));
+ Parrot_store_global(INTERP,
+ const_string(INTERP, "nil"),
+ const_string(INTERP, "tostring"),
+ meth);
+ meth = Parrot_find_global(INTERP,
+ const_string(INTERP, "LuaNil"),
+ const_string(INTERP, "tonumber"));
+ Parrot_store_global(INTERP,
+ const_string(INTERP, "nil"),
+ const_string(INTERP, "tonumber"),
+ meth);
}
}
@@ -223,6 +244,43 @@ MMD_DEFAULT: {
}
}
+/*
+
+=back
+
+=head2 Specific Methods
+
+=over 4
+
+=item C<PMC* tonumber()>
+
+=cut
+
+*/
+ METHOD PMC* tonumber() {
+ PMC *retval;
+
+ retval = pmc_new(INTERP, dynpmc_LuaNil);
+ return retval;
+ }
+
+/*
+
+=item C<PMC* tostring()>
+
+=cut
+
+*/
+ METHOD PMC* tostring() {
+ PMC *retval;
+ STRING *rep;
+
+ retval = pmc_new(INTERP, dynpmc_LuaString);
+ rep = VTABLE_get_string(INTERP, SELF);
+ PMC_str_val(retval) = rep;
+ return retval;
+ }
+
}
/*
Modified: trunk/languages/lua/pmc/luanumber.pmc
==============================================================================
--- trunk/languages/lua/pmc/luanumber.pmc (original)
+++ trunk/languages/lua/pmc/luanumber.pmc Thu Feb 9 00:26:19 2006
@@ -22,6 +22,7 @@ the Lua C<Number> type.
#include "parrot/parrot.h"
static STRING *luanumber_name;
+static INTVAL dynpmc_LuaString;
pmclass LuaNumber
extends Float
@@ -34,7 +35,27 @@ pmclass LuaNumber
*/
void class_init() {
if (pass) {
+ PMC *meth;
+
luanumber_name = const_string(INTERP, "number");
+ dynpmc_LuaString = pmc_type(INTERP,
+ string_from_const_cstring(INTERP, "LuaString", 0));
+
+ /* namespace hack */
+ meth = Parrot_find_global(INTERP,
+ const_string(INTERP, "LuaNumber"),
+ const_string(INTERP, "tostring"));
+ Parrot_store_global(INTERP,
+ const_string(INTERP, "number"),
+ const_string(INTERP, "tostring"),
+ meth);
+ meth = Parrot_find_global(INTERP,
+ const_string(INTERP, "LuaNumber"),
+ const_string(INTERP, "tonumber"));
+ Parrot_store_global(INTERP,
+ const_string(INTERP, "number"),
+ const_string(INTERP, "tonumber"),
+ meth);
}
}
@@ -143,6 +164,43 @@ MMD_DEFAULT: {
}
}
+/*
+
+=back
+
+=head2 Specific Methods
+
+=over 4
+
+=item C<PMC* tonumber()>
+
+=cut
+
+*/
+ METHOD PMC* tonumber() {
+ PMC *retval;
+
+ retval = VTABLE_clone(INTERP, SELF);
+ return retval;
+ }
+
+/*
+
+=item C<PMC* tostring()>
+
+=cut
+
+*/
+ METHOD PMC* tostring() {
+ PMC *retval;
+ STRING *rep;
+
+ retval = pmc_new(INTERP, dynpmc_LuaString);
+ rep = VTABLE_get_string(INTERP, SELF);
+ PMC_str_val(retval) = rep;
+ return retval;
+ }
+
}
/*
Modified: trunk/languages/lua/pmc/luastring.pmc
==============================================================================
--- trunk/languages/lua/pmc/luastring.pmc (original)
+++ trunk/languages/lua/pmc/luastring.pmc Thu Feb 9 00:26:19 2006
@@ -22,6 +22,8 @@ the Lua C<String> type.
#include "parrot/parrot.h"
static STRING *luastring_name;
+static INTVAL dynpmc_LuaNil;
+static INTVAL dynpmc_LuaNumber;
pmclass LuaString
extends String
@@ -34,8 +36,37 @@ pmclass LuaString
*/
void class_init() {
if (pass) {
+ PMC *meth;
+
luastring_name = const_string(INTERP, "string");
- }
+ dynpmc_LuaNil = pmc_type(INTERP,
+ string_from_const_cstring(INTERP, "LuaNil", 0));
+ dynpmc_LuaNumber = pmc_type(INTERP,
+ string_from_const_cstring(INTERP, "LuaNumber", 0));
+
+ /* namespace hack */
+ meth = Parrot_find_global(INTERP,
+ const_string(INTERP, "LuaString"),
+ const_string(INTERP, "tostring"));
+ Parrot_store_global(INTERP,
+ const_string(INTERP, "string"),
+ const_string(INTERP, "tostring"),
+ meth);
+ meth = Parrot_find_global(INTERP,
+ const_string(INTERP, "LuaString"),
+ const_string(INTERP, "tonumber"));
+ Parrot_store_global(INTERP,
+ const_string(INTERP, "string"),
+ const_string(INTERP, "tonumber"),
+ meth);
+ meth = Parrot_find_global(INTERP,
+ const_string(INTERP, "LuaString"),
+ const_string(INTERP, "tobase"));
+ Parrot_store_global(INTERP,
+ const_string(INTERP, "string"),
+ const_string(INTERP, "tobase"),
+ meth);
+ }
}
/*
@@ -140,6 +171,79 @@ MMD_DEFAULT: {
}
}
+/*
+
+=back
+
+=head2 Specific Methods
+
+=over 4
+
+=item C<PMC* tonumber()>
+
+=item C<PMC* tobase(INTVAL base)>
+
+=cut
+
+*/
+ METHOD PMC* tonumber() {
+ PMC *retval;
+ STRING *rep;
+ FLOATVAL d;
+ char *s1;
+ char *s2;
+
+ rep = VTABLE_get_string(INTERP, SELF);
+ s1 = string_to_cstring(INTERP, rep);
+ d = strtod(s1, &s2);
+ if (s1 != s2) { /* at least one valid digit? */
+ while (isspace((unsigned char)(*s2))) s2++; /* skip trailing
spaces */
+ if (*s2 == '\0') { /* no invalid trailing characters? */
+ retval = pmc_new(INTERP, dynpmc_LuaNumber);
+ PMC_num_val(retval) = d;
+ return retval;
+ }
+ }
+ retval = pmc_new(INTERP, dynpmc_LuaNil);
+ return retval;
+ }
+
+ METHOD PMC* tobase(INTVAL base) {
+ PMC *retval;
+ STRING *rep;
+ unsigned long n;
+ char *s1;
+ char *s2;
+
+ rep = VTABLE_get_string(INTERP, SELF);
+ s1 = string_to_cstring(INTERP, rep);
+ n = strtoul(s1, &s2, base);
+ if (s1 != s2) { /* at least one valid digit? */
+ while (isspace((unsigned char)(*s2))) s2++; /* skip trailing
spaces */
+ if (*s2 == '\0') { /* no invalid trailing characters? */
+ retval = pmc_new(INTERP, dynpmc_LuaNumber);
+ PMC_num_val(retval) = n;
+ return retval;
+ }
+ }
+ retval = pmc_new(INTERP, dynpmc_LuaNil);
+ return retval;
+ }
+
+/*
+
+=item C<PMC* tostring()>
+
+=cut
+
+*/
+ METHOD PMC* tostring() {
+ PMC *retval;
+
+ retval = VTABLE_clone(INTERP, SELF);
+ return retval;
+ }
+
}
/*
Modified: trunk/languages/lua/pmc/luatable.pmc
==============================================================================
--- trunk/languages/lua/pmc/luatable.pmc (original)
+++ trunk/languages/lua/pmc/luatable.pmc Thu Feb 9 00:26:19 2006
@@ -26,6 +26,7 @@ Now, Lua 5.0 uses a hybrid data structur
static STRING *luatable_name;
static INTVAL dynpmc_LuaNil;
+static INTVAL dynpmc_LuaString;
pmclass LuaTable
extends Hash
@@ -38,9 +39,29 @@ pmclass LuaTable
*/
void class_init() {
if (pass) {
+ PMC *meth;
+
luatable_name = const_string(INTERP, "table");
dynpmc_LuaNil = pmc_type(INTERP,
string_from_const_cstring(INTERP, "LuaNil", 0));
+ dynpmc_LuaString = pmc_type(INTERP,
+ string_from_const_cstring(INTERP, "LuaString", 0));
+
+ /* namespace hack */
+ meth = Parrot_find_global(INTERP,
+ const_string(INTERP, "LuaTable"),
+ const_string(INTERP, "tostring"));
+ Parrot_store_global(INTERP,
+ const_string(INTERP, "table"),
+ const_string(INTERP, "tostring"),
+ meth);
+ meth = Parrot_find_global(INTERP,
+ const_string(INTERP, "LuaTable"),
+ const_string(INTERP, "tonumber"));
+ Parrot_store_global(INTERP,
+ const_string(INTERP, "table"),
+ const_string(INTERP, "tonumber"),
+ meth);
}
}
@@ -244,6 +265,44 @@ The C<==> operation. Compares reference
return (INTVAL)0;
}
+/*
+
+=back
+
+=head2 Specific Methods
+
+=over 4
+
+=item C<PMC* tonumber()>
+
+=cut
+
+*/
+ METHOD PMC* tonumber() {
+ PMC *retval;
+
+ retval = pmc_new(INTERP, dynpmc_LuaNil);
+ return retval;
+ }
+
+/*
+
+=item C<PMC* tostring()>
+
+=cut
+
+*/
+ METHOD PMC* tostring() {
+ PMC *retval;
+ STRING *rep;
+
+ /* TODO: __tostring */
+ retval = pmc_new(INTERP, dynpmc_LuaString);
+ rep = VTABLE_get_string(INTERP, SELF);
+ PMC_str_val(retval) = rep;
+ return retval;
+ }
+
}
/*
Modified: trunk/languages/lua/pmc/luathread.pmc
==============================================================================
--- trunk/languages/lua/pmc/luathread.pmc (original)
+++ trunk/languages/lua/pmc/luathread.pmc Thu Feb 9 00:26:19 2006
@@ -22,6 +22,9 @@ of the Lua C<Thread> type.
#include "parrot/parrot.h"
static STRING *luathread_name;
+static INTVAL dynpmc_LuaNil;
+static INTVAL dynpmc_LuaString;
+
pmclass LuaThread
extends Coroutine
@@ -34,7 +37,29 @@ pmclass LuaThread
*/
void class_init() {
if (pass) {
+ PMC *meth;
+
luathread_name = const_string(INTERP, "thread");
+ dynpmc_LuaNil = pmc_type(INTERP,
+ string_from_const_cstring(INTERP, "LuaNil", 0));
+ dynpmc_LuaString = pmc_type(INTERP,
+ string_from_const_cstring(INTERP, "LuaString", 0));
+
+ /* namespace hack */
+ meth = Parrot_find_global(INTERP,
+ const_string(INTERP, "LuaThread"),
+ const_string(INTERP, "tostring"));
+ Parrot_store_global(INTERP,
+ const_string(INTERP, "thread"),
+ const_string(INTERP, "tostring"),
+ meth);
+ meth = Parrot_find_global(INTERP,
+ const_string(INTERP, "LuaThread"),
+ const_string(INTERP, "tonumber"));
+ Parrot_store_global(INTERP,
+ const_string(INTERP, "thread"),
+ const_string(INTERP, "tonumber"),
+ meth);
}
}
@@ -95,6 +120,43 @@ Return always false.
return dest;
}
+/*
+
+=back
+
+=head2 Specific Methods
+
+=over 4
+
+=item C<PMC* tonumber()>
+
+=cut
+
+*/
+ METHOD PMC* tonumber() {
+ PMC *retval;
+
+ retval = pmc_new(INTERP, dynpmc_LuaNil);
+ return retval;
+ }
+
+/*
+
+=item C<PMC* tostring()>
+
+=cut
+
+*/
+ METHOD PMC* tostring() {
+ PMC *retval;
+ STRING *rep;
+
+ retval = pmc_new(INTERP, dynpmc_LuaString);
+ rep = VTABLE_get_string(INTERP, SELF);
+ PMC_str_val(retval) = rep;
+ return retval;
+ }
+
}
/*
Modified: trunk/languages/lua/pmc/luauserdata.pmc
==============================================================================
--- trunk/languages/lua/pmc/luauserdata.pmc (original)
+++ trunk/languages/lua/pmc/luauserdata.pmc Thu Feb 9 00:26:19 2006
@@ -22,6 +22,9 @@ the Lua C<Userdata> type.
#include "parrot/parrot.h"
static STRING *luauserdata_name;
+static INTVAL dynpmc_LuaNil;
+static INTVAL dynpmc_LuaString;
+
pmclass LuaUserdata
extends String
@@ -33,7 +36,29 @@ pmclass LuaUserdata
*/
void class_init() {
if (pass) {
+ PMC *meth;
+
luauserdata_name = const_string(INTERP, "userdata");
+ dynpmc_LuaNil = pmc_type(INTERP,
+ string_from_const_cstring(INTERP, "LuaNil", 0));
+ dynpmc_LuaString = pmc_type(INTERP,
+ string_from_const_cstring(INTERP, "LuaString", 0));
+
+ /* namespace hack */
+ meth = Parrot_find_global(INTERP,
+ const_string(INTERP, "LuaUserdata"),
+ const_string(INTERP, "tostring"));
+ Parrot_store_global(INTERP,
+ const_string(INTERP, "userdata"),
+ const_string(INTERP, "tostring"),
+ meth);
+ meth = Parrot_find_global(INTERP,
+ const_string(INTERP, "LuaUserdata"),
+ const_string(INTERP, "tonumber"));
+ Parrot_store_global(INTERP,
+ const_string(INTERP, "userdata"),
+ const_string(INTERP, "tonumber"),
+ meth);
}
}
@@ -77,6 +102,44 @@ So return always true.
return 1;
}
+/*
+
+=back
+
+=head2 Specific Methods
+
+=over 4
+
+=item C<PMC* tonumber()>
+
+=cut
+
+*/
+ METHOD PMC* tonumber() {
+ PMC *retval;
+
+ retval = pmc_new(INTERP, dynpmc_LuaNil);
+ return retval;
+ }
+
+/*
+
+=item C<PMC* tostring()>
+
+=cut
+
+*/
+ METHOD PMC* tostring() {
+ PMC *retval;
+ STRING *rep;
+
+ /* TODO: __tostring */
+ retval = pmc_new(INTERP, dynpmc_LuaString);
+ rep = VTABLE_get_string(INTERP, SELF);
+ PMC_str_val(retval) = rep;
+ return retval;
+ }
+
}
/*
Modified: trunk/languages/lua/t/basic.t
==============================================================================
--- trunk/languages/lua/t/basic.t (original)
+++ trunk/languages/lua/t/basic.t Thu Feb 9 00:26:19 2006
@@ -21,7 +21,7 @@ use strict;
use FindBin;
use lib "$FindBin::Bin";
-use Parrot::Test tests => 17;
+use Parrot::Test tests => 20;
use Test::More;
language_output_like( 'lua', << 'CODE', << 'OUTPUT', "function assert(false,
msg)");
@@ -190,18 +190,56 @@ CODE
number
OUTPUT
-TODO: {
-local $TODO = "tonumber !";
-
language_output_is( 'lua', << 'CODE', << 'OUTPUT', "function tonumber");
-a = "text12"
-i = tonumber(a)
-print(i)
+r = tonumber("text12")
+print(type(r), r)
+r = tonumber("12text")
+print(type(r), r)
+r = tonumber(3.14)
+print(type(r), r)
+r = tonumber("3.14")
+print(type(r), r)
+r = tonumber(" 3.14 ")
+print(type(r), r)
+r = tonumber(111, 2)
+print(type(r), r)
+r = tonumber("111", 2)
+print(type(r), r)
+r = tonumber(" 111 ", 2)
+print(type(r), r)
+a = {}
+r = tonumber(a)
+print(type(r), r)
CODE
-nil
+nil nil
+nil nil
+number 3.14
+number 3.14
+number 3.14
+number 7
+number 7
+number 7
+nil nil
OUTPUT
-}
+language_output_like( 'lua', << 'CODE', << 'OUTPUT', "function tonumber (no
arg)");
+tonumber()
+CODE
+/value expected/
+OUTPUT
+
+language_output_like( 'lua', << 'CODE', << 'OUTPUT', "function tonumber (bad
base)");
+r = tonumber("111", 200)
+print(type(r), r)
+CODE
+/base out of range/
+OUTPUT
+
+language_output_like( 'lua', << 'CODE', << 'OUTPUT', "function tostring (no
arg)");
+tostring()
+CODE
+/value expected/
+OUTPUT
language_output_is( 'lua', << 'CODE', << 'OUTPUT', "function unpack");
print(unpack{})
Modified: trunk/languages/lua/t/pmc/boolean.t
==============================================================================
--- trunk/languages/lua/t/pmc/boolean.t (original)
+++ trunk/languages/lua/t/pmc/boolean.t Thu Feb 9 00:26:19 2006
@@ -17,7 +17,7 @@ Tests C<LuaBoolean> PMC
=cut
-use Parrot::Test tests => 7;
+use Parrot::Test tests => 9;
use Test::More;
pir_output_is(<< 'CODE', << 'OUTPUT', "check inheritance");
@@ -166,3 +166,45 @@ true
1
OUTPUT
+pir_output_is(<< 'CODE', << 'OUTPUT', "check tostring");
+.HLL "Lua", "lua_group"
+.sub _main
+ .local pmc pmc1
+ pmc1 = new .LuaBoolean
+ pmc1 = 1
+ print pmc1
+ print "\n"
+ $P0 = pmc1."tostring"()
+ print $P0
+ print "\n"
+ $S0 = typeof $P0
+ print $S0
+ print "\n"
+.end
+CODE
+true
+true
+string
+OUTPUT
+
+pir_output_is(<< 'CODE', << 'OUTPUT', "check tonumber");
+.HLL "Lua", "lua_group"
+.sub _main
+ .local pmc pmc1
+ pmc1 = new .LuaBoolean
+ pmc1 = 1
+ print pmc1
+ print "\n"
+ $P0 = pmc1."tonumber"()
+ print $P0
+ print "\n"
+ $S0 = typeof $P0
+ print $S0
+ print "\n"
+.end
+CODE
+true
+nil
+nil
+OUTPUT
+
Modified: trunk/languages/lua/t/pmc/function.t
==============================================================================
--- trunk/languages/lua/t/pmc/function.t (original)
+++ trunk/languages/lua/t/pmc/function.t Thu Feb 9 00:26:19 2006
@@ -17,7 +17,7 @@ Tests C<LuaFunction> PMC
=cut
-use Parrot::Test tests => 8;
+use Parrot::Test tests => 10;
use Test::More;
pir_output_is(<< 'CODE', << 'OUTPUT', "check inheritance");
@@ -166,7 +166,6 @@ pir_output_is(<< 'CODE', << 'OUTPUT', "c
.HLL "Lua", "lua_group"
.sub _main
.local pmc pmc1
- pmc1 = new .LuaFunction
# .const .LuaFunction F1 = "f1"
.const .Sub F1 = "f1"
pmc1 = F1
@@ -174,11 +173,14 @@ pir_output_is(<< 'CODE', << 'OUTPUT', "c
bool1 = isa pmc1, "LuaFunction"
print bool1
print "\n"
+ print pmc1
+ print "\n"
+ pmc1()
end
.end
.sub f1
print "f1()\n"
- end
+ .return ()
.end
CODE
1
@@ -204,3 +206,38 @@ OUTPUT
}
+pir_output_like(<< 'CODE', << 'OUTPUT', "check tostring");
+.HLL "Lua", "lua_group"
+.sub _main
+ .local pmc pmc1
+ pmc1 = new .LuaFunction
+ print pmc1
+ print "\n"
+ $P0 = pmc1."tostring"()
+ print $P0
+ print "\n"
+ $S0 = typeof $P0
+ print $S0
+ print "\n"
+.end
+CODE
+/function: [0-9A-Fa-f]{8}\nfunction: [0-9A-Fa-f]{8}\nstring/
+OUTPUT
+
+pir_output_is(<< 'CODE', << 'OUTPUT', "check tonumber");
+.HLL "Lua", "lua_group"
+.sub _main
+ .local pmc pmc1
+ pmc1 = new .LuaFunction
+ $P0 = pmc1."tonumber"()
+ print $P0
+ print "\n"
+ $S0 = typeof $P0
+ print $S0
+ print "\n"
+.end
+CODE
+nil
+nil
+OUTPUT
+
Modified: trunk/languages/lua/t/pmc/nil.t
==============================================================================
--- trunk/languages/lua/t/pmc/nil.t (original)
+++ trunk/languages/lua/t/pmc/nil.t Thu Feb 9 00:26:19 2006
@@ -17,7 +17,7 @@ Tests C<LuaNil> PMC
=cut
-use Parrot::Test tests => 12;
+use Parrot::Test tests => 14;
use Test::More;
pir_output_is(<< 'CODE', << 'OUTPUT', "check inheritance");
@@ -257,3 +257,42 @@ number
2
OUTPUT
+pir_output_is(<< 'CODE', << 'OUTPUT', "check tostring");
+.HLL "Lua", "lua_group"
+.sub _main
+ .local pmc pmc1
+ pmc1 = new .LuaNil
+ print pmc1
+ print "\n"
+ $P0 = pmc1."tostring"()
+ print $P0
+ print "\n"
+ $S0 = typeof $P0
+ print $S0
+ print "\n"
+.end
+CODE
+nil
+nil
+string
+OUTPUT
+
+pir_output_is(<< 'CODE', << 'OUTPUT', "check tonumber");
+.HLL "Lua", "lua_group"
+.sub _main
+ .local pmc pmc1
+ pmc1 = new .LuaNil
+ print pmc1
+ print "\n"
+ $P0 = pmc1."tonumber"()
+ print $P0
+ print "\n"
+ $S0 = typeof $P0
+ print $S0
+ print "\n"
+.end
+CODE
+nil
+nil
+nil
+OUTPUT
Modified: trunk/languages/lua/t/pmc/number.t
==============================================================================
--- trunk/languages/lua/t/pmc/number.t (original)
+++ trunk/languages/lua/t/pmc/number.t Thu Feb 9 00:26:19 2006
@@ -17,7 +17,7 @@ Tests C<LuaNumber> PMC
=cut
-use Parrot::Test tests => 9;
+use Parrot::Test tests => 11;
use Test::More;
pir_output_is(<< 'CODE', << 'OUTPUT', "check inheritance");
@@ -213,3 +213,41 @@ CODE
1
OUTPUT
+pir_output_is(<< 'CODE', << 'OUTPUT', "check tostring");
+.HLL "Lua", "lua_group"
+.sub _main
+ .const .LuaNumber cst1 = "3.14"
+ print cst1
+ print "\n"
+ $P0 = cst1."tostring"()
+ print $P0
+ print "\n"
+ $S0 = typeof $P0
+ print $S0
+ print "\n"
+.end
+CODE
+3.14
+3.14
+string
+OUTPUT
+
+pir_output_is(<< 'CODE', << 'OUTPUT', "check tonumber");
+.HLL "Lua", "lua_group"
+.sub _main
+ .const .LuaNumber cst1 = "3.14"
+ print cst1
+ print "\n"
+ $P0 = cst1."tonumber"()
+ print $P0
+ print "\n"
+ $S0 = typeof $P0
+ print $S0
+ print "\n"
+.end
+CODE
+3.14
+3.14
+number
+OUTPUT
+
Modified: trunk/languages/lua/t/pmc/string.t
==============================================================================
--- trunk/languages/lua/t/pmc/string.t (original)
+++ trunk/languages/lua/t/pmc/string.t Thu Feb 9 00:26:19 2006
@@ -17,7 +17,7 @@ Tests C<LuaString> PMC
=cut
-use Parrot::Test tests => 10;
+use Parrot::Test tests => 13;
use Test::More;
pir_output_is(<< 'CODE', << 'OUTPUT', "check inheritance");
@@ -220,3 +220,67 @@ CODE
1
OUTPUT
}
+
+pir_output_is(<< 'CODE', << 'OUTPUT', "check tostring");
+.HLL "Lua", "lua_group"
+.sub _main
+ .local pmc pmc1
+ pmc1 = new .LuaString
+ pmc1 = "value"
+ print pmc1
+ print "\n"
+ $P0 = pmc1."tostring"()
+ print $P0
+ print "\n"
+ $S0 = typeof $P0
+ print $S0
+ print "\n"
+.end
+CODE
+value
+value
+string
+OUTPUT
+
+pir_output_is(<< 'CODE', << 'OUTPUT', "check tonumber");
+.HLL "Lua", "lua_group"
+.sub _main
+ .local pmc pmc1
+ pmc1 = new .LuaString
+ pmc1 = "3.14"
+ print pmc1
+ print "\n"
+ $P0 = pmc1."tonumber"()
+ print $P0
+ print "\n"
+ $S0 = typeof $P0
+ print $S0
+ print "\n"
+.end
+CODE
+3.14
+3.14
+number
+OUTPUT
+
+pir_output_is(<< 'CODE', << 'OUTPUT', "check tobase");
+.HLL "Lua", "lua_group"
+.sub _main
+ .local pmc pmc1
+ pmc1 = new .LuaString
+ pmc1 = "111"
+ print pmc1
+ print "\n"
+ $P0 = pmc1."tobase"(2)
+ print $P0
+ print "\n"
+ $S0 = typeof $P0
+ print $S0
+ print "\n"
+.end
+CODE
+111
+7
+number
+OUTPUT
+
Modified: trunk/languages/lua/t/pmc/table.t
==============================================================================
--- trunk/languages/lua/t/pmc/table.t (original)
+++ trunk/languages/lua/t/pmc/table.t Thu Feb 9 00:26:19 2006
@@ -17,7 +17,7 @@ Tests C<LuaTable> PMC
=cut
-use Parrot::Test tests => 10;
+use Parrot::Test tests => 12;
use Test::More;
pir_output_is(<< 'CODE', << 'OUTPUT', "check inheritance");
@@ -251,3 +251,39 @@ pir_output_is(<< 'CODE', << 'OUTPUT', "c
CODE
1
OUTPUT
+
+pir_output_like(<< 'CODE', << 'OUTPUT', "check tostring");
+.HLL "Lua", "lua_group"
+.sub _main
+ .local pmc pmc1
+ pmc1 = new .LuaTable
+ print pmc1
+ print "\n"
+ $P0 = pmc1."tostring"()
+ print $P0
+ print "\n"
+ $S0 = typeof $P0
+ print $S0
+ print "\n"
+.end
+CODE
+/table: [0-9A-Fa-f]{8}\ntable: [0-9A-Fa-f]{8}\nstring/
+OUTPUT
+
+pir_output_is(<< 'CODE', << 'OUTPUT', "check tonumber");
+.HLL "Lua", "lua_group"
+.sub _main
+ .local pmc pmc1
+ pmc1 = new .LuaTable
+ $P0 = pmc1."tonumber"()
+ print $P0
+ print "\n"
+ $S0 = typeof $P0
+ print $S0
+ print "\n"
+.end
+CODE
+nil
+nil
+OUTPUT
+
Modified: trunk/languages/lua/t/pmc/thread.t
==============================================================================
--- trunk/languages/lua/t/pmc/thread.t (original)
+++ trunk/languages/lua/t/pmc/thread.t Thu Feb 9 00:26:19 2006
@@ -17,7 +17,7 @@ Tests C<LuaThread> PMC
=cut
-use Parrot::Test tests => 8;
+use Parrot::Test tests => 10;
use Test::More;
pir_output_is(<< 'CODE', << 'OUTPUT', "check inheritance");
@@ -200,3 +200,37 @@ OUTPUT
}
+pir_output_like(<< 'CODE', << 'OUTPUT', "check tostring");
+.HLL "Lua", "lua_group"
+.sub _main
+ .local pmc pmc1
+ pmc1 = new .LuaThread
+ print pmc1
+ print "\n"
+ $P0 = pmc1."tostring"()
+ print $P0
+ print "\n"
+ $S0 = typeof $P0
+ print $S0
+ print "\n"
+.end
+CODE
+/thread: [0-9A-Fa-f]{8}\nthread: [0-9A-Fa-f]{8}\nstring/
+OUTPUT
+
+pir_output_is(<< 'CODE', << 'OUTPUT', "check tonumber");
+.HLL "Lua", "lua_group"
+.sub _main
+ .local pmc pmc1
+ pmc1 = new .LuaThread
+ $P0 = pmc1."tonumber"()
+ print $P0
+ print "\n"
+ $S0 = typeof $P0
+ print $S0
+ print "\n"
+.end
+CODE
+nil
+nil
+OUTPUT
Modified: trunk/languages/lua/t/pmc/userdata.t
==============================================================================
--- trunk/languages/lua/t/pmc/userdata.t (original)
+++ trunk/languages/lua/t/pmc/userdata.t Thu Feb 9 00:26:19 2006
@@ -17,7 +17,7 @@ Tests C<LuaUserdata> PMC
=cut
-use Parrot::Test tests => 7;
+use Parrot::Test tests => 9;
use Test::More;
pir_output_is(<< 'CODE', << 'OUTPUT', "check inheritance");
@@ -160,3 +160,41 @@ pir_output_is(<< 'CODE', << 'OUTPUT', "c
CODE
1
OUTPUT
+
+pir_output_like(<< 'CODE', << 'OUTPUT', "check tostring");
+.HLL "Lua", "lua_group"
+.sub _main
+ .local pmc pmc1
+ pmc1 = new .LuaUserdata
+ pmc1 = "value"
+ print pmc1
+ print "\n"
+ $P0 = pmc1."tostring"()
+ print $P0
+ print "\n"
+ $S0 = typeof $P0
+ print $S0
+ print "\n"
+.end
+CODE
+/userdata: [0-9A-Fa-f]{8}\nuserdata: [0-9A-Fa-f]{8}\nstring/
+OUTPUT
+
+pir_output_is(<< 'CODE', << 'OUTPUT', "check tonumber");
+.HLL "Lua", "lua_group"
+.sub _main
+ .local pmc pmc1
+ pmc1 = new .LuaUserdata
+ pmc1 = "value"
+ $P0 = pmc1."tonumber"()
+ print $P0
+ print "\n"
+ $S0 = typeof $P0
+ print $S0
+ print "\n"
+.end
+CODE
+nil
+nil
+OUTPUT
+