Giter Site home page Giter Site logo

gotoc / cooltojs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nfriend/cooltojs

0.0 1.0 0.0 1.89 MB

A Cool-to-JavaScript transpiler, written in TypeScript

Home Page: http://nathanfriend.io/cooltojs/

License: MIT License

HTML 2.39% JavaScript 70.19% C 0.03% CSS 1.30% TypeScript 26.09%

cooltojs's Introduction

cooltojs

A Cool-to-JavaScript transpiler, written in TypeScript.

Example usage

<script type="text/cool" src="HelloWorld.cl"></script>
<script src="cooltojs-1.0.0.js"></script>
<script>

    // automatically fetch any Cool source referenced 
    // by a <script type="text/cool"> element
    CoolToJS.GetReferencedCoolSources(function(sources) {

        // transpile the source
        var transpilerOutput = CoolToJS.Transpile({
            coolProgramSources: sources
        });
        
        if (transpilerOutput.success) {
            
            // do what you want with the output
            eval(transpilerOutput.generatedJavaScript);
        }
    });
</script>

Setup

By default, the output of your Cool program will be redirected to console.log. You can specify a different output function with the out_string and out_int options:

var transpilerOutput = CoolToJS.Transpile({
    coolProgramSources: sources,
    out_string: function(output) {
        document.getElementById('output').innerHTML += output;
    },
    out_int: function (output) {
        document.getElementById('output').innerHTML += output;
    }
});

Similarly, you can provide input to your Cool program by providing in_string and in_int functions. These functions should accept an iterator object as a parameter. Once input has been entered by the user, the iterator's next() method should be invoked with the user's input. For example:

var myInputField = document.getElementById('my-input-field'),
    onlyNumbers = false,
    iterator;

myInputField.onkeydown = function(e) {
    if (e.which === 13 /* Enter */) {
        // if onlyNumbers === true, input should be validated 
        // to ensure a valid number was entered
    
        iterator.next(myInputField.value)
        myInputField.value = '';
    }
}

var transpilerOutput = CoolToJS.Transpile({
    coolProgramSources: sources,
    in_string: function (newGenerator) {
        onlyNumbers = false;
        iterator = newGenerator;
    },
    in_int: function (newGenerator) {
        onlyNumbers = true;
        iterator = newGenerator;
    }
});

By default, the in_string and in_int functions are mapped to empty functions, so you won't be able to send input to your Cool program without providing these functions.

Output

The output of the CoolToJS transpiler is valid ES6 JavaScript. Note that support for ES6 is still fairly limited, so it's advisable to use a ES6 to ES5 compiler like Babel to generate code that can target current browsers. An example of this transformation using Babel:

<script type="text/cool" src="HelloWorld.cl"></script>
<script src="lib/babel/browser-polyfill.js"></script>
<script src="lib/babel/browser.js"></script>
<script src="cooltojs-1.0.0.js"></script>
<script>

    // automatically fetch any Cool source referenced 
    // by a <script type="text/cool"> element
    CoolToJS.GetReferencedCoolSources(function(sources) {
        
        // transpile the source
        var transpilerOutput = CoolToJS.Transpile({
            coolProgramSources: sources
        });
        
        if (transpilerOutput.success) {
            
            // transform the generated ES6 code to ES5 code
            var es5Code = babel.transform(transpilerOutput.generatedJavaScript, {
                stage: 0
            }).code;
            
            // do what you want with the ES5 code
            eval(es5Code);
        }
    });
</script>

See the live version of this example at http://nathanfriend.io/cooltojs/example/.

cooltojs's People

Contributors

nfriend avatar

Watchers

 avatar

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.