Backtalk Script Language
Tutorial

Version 1.1.23

© 2002: Jan Wolter, Steve Weiss

1. Introduction

Backtalk's script language is, well, unusual. It's a stack-based language, which means everything is written backwards. If you know the Postscript language then Backtalk's language will be very familiar. If you know the Forth language, then you have the basic idea. However, most likely you don't and probably Backtalk scripts will look totally weird to you. If you've used an old Hewlett-Packard RPN calculator or have a reasonably good grounding in computer science then at least you have the advantage of knowing about postfix notation.

This document is meant to give a enough of a basic introduction to the Backtalk script language to help you figure out what you are seeing when you look at a script, and make simple changes.

For full details of the language, see the Backtalk Script Language Reference Manual.

2. Postfix Notation and Stacks

This section introduces the basic idea of the Reverse Polish or Infix Notation used by Backtalk.

In a typical procedural computer language, you might write a statement like this:

   print("The answer is ",(abs(x) + 1) * 5);
This is a mixture of prefix notation and infix notation. The print and abs function are in prefix notation. The operator name preceeds a list of arguments. The + and * functions are in infix notation, with function name in between its arguments.

Postfix notation writes the operator after the arguments. So the statement above would be written as:

   "The answer is " x abs 1 + 5 * print
That's pretty weird looking. Let's take a simpler example:
   2 1 + 5 * print
This says:
  1. Take the number 2.
  2. Take the number 1.
  3. Add the two things you have and keep the result.
  4. Take the number 5.
  5. Multiply the two things you have and keep the result.
  6. Print everything you have.
So this prints the number 15. Note that we didn't need parenthesis to distinguish between (1 + 2)*5 and 1 + (2 * 5). That kind of ambiguity happens only with infix operators.

Returning to our more complex example:

   "The answer is " x abs 1 + 5 * print
what this does is
  1. Take the string "The answer is ".
  2. Take the value of the variable x.
  3. Find the absolute value of the thing you have and keep the result.
  4. Take the number 1.
  5. Add the two things you have and keep the result.
  6. Take the number 5.
  7. Multiply the two things you have and keep the result.
  8. Print everything you have.
Note that we start by taking the string and just hold onto it without operating on it at all. The abs function operates on the most recent thing we took, and the + and * times functions operate on the two most recent things we took. So clearly the computer needs to be able to keep track of a large number of things and their order.

The data structure used by computer to do this is a stack. It's just a linear pile of values which allows us to add a value onto the top, or take a value off the top. Adding a value is called pushing it. Deleting a value is called popping it.

So we'll run the above command again, this time using stack terminology and showing the stack after each step. We'll assume that the value of x is -7 and we'll write the stack with the bottom at the left and the top at the right:
OperationResulting Stack
(1)Push the string "The answer is ". "The answer is "
(2)Push the value of x. "The answer is " -7
(3)Pop a number, push its absolute value. "The answer is " 7
(4)Push 1. "The answer is " 7 1
(5)Pop two numbers, push their sum. "The answer is " 8
(6)Push 5. "The answer is " 8 5
(7)Pop two numbers, push their product. "The answer is " 40
(8)Print everything on the stack from bottom up. empty

So the execution of a Backtalk program is really very simple. Backtalk starts at the beginning of the program and executes one item at a time according to the following rules: