RReverser's

Ingvar Stepanyan

JavaScript developer, speaker and reverse engineer. D2D programmer. Sometimes human.


Bootstrap package for creating compilers to JavaScript

View on GitHub

This is bootstrap package for easy extending to own compilers from any language to JavaScript, inspired by article in Mozilla blog.

It uses Jison as parser and Mozilla's source-map library for generating JavaScript code with source map. Earlier used ast-types + escodegen but those were more efficient for manipulating existing JavaScript AST and not that much for generating completely new code.

Inside

This bootstrap consists of:

  • package.json - obviously, basic npm manifest with needed modules.
  • syntax.jison - syntax of your language in Jison format with minimal needed helpers for building JS source nodes.
  • VM.js - "virtual machine" object for initialization logic and methods or properties that can be used by generated code; in most cases you will need such object for simplicity of actual generated code.
  • sample.txt - sample input file for compiler.
  • options.js - command line option handler, it can be used "as is" in almost any child project but you can add own options if needed here.
  • index.js - actually compiler runner; currently it generates parser from .jison syntax on each execution, and for better performance it's recommended to disable this in release when your syntax is completely defined.

Syntax

Sample "language" skips any whitespaces and parses identifiers splitted by semicolon from input file and converts those to string values being assigned to VM.current property.

Usage

compile2js> node .

  Usage: compile2js [options]

  Options:

    -h, --help              output usage information
    -V, --version           output the version number
    -i, --input <source>    specify input file
    -o, --output <js>       specify output file (default: <input>.js)
    -a, --ast [json]        save AST (default path: <output>.json)
    -m, --source-map [map]  generate source map (default path: <output>.js.map)
    -r, --runner [html]     generate HTML runner (default path: <output>.html)
    -d, --debug             output debug information
comments powered by Disqus