Skip to main content

BlackSpace

BlackSpace is a small programming language that compiles to Whitespace. It provides a readable surface syntax, parses .bsp source files into an AST, and then emits Whitespace code.

Status

The project currently supports:

  • hand-written lexer and parser
  • functions and recursion
  • local variables and parameters
  • arithmetic, boolean, and comparison expressions
  • arrays and strings
  • if / else
  • while, do ... while, and for
  • built-in input/output expressions

The compiler entry point expects a main function:

  • the entry function name must be main
  • main must not take parameters
  • main must return void

Language Overview

BlackSpace programs are made of function definitions.

Functions

Function syntax:

fn fib(n: int) -> int {
    if (n <= 0) {
        return 0;
    } else if (n <= 1) {
        return 1;
    } else {
        return fib(n - 1) + fib(n - 2);
    }
}

Functions may optionally declare locals in a declaration block placed between the return type and the body:

fn main() -> void [
    var n: int,
    var result: int
] {
    n = read_int();
    result = fib(n);
    print_int(result);
}

Notes:

  • top level contains only function definitions
  • the local declaration block is optional
  • if a function has no locals, omit the declaration block entirely
  • empty [] is not used for "no locals"

Types

Currently supported types:

  • int
  • bool
  • string
  • void
  • array types such as int[] and int[][]

void is valid for function return types, but not for parameters or variables.

Variables

Parameters are written as:

n: int

Local declarations use:

var value: int
const limit: int

var creates a mutable variable, const creates an immutable one.

Statements

Supported statement forms include:

x = 1;
return;
return x + 1;
if (x < 10) { ... } else { ... }
while (x < 10) { ... }
do { ... } while (x < 10)
for (i = 0; i < n; i = i + 1) { ... }
try { ... } catch { ... }
throw;
myfunc();
;

Notes:

  • assignment statements end with ;
  • expression statements are allowed and become void statements internally
  • do ... while does not require a trailing semicolon
  • ; by itself is a valid empty statement
  • throw; transfers control to the nearest enclosing catch, including across function calls; an interrupted expression is abandoned

Expressions

Supported expression forms include:

  • integer, boolean, and string literals
  • variable usage
  • function calls
  • array indexing
  • parenthesized expressions
  • unary operators: -, !
  • binary operators:
    • *, /, %
    • +, -
    • <, <=, >, >=
    • ==, !=
    • &&, ||

Examples:

fib(n - 1) + fib(n - 2)
arr[i + 1]
!(a < b)

Built-in Functions

Some names are handled as built-ins rather than normal user-defined function calls:

read_int()
read_text()
print_int(expr)
print_text(expr)
sizeof(expr)

Examples:

n = read_int();
print_int(fib(n));
print_text("done\n");

Notes:

  • read_int() reads a number
  • read_text() currently reads a single character value
  • print_int(expr) prints numbers
  • print_text(expr) prints strings and character-like integer values as text

Example Programs

Iterative Fibonacci:

fn main() -> void [
    var n: int,
    var a: int,
    var b: int,
    var c: int,
    var i: int
] {
    n = read_int();
    a = 0;
    b = 1;
    c = 0;

    for (i = 0; i < n; i = i + 1) {
        c = a + b;
        a = b;
        b = c;
    }

    print_int(a);
}

Recursive Fibonacci:

fn fib(n: int) -> int {
    if (n <= 0) {
        return 0;
    } else if (n <= 1) {
        return 1;
    } else {
        return fib(n - 1) + fib(n - 2);
    }
}

fn main() -> void {
    print_int(fib(read_int()));
}

Compiler Usage

After installing the project, the compiler is available as:

blackspace input.bsp -o output.ws

You can also run it as a module:

python -m blackspace_compiler input.bsp -o output.ws

Arguments:

  • input_file: path to the BlackSpace source file
  • -o, --output: path to write the generated Whitespace program
  • --optimize: enable AST optimizations (disabled by default)

Example:

blackspace blackspace_compiler/__tests__/scripts/fib_iterative.bsp -o fib.ws

By default the output file is named a.out.

Optimization

Use --optimize to enable literal folding:

blackspace input.bsp --optimize -o output.ws

Literal folding evaluates supported operations on integer and boolean literals at compile time. For example, x + (2 * 3) becomes x + 6. It handles integer arithmetic and comparisons, boolean equality and inequality, and boolean !, &&, and ||, including nested literal expressions.

The optimizer does not track values of variables, evaluate function calls, or remove branches, loops, or unused variables. Operations that are not supported or whose operands are not literals remain in the program; division and modulo by zero are also left for runtime.

Metadata

Release files for blackspace 0.0.124

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for blackspace 0.0.124
File Size Uploaded Provenance Actions
blackspace-0.0.124.tar.gz 43.9 kB Without provenance Details for blackspace-0.0.124.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for blackspace 0.0.124
File Interpreter ABI Platform Actions
blackspace-0.0.124-py3-none-any.whl Python 3 none any Details for blackspace-0.0.124-py3-none-any.whl

Total release size: 119.8 kB

Release files / blackspace-0.0.124.tar.gz

Download URL blackspace-0.0.124.tar.gz
Size 43.9 kB
Tags Source
SHA-256 checksum
How to use checksums
673fd176c24dc268435a5daa4b92e628a82523cc2c681478d2f71920fe71f9ee
BLAKE2b-256 checksum
How to use checksums
b6cb0715e7c94e951649bc47813748fed7f2c386247c69122a7f77080e6efcd2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.2

Release files / blackspace-0.0.124-py3-none-any.whl

Download URL blackspace-0.0.124-py3-none-any.whl
Size 75.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9388abc5efa2bb3498aa4197ba8ea23ca7c87ce5246ab7491cbd41fe076111e6
BLAKE2b-256 checksum
How to use checksums
7694ae42c63202b9398acf69b34c4117f63e17977e0d235ecc1b9efda50bf00f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.2

Security for blackspace 0.0.124

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page