Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ronami/typelang
🌳 A tiny language interpreter implemented purely in TypeScript's type-system
https://github.com/ronami/typelang
compiler functional-programming learning recursion type-system typescript
Last synced: 13 days ago
JSON representation
🌳 A tiny language interpreter implemented purely in TypeScript's type-system
- Host: GitHub
- URL: https://github.com/ronami/typelang
- Owner: ronami
- License: mit
- Created: 2020-10-22T19:49:44.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2020-10-28T16:14:58.000Z (about 4 years ago)
- Last Synced: 2024-08-01T16:40:18.027Z (3 months ago)
- Topics: compiler, functional-programming, learning, recursion, type-system, typescript
- Language: TypeScript
- Homepage:
- Size: 146 KB
- Stars: 244
- Watchers: 5
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-list - typelang - system | ronami | 156 | (TypeScript)
README
## 🌳 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](https://www.typescriptlang.org/play?ts=4.1.0-dev.20201027#code/PTAECkFcGcBdQIYDtQFMAeCC2AHANqgDSgAWA9gG6oBOaCAxiaHKjqLGc6qqAJazRQZSLBwiAULACeOHgGEyAEx4BeUAANxoUAAoAIqgBmodKABEACVR48ZMwEot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P070jwv1IL+EdaxFL17b+E32rbAPG2gPkL70DgC6QMDEhHf3Hbi7DVwOvYINP1gOT2XboNLWYPq6fEEB4MzIEOUbP3kK8QvHEACRIkV1yioDD210ij10z3NIQRD2GLY6koVzbIJ3kP+rt6DZlV524WGTAMG2cWKkpVsNdDt5CMahOBcNcQsSt1Jj+2+1ZUUni1N0t28Ni3FAd0QQoNMQ928PWT2Cf2Cly1dDCJqSIP0N3haOClh1JVOSV3qI-hx0epXmD3sNqMEpGWZ0sX8Ifjm0iIKNwVGRF2y6no8BHC6011C26M6j6OT6GNFF8R0VFlS2m6UrZPP0rD+PV0jy0leOWTTV+PsMZP1CGoxOaRCIiItOx2Ijl6yguNeM8MFB8NyF0SHBNM81cKsbVbsAz1lBSFdi8hZBV0aJ6lMQAmgADwlxlwXq0hBM2T7MvQAA6Sy5h3cYEBcmzQ8OzHjTklz2z-I-i-dIRwSkq1SYuJdA2Ma2gADfdwaPiMIwS+chcvAxcVz-IKZe9ILg89zuz-iDjzzSSrzPi-qHzqqVSPzDjMaALSSQLdz5cuzELyDULWz+Lez-hIUhCizqA-hdIxAZzGzxLYLuz49jLMLez1kZg-0MoVLAuvCFzrLpLLLoLbLr9l2FLVkPLU9tLNk1kxAeL1zH1WZNEVLNLupiUYN2gE1p8w1eGZu25-xOrag8r-ItJ+r7a5L14Kr9OfLwLwrpL1GW8dGw1TaUYjyTkvEWDqSNmo01DqwwDtIrDCzszmww1Xpqz2rDGO4EE4bE8zcMSFgUIZgcbV5bc4jYuB2-9JeFgwDAsSj+YyJkrl2Yb6rw1nVBrDGurW5JbFbRrAr1zprorD24r3Lwbf8UruLdb4LKING28zrl6cgbraYHre2d2l2vrCU29Ab8OPLEzetMbzApbCCWTZjCKxjRuUtZB9y-EPIrbyzSouztJ873cJE+arbzTrTHQc1GljkDTaYZtHTTZLGPT5dQbBQfI876zzj9EZ7s7-IiI4gQAA))
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.
```typescript
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)
``````typescript
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 ...)
``````typescript
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")'>; // falsetype Result = Eval<'(And True True)'>; // true
type Result = Eval<'(And False False)'>; // falsetype Result = Eval<'(Or True False)'>; // true
type Result = Eval<'(Or False True)'>; // truetype 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:
```typescript
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.
```typescript
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:
```typescript
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`:
```typescript
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
- [The Super Tiny Compiler](https://github.com/jamiebuilds/the-super-tiny-compiler)
- [TypeScripts Type System is Turing Complete](https://github.com/microsoft/TypeScript/issues/14833)
- [Typing the Technical Interview in TypeScript](https://gal.hagever.com/posts/typing-the-technical-interview-in-typescript/)
- [Functions and algorithms implemented purely with TypeScript's type system](https://github.com/ronami/meta-typing)
- [A SQL database implemented purely in TypeScript type annotations](https://github.com/codemix/ts-sql)