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

3.3 KiB

Jumps and labels

Granite does not have conventional loops similar to a C-like programming language does. It instead is more alike to BASIC having labels and jumps.

Labels are operations that have no direct function when ran in the program but they mark where you can jump to! Jumps move where the program is being ran from to labels! These tools together allows you to create loops!

New operations

  • :@(label), label, places a new label at this location
  • :<(label), unconditional jump, jumps to this label without any condition
  • :-(label),(variable),(check), jump if less than, jumps to this label if (variable) is less than (check) which can be a variable or an integer literal.
  • :a(left),(right),(output), add, performs (left) add (right) and stores the result into (output)

There are also + and = operators which function like the - operator but check if (variable) is greater than (for +) or equal to (for =) (check) instead.

There are also s, m and d operators which function like the a operator but perform subtraction, multiplication, and division respectively.

Tutorial

Open up your program! It should look something like this:

:>0,starting_number
:^starting_number
:!starting_number

At the top of your program's source code add this operation:

:>1,step

This defines your counter's step!

Now replace the increment operation with this add operation:

:astarting_number,step,starting_number

What this operation does is takes starting_number and step and adds them together and then stores that into starting_number!

If you run your program now, you should get the number 1 as an output. Wonderful!

Now to add looping!

Your program should look like this:

:>1,step
:>0,starting_number
:astarting_number,step,starting_number
:!starting_number

As we are counting up, we want to loop where the counting happens and not where the variable setting happens!

This means we want to set a new label above the add operation so above the add operation, add this operation:

:@count

By itself, this won't create a loop but we can do that easily! At the bottom of your program, below the print operation, add a new unconditional jump operation!

:<count

Now if you start your program, you will see it spit out a stream of numbers as it quickly keeps counting from 0! If this happens, wonderful, you've just created your first loop!

Exit the program (this will most likely be with Control + C) and re-enter the source code! We want to count up to a certain number, let's say 10!

To do this, at the top of your program, initialise a new variable called finishing_number with the number 10 in it!

Now at the bottom of your program, replace your unconditional jump operation with this conditional one!

:-count,starting_number,finishing_number

This conditional jump will continue looping until starting_number is not less than finishing_number!

If you run your program now you should see this output:

1
2
3
4
5
6
7
8
9
10

Congratulations! You've not only made loops but also loops that break when a condition is met!

Extension

Try messing with the step and finishing_number variables to see different outputs when you add more in one loop around!

Continue the guidebook