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