granite-docs/docs/manipulating-values.md
2024-08-16 14:48:06 +01:00

50 lines
2 KiB
Markdown

# Manipulating values
Granite does not allow you to modify literals directly, instead requiring you to use bindings to variables and modifying them within bindings, keeping all values tracked and stored.
## New operations
* `:>(value),(variable)`, set variable, Stores `(value)` into `(variable)`
* `:^(variable)`, increment, if `(variable)` is a number, add one to it.
* `:v(variable)`, decrement, if `(variable)` is a number, remove one from it.
* `:!(variable)`, print, outputs the value in `(variable)`
## Tutorial
To begin your program, make a new file called `counter.granite`. This will be the source code of your program!
Throughout this guidebook, we will be creating a program which is able to count up from a starting number to a final number by a certain step each time! To do this, we first need to learn the basics of value manipulation.
In your program's source place these two statements in:
```
:>0,starting_number
:!starting_number
```
These two statements will simply create (initialise) the `starting_number` variable with the value 0 and print it to the screen!
Now run your program with the Granite runtime. There are two ways to do this:
* If you have installed Granite to your path, type `granite -i counter.granite`.
* If you are in your local clone of the Granite runtime repository, type `cargo run -- -i counter.granite`.
You should now see the program run and output 0!
If the program does _not_ run and gives you an error, make sure your source code matches the above example _exactly_. The Granite language does not like having spaces between variables and commas, and will fail if you add spaces.
Next, we want to _manipulate_ the value. We can do this by using the `:^(variable)` operation!
Make your program's source code look like _this_ now:
```
:>0,starting_number
:^starting_number
:!starting_number
```
Now run your program, you should see the number 1!
## Extension
Try using the _decrement_ operation (`:v(variable)`) to make the output a negative number!
## Continue the guidebook
* [Next page: Jumps and labels](jumps-labels.md)