Skip to content

Sample Programs

Brandon Ingli edited this page Jan 25, 2022 · 7 revisions

These are some sample programs for BADI. They'll eventually be moved into automated testing, proper documentation, and what not.

Hello, World

echo is used to send output.

echo "Hello, World!"

Output:

Hello, World!

Basic Arithmetic

set is used to assign the value of an arithmetic expression to a variable. Nothing is sent to output without being echoed out.

BADI differs from bash here in that floating point numbers will be supported and the default type. We also introduce an integer division operator.

set a = 5 + 2
echo $a
echo $(5 + 2)

set b = 5 - 2
echo $b

set c = 5 * 2
echo $c

set d = 5 / 2
echo $d

set e = 5 // 2
echo $e

set f = 5 % 2
echo $f

set g = 5 ** 2
echo $g

Output:

7
7
5
10
2.5
2
1
25

Conditionals

BADI follows the same syntax as standard bash for conditional statements. However, we use standard JavaScript-style comparison operators in all situations. Indentation does not matter.

if 1 == 2
then
   echo "Insanity"
elif 2 == 3
then
  echo "Also Instanity"
else
  echo "The universe is correct."
fi

For Loops

Like bash, BADI supports a few basic types of for loops. Indentation does not matter for the interpreter, and is only for ease of reading.

for i in 1 2 3
do
  echo $i
done

for j in {4..6}
do
  echo $j
done

for k in {7..10..2}
do
  echo $k
done

for l in {11..17..3}
do
  echo $l
done

Output:

1
2
3
4
5
6
7
9
11
14
17

While Loop

BADI supports just one form of while loop. Modifications to the loop variable must be made in the loop body. Note that there are subtle syntactical differences between BADI and bash in the loop condition.

set x = 1
while $x <= 5
do
  echo $x
  set x = $x + 1
done
echo "X is $x"

Output:

1
2
3
4
5
X is 6

Functions

Unlike bash, BADI has only one function syntax, using the function reserved word. Parameters passed are accessible through special numbered variables, starting at $1. $0 is reserved for future use. Use $# to get the number of arguments passed.

function hello {
  echo "Hello $1"
  echo "Got $# args"
}
hello "World" "again"

Output:

Hello World
Got 2 args

Clone this wiki locally