An open API service indexing awesome lists of open source software.

https://github.com/1computer1/rpnlang

An esolang based on RPN
https://github.com/1computer1/rpnlang

esoteric-language rpn stack-based

Last synced: about 2 months ago
JSON representation

An esolang based on RPN

Awesome Lists containing this project

README

        

# RPNLang

A dynamically-typed, RPN-based, and stack-based esoteric language.

```
> 'Hello World!'; // Hello World!
> 1 1 + 2 2 + -; // -2

x = 5;
< 1 2 3 $x [>]; // [5, 3, 2, 1]

adder = (a) => (
(b) => $a $b +
);

> 3 5 $adder@@; // 8

is_prime = (num) => (
$num $sqrt@ $floor@ 2 (sqnum, i) => ({
$num $i % 0 == : !?,
$i $sqnum >= : $num 1 !=,
!! : $sqnum $i 1 + $@
})@
);

> 1231 $is_prime@; // true
```

## Language

See the explanation for the language at the [documentations](./docs/README.md).
Note that it is not a guide but rather just the syntax and operators.

## API

RPNLang is interpreted with JavaScript.
You can require the module and run code:

```js
const RPNProgram = require('rpnlang');

const program = new RPNProgram('> 1 1 +;');
program.execute(); // 2
```

You can also use `RPNProgram.makeModule` in order to make a module compatible with RPNLang.
To call a lambda from RPNLang, use `RPNProgram.callLambda`.

```js
const RPNProgram = require('rpnlang');
module.exports = RPNProgram.makeModule({
num: 5,
add: (a, b) => a + b,
map: (...args) => {
const mapper = args[0];
return args.slice(1).map(e => RPNProgram.callLambda(mapper, [e])[0]);
}
});
```

```
math << 'math.js';
> math::num 10 math::add@; // 15
```

#### `RPNProgram(source[, options])`

- `source` - The source code.
- `options.log` - Function for standard output.
- `options.debug` - Debug mode, prints more errors.
- `options.safe` - Disables file I/O and stdin.

## CLI

You can also run a file via the CLI:

```
$ rpn input_file.rpn
```

#### `Flags`

- `-c [code]` - Runs code.
- `-d` - Enables debug mode.
- `-s` - Enables safe mode.

## Other

RPNLang is made purely for fun, don't take it too seriously!
Created with [jison](http://zaa.ch/jison/).