Home

Awesome

Bau

Bau is a simple, concise, safe, powerful and fast programming language. Features:

<a href="https://thomasmueller.github.io/bau-lang/">Try it out in the browser.</a>

It addresses other languages' issues:

Example

fun fact(x int) int
    if x <= 1
        return 1
    return x * fact(x - 1)

for i:= range(0 20)
    println(fact(i))

Keywords

Control flow

Assignment, comparison, operations

Data types and miscellaneous

Constants, Variables

Identifiers contain letters, digits, and _. : defines a constant. := defines a variable. = += -= *= /= &= |= ^= <<= >>= updates it:

PI : 3.14159
x := 10
x = x + 1
x += 1      # shortcut

A variable without value requires a type:

x int

Built-In Types

The built-in types are int i32 i16 i8 (signed integer), and float f32 (floating point). int can be restricted to a range using 0... Defaults are int and float; both are 64 bit. Conversion functions change the type, and may truncate.

c := i8(10)

Conditions

if starts a condition. Spaces group statements into blocks. elif (else if) and else are optional.

if a = 0
    println('zero')
elif a = 1
    println('one')
else
    println('many')

Loops

There are for and while loops. , is optional if the arguments are simple:

# loop from 0 to 9
for i := range(0 10)
    println(i)

for is internally converted to while:

i := 0
while i < 10
    println(i)
    i += 1

break exits a loop. It may have a condition:

# prints 1 to 4
for i := range(1 10)
    break i = 5
    println(i)

Comments

# starts a line comments; two or more start and end a block comment.

# Line comment

##
Block comment
##

Comments before types and functions are converted to documentation.

Literals

Numbers start with a digit. _ is ignored. . is floating point, 0x hexadecimal.

Strings starting with ' may contain \n newline, \t tab, \' single quote, \\ backslash, \x00 byte. UTF-8 is used.

Raw strings don't have escapes and start and end with one or more `. Multi-line ones begin on the next line and may be indented.

a : 1_000_000
b : 3.1415
c : 0xcafe
d : 'String literal'
e : `Raw string`
f : ``
    Two-line
    raw string with `
    ``

Operators

= < > <= >= <> compare two values and return 1 or 0. not inverses a comparison. and or combines comparisons; the right side is only evaluated when needed. Integer + - * wrap around on over- / underflow. / %: integer division by 0 returns max, min, or 0. & | ^ ~ << >> are bitwise and, or, xor, not, shift right, and logical shift right: the leftmost bits become 0.

Functions

fun starts a function. It may return a value. .. means variable number of arguments. Functions can share a name if the number of arguments is different. They can be declared first and implemented later. const functions are executed at compile time if the arguments are constants. macro function calls are replaced at compile time with the implementation. Types can be passed as parameters or implicitly (internally, this functions are templates).

fun square(x int) int
    return x * x

fun sum(x int..) const int
    sum := 0
    for i := until(x.len)
        sum += x[i]
    return sum

fun if(cond int, true T, false T) macro T
    if cond
        return true
    else
        return false

println('sum: ' sum(1 2 3))
for i := until(5)
    println(square(i))
    println(if(i % 2, 'odd', 'even'))

Types

Types can have fields and functions:

type Square
    length int
fun Square area() int
    return length * length
s : new(Square)
  

If a type has a close function, then it is called before the memory is freed. int and other lowercase types are copied when assigned; uppercase types are referenced. Functions on built-in types are allowed:

fun int square() int
    return this * this
println(12.square())

Types can have parameters:

type List(T)
    array T[]
    size int
fun newList(T type) List(T)
    ...
list := newList(Circle)

Null

? means it may be null. An explicit check is required before using the value. There are no null pointer errors at runtime.

fun get(key int) Circle?
    # may return null

v : get(key) 
if v
    print(v.area())

Value types (eg. int) can't be null.

Arrays Access

To create and access arrays, use:

data : new(i8[], 3)
data[0] = 10

Bounds are checked where needed. Access without runtime checks require that the compiler verifies correctness. Index variables with range restrictions allow this. For performance-critical code, use [ ]! to ensure no runtime checks are done. The conditional break guarantees that i is within the bounds.

if data.len
    i := 0..data.len
    while 1
        data[i]! = i
        next : i + 1
        break next >= data.len
        i = next

Exceptions

throw throws an exception. catch is needed, or the method needs throws. Custom exception types are allowed.

import org.bau.Exception
    exception

fun square(x int) int throws exception
    if x > 3_000_000_000
        throw exception('Too big')
    return x * x

x := square(3_000_000_001)
println(x)
catch e
    println(e.message)

Modules and Import

import allows using types and functions from a module. The last part of the module name is the identifier. The module identifier can be omitted if the type, function, or constant is listed after import. The full module name can be used as well.

import org.bau.Utils
    random
import org.bau.Math
println(random())
println(Utils.getNanoTime())
println(Math.PI)
println(org.bau.Math.PI)

module defines a module. The name needs to match the file path, here org/bau/Math.bau:

module org.bau.Math
PI : 3.14159265358979323846

Tour

Hello World

println('Hello World')

Assignment

Import, Functions

import org.bau.Utils

fun printTime()
    println(Utils.getNanoTime())

printTime()

Random

import org.bau.Utils

println(Utils.random())

Math

import org.bau.Math

println('Pi: ' Math.PI)
println(Math.sqrt(2))
Functions
fun add(x int, y int) int
    return x + y

println(add(42 1))
Data Types
a := 10_000_000
b := i8(110)
c := i16(65000)
d : 'text'
e := 3.1416
f := 0..10
println(a ' ' b)
Type Conversion
a := 10_000_000
b := 3
println(a / b)
println(float(a) / b)
Constants
PI : 3.1415
println(PI)
For Loops
sum := 0
for i := range(0 10)
    sum += i
println(sum)
While Loops
sum := 1
while sum < 10_000
    sum += sum
println(sum)
If
for i := range(1, 10)
    if i < 5
        println(i)
If Else
for i := range(1, 10)
    if i < 5
        println(i)
    else
        println(-i)
If Elif Else
for i := range(1, 10)
    if i = 0
        println('zero')
    elif i = 1
        println('one')
    elif i = 2
        println('two')
    else
        println('many')
Switch
import org.bau.Utils

for i := range(1, 10)
    switch Utils.random() & 7
    case 0
        println('zero')
    case 1
        println('one')
    case 2, 3
        println('2 or 3')
    else  
        println('other') 
Types
type point
    x int
    y int

p := new(point)
p.x = 10
p.y = 20
Arrays
array : new(i8[], 10)
for i := until(array.len)
    array[i] = i
List
import org.bau.List
    List
    newList

list := newList(int)
list.add(100)
list.add(80)
println(list.size)
println(list.array[0])
Enum
enum weekday
    sunday
    monday
    tuesday
    wednesday
    thursday
    friday
    saturday

for a := until(weekday.saturday + 1)
    switch a
    case weekday.sunday
        println('sunday')
    case weekday.monday
        println('monday')
    else
        println('some other day: #' a)
Macros and Ternary Condition
fun if(cond int, a T, b T) macro T
    if cond
        return a
    else
        return b

for i := until(3)
    println(i ':', if(i, '>0', 'zero'))

Comparison

FeatureBauPythonCC++JavaC#GoRustSwift
Memory Safety
Easy to Learn and Use
Concise Syntax
Vendor Independent
Strongly Typed
Fast Execution
No GC Pauses
Runs Everywhere
Generics / Templates
Macros
Exception Handling
Null Safety
Array Bounds Checks
Compile-Time Execution

Non-Features

Syntax

Safety

Memory Management

Exceptions and Panic