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.
|
The following list of keywords cannot be used as names, due to their significance (or potential later use) in the language.
|
||||||
|
|
||||||
* any
|
* any
|
||||||
* array
|
|
||||||
* as
|
* as
|
||||||
* assert
|
* assert
|
||||||
* bool
|
* bool
|
||||||
@@ -31,7 +30,6 @@ The following list of keywords cannot be used as names, due to their significanc
|
|||||||
* class
|
* class
|
||||||
* const
|
* const
|
||||||
* continue
|
* continue
|
||||||
* dictionary
|
|
||||||
* do
|
* do
|
||||||
* else
|
* else
|
||||||
* export
|
* 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
|
## Types
|
||||||
@@ -86,8 +84,8 @@ Variable names in Toy may have a type. These types are:
|
|||||||
* array - a collection of 0-indexed variables
|
* array - a collection of 0-indexed variables
|
||||||
* dictionary - a collection of indexable key-value pairs
|
* dictionary - a collection of indexable key-value pairs
|
||||||
* function - chunks of reusable code
|
* function - chunks of reusable code
|
||||||
* any - any of the above
|
|
||||||
* type - the type of types
|
* type - the type of types
|
||||||
|
* any - any of the above
|
||||||
|
|
||||||
Types are optional attachments to names - they can be specified as such:
|
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":
|
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
|
//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;
|
var y : float = float x;
|
||||||
```
|
```
|
||||||
|
|
||||||
Defining the type of a variable is not required - in such a case, the type is "any".
|
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;
|
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
|
//this was originally unintended, but interesting
|
||||||
@@ -506,22 +504,22 @@ fn _get(self, key) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//usable with arrays
|
//usable with arrays
|
||||||
fn push(self, val) {
|
fn _push(self, val) {
|
||||||
//native code
|
//native code
|
||||||
}
|
}
|
||||||
|
|
||||||
//usable with arrays
|
//usable with arrays
|
||||||
fn pop(self) {
|
fn _pop(self) {
|
||||||
//native code
|
//native code
|
||||||
}
|
}
|
||||||
|
|
||||||
//usable with arrays, dictionaries and strings
|
//usable with arrays, dictionaries and strings
|
||||||
fn length(self) {
|
fn _length(self) {
|
||||||
//native code
|
//native code
|
||||||
}
|
}
|
||||||
|
|
||||||
//usable with arrays and dictionaries
|
//usable with arrays and dictionaries
|
||||||
fn clear(self) {
|
fn _clear(self) {
|
||||||
//native code
|
//native code
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -1,33 +1,37 @@
|
|||||||
//boolean origin
|
//boolean origin
|
||||||
var b: bool = true;
|
var b: bool = true;
|
||||||
|
|
||||||
assert (int)b == 1, "bool -> int";
|
assert bool b == true, "bool -> bool";
|
||||||
assert (float)b == 1, "bool -> float";
|
assert int b == 1, "bool -> int";
|
||||||
assert (string)b == "true", "bool -> string";
|
assert float b == 1, "bool -> float";
|
||||||
|
assert string b == "true", "bool -> string";
|
||||||
|
|
||||||
|
|
||||||
//integer origin
|
//integer origin
|
||||||
var i: int = 42;
|
var i: int = 42;
|
||||||
|
|
||||||
assert (bool)i == true, "int -> bool";
|
assert bool i == true, "int -> bool";
|
||||||
assert (float)i == 42, "int -> float";
|
assert int i == 42, "int -> int";
|
||||||
assert (string)i == "42", "int -> string";
|
assert float i == 42, "int -> float";
|
||||||
|
assert string i == "42", "int -> string";
|
||||||
|
|
||||||
|
|
||||||
//float origin
|
//float origin
|
||||||
var f: float = 3.14;
|
var f: float = 3.14;
|
||||||
|
|
||||||
assert (bool)f == true, "float -> bool";
|
assert bool f == true, "float -> bool";
|
||||||
assert (int)f == 3, "float -> int";
|
assert int f == 3, "float -> int";
|
||||||
assert (string)f == "3.14", "float -> string";
|
assert float f == 3.14, "float -> float";
|
||||||
|
assert string f == "3.14", "float -> string";
|
||||||
|
|
||||||
|
|
||||||
//string origin
|
//string origin
|
||||||
var s: string = "78.9";
|
var s: string = "78.9";
|
||||||
|
|
||||||
assert (bool)s == true, "string -> bool";
|
assert bool s == true, "string -> bool";
|
||||||
assert (int)s == 78, "string -> int";
|
assert int s == 78, "string -> int";
|
||||||
assert (float)s == 78.9, "string -> float";
|
assert float s == 78.9, "string -> float";
|
||||||
|
assert string s == "78.9", "string -> string";
|
||||||
|
|
||||||
|
|
||||||
print "All good";
|
print "All good";
|
||||||
|
|||||||
@@ -1051,6 +1051,10 @@ static bool execValCast(Interpreter* interpreter) {
|
|||||||
result = TO_INTEGER_LITERAL(AS_BOOLEAN(value) ? 1 : 0);
|
result = TO_INTEGER_LITERAL(AS_BOOLEAN(value) ? 1 : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (IS_INTEGER(value)) {
|
||||||
|
result = copyLiteral(value);
|
||||||
|
}
|
||||||
|
|
||||||
if (IS_FLOAT(value)) {
|
if (IS_FLOAT(value)) {
|
||||||
result = TO_INTEGER_LITERAL(AS_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));
|
result = TO_FLOAT_LITERAL(AS_INTEGER(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (IS_FLOAT(value)) {
|
||||||
|
result = copyLiteral(value);
|
||||||
|
}
|
||||||
|
|
||||||
if (IS_STRING(value)) {
|
if (IS_STRING(value)) {
|
||||||
float val = 0;
|
float val = 0;
|
||||||
sscanf(AS_STRING(value), "%f", &val);
|
sscanf(AS_STRING(value), "%f", &val);
|
||||||
@@ -1096,6 +1104,10 @@ static bool execValCast(Interpreter* interpreter) {
|
|||||||
snprintf(buffer, 128, "%g", AS_FLOAT(value));
|
snprintf(buffer, 128, "%g", AS_FLOAT(value));
|
||||||
result = TO_STRING_LITERAL(copyString(buffer, strlen(buffer)), strlen(buffer));
|
result = TO_STRING_LITERAL(copyString(buffer, strlen(buffer)), strlen(buffer));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (IS_STRING(value)) {
|
||||||
|
result = copyLiteral(value);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|||||||
Reference in New Issue
Block a user