mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 23:04:08 +10:00
Changed truthiness
This commit is contained in:
@@ -9,8 +9,8 @@ DONE: optional block around a path if it's only one statement
|
|||||||
DONE: while-then
|
DONE: while-then
|
||||||
DONE: for-then
|
DONE: for-then
|
||||||
DONE: break and continue statements
|
DONE: break and continue statements
|
||||||
|
DONE: truthiness rethink
|
||||||
|
|
||||||
TODO: truthiness rethink
|
|
||||||
TODO: string concat with the + operator
|
TODO: string concat with the + operator
|
||||||
TODO: increment & decrement operators
|
TODO: increment & decrement operators
|
||||||
TODO: a = b = c = 1;
|
TODO: a = b = c = 1;
|
||||||
|
|||||||
@@ -123,11 +123,7 @@ Otherwise, constants act just like normal variables.
|
|||||||
|
|
||||||
## Truthyness
|
## Truthyness
|
||||||
|
|
||||||
Everything is considered "truthy" except:
|
Everything is considered "truthy" except the value `false`. Trying to use `null` in a conditional (except assert) will give an error.
|
||||||
|
|
||||||
* the value `null`
|
|
||||||
* the value `false`
|
|
||||||
* the integer and float value `0`
|
|
||||||
|
|
||||||
## Print
|
## Print
|
||||||
|
|
||||||
@@ -324,7 +320,7 @@ fn omitFirstInteger(arg1: int, ...rest: [int]) {
|
|||||||
|
|
||||||
## Assert
|
## Assert
|
||||||
|
|
||||||
The `assert` keyword takes 2 parameters, separated by a comma. If the first parameter resolves to be falsy, then the program terminates, and the value of the second parameter is displayed to the user as an error. By default, the error is printed to stderr, but this can be overwritten by the host program.
|
The `assert` keyword takes 2 parameters, separated by a comma. If the first parameter resolves to be false or is null, then the program terminates, and the value of the second parameter is displayed to the user as an error. By default, the error is printed to stderr, but this can be overwritten by the host program.
|
||||||
|
|
||||||
```
|
```
|
||||||
assert true, "This is fine"; //Good!
|
assert true, "This is fine"; //Good!
|
||||||
|
|||||||
@@ -137,7 +137,7 @@ static bool execAssert(Interpreter* interpreter) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!IS_TRUTHY(lhs)) {
|
if (IS_NULL(lhs) || !IS_TRUTHY(lhs)) {
|
||||||
(*interpreter->assertOutput)(AS_STRING(rhs));
|
(*interpreter->assertOutput)(AS_STRING(rhs));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -396,6 +396,11 @@ static bool execValCast(Interpreter* interpreter) {
|
|||||||
|
|
||||||
Literal result = TO_NULL_LITERAL;
|
Literal result = TO_NULL_LITERAL;
|
||||||
|
|
||||||
|
if (IS_NULL(value)) {
|
||||||
|
printf("Can't cast a null value\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
//cast the rhs to the type represented by lhs
|
//cast the rhs to the type represented by lhs
|
||||||
switch(AS_TYPE(type).typeOf) {
|
switch(AS_TYPE(type).typeOf) {
|
||||||
case LITERAL_BOOLEAN:
|
case LITERAL_BOOLEAN:
|
||||||
@@ -599,6 +604,11 @@ static bool execFalseJump(Interpreter* interpreter) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (IS_NULL(lit)) {
|
||||||
|
printf("Null detected in comparison\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (!IS_TRUTHY(lit)) {
|
if (!IS_TRUTHY(lit)) {
|
||||||
interpreter->count = target + interpreter->codeStart;
|
interpreter->count = target + interpreter->codeStart;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -323,6 +323,7 @@ void freeLiteral(Literal literal) {
|
|||||||
|
|
||||||
bool _isTruthy(Literal x) {
|
bool _isTruthy(Literal x) {
|
||||||
if (IS_NULL(x)) {
|
if (IS_NULL(x)) {
|
||||||
|
fprintf(stderr, "Null is neither true nor false");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -330,16 +331,6 @@ bool _isTruthy(Literal x) {
|
|||||||
return AS_BOOLEAN(x);
|
return AS_BOOLEAN(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IS_INTEGER(x)) {
|
|
||||||
return AS_INTEGER(x) != 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (IS_FLOAT(x)) {
|
|
||||||
return AS_FLOAT(x) != 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO: empty string as falsy?
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user