Champii / Steel

Strongly Typed Experimental Expressive Language
Apache License 2.0
3 stars 0 forks source link
compiler javascript language strongly-typed transpiler typescript

Steel

Strongly Typed Experimental Expressive Language

Build Status Coverage Status

Language that transpile to TypeScript and JavaScript.

Steel is a bootstraped language. That means the code itself is developed in Steel.

Check ./src folder for steel sources. Check ./lib for the compiled sources.

Highly inspired from LiveScript.

Goals

This language tries to implement strong static typing over a subset of Livescript.

To do so, it transpile first to TypeScript, and let it transpile to Javascript. This allow to use the power of a well typed and designed language while smoothing its syntax.

It alse tries not to fall into the same 'over-simplification' Livescript does, and avoid to implement features complexifying the reading and understanding of the code.

At term, this language aim to be more functional, and might borrow some concepts from language like Haskell or Ocaml (custom operator?, immutability?, infinite lists?, ...).

Exemples

Basics

require
  fs                           # require fs as a whole
  path: { resolve }            # require path but use only resolve
  './localFile'                # require a local file
  './localFile2': customName   # same but with a custom name

# external type declaration
foo := number -> number -> number
foo = (a, b) -> a + b

# inline type declaration
bar = (c: number, d: number): number ~>
  if c? and d? and d isnt 0
    c + d
  else
    0

# returns undefined
nonReturning = !-> 1

# inline type declaration with templating and currying
map    = <T>(f: (x: T): T, arr: T[]): T[] --> arr.map f

# external type declaration with templating and currying (alpha)
filter := <T> => (T -> T) -> T[] -> T[]
filter = (f, arr) --> arr.filter f

# call chaining and function shorthand
[1, 2, 3]
  |> map (+ 2)     # stands for map(it => it + 2)
  |> filter (> 2)

# Class
class Animal
  a: 1
  b: -> 2
  constructor: (val: number) -> @a = val

# Inheritance
class Dog: Animal

dog: Dog = new Dog 1

Transpiled in TypeScript with sc -cts source.s turns into:

(function () {
  const fs = require('fs');
  const _path = require('path');
  let {resolve} = _path;
  const localFile = require('./localFile');
  const customName = require('./localFile2');
  let foo:(a:number,b:number) => number = function (a, b) {
    return a + b;
  };
  let bar = (c:number, d:number): number => {
    if ((c != null) && (d != null) && d !== 0) {
      return c + d;
    } else {
      return 0;
    }
  };
  let nonReturning = function (it?:any) {
    1;
  };
  let map = curry$(function <T>(f:(x: T)=> T, arr:T[]): T[] {
    return arr.map(f);
  });
  let filter = curry$(function <T>(f:(x: T)=> T, arr:T[]): T[] {
    return arr.filter(f);
  });
  (filter(function (it:any) {
    return it>2;
  })(map(function (it:any) {
    return it+2;
  })([1, 2, 3])));
  class Animal {
    a = 1;
    b(it?:any) {
      return 2;
    }
    constructor(val:number) {
      this.a = val;
    }
  };
  class Dog extends Animal {};
  let dog:Dog = new Dog(1);
  function curry$(f: any, bound?: any){ let context: any, _curry: any = function(args?: any){ return f.length > 1 ? function(){ var params = args ? args.concat() :[]; context = bound ? context || this : this; return params.push.apply(params, arguments) < f.length && arguments.length ? _curry.call(context, params) : f.apply(context, params); } : f; }; return _curry(); }
})();

Install

  npm install -g steel-lang

Compiler usage

Compiler name is sc, and stands for Steel Compiler.

Usage

  $> sc -h

    Usage: sc [options] <files ...>

    Options:

      -V, --version          output the version number
      -c, --compile          Compile files
      -p, --print            Print files
      -o, --output <folder>  File/folder of output
      -s, --strict           Disallow implicit use of <Any> type
      -t, --typescript       Output Typescript instead of Javascript (no typechecking)
      -b, --bare             Dont wrap into top level anonymous function
      -q, --quiet            Dont output types errors/warnings (useful with -p)
      -h, --help             output usage information

Compile and execute on the fly

  sc file.s

You can only compile and execute single file on the fly

Files extensions are .s. This extension is registered inside NodeJS when loading steel-lang to auto-compile steel files on the fly when required.

This means you can write

  require('steel-lang');
  const obj = require('./someFile.s');

Compile a file/folder

  sc -c file1.s file2.s
  sc -c folder/*.s
  sc -c folder/**/*.s

The path use the same format as gulp. You can compile recursively or exclude some folders.

Compile to a diferent output

  sc -o dir -c file1.s file2.s
  sc -o dir -c folder/*.s

Will output compiled files into dir folder.

If the folder doesnt exists, it is created on the fly.

The arborescence is recreated if a folder is recursively compiled.

Features

TODO: