B^2
B2, also known as B^2 and B2, is a programming language intended to make programming very simple and use plain language. It is pronounced bee-squared. B2 stands for "Basic Basic." It has a compiler (and editor) written in Javascript which compiles B2 to WebAssembly text format. When version 1.0 comes out it is planned that it will also compile to WebAssembly binary format and will allow users to easily download their program and run it. It has many common elements seen in programming languages. B2 is a bounded-storage machine, see "Computational Class" for details.
Editor
The editor (as of prerelease) compiles to WebAssembly Text Format, (.wat file) which means that before it can be used, it must be converted to WebAssembly Binary Format using wat2wasm. This functionality is planned to be built-in in version 1.0.
The editor supports saving and loading. Its compile time is extremely fast. It has a console which will throw compilation errors. The compiler also supports full syntax highlighting, though it is not automatic (the highlight button must be pressed). Additionally, the editor can run B2 code via the WebAssembly output. If the output box is clicked, it brings up a menu which allows parameters to be specified.
Note: Copy and pasting examples from a website like this may cause compilation errors that should not be happening do to how some browsers handle copy and pasting HTML. Link to the editor hosted on GitHub.
Language Goals
The goal of the language is to be an extremely easy language to learn, while still having functionality. Some syntax is unique, while most is a mix of python and C syntax.
Language
The language syntax is described in the documentation. Some of the syntax is described here.
General Information
The B2 is separated into lines and words. Each line is separated into words, which are separated from each other by spaces. Lines are not required by the language, so they are purely for readability. However, spaces between words are very much required. Every command should end in a semicolon (though this is not always necessary it may cause issues without them)
Variables
There are two variable types in B^2, number
and decimal
. Number variables are called integers in most languages, and decimal variables are floats*.
Number variables are defined with the number command, while decimal variables are defined with the decimal command. Both types can be declared without being set to anything by not including an equal sign in the declaration.
Variables can be set by simply typing the variable name, an equal sign, and what it should be set to. E.G.
number x = 0 ; x = 5 ; output x ; // 5 /;
*More specifically, numbers are of the type i32 while decimals are of the type f64.
Comments
As seen above, start comments are started with a //
and must end with a /;
. Due to this syntax, all comments are "block" in nature, meaning they can span as few words or as many lines as you'd like.
Math
Math operators are done with the first three letters of the operation then two numbers. All math operations support both the decimal and number types. Example:
number z = 2 ; number a = add z 1 ; // 3 /; number b = sub z 2 ; // 0 /; number c = mul z 2 ; // 4 /; number d = div z 2 ; // 1 /; output 0 ;
Comparison Operators
Use <,>,<=,>=,==, and != for comparison. There is one special comparison operator: %%. This checks for a remainder of zero (if x is divisible by y). And and Or is accomplished with && and || respectively.
If and Else Statements
If statements allow for what is inside curly brackets to be run if a condition is true.
If Example:
number out ; number x = 0 ; if ( x == 0 ) { out = 1 ; } else { out = 0 ; } output out ;
For and While Loops
While statements run what is inside curly brackets until the condition is false. While loops work similarly to how they work in Javascript, meaning crashes can and will happen if the condition is never made false inside the curly brackets. This means infinitely loops should never be used. While Example:
number x = 0 ; while ( x < 5 ) { x = add x 1 ; } output x ;
For loops are effectively while loops with declarations in them. They use the to
command to count to a certain number before they stop executing. Note that the to
command can only be used in the context of the second part of a for loop.
For Example:
number count = 0 ; for ( number x = 0 ; x to 10 ; x = add x 1 ; ) { count = add count 1 ; } output count ;
Type Conversion/Rounding
Type conversion is accomplished with the toDecimal and toNumber commands. toDecimal converts a number to a decimal. toNumber converts a decimal to a number, rounding the number to the nearest integer first.
round
is a command which is only available for decimal types which rounds the float to the nearest integer while still keeping it in float form.
Example:
decimal in = input ; number num = toNumber in ; decimal dec = toDecimal num ; dec = add dec 0.6 ; dec = round dec ; output dec ; // The input rounded, plus 0.6, and rounded again /;
Compiler Errors
All lines start from zero. Thus, given the program:
number z = 2 ; decimal x = 4.5 ; output add z x ;
The compiler will throw the error "Type mismatch. Expected number but got decimal. Line 2 word 3." This means the error is on the line with the output command and the word x.
Examples
Common code examples for languages are as follows. Noticeably, there is no hello world example, as B2 does not support strings.
number in = input // 1 /; ; number out = 0 ; if ( in %% 3 ) { out = add out 1 ; } if ( in %% 5 ) { out = add out 2 ; } if ( out == 0 ) { out = in ; } output out ; // 1=fizz, 2=buzz, 3=fizzbuzz /;
number x = input ; output x ;
number x = input ; number cur = 0 ; number out = 0 ; while ( cur <= x ) { if ( cur %% 2 ) { // is even /; out = add out cur ; } cur = add cur 1 ; } output out ;
Note: This program outputs all of them added together do to the limitations of WebAssembly.
Advantages and Limitations
Since B2 compiles to WebAssembly, it has the following limitations shared for all languages that compile in this way:
- Only one output per program is allowed. (WebAssembly allows for this, though it is not in the specifications. However, it is expected that it will make its way into the specifications at some point.)
- There are no string types (May be changed in future versions)
Despite these limitations, the fact that B2 compiles to WebAssembly means that:
- It has extremely fast runtime, significantly faster than Javascript.
- It can be run (and its output read) in many different programming languages
Computational class
B2 is a bounded-storage machine, as both its datatypes (number and decimal) take up a limited amount of memory (32 and 64 bits respectively), and therefore it has a limit on storage.