1.0.2 • Published 1 year ago

trastpiler v1.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
1 year ago

Trastpiler

Trastpiler is an agnostic transpiler for Abstract Syntax Trees.

Installation

npm install trastpiler --save

Basic Usage

import createTranspiler from "trastpiler";

// Generate an AST using parser of choice
const tree = esprima.parse(javascriptCode);

// Create a <AST-type, callback> hashmap for the supported declarations, statements and expressions.
const mappers = {
  WhileStatement: ({ body, test }, { transpile }) =>
    `while ${transpile(test)} do
      ${transpile(body)}
    end`,

  // ...
};

// Create a transpiler and call it,
// supplying an AST node to start from
const transpile = createTranspiler({ mappers });
const transpiledCode = transpile(tree.body);

Arguments

PropertyTypeDescription
configurationobject
    mappersHashMap<string, function>Key-value pairs of a declaration/expression/statement type and function to process the AST node.
    initialScopeobjectInitial data to use for the scope.

Declaration/Expression/Statement Interface

A declaration, expression or statement must have the following signature:

/**
 * @param {object} node AST node. Nodes must atleast have a `type` property
 * @param {{ transpile, scope }} references References to the current scope and transpiler function - optional.
 * @return {string} transpiled node
 */
function expressionTypeName (node, { transpile, scope }) {
  // Do stuff relevant for this expression (transpile child nodes, add variables to scope etc)
  return transpiledCode;
}

Examples

In the examples folder you may find some practical examples of how to build your own libraries on top of trastpiler.

Showcases

  • jspicl - Transpiles JavaScript into a subset of LUA.
1.0.2

1 year ago

1.0.1

2 years ago

1.0.0

2 years ago