forge Documentation

expressions

Expressions

Expressions are used to perform calculations in the language.

Expression Types

There are three main types of expressions:

  • Numeric expressions (integer and floating point)
  • Boolean expressions
  • String expressions

Numeric Expressions

Integer Expressions

In FORGE, standard numeric expressions are evaluated as integers from -32768 to 32767, this is called 16-bit signed integer values.

Floating Point Expressions

Floating point expressions are used only if numbers have a decimal point. Floating point numbers are stored with standard Atari binary-coded decimal (BCD) representation, with a range from 1E-98 to 1E+98.

Boolean Expressions

Boolean expressions are "true" or "false", represented as the numbers 1 and 0, respectively.

Note that any boolean value can also be used as a numeric value, in this case, "true" is converted to 1 and "false" converted to 0.

String Expressions

String expressions contain arbitrary text. In FORGE strings can have up to 255 characters of length. (This is similar to Microsoft BASICs found on other 8-bit microcomputers, and in contrast to the 32K limit found in Atari BASIC and TurboBASIC XL.)

Numeric Values

Integer Values

Integer values can be written as:

  • Decimal numbers (like 123, 1000, etc.)
  • Hexadecimal numbers with a $ sign before (like $1C0, $A00, etc.)
  • Variable names

Examples:

A = 123
B = $FF
C = A + B

Floating Point Values

Floating point values are written with a decimal dot and an optional exponent (like 3.14159, -3.2, or 1.0E+10).

Examples:

PI = 3.14159
BIG = 1.0E+10
SMALL = -3.2

Numeric Variables

Variable Naming

Variable names must begin with a letter or the symbol _, and can contain any letter, number or the symbol _.

Examples of valid integer variable names:

  • COUNTER
  • My_Var
  • num1
  • _temp

Floating Point Variables

Floating point variables have an % as last character in the name.

Examples:

  • MyNum%
  • x1%
  • result%

Variable Declaration

In FORGE, variables can't be used in an expression before being assigned a value; the first assignment declares the new variable.

A = 10    ' Declares A as integer
B% = 3.14 ' Declares B% as floating point
C = A + B ' Error! Can't mix types directly

Numeric Operators

There are various "operators" that perform calculations in expressions. The operators with higher precedence are always executed first.

Integer Operator Precedence

The order of precedence for integer operators (from lowest to highest):

  1. Addition, Subtraction (+, -) - from left to right
  2. Multiplication, Division, Modulus (*, /, MOD) - from left to right
  3. Binary Operations (&, !, EXOR) - binary 'and', 'or', 'exclusive or' - from left to right
  4. Unary Positive/Negative (+, -) - positive / negative

Note: MOD and EXOR can be abbreviated M. and E. respectively.

Example: Operator Precedence

For example, an expression like 1 + 2 * 3 - 4 * -5 is evaluated in the following order:

  1. First, the unary - before the 5, giving the number -5.
  2. The first multiplication, giving 2*3 = 6.
  3. The second multiplication, giving 4*-5 = -20.
  4. The addition, giving 1+6 = 7.
  5. The subtraction, giving 7 - -20 = 27.

So, in this example the result is 27.

Parentheses

If there is a need to alter the precedence, you can place expressions within parentheses (e.g., (2+5)*10, which results in 70).

Floating Point Operators

When using floating point expressions, the operators are:

  1. Addition, Subtraction (+, -) - from left to right
  2. Multiplication, Division (*, /) - from left to right
  3. Exponentiation (^) - from left to right
  4. Unary Positive/Negative (+, -) - positive / negative

Type Conversion

Note that integer expressions are automatically converted to floating point if needed, as this allows mixing integers and floating point in some calculations, but you must take care to force floating point calculations to avoid integer overflows.

Example - Correct:

a% = 1000 * 1000 + 1.2

Gives correct result as 1000 is converted to floating point before calculation.

Example - Incorrect:

x = 1000
a% = x * x + 1.2

Gives incorrect results as the multiplication result is bigger than 32767.

Note: After any floating point errors (division by 0 and overflow), ERR() returns 3.

Boolean Operators

Boolean operators return a "true" or "false" value instead of a numeric value, useful as conditions in loops and IF statements.

Boolean Operator Precedence

The supported boolean operators, in order of precedence (from lowest to highest):

  1. OR - Logical OR, true if one or both operands are true
  2. AND - Logical AND, true only if both operands are true
  3. NOT - Logical NOT, true only if operand is false
  4. Comparison Operators (<=, >=, <>, <, >, =) - For integer or floating point comparisons, compare the two numbers and return true or false. Note that <> is not equal.

Note: The words OR, AND and NOT can be abbreviated O., A. and N. respectively.

Type Restrictions

You can only compare two values of the same type, so an expression like x = 1.2 is invalid, but 1.2 = x is valid as the second operand is converted to floating point before comparison.

Arrays

Arrays hold many ordered values (called elements). The array elements can be accessed by an index.

Array Declaration

In FORGE, arrays must be dimensioned before use (see DIM statement). The index of the element is written between parentheses and goes from 0 to the number of elements.

Warning: FORGE does not check for out of boundary accesses, so you must be careful with your code to not overrun the size of the arrays.

Array Usage

You can use an array position (the variable name followed by the index) in any location where a standard numeric variable or value is expected.

Array Types

Arrays can be of four types:

WORD Arrays

WORD arrays (the default if no type is given) use two bytes of memory for each element, and work like normal numeric integer variables: -32768 to 32767 (signed).

DIM A(10)      ' WORD array
A(0) = 100
A(5) = -500

BYTE Arrays

BYTE arrays use only one byte for each element, so the numeric range is reduced from 0 to 255 (unsigned).

DIM B(10) BYTE
B(0) = 100
B(1) = 255

Floating Point Arrays

Floating point arrays work like any floating point variable, and use six bytes of memory for each element.

DIM C(10)      ' Floating point array (name ends with %)
C%(0) = 3.14
C%(1) = 2.718

String Arrays

String arrays store a string in each element. String arrays use two bytes of memory for each element that is not yet assigned (containing empty strings), and 258 bytes for each element with a string assigned.

DIM D$(10)     ' String array
D$(0) = "Hello"
D$(1) = "World"

String Values

String Literals

String values are written as text surrounded by double quotes ("). If you need to include a double quote character in a string, you must write two double quotes together.

Example:

PRINT "Hello ""world"""

Will print:

Hello "world"

Hexadecimal Characters

You can also include any character with its hexadecimal code using $ just after the closing quote, with no spaces around. This is the only way to include an ENTER character inside a string constant.

Example:

PRINT "Hello"$9B"world"$2E$2E

Will print:

Hello
world..

String Substrings

The bracket operator [ ] allows creating a string from a portion of another, and accepts two forms:

Form 1: [start]

This form selects all characters from start up to the end of the string, counting from 1.

  • A$[1] selects the entire string
  • A$[3] selects from the third character to the end, effectively removing the two leftmost characters

Form 2: [start, len]

This form selects at most len characters from start, or up to the end of the string if there are not enough characters.

Example:

PRINT "Hello World"[7]
A$ = STR$(3.1415)[3,3]
? A$
? A$[2,1]

Will print:

World
141
4

Important Note on String Substrings

Note that the bracket operator creates a new string and copies the characters from the original string to the new one. As the same buffer is always used for the new string, you can't compare two values without first assigning one of them to a new string variable.

This will print ERROR:

A$="Don't Work"
IF A$[2,2] = A$[3,3] THEN ? "ERROR"

While this will print GOOD:

A$="Long string"
B$=A$[2,2]
IF B$ <> A$[3,3] THEN ? "GOOD"

String Variables

Naming

The naming convention for string variables is the same as for numeric variables, but must end with a $ symbol.

Examples:

  • Text$
  • NAME1$
  • my_string$

Memory Usage

String variables always use 256 bytes:

  • The first byte stores the string length
  • The following bytes store up to 255 ATASCII characters

String Assignment

There are two types of string assignments:

Standard Assignment (=)

The standard = sign copies the string expression on the right to the variable on the left.

A$ = "Hello"
B$ = A$

Concatenation Assignment (=+)

The =+ sign copies the string expression on the right to the end of the current string, concatenating the text.

Example:

A$ = "Hello "
A$ =+ "World"
? A$

Will print:

Hello World

Operator Precedence Summary

Integer Operators (lowest to highest)

PrecedenceOperatorsDescription
1+, -Addition, subtraction
2*, /, MODMultiplication, division, modulus
3&, !, EXORBinary AND, OR, XOR
4+, - (unary)Positive, negative

Floating Point Operators (lowest to highest)

PrecedenceOperatorsDescription
1+, -Addition, subtraction
2*, /Multiplication, division
3^Exponentiation
4+, - (unary)Positive, negative

Boolean Operators (lowest to highest)

PrecedenceOperatorsDescription
1ORLogical OR
2ANDLogical AND
3NOTLogical NOT
4<=, >=, <>, <, >, =Comparison operators

Type Conversion Notes

  • Integers are automatically converted to floating point when needed
  • Boolean values (true/false) can be used as integers (1/0)
  • String comparisons require both operands to be strings
  • Mixed integer/floating point comparisons convert integer to float

Next Steps