Another JavaScript Language - ColaScript


    Hello! In this article I want to introduce you to my language - ColaScript . In a nutshell, this is a new syntax language that translates into JavaScript. I will start by telling you about the reasons for the appearance of this language.

    Reasons for the appearance



    Why do we need languages ​​translated into JavaScript?

    For me personally, the benefit of using such languages ​​is, of course, primarily syntactic sugar, which reduces the amount of code. Secondly, it is structural (OOP, modules, packages ...), which is not so easy to achieve in pure JavaScript. At the moment, there are 3 languages ​​that have almost all of the listed qualities:

    CoffeeScript

    This is the very first language of this type that I learned about. At the moment, there are a large number of syntax chips in this language and OOP is present in the face of classes. The general syntax style in CoffeeScript is the same as in Ruby and Python, it looks cool, but personally I wanted to see all the same chips, but with C-like syntax. Also in CoffeeScript there is no connection of source / modules from the code.

    TypeScript

    TypeScript is a language developed by Microsoft that has static typing, OOP implementation, and plug-ins. In terms of structure, everything is fine, but it does not have sugar syntax.



    Dart

    My first acquaintance with this language took place shortly after the appearance of its first version: the language was still raw, and I myself still did not really understand why it was so necessary. Last summer, I decided to see what happened to this language - it was what I needed: OOP, packages (even with my own manager), a good standard library and syntactic sugar, from which the cascading operator was especially sunk to me:

    query("#myElement")
        ..innerHtml = "Hello World!"
        ..style.backgroundColor = "red";
    

    Dart is a compiled JavaScript language, but without backward compatibility. In particular, this leads to a large weight of runtime and libraries, as well as to difficulties in working with existing JavaScript code.

    Eventually

    As a result, I was overcome by the desire to create my own language, which borrows the best sides of the three above-mentioned languages. So ColaScript was invented.

    A little bit about the creation process


    A tool such as UglifyJS was taken as the basis . UglifyJS was created to compress JavaScript code, for this it parses the code and works with the AST tree. My task was to modify the parser to the new syntax, as well as directly writing the ColaScript-AST translator in JavaScript-AST, everything else is already in UglifyJS. The parser has been improved to support both ColaScript and JavaScript at the same time, this is done so that you can easily connect libraries and frameworks written in JavaScript.

    What happened


    You can see the result of my work on github , you can play with the language live here . At the moment, only the first stage of development has been completed and the language does not yet have any special advantages over the same CoffeeScript, TypeScript and Dart, but there are still many ideas for implementation that are described here .

    A very small code example:
    @use strict
    main(){
        console.log("
            Hello World!
            This is simple example for Habrahabr.ru!
            P.S. I love haters so much :-)
            @{Date()}
            ");
    }
    

    Translation Result:
    "use strict";
    window.addEventListener("DOMContentLoaded", function main() {
        console.log("\nHello World!\nThis is simple example for Habrahabr.ru!\n\nP.S. I love haters so much :-)\n" + Date() + "\n\n");
    }, false);
    

    Waiting for your opinions about the language. Thank you all for your attention.

    Also popular now: