Tweaked wording

This commit is contained in:
2025-02-02 22:33:11 +11:00
parent d95c6e6479
commit f7bab4da10

View File

@@ -10,9 +10,7 @@ To help you start using Toy as fast as possible, here are the most useful elemen
## Keyword 'print'
The `print` keyword takes one value as a parameter, which is sent to stdout by default.
The value can be redirected elsewhere using the [debug C API](c_api/debug).
The `print` keyword takes one value as a parameter, which is sent to stdout by default, or can be redirected elsewhere using the [output C API](/c_api/output).
```
print "Hello World!";
@@ -20,21 +18,21 @@ print "Hello World!";
## Keyword 'assert'
The `assert` keyword takes two values as parameters, separated by a comma. If the first value is `null` or `false`, the second parameter is sent to stderr by default. Otherwise, this statement is ignored.
The `assert` keyword takes two values as parameters, separated by a comma. If the first value is falsy or `null`, the optional second parameter is sent to stderr by default, or can be redirected elsewhere using the [output C API](/c_api/output). If no second parameter is provided, a generic message is used instead.
The second value, which is replaced by a default error message if omitted, can be redirected elsewhere using the [debug C API](c_api/debug).
The generation of assert statements can be disabled using the [parser C API](c_api/parser).
An option to disable the `assert` keyword during compilation is provided in the [parser C API](c_api/parser).
```
//nothing happens
assert 1 < 2;
//this assert will fail, and output the second parameter
assert null, "Hello world!";
```
## Variables and Types
Values can be stored in variables, by specifying a name with the `var` keyword. The name can also be declared with a type, which restricts what kind of value can be stored in the name. Types are optional, and defaults to `any` when not used.
Values can be stored in variables, by specifying a name with the `var` keyword. The name can be declared with an optional type, which restricts the type of value that can be stored in the name. If no type is specified, `any` is used instead.
```
var answer = 42;
@@ -42,39 +40,39 @@ var answer = 42;
var question: string = "How many roads must a man walk down?";
```
To make a variable unchangeable after declaration, you can add the `const` keyword after the type.
To make a variable immutable, you can add the `const` keyword after the type when it's declared. If you do, it must be assigned a value.
```
var quote: string const = "War. War never changes.";
```
The usable types are as follows:
The types available in Toy are:
| type | name | description |
| --- | --- | --- |
| `bool` | boolean | Either `true` or `false`. |
| `int` | integer | Any whole number, representable by a signed 32-bit integer. |
| `float` | float | A decimal number, represented by 32-bits for floating-point arithmetic. |
| `string` | string | A piece of text, supports UTF-8. |
| `array` | array | A series of values stored in sequential memory. |
| `table` | table | A series key-value pairs stored in such a way that allows for fast lookups. Booleans, functions and opaques can't be used as keys. |
| `function` | function | A chunk of code to be called. It can take multiple parameters, and provide multiple return values. Unlike other variables, functions are declared with the `fn` keyword. |
| `opaque` | opaque | The contents of this value is unusable in the script, but can be passed from one API to another. |
| `any` | any | The default type used when none is specified. This type can hold any value. |
| `int` | integer | Any whole number (32-bits). |
| `float` | float | A decimal number (32-bits), using floating-point arithmetic. |
| `string` | string | A piece of text, supports UTF-8, [in theory](https://github.com/Ratstail91/Toy/issues/174). |
| `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. |
| `function` | function | A chunk of reusable code that takes zero or more parameters, and returFunctions are declared with the `fn` keyword. |
| `opaque` | opaque | This value is unusable in the script, but can be passed from one imported function to another. |
| `any` | any | The default type when nothing is specified. Theis can hold any value. |
*Note: Functions and opaques are not yet implemented at the time of writing.*
*Note: Functions and opaques are not fully implemented at the time of writing, so details may change.*
## Control Flow
Performing different actions, or repeating an action multiple times, is essential for any general purpose language.
Choosing an option, 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. If the condition is not truthy, 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.
```
var answer = 42;
if (answer < 56) {
print "Success";
print "Cod dang it!";
}
else {
print "Something's fishy here...";
@@ -115,6 +113,8 @@ while (true) {
}
```
*Note: The `for` loop is coming, eventually, but isn't vital right now.*
## Functions
Watch this space.