B++ is a programming language initially developed by the developers for The Brain of TWOW Central. Check out the source code at their GitHub repository!
- Introduction
- Hello, World!
- Variables
- Basic Functions
- Comparison
- Blocks
- Type Conversions
- Import Statements
- Builtin Functions
In B++, everything is a tag. A tag is made of square brackets, with a function call in them! You can also provide tags as input to another tag. Arguments to a tag are seperated by spaces. For example:
[MATH 5 * 7]
[CONCAT "hello w" "orld"]
[IF [COMPARE 6 != 4] "6 is not 4" "6 is 4"]
You can also do comments using a "#". For example:
# This is a comment.
In B++, the return value of a tag is automatically printed. That means that, to make a hello, world!, you just need to do:
"Hello, World!"
Variables are made using the DEFINE and VAR statements. To define a variable, use:
[DEFINE helloworld "Hello, World!"]
You can also use DEFINE to change a variable.
To get the value of a variable, use
[VAR helloworld]
We can make a hello world program using variables by doing:
[DEFINE helloworld "Hello, World!"]
[VAR helloworld]
B++ is a type-safe language. There are 4 types in B++:
- Strings (words/letters)
- Integers (whole numbers)
- Floats (decimals)
- Arrays (lists)
Strings can be defined like any other variable. You can get a letter of a string using the INDEX function. For example:
[INDEX "Hi!" 0]
This gets the first letter of the string "Hi!", or "H". Note that the first letter has an index of 0.
You can also get the length of a string using
[LENGTH "Hello, World!"]
You can do math on floats an integers, using the MATH function. Integers are defined by doing:
[DEFINE a 7]
Floats are defined by doing:
[DEFINE b 0.21]
Arrays can have any type as values. You can even store an array in an array! Define arrays using the ARRAY function. For example, the following program makes an array with the values 1, 2, 3 and 4:
[ARRAY 1 2 3 4]
You can get a value at an index using.
[INDEX [ARRAY 1 2 3 4] 0]
This gets the first element in the array. Note that the first element has an index of 0.
You can get the length of an array using
[LENGTH [ARRAY 1 2 3 4]]
This would return 4, which is the number of items in the array.
There are a few basic functions which you will most likely ue a lot. They are explained below.
The first one is math. To do math, simply use the MATH tag with a value, operator, and another value. For example:
[MATH 100 + 100]
Supported operators are:
| Operator | Math Function |
|---|---|
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
^ |
Power |
To format a string, you would use the CONCAT function. This accepts any number of strings and concatenates them. For example:
[CONCAT "Hello" ", " "World" "!"]
Prints "Hello, World!".
To compare values, you use the COMPARE function. For example:
[COMPARE 6 = 4]
In B++, there aren't booleans. COMPARE just returns 1 if true, and 0 if false.
B++ Supports many comparison operators:
| Operator | What it Does |
|---|---|
= |
Equals |
!= |
Not Equal |
> |
Greater Than |
< |
Less Than |
>= |
Greater Than or Equal To |
<= |
Less Than or Equal To |
If statements are ternary. Simply just do:
[IF [COMPARE 6 != 4] "6 is not 4" "6 is 4"]
To have multiline IF statements, check out the IFB block.
Blocks allow block if statements, loops, and functions!
What if you need to have multiple lines of code in an if statement? Use a block if statement!
[IFB [COMPARE 1 == 1]]
"Awesome!"
"Everything works!"
[ELSE]
"Is 1 not equal to 1?"
"Thats not good..."
[ENDIF]
Note: You don't need to indent the contents of block statements, but it makes it cleaner and easier to read.
B++ supports loops, in the form of WHILE loops! For example, to print the numbers 1-100:
# Define i, which we will be using to control the number of iterations
[DEFINE i 1]
# While i is less than 100, do something
[WHILE [COMPARE [VAR i] <= 100]]
# Print i
[VAR i]
# Increase i by 1
[DEFINE i [MATH [VAR i] + 1]]
[ENDWHILE]
Functions allow code to be put in blocks and run in a safe environment, for example, to add 2 numbers:
# Define add, which accepts a, which is an integer, and b, which is also an integer
[FUNCTION ADD [PARAM a INT] [PARAM b INT]]
# Add the two numbers
[DEFINE result [MATH [VAR a] + [VAR b]]]
# Return the value
[RETURN INT [VAR result]]
Now, to add 1 and 2, using this function, you would just do
[ADD 1 2]
Side note: You can use
[RETURN NULL [NULL]]
To return a blank value.
⚠️ You can't access global variables, only variables defined in the function and the parameters.
B++ supports recursion too! For example, to make a factorial function:
# Define factorial function
[FUNCTION FACTORIAL [PARAM inp INT]]
# If the number is over 1, then take the factorial of 1 less than the number and multiply that with the number
[IFB [COMPARE [VAR inp] >= 1]]
# Take factorial of 1 less than number
[DEFINE mul [FACTORIAL [MATH [VAR inp] - 1]]]
# Return the number multiplied by the input
[DEFINE result [MATH [VAR mul] * [VAR inp]]]
[ELSE]
# Otherwise, return 1
[DEFINE result 1]
[ENDIF]
# Return the result
[RETURN INT [VAR result]]
Now, just use
[FACTORIAL 10]
To get 10 factorial, or 3628800!
Sometimes, you need to convert types. To do so, just do:
[FLOAT 100]
or
[STRING 0.1]
You can use INT, FLOAT, STRING, and ARRAY in any combination!
B++ also supports multiple files! To do this, use
[IMPORT "file.bpp"]
This executes the file when the statement is reached. That means conditional imports are also possible, for example
[IF [COMPARE [ARGS 0] = "yes"] [IMPORT "yes.bpp"] [IMPORT "no.bpp"]]
Make sure to include the other files when running the code!
B++ has many builtin functions, which are listed below.
| Function Signature | Description |
|---|---|
[CHOOSE val] |
Gets a random index of val, which can be an array or a string. |
[RANDINT lower upper] |
Gets a random integer within the range lower, upper. |
[RANDOM lower upper] |
Gets a random float in the range lower, upper. |
[FLOOR val] |
Gets the floor, or rounds down float val. |
[CEIL val] |
Gets the ceiling, or rounds up float val. |
[ROUND val] |
Rounds float val to the nearest integer, or whole number. |
[ARGS index] |
Gets argument with index index of argument array. |