/muta-minits

TypeScript to LLVM compiler

Primary LanguageTypeScriptMIT LicenseMIT

Minits

Build Status

Typescript with LLVM backend.

img

Installing minits on Linux or macOS

First of all, you need install LLVM, See https://llvm.org/docs/GettingStarted.html. But if you are using Ubuntu or other distributions, using the built-in package management tool is a more convenient option like:

$ apt install -y llvm

Then install minits:

$ git clone https://github.com/cryptape/minits
$ npm install
$ npm run build

Writing and Compiling a TypeScript Program

Filename: main.ts

function main(): number {
    console.log("Hello World!");
    return 0;
}

Save the file and open your terminal:

$ node build/main/index.js build main.ts -o main.ll
$ clang main.ll -o main
$ ./main

Analysis for a minits Program: Brainfuck

minits is a completely static language, similar to clang or rust, function main() is the entry point to every executable minits program. It receives(optional) 2 parameters argc: number and argv: string[], and returns the exit code. So you can also write it as function main(argc: number, argv: string[]): number.

We suggest you read the source code under ./examples. Let's hava a look at ./examples/brainfuck.ts. Brainfuck is an esoteric programming language created in 1993 by Urban Müller, and is notable for its extreme minimalism. We wrote a brainfuck interpreter by minits. Compile this interpreter by

$ node build/main/index.js build examples/brainfuck.ts -o brainfuck.ll
$ clang brainfuck.ll -o brainfuck

And then execute a piece of code that generates Sierpinski Triangle:

$ ./brainfuck ">++++[<++++++++>-]>++++++++[>++++<-]>>++>>>+>>>+<<<<<<<<<<[-[->+<]>[-<+>>>.<<]>>>[[->++++++++[>++++<-]>.<<[->+<]+>[->++++++++++<<+>]>.[-]>]]+<<<[-[->+<]+>[-<+>>>-[->+<]++>[-<->]<<<]<<<<]++++++++++.+++.[-]<]+++++"

img

Most ts syntax is available at now, but there is still a lot of work to be done.

Project status

We plan to implement the following syntax:

Types

  • Primitive Types
    • number(support signed 64): 0x10, 12
    • boolean: true, false
    • string: "Hello"
    • void
    • null
    • * undefined
    • enum: enum { a = 1, b, c }
  • Object
    • Array
    • Tuple

Expression

  • Assignment: let a: number = 1;, let a: number[] = [1, 2, 3]
  • Parentheses
  • Function Expressions
  • Arrow Functions
  • * Class Expressions
  • Function Calls
  • ++ / --
  • + / - / ~
  • !
  • * typeof
  • +: number + number, string + string, eg.
  • *, /, %, , <<, >>, >>>, &, ^, and | operators
  • <, >, <=, >=, ==, !=, ===, and !== operators
  • * in
  • && and ||
  • The Conditional Operator: test ? expr1 : expr2
  • *=, /=, %=, +=, -=, <<=, >>=, >>>=, &=, ^=, |=
  • Destructuring Assignment: [x, y] = [y, x];

Statements

  • Block
  • Variable Statements
  • Let and Const Declarations
  • If, Do, and While Statements
  • For Statements
  • For-In Statements
  • For-Of Statements
  • Continue Statements
  • Break Statements
  • Return Statements
  • With Statements
  • Switch Statements
  • Throw Statements
  • * Try Statements

Function

  • Function Declarations
  • Function Implementations

Interfaces

TODO

Class

  • Class Declarations
  • New class

Build in functions

Licences

MIT