Giter Club home page Giter Club logo

thinscript's Introduction

ThinScript

ThinScript is an experimental programming language that compiles to JavaScript, WebAssembly, and C. It's meant to be a thin layer on top of WebAssembly that makes it easier to work with: no dependencies and fast compile times. The syntax is inspired by TypeScript and the compiler is open source and bootstrapped (it can compile itself).

This is still an experiment and isn't intended for real use yet. The biggest issue is that the generated code currently doesn't delete anything (garbage collection is planned but not yet implemented). Also the WebAssembly specification is still being developed and the current binary format will stop working when WebAssembly is officially released.

Demo

An interactive compiler demo is available online at http://evanw.github.io/thinscript/. Here's some example code to demonstrate the language (documentation will be written at some point):

declare function print(text: string): void;

class Link {
  value: int;
  next: Link;
}

class List {
  first: Link;
  last: Link;

  append(value: int): void {
    var link = new Link();
    link.value = value;

    // Append the new link to the end of the chain
    if (this.first == null) this.first = link;
    else this.last.next = link;
    this.last = link;
  }
}

extern function main(): int {
  var list = new List();
  list.append(1);
  list.append(2);
  list.append(3);

  var total = 0;
  var link = list.first;
  while (link != null) {
    total = total + link.value;
    link = link.next;
  }

  #if JS
    print("Hello from JavaScript");
  #elif WASM
    print("Hello from WebAssembly");
  #elif C
    print("Hello from C");
  #else
    print("Unknown target");
  #endif

  return total;
}

Building

Run node build.js to build the compiler using itself. This generates updated versions of out/compiled.js, out/compiled.wasm, and out/compiled.c. If you have a C compiler installed, this also builds a native version of the compiler by compiling out/compiled.c and lib/thinc.c together into the out/thinc binary.

thinscript's People

Contributors

evanw avatar tcyrus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

thinscript's Issues

Updated wasm target to 0x1 (MVP) format

I have a commit here that implements the 0x1 format but as there's no license, yet, I am holding back the PR for now to not complicate things even further. Aside from that, thinscript is the most refreshing piece of code I've seen in a while and I'd love to continue working on it!

  • Updated output files to use the 'WebAssembly' API

  • Added optional tracing to wasm target for debugging purposes

  • Fixed a couple of wasm target issues (and probably introduced new ones)

  • Extended wasm custom name section support

  • Implemented additional convenience utility

  • Implemented a few simple wasm specific compiler optimizations

  • Tweaked source files for TypeScript syntax compatibility where applicable

  • Changed the js target's generated iife signature from (__declare, __extern) to (__imports, __exports)

P.S.: Did I mention how awesome this project is?

Support for int64, float32 and float64

What?
Add new native types (WASM) int64, float32 and float64

Why?
Currently only int32 and below are supported, I tried to add missing native types but not able to compile. Please add these type or write a how to note.

Interacting with JavaScript, DOM and Web APIs?

Are there any plans to support interaction with JavaScript, DOM and Web APIs with ThinScript? I'd be really interested in seeing the performance benefit that low-level WebAssembly can bring when creating large web apps. I'd also be interested in exposing API end-points for other JavaScript libraries to consume/interact with the compiled application.

I haven't got a huge experience in this field and I'm not even sure it might all be possible/feasible, but I thought it might be a good discussion point.

Darn Good

No real ish listed in this message here.

Only a few 2c suggestions:

keep the language as thin and clear as possible:

  • at first do not implement a GC (then, maybe)
  • the lang should be as near as possible to typescript or some other language that already has intellisense plugins in editors like atom or vsc

make it a blockbuster:

  • see if you can integrate a basic WebGL/OpenGL backend on top of which a standard GUI system can be implemented
  • shared memory thread support , since JS/WASM/C are already there
  • socket/comm support

I know these things heavily depend on WASM evolution path but having a single source code that can be compiled/transpiled into native applications for desktop,mobile,web is ... well... :-D

Keep on with this nice piece of work.

Rob

(I will follow this project for sure and see if I can somehow be of help).

Wasm Compilator

As I see there is inbrowser wasm compilattion. I need pure C to WASM compiler in JS. I'm stuck with binaryen. Can you help me with it?

Initial decorators

I now also have a branch with an initial proposal for decorators, where these are just AST transformers. Example:

@metadata
extern main(): string {
  return __file;
}

The decorator then transforms the source to the following in JavaScript:

(function(__imports, __exports) {
  function main() {
    var __name = "main", __file = "sandbox/decorators.thin", __line = 1, __column = 0;
    return __file;
  }
}(
  typeof global !== 'undefined' ? global : this,
  typeof exports !== 'undefined' ? exports : this
));

Or in C:

static const uint32_t __string_0_main[] = {4, S('m', 'a'), S('i', 'n')};
static const uint32_t __string_1_sandbox_decorators_thin[] = {23, S('s', 'a'), S('n', 'd'), S('b', 'o'), S('x', '/'), S('d', 'e'), S('c', 'o'), S('r', 'a'), S('t', 'o'), S('r', 's'), S('.', 't'), S('h',
  'i'), S('n', 0)};

static uint16_t *main() {
  const uint16_t *__name = (const uint16_t *)__string_0_main;
  const uint16_t *__file = (const uint16_t *)__string_1_sandbox_decorators_thin;
  int32_t __line = 1;
  int32_t __column = 0;

  return __file;
}

The feature, as proposed, is compatible with all possible backends because it operates on the AST directly. However, to become actually useful, ways to extend the compiler (i.e. through imports of custom modules) must be implemented first.

The reason why I added this so early already is that I was trying to improve the output of assert calls for debugging purposes, but I ultimately realized that some sort of an actually usable stack trace (ideally with source maps) would be a lot better suited for this task. Unfortunately, browsers I have tested do not yet pull useful information from name sections.

Using SquiLu to compile thinscript

Hello !
I just saw this project on hacker news and I was looking to do something like this but modifying my fork of squirrel https://github.com/mingodad/squilu and just now I could get it to parse this repository src with few changes.
I saw your other projects and I think that we could join efforts.

Tomorrow I'll see if I get SquiLu to run thinscript, by the way why thinscript if you already has skew ?

Cheers !

Java Compiler

Hi,

I started to wear thin compiler in Java
in order to use in a production line Maven

I added the target java
and java.thin file

this produces a file org/wasm/thin/Thin.java
I currently generates classes, enums, functions, methods.

The generated code is not yet operational. missing translation of thin type to java type.
It also lacks the managements of the externs.

How can I send you my changes.
I'm not familiar with GitHub.

A+JYT

Syntax

Hi, Evan. I'm impressed with this project. I have some thoughts about c-compilant language: meta programming on comments with all this # if is very strange and it look's like dinosaur today! Why not to make compilation loops to produce result until all known constants and if statements will not solved. Let me explain:

We have ENV constant which hold target environment, let it be 'BROWSER'. We write an usual if statement:

if (ENV == "BROWSER") {
   const MSG="It's a Browser"
}
else if (ENV == "NODEJS") {
   const MSG="it's Node.js" 
}

print(MSG)

So the first compilation loop should remove if branches and leave const MSG="It's a Browser". Now we have the second known constant MSG which should be equal to "It's a Browser". And on the second cycle loop print will get MSG value and become print("It's a Browser"). On the third loop we see no other links to constant message and remove it. And so we got:

print("It's a Browser")

So if there is runtime computations in the if statement than it shall not be reduced:

if (ENV == "BROWSER" && random() > 0.5) {
   const MSG="It's a Browser"
}

There is only one problem with block scoped vars which should be solved somehow. But this code looks more elegant.

Add operators

I think that += and -= should be included in ThinScript

Documentation

I know that the readme says you'll write it at some point, but the sooner the better ๐Ÿ˜„. I'm just asking if you planned on it anytime soon (looks like a really cool project ๐Ÿ‘), plus docs are needed for creating a syntax package (I need one for Atom).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.