|
|
@@ -24,12 +24,12 @@ assert null, "Hello world!";
|
|
|
|
|
|
|
|
|
|
|
|
## Variables and Types
|
|
|
|
## Variables and Types
|
|
|
|
|
|
|
|
|
|
|
|
Variables can be declared with the `var` keyword, and can be given an optional type from the list below. If no type is specified, `any` is used by default.
|
|
|
|
Variables can be declared with the `var` keyword, and can be given an optional type from the list below. If no type is specified, `Any` is used by default.
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|
var answer = 42;
|
|
|
|
var answer = 42;
|
|
|
|
|
|
|
|
|
|
|
|
var question: string = "How many roads must a man walk down?";
|
|
|
|
var question: String = "How many roads must a man walk down?";
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
To make a variable immutable, use the `const` keyword after the type declaration. In this case, it must be assigend a value.
|
|
|
|
To make a variable immutable, use the `const` keyword after the type declaration. In this case, it must be assigend a value.
|
|
|
@@ -42,19 +42,19 @@ The types available in Toy are:
|
|
|
|
|
|
|
|
|
|
|
|
| type | name | description |
|
|
|
|
| type | name | description |
|
|
|
|
| --- | --- | --- |
|
|
|
|
| --- | --- | --- |
|
|
|
|
| `bool` | boolean | Either `true` or `false`. |
|
|
|
|
| `Bool` | Boolean | Either `true` or `false`. |
|
|
|
|
| `int` | integer | Any whole number (32-bits). |
|
|
|
|
| `Int` | Integer | Any whole number (32-bits). |
|
|
|
|
| `float` | float | A decimal number (32-bits), using floating-point arithmetic. |
|
|
|
|
| `Float` | Float | A decimal number (32-bits), using floating-point arithmetic. |
|
|
|
|
| `string` | string | A series of characters used for text processing. |
|
|
|
|
| `String` | String | A series of characters used for text processing. |
|
|
|
|
| `array` | array | A series of values stored sequentially in memory. |
|
|
|
|
| `Array` | Array | A series of values stored sequentially in memory. |
|
|
|
|
| `table` | table | A series key-value pairs stored in such a way that allows for fast lookups. Booleans, functions, opaques and `null` can't be used as keys. |
|
|
|
|
| `Table` | Table | A series key-value pairs stored in such a way that allows for fast lookups. Booleans, functions, opaques and `null` can't be used as keys. |
|
|
|
|
| `function` | function | A chunk of reusable code that takes zero or more parameters, and returns zero or more results. Functions are declared with the `fn` keyword. |
|
|
|
|
| `Function` | Function | A chunk of reusable code that takes zero or more parameters, and may return a result. Functions are declared with the `fn` keyword. |
|
|
|
|
| `opaque` | opaque | This value is unusable in Toy, but allows you to pass data between C functions. |
|
|
|
|
| `Opaque` | Opaque | This value is unusable in Toy, but allows you to pass data between C bindings. |
|
|
|
|
| `any` | any | The default type when nothing is specified. Theis can hold any value. |
|
|
|
|
| `Any` | Any | The default type when nothing is specified. Theis can hold any value. |
|
|
|
|
|
|
|
|
|
|
|
|
## Control Flow
|
|
|
|
## Control Flow
|
|
|
|
|
|
|
|
|
|
|
|
Choosing an option, or repeating a chunk of code multiple times, is essential for any general purpose language.
|
|
|
|
Making a decision, or repeating a chunk of code multiple times, is essential for any general purpose language.
|
|
|
|
|
|
|
|
|
|
|
|
Choosing between two options can be done with the `if-then-else` else statement. If the condition is truthy, the 'then-branch' will be executed. Otherwise, the optional 'else-branch' is executed instead.
|
|
|
|
Choosing between two options can be done with the `if-then-else` else statement. If the condition is truthy, the 'then-branch' will be executed. Otherwise, the optional 'else-branch' is executed instead.
|
|
|
|
|
|
|
|
|
|
|
@@ -110,37 +110,70 @@ while (true) {
|
|
|
|
Arrays are defined with a pair of brackets, and can contain a list of comma-separated values.
|
|
|
|
Arrays are defined with a pair of brackets, and can contain a list of comma-separated values.
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|
//'array' is a reserved keyword, so it can't be used as a name
|
|
|
|
//define an array
|
|
|
|
var a = [1,2,3];
|
|
|
|
var array = [1,2,3];
|
|
|
|
|
|
|
|
|
|
|
|
//instead, it's used as a type
|
|
|
|
//specify the type
|
|
|
|
var b: array = [4,5,6];
|
|
|
|
var bray: Array = [4,5,6];
|
|
|
|
|
|
|
|
|
|
|
|
//define an empty array like this
|
|
|
|
//define an empty array
|
|
|
|
var c: array = [];
|
|
|
|
var craycray: Array = [];
|
|
|
|
|
|
|
|
|
|
|
|
//arrays are zero-indexed
|
|
|
|
//arrays are zero-indexed
|
|
|
|
print a[0]; //'1'
|
|
|
|
print array[0]; //'1'
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
Tables are also defined with brackets, and contain a comma-separated list of key-value pairs defined by colons:
|
|
|
|
Tables are also defined with brackets, and contain a comma-separated list of key-value pairs defined by colons:
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|
//most types can be used as keys
|
|
|
|
//most types can be used as keys
|
|
|
|
var t = ["alpha": 1, "beta": 2, "gamma": 3];
|
|
|
|
var table = ["alpha": 1, "beta": 2, "gamma": 3];
|
|
|
|
|
|
|
|
|
|
|
|
//the 'table' keyword can define the type, and an empty table still has a colon
|
|
|
|
//the 'Table' keyword can define the type, and an empty table still has a colon
|
|
|
|
var u: table = [:];
|
|
|
|
var under: Table = [:];
|
|
|
|
|
|
|
|
|
|
|
|
//printing a table does NOT guarantee internal order, but the elements can be accessed with the keys.
|
|
|
|
//printing a table does NOT guarantee internal order, but the elements can be accessed with the keys.
|
|
|
|
print t["beta"];
|
|
|
|
print table["beta"];
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Attributes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Some value types, including Strings, Arrays and Tables, have "attributes" which are accessible with the dot `.` operator. These can expose internal values or components for manipulating said values.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
var string = "Hello World";
|
|
|
|
|
|
|
|
print string.length; //11
|
|
|
|
|
|
|
|
print string.asUpper; //HELLO WORLD
|
|
|
|
|
|
|
|
print string.asLower; //hello world
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var array = [1,2,3];
|
|
|
|
|
|
|
|
array.pushBack(4); //array = [1,2,3,4]
|
|
|
|
|
|
|
|
var element = array.popBack(); //element = 4
|
|
|
|
|
|
|
|
var emptyArray = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var table = ["alpha": 1, "beta":2];
|
|
|
|
|
|
|
|
print table.length; //2
|
|
|
|
|
|
|
|
table.insert("key",element); //table["key"] = 4
|
|
|
|
|
|
|
|
print table.hasKey("alpha"); //true
|
|
|
|
|
|
|
|
table.remove("alpha"); //table = ["beta":2,"key":4]
|
|
|
|
|
|
|
|
var emptyTable = [:];
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Functions
|
|
|
|
## Functions
|
|
|
|
|
|
|
|
|
|
|
|
Watch this space.
|
|
|
|
Functions are defined with the `fn` keyword, and follow a c-like syntax, with optional types on the parameters:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
fn fib(n: Int) {
|
|
|
|
|
|
|
|
if (n < 2) return n;
|
|
|
|
|
|
|
|
return fib(n-1) + fib(n-2);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print fib(12); //144
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## External Libraries and Extending Toy
|
|
|
|
## External Libraries and Extending Toy
|
|
|
|
|
|
|
|
|
|
|
|
Watch this space.
|
|
|
|
Watch this space, docs for the C API are coming soon.
|
|
|
|
|
|
|
|
|