mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 23:04:08 +10:00
So apparently, casting doens't need parens
This commit is contained in:
22
docs/spec.md
22
docs/spec.md
@@ -23,7 +23,6 @@ Names used in the language must start with a letter or underscore, and must cont
|
||||
The following list of keywords cannot be used as names, due to their significance (or potential later use) in the language.
|
||||
|
||||
* any
|
||||
* array
|
||||
* as
|
||||
* assert
|
||||
* bool
|
||||
@@ -31,7 +30,6 @@ The following list of keywords cannot be used as names, due to their significanc
|
||||
* class
|
||||
* const
|
||||
* continue
|
||||
* dictionary
|
||||
* do
|
||||
* else
|
||||
* export
|
||||
@@ -68,10 +66,10 @@ Likewise, the following logical operators are available (`&&` is more tightly bo
|
||||
( ) [ ] { } ! != == < > <= >= && ||
|
||||
```
|
||||
|
||||
Other operators used throughout the language are: the assignment, colon, semicolon, comma, dot, pipe, rest operators:
|
||||
Other operators used throughout the language are: the assignment, colon, semicolon, comma, dot, rest operators:
|
||||
|
||||
```
|
||||
= : ; , . | ...
|
||||
= : ; , . ...
|
||||
```
|
||||
|
||||
## Types
|
||||
@@ -86,8 +84,8 @@ Variable names in Toy may have a type. These types are:
|
||||
* array - a collection of 0-indexed variables
|
||||
* dictionary - a collection of indexable key-value pairs
|
||||
* function - chunks of reusable code
|
||||
* any - any of the above
|
||||
* type - the type of types
|
||||
* any - any of the above
|
||||
|
||||
Types are optional attachments to names - they can be specified as such:
|
||||
|
||||
@@ -99,8 +97,8 @@ var x : int = 0;
|
||||
Types are not interoperable, but in some cases they can be converted from one type to another by "casting":
|
||||
|
||||
```
|
||||
//declare the variable named "y" as a float, and assign it the value stored in x
|
||||
var y : float = (float)x;
|
||||
//declare the variable named "y" as a float, and assign it the value stored in x (which is being cast to a float)
|
||||
var y : float = float x;
|
||||
```
|
||||
|
||||
Defining the type of a variable is not required - in such a case, the type is "any".
|
||||
@@ -110,7 +108,7 @@ Defining the type of a variable is not required - in such a case, the type is "a
|
||||
var z = 1;
|
||||
```
|
||||
|
||||
Variables are first-class citizens, meaning they can be stored in and used from variables:
|
||||
Types are first-class citizens, meaning they can be stored in and used from variables:
|
||||
|
||||
```
|
||||
//this was originally unintended, but interesting
|
||||
@@ -506,22 +504,22 @@ fn _get(self, key) {
|
||||
}
|
||||
|
||||
//usable with arrays
|
||||
fn push(self, val) {
|
||||
fn _push(self, val) {
|
||||
//native code
|
||||
}
|
||||
|
||||
//usable with arrays
|
||||
fn pop(self) {
|
||||
fn _pop(self) {
|
||||
//native code
|
||||
}
|
||||
|
||||
//usable with arrays, dictionaries and strings
|
||||
fn length(self) {
|
||||
fn _length(self) {
|
||||
//native code
|
||||
}
|
||||
|
||||
//usable with arrays and dictionaries
|
||||
fn clear(self) {
|
||||
fn _clear(self) {
|
||||
//native code
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1,33 +1,37 @@
|
||||
//boolean origin
|
||||
var b: bool = true;
|
||||
|
||||
assert (int)b == 1, "bool -> int";
|
||||
assert (float)b == 1, "bool -> float";
|
||||
assert (string)b == "true", "bool -> string";
|
||||
assert bool b == true, "bool -> bool";
|
||||
assert int b == 1, "bool -> int";
|
||||
assert float b == 1, "bool -> float";
|
||||
assert string b == "true", "bool -> string";
|
||||
|
||||
|
||||
//integer origin
|
||||
var i: int = 42;
|
||||
|
||||
assert (bool)i == true, "int -> bool";
|
||||
assert (float)i == 42, "int -> float";
|
||||
assert (string)i == "42", "int -> string";
|
||||
assert bool i == true, "int -> bool";
|
||||
assert int i == 42, "int -> int";
|
||||
assert float i == 42, "int -> float";
|
||||
assert string i == "42", "int -> string";
|
||||
|
||||
|
||||
//float origin
|
||||
var f: float = 3.14;
|
||||
|
||||
assert (bool)f == true, "float -> bool";
|
||||
assert (int)f == 3, "float -> int";
|
||||
assert (string)f == "3.14", "float -> string";
|
||||
assert bool f == true, "float -> bool";
|
||||
assert int f == 3, "float -> int";
|
||||
assert float f == 3.14, "float -> float";
|
||||
assert string f == "3.14", "float -> string";
|
||||
|
||||
|
||||
//string origin
|
||||
var s: string = "78.9";
|
||||
|
||||
assert (bool)s == true, "string -> bool";
|
||||
assert (int)s == 78, "string -> int";
|
||||
assert (float)s == 78.9, "string -> float";
|
||||
assert bool s == true, "string -> bool";
|
||||
assert int s == 78, "string -> int";
|
||||
assert float s == 78.9, "string -> float";
|
||||
assert string s == "78.9", "string -> string";
|
||||
|
||||
|
||||
print "All good";
|
||||
|
||||
@@ -1051,6 +1051,10 @@ static bool execValCast(Interpreter* interpreter) {
|
||||
result = TO_INTEGER_LITERAL(AS_BOOLEAN(value) ? 1 : 0);
|
||||
}
|
||||
|
||||
if (IS_INTEGER(value)) {
|
||||
result = copyLiteral(value);
|
||||
}
|
||||
|
||||
if (IS_FLOAT(value)) {
|
||||
result = TO_INTEGER_LITERAL(AS_FLOAT(value));
|
||||
}
|
||||
@@ -1071,6 +1075,10 @@ static bool execValCast(Interpreter* interpreter) {
|
||||
result = TO_FLOAT_LITERAL(AS_INTEGER(value));
|
||||
}
|
||||
|
||||
if (IS_FLOAT(value)) {
|
||||
result = copyLiteral(value);
|
||||
}
|
||||
|
||||
if (IS_STRING(value)) {
|
||||
float val = 0;
|
||||
sscanf(AS_STRING(value), "%f", &val);
|
||||
@@ -1096,6 +1104,10 @@ static bool execValCast(Interpreter* interpreter) {
|
||||
snprintf(buffer, 128, "%g", AS_FLOAT(value));
|
||||
result = TO_STRING_LITERAL(copyString(buffer, strlen(buffer)), strlen(buffer));
|
||||
}
|
||||
|
||||
if (IS_STRING(value)) {
|
||||
result = copyLiteral(value);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user