PreCalc – Compile-Time Tables

A PreCalc block runs at build time, inside the transpiler, and bakes its results into your program as an array literal. The maths happens once, when you compile – the finished program just has the numbers, with no runtime cost and no runtime library involved. It's perfect for lookup tables: sine tables, gradients, curves, precomputed constants.

PreCalc sinTable:Int[256]
    For i:Int = 0 To 255
        sinTable[i] = Round(Sin(i * 360 / 256) * 1024)
    Next
EndPreCalc

After the block, sinTable is a normal array you can use anywhere – but its 256 values were all worked out at compile time. In the generated code it is literally { 0, 25, 50, ... }.



The header declares the array
PreCalc <name>:<Int|Double>[<size>]

The block builds exactly that one array. The declared type decides how the values are baked: Int rounds to whole numbers, Double keeps the decimals.

Add more [size] groups for a multi-dimensional array – index it with one subscript per dimension:

PreCalc mulTable:Int[10][10]
    For y:Int = 0 To 9
        For x:Int = 0 To 9
            mulTable[y][x] = y * x
        Next
    Next
EndPreCalc

' ... later ...
Print ToString(mulTable[7][3])    ' 21, computed at compile time


What you can do inside

A PreCalc block is a compile-time math sandbox. It understands:

PreCalc triangle:Int[8]
    triangle[0] = 0
    For i:Int = 1 To 7
        triangle[i] = triangle[i - 1] + i    ' reads triangle[i-1] at compile time
    Next
EndPreCalc
' -> { 0, 1, 3, 6, 10, 15, 21, 28 }


What you cannot do (on purpose)

A PreCalc block is sealed off from the runtime – it is pure maths. Anything that belongs to the running program (drawing, input, strings, calling your own Functions, If and While) is rejected with a friendly error. If it isn't a number, a loop, or a math function, it doesn't belong in a PreCalc block. This is what guarantees the whole block can be evaluated at compile time.



Why not C++ constexpr?

This is exactly the case C++'s compile-time evaluation handles badly – you can't call std::sin in a constant expression, and filling an array is awkward. PreCalc sidesteps all of that: the transpiler simply runs the maths and writes the answers out.



Key Points

See Also

Examples

See PreCalc.bam.


BambooBasic © 2026 Michael Denathorn