/b

👩‍💻 A very simple programming language

Primary LanguageTypeScript

B

👩‍💻 A very simple programming language. Created for practise purposes.

Interpreter

Interactive Mode

The interpreter CLI ships with an interactive mode that can be used to play around with the language:

git clone git@github.com:kvendrik/b.git && cd b && yarn && yarn build

./b
v1.0.0-alpha
>>> count = 2;
>>> multiply = {(x, y) x * y};
>>> multiply(count, 2);
4
>>> exit

Using files

You can also use files by passing the file path into the CLI.

example-program.b

iteration = 0;

while(iteration < 100, {()
  iteration = iteration + 1;

  if(iteration == 50, {() log("Halfway!")});

  message = concat("Currently at: ", iteration);
  log(message);
});
./b ./example-program.b
Currently at: 1
Currently at: 2
Currently at: 3
Currently at: 4
Currently at: 5
...

Programmatically

If you'd like to play around with the parser and/or interpreter in code you can install the package using its Github URL:

yarn add https://github.com/kvendrik/b
import {Interpreter, toAST} from 'b';

const ast = toAST(`
  count = 2;
  multiply = {(x, y) x * y};
  multiply(count, 2);
`);

const interpreter = new Interpreter();

console.log(interpreter.evaluate(ast)); // {type: 'Number', value: '4'}

Why

I created this for practise purposes. I wanted to learn more about the inner workings of lexers, parsers, and interpreters. It seems like a good way to do so was by writing my own.

It's all written in JS which, depending on what it's used for, might not be what I would write this in if used in a production scenario, but it works well for practise purposes.

Features

  • Basic math operations
  • String & Number literals
  • Variable assignments
  • Function expressions & assignments
  • Function calls
  • Multiline support
  • Scoped variables
  • Conditional logic
  • Loops
  • Using function expressions as function arguments
  • Scoped functions
  • Loop continues and returns without value (using break keyword)
  • Using function calls directly as function arguments
  • Early returns with value
  • Line and character numbers in error messages
  • Chained math operations (not implemented in interpreter yet e.g. 2 + 2 * 4 / 2)
  • Priority groups (e.g. (2 + 2) * 2)
  • Arrays