A
Cool-to-JavaScript
transpiler, written in TypeScript.
<script type="text/cool" src="HelloWorld.cl"></script>
<script src="cooltojs-1.0.1.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>
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.
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.1.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/.
- If you haven't already, install Git and Node
- Clone this repository
- Inside the
CoolToJS
directory, runnpm install
- Build the transpiler by running
npm run build
. This will update theCoolToJS/cooltojs-X.X.X.*
files. - To run the example site locally, run
npx http-server
inside theCoolToJS
directory. This will start a local webserver (using thehttp-server
npm package) and print the local URL to the site. Navigate to the URL in a browser and start transpiling 🚀