Skip to content

Instantly share code, notes, and snippets.

@danielepolencic
Last active August 29, 2015 14:10
Show Gist options
  • Save danielepolencic/82392b5f69ecc5243d4c to your computer and use it in GitHub Desktop.
Save danielepolencic/82392b5f69ecc5243d4c to your computer and use it in GitHub Desktop.
gravity language

The gravity language operates on a stack and has three basic operations:

  • Zi sets the i th element of the stack to zero
  • Ii increments the i th element by one
  • Ji,j compares the i th and j th elements and jumps if not equal

The stack looks like this:

+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+
  0   1   2   3   4   5   6   7

Assume you want to write an algorithm that compares two numbers and returns 1 if the former is greater than the latter. Let's pick 2 and 3. You can load the number 2 on the stack like this:

  1. Z0
  2. I0
  3. I0
+---+---+---+---+---+---+---+---+
| 2 |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+
0   1   2   3   4   5   6   7

This will load the number 3 on the stack:

  1. Z1
  2. I1
  3. I1
  4. I1
+---+---+---+---+---+---+---+---+
| 2 | 3 |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+
0   1   2   3   4   5   6   7

We can compare the numbers in position 0 and 1 and if they're different we jump and execute a different instruction:

  1. Z4
  2. J0,1 JUMP TO 4
  3. END
  4. I4
  5. END
+---+---+---+---+---+---+---+---+
| 2 | 3 |   |   | 1 |   |   |   |
+---+---+---+---+---+---+---+---+
  0   1   2   3   4   5   6   7

Great, we just compared 2 numbers!

You're now familiar with the gravity language. Can you write an algorithm that sums two numbers?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment