BPL is a simple programming language designed to be easy to learn and use. It is a language with a focus on simplicity and readability, created from stratch with a tree-walking interpreter in Golang.
To run BPL code, you can use the provided playground. Write your code in the input area and click the "Run BPL Code" button to see the output.
To build BPL from source, clone the repository (at https://github.com/Chanadu/better-language) and run the following commands inside the project directory:
go build -o gbpl .
./gbpl
Optionally, you can also run in a limited REPL environment by running:
./gbpl
It was a vibe to make.
BPL syntax is designed to be simple and easy to read (subjectively). Here are some examples of the syntax:
Variables are declared using the var keyword:
var x = 10
Variables can be reassigned:
x = 20
Functions are defined using the function keyword:
function add(a, b, c) {...}
Call functions using their name and passing arguments:
add(1, 2, 3)
Functions can return values:
return a + b + c
Functions can call themselves recursively:
function factorial(n) {
if n == 0 {
return 1
} return n * factorial(n - 1)
}
if and else statements:
if (x > 10) {
print "x is greater than 10"
} else {
print "x is less than or equal to 10"
}
for loops:
for (var i = 0; i < 10; i++) {
print i
}
while loops:
while (x < 10) {
print x
x = x + 1
}
Ternary operator(?)for conditional expressions:
print x > 10 ? "x is greater than 10" : "x is less than or equal to 10"
Scope is determined by {} blocks, so variables declared inside a block are not accessible outside of it.
var x = 5; { var x = 10; print(x); } print(x)
Addition(+), subtraction (-), multiplication (*), and division (/) are supported:
var a = 10
var b = 5
var c = a + b // c is 15
var d = a - b // d is 5
var e = a * b // e is 50
var f = a / b // f is 2
Logical operations such as && (and), || (or), and ! (not) are supported:
var a = true
var b = false
var c = a && b // c is false
var d = a || b // d is true
var e = !a // e is false
Comparison operations such as == (equal), != (not equal), < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) are supported:
var a = 10
var b = 5
var c = a == b // c is false
var d = a != b // d is true
var e = a < b // e is false
var f = a > b // f is true
var g = a <= b // g is false
var h = a >= b // h is true
Bitwise operations such as & (and), | (or), ^ (xor), ~ (not), and bit shifts (<< and >>) are supported:
var a = 10 // 1010 in binary
var b = 5 // 0101 in binary
var c = a & b // c is 2 (0010 in binary)
var d = a | b // d is 15 (1111 in binary)
var e = a ^ b // e is 13 (1101 in binary)
var f = ~a // f is -11 (inverts bits)
var g = a << 1 // g is 20 (10100 in binary)
var h = a >> 1 // h is 5 (0101 in binary)
Available Datatypes include Integers, Floats, Booleans and Strings
var i = 10
var f = 3.14
var b = true
var s = "Hello, BPL!"
Comments can be added using // for single-line comments
// This is a single-line comment
var x = 10 // This is an inline comment
The print function is available to output values to the console:
print("Hello, BPL!")
The print function can also be used without parentheses for simple output:
print "Hello, BPL!"
The clock function is available to get the current time in milliseconds since the epoch:
print clock()
Here is a simple example of a BPL program that calculates the fibonacci sequence and prints the first 10 numbers and times the operations:
var startTime = clock()
function fib(n) {
if (n <= 1) return n
return fib(n - 2) + fib(n - 1)
}
for (var i = 0; i < 10; i = i + 1) {
print fib(i)
}
var endTime = clock()
print "Time taken: " + (endTime - startTime) + "ms"
Scanner: ✔
Parser: ✔
Interpreter: ✔