Need help with typelang?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

ronami
149 Stars 0 Forks MIT License 85 Commits 2 Opened issues

Description

🌳 A tiny language interpreter implemented purely in TypeScript's type-system

Services available

!
?

Need anything else?

Contributors list

# 15,642
React
Sass
Deno
GraphQL
85 commits

🌳 TypeLang

A tiny language interpreter implemented purely in TypeScript's type-system

Introduction

This is an extremely simplified language interpreter implemented purely in TypeScript type annotations. You pass your code as a string and get back the result by hovering with your mouse on the resulting type annotation.

The syntax is Lisp-like. If you're not familiar with it, here's a quick comparison to JavaScript's syntax:

 LISP                      JavaScript

(add 1 2) add(1, 2) (subtract 5 2) subtract(5, 2) (add 3 (subtract 2 1)) add(3, subtract(2, 1))

The language supports booleans, numbers, strings, conditionals (if statements), and calling the following built-in functions:

  • ++
    : Increases a number by one.
  • --
    : Decreases a number by one.
  • Eq
    : Checks if both of its arguments are equal.
  • And
    : Returns
    true
    if both of its arguments are
    true
    .
  • Or
    : Returns
    true
    if at least one of its arguments are
    true
    .
  • Join
    : Concatenates two strings together.

It also supports declaring variables and functions (See bellow).

Try running the code (See it live)

Install

typelang
with
npm install typelang
or with
yarn install typelang
(requires TypeScript v4.1.0 or above).

Primitives

The language has support for nulls (no value), numbers, strings, and booleans.

import { Eval } from "typelang";

type Result = Eval; // null type Result = Eval; // '123' type Result = Eval; // 'hello' type Result = Eval; // true type Result = Eval; // false

Conditionals

If statements are in the form of:

(If predicate then-expression else-expression)
import { Eval } from "typelang";

type Result = Eval; // 'yes' type Result = Eval; // 'no'

Calling functions

The language supports a few built-in functions (listed above) and you can also define custom functions.

Here's how you call a function:

(function-name arg1 arg2 arg3 ...)
import { Eval } from "typelang";

type Result = Eval; // 'abcd'

type Result = Eval; // true type Result = Eval; // false

type Result = Eval; // true type Result = Eval; // false

type Result = Eval; // true type Result = Eval; // true

type Result = Eval; // '3' type Result = Eval; // '4'

Declaring variables

Declare a variable to hold a value and reference it later on. Here's how you do it:

(Def variable-name value)

Notice that the language returns the last expression, so the first example declares a variable and then returns it:

import { Eval } from "typelang";

type Result = Eval; // '1' type Result = Eval; // null type Result = Eval; // '3' type Result = Eval; // 'result: 5'

Declaring functions

To declare a custom function, use the following syntax:

(Fun function-name (arg1 arg2) (function-body))

Note that you can access

arg1
and
arg2
only from inside the function body and not from outside. You can still access variables declared on the global scope from inside the function scope.
import { Eval } from "typelang";

type Result = Eval; // 'Hello John Doe'

The last expression is returned

Notice that all expressions are evaluated but only the last one is returned:

import { Eval } from "typelang";

type Result = Eval; // '3' type Result = Eval; // false

Invalid syntax returns
never

In case of an error,

Eval
returns
never
:
import { Eval } from "typelang";

type Result = Eval; // never type Result = Eval; // never type Result = Eval; // never

Note: TypeScript has a limitation on how deep its computation can get. Because of this, we're limited to small inputs. If you're getting the following error:

Type instantiation is excessively deep and possibly infinite
, please try using a smaller input.

Additional links

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.