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

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.77.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.77-py3-none-any.whl (51.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: blackspace-0.0.77.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.77.tar.gz
Algorithm Hash digest
SHA256 77997d538ab8e0a07ba15821c19d5221036b7e6e9226a36629d2f7c11e761006
MD5 bf04eb2ef585e8452101e278cae90446
BLAKE2b-256 d827237bc804fe7f3cb586cd2942df640d4fb3091990a1c3f4519bbdb7102221

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blackspace-0.0.77-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.77-py3-none-any.whl
Algorithm Hash digest
SHA256 546d4cae11048e433e922ecf5ab10e0011d4532239db083126736a765d4afb55
MD5 bbb8b1022283eb51872040cb8a6e787c
BLAKE2b-256 ff61e3dc3a6611d1d372e3ba451d5bf151c106e671fa0c41f31191f5bfdf24aa

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