Bad Code Specification

Description

Bad Code (Intermediate Code) consists of a series of instructions that are executed sequentially. Each instruction requires a specific number of arguments (described in the next section) separated by at least one space or tab. Below are the complete list of available instructions in TubeIC, along with the specifications of what arguments they take and a brief description of how they function.

Name Arguments Description
val_copy [NUM:from] [VAR:to] Copy the value from into the variable to.
add [NUM:num1] [NUM:num2] [VAR:result] Calculate "num1 + num2" and place the answer in result.
sub [NUM:num1] [NUM:num2] [VAR:result] Calculate "num1 - num2" and place the answer in result.
mult [NUM:num1] [NUM:num2] [VAR:result] Calculate "num1 * num2" and place the answer in result.
div [NUM:num1] [NUM:num2] [VAR:result] Calculate "num1 / num2" and place the answer in result.
test_less [NUM:num1] [NUM:num2] [VAR:result] If (num1 < num2) set result to 1, else set result to 0.
test_gtr [NUM:num1] [NUM:num2] [VAR:result] If (num1 > num2) set result to 1, else set result to 0.
test_equ [NUM:num1] [NUM:num2] [VAR:result] If (num1 == num2) set result to 1, else set result to 0.
test_nequ [NUM:num1] [NUM:num2] [VAR:result] If (num1 != num2) set result to 1, else set result to 0.
test_gte [NUM:num1] [NUM:num2] [VAR:result] If (num1 >= num2) set result to 1, else set result to 0.
test_lte [NUM:num1] [NUM:num2] [VAR:result] If (num1 <= num2) set result to 1, else set result to 0.
jump [NUM:num1] Jump Instruction Pointer (IP) to position designated by num (typically a label).
jump_if_0 [NUM:num1] [NUM:num2] If num1 == 0, jump IP to position designated by num2.
jump_if_n0 [NUM:num1] [NUM:num2] If num1 != 0, jump IP to position designated by num2.
random [NUM:num] [VAR:result] Pick a random integer x, where 0 <= x < num, and put it in result.
out_val [NUM:num] Write a floating-point value of num to standard out.
out_char [NUM:num] Write num as char (with given ASCII code) to standard out.
nop None This is a no-operation instruction and will do nothing if executed.
push [NUM:num] Store num in an internal control stack.
pop [VAR:result] Retrieve result from an internal control stack.
ar_get_idx [ARRAY:array] [NUM:num] [VAR:result] In array, find value at index num, and put into result.
ar_set_idx [ARRAY:array] [NUM:num1] [NUM:num2] In array, set value at index num1 to the value num2
ar_get_size [ARRAY:array] [VAR:result] Calculate the size of array and put into result.
ar_set_size [ARRAY:array] [NUM:num] Resize array to have num entries.
ar_copy [ARRAY:array1] [ARRAY:array2] Duplicate all values within array1 into array2.
ar_push [ARRAY:array1] Store array in an internal control stack.
ar_pop [ARRAY:result] Retrieve result from an internal control stack.

Arguments

An argument can be a variable name, a literal integer, a literal character (in single quotes), or a label name (which must be defined elsewhere in the code). The categories of arguments are:

  • NUM: These arguments use the value of a literal integer or character, position of a label, or the current state of a variable.
  • VAR: These arguments can only be the name of a scalar variable (which will be written to)
  • ARRAY: These arguments can only be the name of an array variable

For simplicity, all arguments are required.

Labels

Labels can be set anywhere in the code and refer to the line number of the next instruction. Labels are a sequence of letters, numbers, and underscores, beginning with a letter. When a label is set, it is the full ID followed by a colon (':'). When a label is used, the full name is simply put in the place of an argument.

Variables

There are two types of variables in Bad Code, scalar variables (single values) and array variables (sets of values). Scalar variables are represented by an 's' followed by a non-negative integer. Similarly, array variables are represented by an 'a' followed by a non-negative integer. All instructions specify whether they operate on scalar values or arrays.

Literal Values

Both literal floats (including negative numbers) and literal characters are allowed in Bad Code. Any literal characters are immediately converted into their ASCII values upon loading.

Comments

As with Good Code, comments are initiated with a pound sign ('#') and continue until the end of the line.