# plang-interpreter

> A Deviation Language Interpreter

Latest version **0.0.2** (published 2021-09-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install plang-interpreter
pnpm add plang-interpreter
yarn add plang-interpreter
bun add plang-interpreter
```

Provides the command `plang-interpreter`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.2 |
| Published | 2021-09-02 |
| First published | 2021-05-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 164.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Terry Qi |
| Maintainers | troppydash |

## Links

- npm: https://www.npmjs.com/package/plang-interpreter
- Repository: https://github.com/Troppydash/plang-interpreter
- Homepage: https://github.com/Troppydash/plang-interpreter#readme
- Issues: https://github.com/Troppydash/plang-interpreter/issues
- npm.io page: https://npm.io/package/plang-interpreter

## Dependencies (5)

- [blessed](https://npm.io/package/blessed.md) ^0.1.81
- [deasync](https://npm.io/package/deasync.md) ^0.1.21
- [node-fetch](https://npm.io/package/node-fetch.md) ^2.6.1
- [prompt-sync](https://npm.io/package/prompt-sync.md) ^4.2.0
- [prompt-sync-history](https://npm.io/package/prompt-sync-history.md) ^1.0.1

## Recent versions

- 0.0.2 (latest) — 2021-09-02
- 0.0.1 — 2021-05-22

## README

## The Deviation Interpreter
or Devia for shorten.

### Description
This repository houses the source code of the deviation interpreter written entirely in Typescript.

The source code are all in the `src/` folder. `compiler/` contains the lexer and parser; `inout/` contains the input and output functions; `linking/` contains the linker; `problem/` contains the error printer; `repl/` contains the REPL; `vm/` contains the virtual machine emitter and stack machine; `extensions/` are code that I've copied from stackoverflow.

### Examples
```
# this is a comment
```

```
# fibonacci calculator
func fib(n) {
    if n < 2 {
        return n
    }
    return fib(n - 1) + fib(n - 2)
}
# print the 12th fibonacci number
say(fib(12))
```

```
# program that asks for your name forever
loop {
    name = ask("What is your name? ")
    if name == null {
        break
    }
    say("Hello", name)
}
```

```
# type functions
impl times(self, function) for Num {
    loop self { # loop self amount of times
        function()
    }
}

func sayHello() {
    say("Hello")
}
# or
sayHello = [] say("Hello")

# print Hello 3 times
3.times(sayHello)

# or alternatively
3.times([] {
    say("Hello")
})
```

```
# closures are supported
func makeCounter(initial) {
    count = initial
    return [] {
        old = count
        outer count = count + 1  # note we are assigning to the outer `count` variable
        return old
    }
}

counter = makeCounter(1)
say(counter()) # prints 1
say(counter()) # prints 2
say(counter()) # prints 3
```

```
# who needs classes anyways
func makeVector(x, y) {
    data = dict(x: x, y: y)
    return dict(
        get: [axis: Str] data[axis],
        set: [axis: Str, value] {
            data[axis] = value
            return value
        },
        str: [] "(\(data.x), \(data.y))"
    )
}

v1 = makeVector(1, 2)
say(v1.str())  # prints "(1, 2)"

v1["y"] = v1["x"] = 3
say(v1.str())  # prints "(3, 3)"
```

```
# using javascript inside of devia
values = list(1, 10, 20, 30)

javascript """
    const values = de.import("values"); // import the variable "values" from devia
    function factorial(n) {
        if (n < 2) {
            return 1;
        }
        return n * factorial(n-1);
    }
    const out = [];
    for (const value of values) {
        out.push(factorial(value));
    }
    de.export("out", out); // export the variable as "out" to devia
"""

each item, index of out {
    say("factorial", values[index], "=", item)
}
```

```
type Complex(real, imag)

impl +(self, other: Complex) for Complex {
    return Complex(self.real + other.real, self.imag + other.imag)
}

impl str(self) for Complex {
    return "\(self.real)+\(self.imag)i"
}

c1 = Complex(1, 2)
c2 = Complex(2, 3)
say("\(c1) + \(c2) = \(c1 + c2)")
```

### How do I use it
Under Github Release, download `tdu.exe` and the executable of your os (`plang-interpreter-win.exe` for windows users).
Place both under the same folder. Add the folder to path if you wish, and rename `plang-interpreter` to `devia` if you wish.


If you have the executable, `devia.exe <file>`
will run the file. You can also start the REPL by running `devia.exe` directly with no arguments.

If there is no executable, see the section below to build it yourself.

### How do I build it
Node 12 or higher is used to build the interpreter, but feel free to try it with a lower node version. The command `node -v` will tell you if node is installed on your machine.
Run `npm install` first to install all the dependencies, and run `npm run build-cli` to make an executable in the `dist/` folder. Rename the executable for your platform to `devia.exe`. 

Other commands located in package.json are as follows
```
npm run build         ;; compiles typescript into javascript
npm run run           ;; runs the compiled javascript using node
npm run clean         ;; cleans the compiled javascript folder
npm run build-run     ;; clean, compile typescript then runs it
npm run build-browser ;; produce a AMD format single file index.js for use in a browser
npm run build-umd     ;; produce a UMD format single file used for use in a browser
```

---
_Source: https://npm.io/package/plang-interpreter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
