ToneBasic: A quick reference

ToneBasic is actually really simple by design. So, stick with me while I go through it’s tiny set of statements and you’ll have learned it in no time!

Tone

The tone statement is the most essential part of the whole language and it’s where the “tone” part of the name comes from. The tone statement takes two parameters, a frequency in hertz, and a duration in seconds like this: tone 400, 1 . That’s like saying play a note at 400 hertz for 1 second.

Print

Print is the second way your program can produce output. Print takes a value as a parameter and displays it in the built in console followed by a newline. The value can either be a string like in print "hello, world!" or an expression like print 5 * 6 or print variable or print 5.

Variables

Like any other language, ToneBasic has variables. Their names can be made up of any word or phrase, as long as it doesn’t have any spaces. Before a variable can be used it must be initialized like this let a = 5 . Here we’ve just declared a variable called “a” and initialized it to 5! Now if we want to modify the value of “a” we can drop the “let” since “a” is already declared, and just say a = 6. But why should we limit ourselves to numbers? We can try some math by writing a = 5 * 6 . This sets “a” to the result of the expression 5 * 6 which is of course 30! We can even change “a” relative to itself like a = a + 1 which increments “a” by one or assign another variable to it with a = b.

Expressions

It’s worth mentioning now that the same principle we saw with variables applies everywhere else too. Anywhere we might place a number we can instead place an expression. An expression is like an equation, and it simplifies to a value that can be used somewhere in your program. a, b + 1, and (5 * 10) / 2 are all examples of expressions. Expressions can use all the usual arithmetic operators like + - * / % and also the comparison operators = < > <= >=.

If … then … end

Here’s where it starts to get interesting. Using an if … then statement we can choose whether to run one piece of code or a different one depending on what’s happening in our program. An if statement looks like this:

if condition then
    do some code
end

The code inside the if statement will only run if the condition is “true”. An example condition is 1 < 2, which is of course “true”. An end statement is needed to close an if statement.

If … then … else … end

This is an alternate form of the if statement which has two blocks of code, separated by an else statement. The first only runs if the condition is “true”, and the second if it’s “false”.

if condition then
    do some code if true
else
    do some code if false
end

For

The for statement allows you to easily repeat some code a certain number of times while incrementing a variable. Here’s what it looks like ( x is a variable name):

for x = start to finish
    some code to loop
end

The syntax is a little complicated so here’s an example with real numbers:

for i = 1 to 10
    print i
end

This for loop prints out the numbers 1 to 10. It starts by initializing the variable “i” to 1. It then repeats the print statement, adding 1 to “i” each loop until “i” passes 10. Remember that for loops also need to be closed with an end statement.

While

The while loop is another, simpler way to repeat a piece of code. It’ll keep going so long as its condition stays true, and it looks like this:

while condition do
    Some code to loop
end

Pause

Pause is the simplest and our last statement. It just takes a time in seconds as a parameter and waits for that long before moving to the next statement.

Comments

Remember to comment your code so you can keep track of what you’ve done! ToneBasic uses the double forward-slash style:

// This is a comment!

And that’s it!

If you want to see some code in action there are plenty of examples to check out in the editor. Alternatively, just start playing around and make something cool, either way have fun!

Gotchas

I’m sure there are plenty of quirks left in the code just waiting to be found, but here are the most obvious ones:

Expressions

While ToneBasic can crunch some pretty complex equations, it’s worth noting that it won’t be using order of operations. Expressions in ToneBasic are evaluated strictly from left to right, so make sure to use plenty of parentheses!

Numbers

All numbers in ToneBasic are really just javascript floats behind the scene.

Comparisons

In ToneBasic the comparison/Boolean operators work a little differently. ToneBasic doesn’t actually have any kind of true or false values, just numbers. This means that an expression like 1 < 2 would evaluate to 1 for “true” and 0 for “false”. So, while I wouldn’t recommend it (1 < 2) + 5 is a valid expression and equals 6. However, in the context of if statements, anything is “true” that’s not zero.

Written with StackEdit.