All Projects â†’ ronami â†’ Typelang

ronami / Typelang

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

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Typelang

Meta Typing
📚 Functions and algorithms implemented purely with TypeScript's type system
Stars: ✭ 628 (+321.48%)
Mutual labels:  recursion, learning
J8plus
Library containing useful tools for Java 8
Stars: ✭ 23 (-84.56%)
Mutual labels:  recursion, functional-programming
Mlton
The MLton repository
Stars: ✭ 683 (+358.39%)
Mutual labels:  compiler, functional-programming
Groovy
Apache Groovy: A powerful multi-faceted programming language for the JVM platform
Stars: ✭ 4,359 (+2825.5%)
Mutual labels:  compiler, functional-programming
Faust
Functional programming language for signal processing and sound synthesis
Stars: ✭ 1,360 (+812.75%)
Mutual labels:  compiler, functional-programming
Felix
The Felix Programming Language
Stars: ✭ 609 (+308.72%)
Mutual labels:  compiler, functional-programming
Grin
GRIN is a compiler back-end for lazy and strict functional languages with whole program optimization support.
Stars: ✭ 834 (+459.73%)
Mutual labels:  compiler, functional-programming
Never
Never: statically typed, embeddable functional programming language.
Stars: ✭ 248 (+66.44%)
Mutual labels:  compiler, functional-programming
Idiolisp
A statically typed functional programming language
Stars: ✭ 78 (-47.65%)
Mutual labels:  compiler, functional-programming
Jhc Components
JHC Haskell compiler split into reusable components
Stars: ✭ 55 (-63.09%)
Mutual labels:  compiler, functional-programming
Javascript Exercises
📚 Collection of JavaScript exercises and coding challenges.
Stars: ✭ 385 (+158.39%)
Mutual labels:  learning, functional-programming
Ghc Grin
GRIN backend for GHC
Stars: ✭ 123 (-17.45%)
Mutual labels:  compiler, functional-programming
Scriptum
A fool's scriptum on functional programming
Stars: ✭ 346 (+132.21%)
Mutual labels:  recursion, functional-programming
Clio
Clio is a functional, parallel, distributed programming language.
Stars: ✭ 555 (+272.48%)
Mutual labels:  compiler, functional-programming
Write You A Haskell
Building a modern functional compiler from first principles. (http://dev.stephendiehl.com/fun/)
Stars: ✭ 3,064 (+1956.38%)
Mutual labels:  compiler, functional-programming
Ring
Innovative and practical general-purpose multi-paradigm language
Stars: ✭ 716 (+380.54%)
Mutual labels:  compiler, functional-programming
Silt
An in-progress fast, dependently typed, functional programming language implemented in Swift.
Stars: ✭ 217 (+45.64%)
Mutual labels:  compiler, functional-programming
Fsharp
The F# compiler, F# core library, F# language service, and F# tooling integration for Visual Studio
Stars: ✭ 2,966 (+1890.6%)
Mutual labels:  compiler, functional-programming
Cfl
a Compileable statically typed Functional programming Language
Stars: ✭ 7 (-95.3%)
Mutual labels:  compiler, functional-programming
Bqn
An APL-like programming language. Self-hosted!
Stars: ✭ 100 (-32.89%)
Mutual labels:  compiler, functional-programming

🌳 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'>; // '123'
type Result = Eval<'"hello"'>; // 'hello'
type Result = Eval<'True'>; // true
type Result = Eval<'False'>; // false

Conditionals

If statements are in the form of:

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

type Result = Eval<'(If True "yes" "no")'>; // 'yes'
type Result = Eval<'(If False "yes" "no")'>; // '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<'(Join "a" "b" "c" "d")'>; // 'abcd'

type Result = Eval<'(Eq 2 2)'>; // true
type Result = Eval<'(Eq "you" "me")'>; // false

type Result = Eval<'(And True True)'>; // true
type Result = Eval<'(And False False)'>; // false

type Result = Eval<'(Or True False)'>; // true
type Result = Eval<'(Or False True)'>; // true

type Result = Eval<'(++ 2)'>; // '3'
type Result = Eval<'(-- 5)'>; // '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<'(Def x 1) x'>; // '1'
type Result = Eval<'undefined_variable'>; // null
type Result = Eval<'(Def x 2) (++ x)'>; // '3'
type Result = Eval<`
  (Def x (++ 3))
  (Def y (++ x))
  (Join "result: " y)
`>; // '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<`
  (Fun SayHello (first last) (Join "Hello " first " " last))
  (SayHello "John" "Doe")
`>; // '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<'(++ 1) (++ 2)'>; // '3'
type Result = Eval<'(Eq 1 1) (Eq 2 3)'>; // 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<'"aa'>; // 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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].