Documentation

Language syntax, built-ins, and examples for Better Programming Language.

Getting started

Write code in the playground and select Run program to execute it. You can also press Ctrl + Enter from the editor.

Build from source

Clone the Better Language repository, then build the interpreter from its project directory:

go build -o gbpl .
./gbpl <filename>.bpl

# Start the limited REPL
./gbpl

Variables

Declare variables with the var keyword. Existing variables can be reassigned without repeating the keyword.

var x = 10
x = 20
print x

Functions

Define functions with function, pass arguments by position, and return a value with return. Recursion is supported.

function add(a, b, c) {
  return a + b + c
}

print add(1, 2, 3)

function factorial(n) {
  if (n == 0) {
    return 1
  }
  return n * factorial(n - 1)
}

Control flow

Conditionals

if (x > 10) {
  print "x is greater than 10"
} else {
  print "x is less than or equal to 10"
}

Loops

for (var i = 0; i < 10; i = i + 1) {
  print i
}

while (x < 10) {
  print x
  x = x + 1
}

Expressions and scope

Use the ternary operator for compact conditions. Curly-brace blocks create scope, so declarations inside a block are not available outside it.

print x > 10 ? "greater" : "not greater"

var x = 5
{
  var x = 10
  print x
}
print x

Operators

Arithmetic

BPL supports addition +, subtraction -, multiplication *, and division /.

var a = 10
var b = 5
var sum = a + b
var product = a * b

Logical and comparison

Combine values with &&, ||, and !. Compare them with ==, !=, <, >, <=, and >=.

var canContinue = isReady && !hasError
var isEqual = a == b
var isLarger = a > b

Bitwise

Bitwise operations include &, |, ^, ~, <<, and >>.

Data types

The language includes integers, floating-point numbers, booleans, and strings. Add comments with //.

var integer = 10
var decimal = 3.14
var boolean = true
var message = "Hello, BPL!"

// This is a comment
var x = 10 // This is an inline comment

Built-in functions

Use print with or without parentheses to send values to the output. clock() returns the current time in milliseconds since the epoch.

print("Hello, BPL!")
print "Hello again"
print clock()

Full example

This program calculates the first ten Fibonacci numbers and reports the execution time.

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"

Under the hood

Scanner ✓
Parser ✓
Interpreter ✓