sunjay / brain

A high level programming language that compiles into the brainfuck esoteric programming language
MIT License
167 stars 12 forks source link

New Syntax #24

Closed sunjay closed 7 years ago

sunjay commented 7 years ago

The current syntax isn't perfect. It was designed when we only really had one type (bytes). We eventually want to support an entire type system. I'll probably base the design off of Rust since I love the language but I'm open to all ideas. Most importantly, this has to be a language suitable for generating brainfuck programs and meet the optimization goals of the compiler while still being ergonomic and usable.

All the syntax (syntax.brn)

// complete type inference isn't currently supported, so this doesn't work:
//let foo = "bar";
// statically allocated array of bytes initialized to the given string
// length is automatically determined by the compiler
let s: [u8; _] = "foo bar";
// We can get the length of a string using the len() property
// The type that len() returns is `usize` and for the time-being that is u8
// This mean
// writeln outputs a "\n" at the end
stdout.writeln(s.len());
// The write statement supports a variable number of both identifier and string literal arguments
stdout.write(s, "\n");

// if the length cannot be determined automatically, it must be specified
let a: [u8; 4];
// length must be greater than zero
let b: [u8; 1];
// read needs to be a statement so the type information can be used to determine the length
stdin.read(a, b);

// both sides have to have the same length
if b == "a" {
    stdout.writeln("equal");
}
else if a == "fooo" {
    stdout.writeln("foo");
}
else {
    stdout.writeln("not equal");
}

// A single byte-sized numeric type is supported
// value must be in the range for the type
let counter: u8 = 200;
// the while condition must evaluate to a boolean
while counter > 0 {
    // This is a placeholder function that mutates counter and subtracts one
    // This exists because we don't want to implement a complete set of numeric operations right now
    decrement(counter);
    stdout.writeln(counter);
}

// the type of i is usize
// i goes from 1 <= i < 10
// This currently must be a static, finite counter
// variables are not supported as counter limits
// This is basically implemented as a brainfuck loop that goes from
// 0 to (end-start) and start is added to the cell *within* the loop
for i in 1..10 {
    stdout.writeln("i = ", counter);
}

Name guesser (names.brn)

// Asks for the first letter of your name, then tries to guess your name
stdout.writeln("MAGIC NAME GUESSER");

let prompt: [u8; _] = "Enter the first letter of your name:";

let c: [u8; 1];
stdout.writeln(prompt);
stdin.read(c);

while c {
    stdout.writeln("My guess is that your name is:");
    if c == "a" {
        stdout.writeln("Alexander");
    }
    else if c == "e" {
        stdout.writeln("Emily");
    }
    else if c == "m" {
        stdout.writeln("Matthew");
    }
    else {
        stdout.writeln("I don't know.");
    }

    stdout.writeln(prompt);
    stdin.read(c);
}
sunjay commented 7 years ago

Considering this completed with the merging of the PR #34.