Skip to main content

BlackSpace is a programming language that compiles to WhiteSpace.

Project description

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) { ... }
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

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
  • --disable-optimizations: disables the compiler's optimization-related behavior

Example:

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

By default the output file is named a.out.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

blackspace-0.0.76.tar.gz (28.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

blackspace-0.0.76-py3-none-any.whl (51.8 kB view details)

Uploaded Python 3

File details

Details for the file blackspace-0.0.76.tar.gz.

File metadata

  • Download URL: blackspace-0.0.76.tar.gz
  • Upload date:
  • Size: 28.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for blackspace-0.0.76.tar.gz
Algorithm Hash digest
SHA256 8a26fd1a27ead69b791b4e1b8b7b49096c5c2ea639861aa3b8abaed67418c822
MD5 4f3c19aa5164ba52c26c6e5f6f123f85
BLAKE2b-256 ae460a97d792b52a850ac71a9bdd39fa3848e7a32895ec6e79f868cf5b1aeec0

See more details on using hashes here.

File details

Details for the file blackspace-0.0.76-py3-none-any.whl.

File metadata

  • Download URL: blackspace-0.0.76-py3-none-any.whl
  • Upload date:
  • Size: 51.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for blackspace-0.0.76-py3-none-any.whl
Algorithm Hash digest
SHA256 2eed8e0b508b6db80f1a6dce930e04bbf45e9f5ff9f72bbac8b81b0eba959a17
MD5 b43e7e3f7f765022b14a7c73a46c9d1c
BLAKE2b-256 3d004afcb52d4190866ab76dc24ed2d33e1738907d5eeed60b7b1439d922101e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page