Skip to content
A edited this page Aug 21, 2019 · 1 revision

A Few Conventions of This Document

  • ∆ ... ∆ in a code snippet means that the code in the ... is optional
  • > in a code snippet means an input prompt
  • >>> in a code snippet means a command prompt

The Basics

Most tutorials show how to print the string Hello, World! , so that's what this tutorial will do as well. Here is a simple 21 byte program to achieve the goal.

Hello\, World\!^(!|,)

Explanation

Hello #Push the characters "H", "e", "l", "l" and "o" to the stack
\, #Escape the "," and push it to the stack
World #Push the characters "W", "o", "r", "l" and "d" to the stack
\! #Escape the "!" and push it to the stack
^ #Reverse the stack
(!| #Start a for loop and set the count to the length of the stack
    , #Print the last item on the stack as a character
)

In the above example, 6 new functions and keywords are introduced:

\ : Escapes the next command, and instead pushes it as a string (pushes its ascii value) , : Prints the last item on the stack as a character ! : Pushes the length of the stack onto the stack ^ : Reverses the stack (...) : The for loop structure | : Used in structures to switch from one branch to the other.

The Stack

One of the most important parts of Keg is the stack, which is where all operations are performed. A stack is a type of container (or list) where the last item in the container is the first item to be operated on (LIFO -- Last In First Out). In the following examples, the stack will be investigated.

3# [3]
4# [3, 4]
+# [7]

In the above example, the numbers 3 and 4 are pushed onto the stack, and are then added using the + operator. The way it works is that the + pops what will be called x and y off the stack (the first and second last item) and pushes y + x back onto the stack. Note that the order of x and y are important when using the - and \ operators, as x - y doesn't equal y - x most of the time (as is the same with x / y and y / x). This can be seen in the following example:

34-.#Outputs -1
43-.#Outputs 1
34/.#Outputs 0.75
43/.#Outputs 1.333333333333

Note that the . function prints the last item on the stack as an integer.

Input and Output

Keg has two output functions and one input function. When taking input from the user, the next line from the Standard Input and push the ascii value of each character onto the stack. It will not push -1 anymore onto the stack to sigify the end of input (input as integers will be coming in a later version of Keg). Input is taken using the ? command, as shown in the example program:

?(!|,)

# > Example text
# Example text

The two output functions (. -- Print as integer and , -- Print as string) have already been detailed in other sections.