2.6.0 • Published 4 years ago

jspicl v2.6.0

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

jspicl

jspicl is a Javascript to PICO-8 Lua transpiler. It creates an AST out of the JavaScript code and then transpiles it down to the LUA subset of which PICO-8 supports.

npm version library size npm downloads npm.io

CLI

Consider using jspicl-cli instead to simplify your project setup. It includes several additional features such as watch mode (reloading your game when code changes), using PNGs as your game's spritesheet etc.

Installation

npm install jspicl --save

Usage

import jspicl from "jspicl";

const javascriptCode = `...`;
const { output, polyfills } = jspicl(javascriptCode);
console.log(
  Object.values(polyfills).join("\n"),
  output
);

By default, jspicl formats the LUA output for you but if performance ever becomes an issue you can turn this off through the options argument.

const { output, polyfills } = jspicl(javascriptCode, { prettify: false });

Options

Return value

PropertyTypeDescription
outputstringThe transpiled javascript code
polyfillsobjectTable of required polyfills with their corresponding lua code.

Customized transpilation

jspicl does not support all expressions or statements out of the box but it is extensible enough to allow for these to be added. It also allows existing ones to be replaced if the implementation is considered unsatisfactory. This is done by supplying a customMappers option. The only requirement imposed on the AST node is that it contains a string property called type since this is used to identify the appropriate declaration, expression or statement.

const customMappers = {
  // Replace the default while-statement implementation
  WhileStatement: ({ body, test }, { transpile }) =>
    `while ${transpile(test)} do
      -- We have full control of the output!
      ${transpile(body)}
    end`,

  // Add support for missing features
  ForOfStatement: ({ left, right, body }) => {
    // https://esprima.readthedocs.io/en/latest/syntax-tree-format.html#for-of-statement
  },

  // Custom statements, declarations and expressions are also valid
  // as long as they are included in the AST
  SuperDuperComment: () =>
    `-- You're doing great!`

  // ...
};

const { output } = jspicl(javascriptCode, { customMappers });

Known limitations

ES2015+Not all ES2015+ features are supported. Run your code through a transpiler first such as bublé or babel.
prototype chainsNot supported
Array methodsNot all prototype methods have been polyfilled yet.
Math.maxOnly supports two arguments.
ASTNot all declarations, expressions and statements have been implemented. More will be added as needed.

Versioning

This project uses semantic versioning

Related projects

jspicl CLI - Command line interface for jspicl

games - Games created with jspicl

2.6.0

4 years ago

2.5.0

5 years ago

2.4.4

5 years ago

2.4.3

5 years ago

2.4.2

5 years ago

2.4.1

5 years ago

2.4.0

5 years ago

2.3.3

5 years ago

2.3.2

5 years ago

2.3.1

6 years ago

2.3.0

6 years ago

2.2.4

6 years ago

2.2.3

6 years ago

2.2.2

6 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.7.0

6 years ago

1.6.0

6 years ago

1.5.0

6 years ago

1.4.3

6 years ago

1.4.2

6 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.2.7

7 years ago

0.2.6

7 years ago

0.2.5

7 years ago

0.2.4

7 years ago

0.2.3

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago