Getting started

Building the compiler

The stage-0 compiler is written in PureBasic. With PureBasic 6.30 or later installed:

build.bat

This produces bin\x.exe. If PureBasic is not in C:\Program Files\PureBasic, set PUREBASIC_HOME first.

The compiler needs the runtime folder that sits next to bin. It holds the runtime library, which is written in x and compiled into every program.

A first program

Save this as hello.pb:

OpenConsole()
PrintN("Hello, world")

Output:

Hello, world

Compile and run it:

bin\x.exe hello.pb
hello.exe

x writes hello.exe next to the source. Use -o to choose another name: bin\x.exe hello.pb -o greet.exe.

There is no linker, no assembler and no C compiler involved: x writes the executable directly. hello.exe is about 3.5 KB, because only the library procedures a program uses are included.

File extensions

x reads PureBasic sources (.pb, .pbi) and x sources (.x). The extension makes no difference to the language.

Two styles, one language

PureBasic code is x code. The C-style spellings are alternatives, not a separate dialect. These two programs are the same:

; PureBasic style
Procedure.i Square(n.i)
  ProcedureReturn n * n
EndProcedure

OpenConsole()
For i = 1 To 3
  PrintN(Str(Square(i)))
Next

Output:

1
4
9
// C style
int Square(int n) {
  return n * n
}

OpenConsole()
for (int i = 1; i <= 3; i++) {
  PrintN(Str(Square(i)))
}

Output:

1
4
9

Things both styles share:

  • A new line ends a statement, so no ; is needed.
  • = is both assignment and comparison. Which one it is depends on where it appears (Operators).
  • Names are case-insensitive: PrintN, printn and PRINTN are the same.

Building for Linux

x also makes programs for Linux on x64 machines:

bin\x.exe hello.pb -target linux-x64 -o hello

The result is a single ELF file with no dependencies, not even the C library: the runtime talks to Linux directly through system calls. Copy it to the Linux machine, mark it executable (chmod +x hello) and run it.

x itself runs on Linux as well (build it with bin\x.exe src\x.pb -target linux-x64 -o x). There it makes Linux programs by default, already executable, and -target windows-x64 makes Windows ones.

Programs behave the same on both, with the usual differences: lines end with a line feed (Chr(10)) instead of carriage return and line feed, paths use /, #PB_Compiler_OS is #PB_OS_Linux, and Import (which loads Windows DLLs) isn't available. Use CompilerIf #PB_Compiler_OS = ... for code that differs.

Other compiler modes

Command Does
x file.pb compile to file.exe
x file.pb -o out.exe compile to out.exe
x file.pb -target linux-x64 -o out compile for Linux x64 (see above)
x -check file.pb check the program and print its types and symbols
x -ast file.pb print the syntax tree
x -tokens file.pb print the tokens

Status

The compiler produces 64-bit executables for Windows and Linux. macOS, ARM64, RISC-V, the 68000 (Atari ST, Amiga) and the Z80N (ZX Spectrum Next) are planned: see the roadmap.