Merge branch 'num-from-string' of git://github.com/gsmaverick/wren into gsmaverick-num-from-string
This commit is contained in:
commit
4b88292ec1
@ -90,3 +90,10 @@ It is a runtime error if `other` is not a number.
|
|||||||
### **...**(other) operator
|
### **...**(other) operator
|
||||||
|
|
||||||
**TODO**
|
**TODO**
|
||||||
|
|
||||||
|
### Num.**fromString**(value)
|
||||||
|
|
||||||
|
Attempts to parse `value` as a decimal literal and return it as an instance of
|
||||||
|
`Num`. If the number cannot be parsed `null` will be returned.
|
||||||
|
|
||||||
|
It is a runtime error if `value` is not a string.
|
||||||
@ -919,6 +919,22 @@ DEF_NATIVE(num_toString)
|
|||||||
RETURN_VAL(wrenNewString(vm, buffer, length));
|
RETURN_VAL(wrenNewString(vm, buffer, length));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DEF_NATIVE(num_fromString)
|
||||||
|
{
|
||||||
|
if (!validateString(vm, args, 1, "Argument")) return PRIM_ERROR;
|
||||||
|
|
||||||
|
ObjString* candidate = AS_STRING(args[1]);
|
||||||
|
|
||||||
|
char* end;
|
||||||
|
double number = strtod(candidate->value, &end);
|
||||||
|
|
||||||
|
// Invalid number literal.
|
||||||
|
// TODO: Check errno == ERANGE here.
|
||||||
|
if (end == candidate->value) RETURN_NULL;
|
||||||
|
|
||||||
|
RETURN_NUM(number);
|
||||||
|
}
|
||||||
|
|
||||||
DEF_NATIVE(num_negate)
|
DEF_NATIVE(num_negate)
|
||||||
{
|
{
|
||||||
RETURN_NUM(-AS_NUM(args[0]));
|
RETURN_NUM(-AS_NUM(args[0]));
|
||||||
@ -1455,6 +1471,7 @@ void wrenInitializeCore(WrenVM* vm)
|
|||||||
NATIVE(vm->nullClass, "toString", null_toString);
|
NATIVE(vm->nullClass, "toString", null_toString);
|
||||||
|
|
||||||
vm->numClass = defineClass(vm, "Num");
|
vm->numClass = defineClass(vm, "Num");
|
||||||
|
NATIVE(vm->numClass->obj.classObj, "fromString ", num_fromString);
|
||||||
NATIVE(vm->numClass, "-", num_negate);
|
NATIVE(vm->numClass, "-", num_negate);
|
||||||
NATIVE(vm->numClass, "- ", num_minus);
|
NATIVE(vm->numClass, "- ", num_minus);
|
||||||
NATIVE(vm->numClass, "+ ", num_plus);
|
NATIVE(vm->numClass, "+ ", num_plus);
|
||||||
@ -1480,7 +1497,7 @@ void wrenInitializeCore(WrenVM* vm)
|
|||||||
NATIVE(vm->numClass, "isNan", num_isNan);
|
NATIVE(vm->numClass, "isNan", num_isNan);
|
||||||
NATIVE(vm->numClass, "sin", num_sin);
|
NATIVE(vm->numClass, "sin", num_sin);
|
||||||
NATIVE(vm->numClass, "sqrt", num_sqrt);
|
NATIVE(vm->numClass, "sqrt", num_sqrt);
|
||||||
NATIVE(vm->numClass, "toString", num_toString)
|
NATIVE(vm->numClass, "toString", num_toString);
|
||||||
|
|
||||||
// These are defined just so that 0 and -0 are equal, which is specified by
|
// These are defined just so that 0 and -0 are equal, which is specified by
|
||||||
// IEEE 754 even though they have different bit representations.
|
// IEEE 754 even though they have different bit representations.
|
||||||
|
|||||||
13
test/number/from_string.wren
Normal file
13
test/number/from_string.wren
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
IO.print(Num.fromString("123") == 123) // expect: true
|
||||||
|
IO.print(Num.fromString("-123") == -123) // expect: true
|
||||||
|
IO.print(Num.fromString("-0") == -0) // expect: true
|
||||||
|
IO.print(Num.fromString("12.34") == 12.34) // expect: true
|
||||||
|
IO.print(Num.fromString("-0.0001") == -0.0001) // expect: true
|
||||||
|
IO.print(Num.fromString(" 12 ") == 12) // expect: true
|
||||||
|
IO.print(Num.fromString("1.2prefix") == 1.2) // expect: true
|
||||||
|
|
||||||
|
// Test some non-number literals and ensure they return null.
|
||||||
|
IO.print(Num.fromString("test1") == null) // expect: true
|
||||||
|
IO.print(Num.fromString("") == null) // expect: true
|
||||||
|
IO.print(Num.fromString("prefix1.2") == null) // expect: true
|
||||||
|
|
||||||
1
test/number/from_string_not_string.wren
Normal file
1
test/number/from_string_not_string.wren
Normal file
@ -0,0 +1 @@
|
|||||||
|
Num.fromString(1) // expect runtime error: Argument must be a string.
|
||||||
@ -6,5 +6,5 @@ IO.print(-0) // expect: -0
|
|||||||
IO.print(123.456) // expect: 123.456
|
IO.print(123.456) // expect: 123.456
|
||||||
IO.print(-0.001) // expect: -0.001
|
IO.print(-0.001) // expect: -0.001
|
||||||
|
|
||||||
// TODO: Hex? Scientific notation?
|
// TODO: Scientific notation?
|
||||||
// TODO: Literals at and beyond numeric limits.
|
// TODO: Literals at and beyond numeric limits.
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user