Did you know ... Search Documentation:
Packs (add-ons) for SWI-Prolog

Package "transpiler"

Title:A universal translator for programming languages
Rating:Not rated. Create the first rating!
Latest version:0.1
SHA1 sum:feba96c898365107f24e690cbf0ad9b30e6ede94
Author:Anderson Green <jarble@gmail.com>
Maintainer:Anderson Green <jarble@gmail.com>
Packager:Anderson Green <jarble@gmail.com>
Home page:https://github.com/jarble/transpiler/
Download URL:https://github.com/jarble/transpiler/archive/v0.1.zip


No reviews. Create the first review!.

Details by download location


![Build Status](https://travis-ci.org/jarble/transpiler)


Universal-transpiler is a source-to-source compiler that translates a subset of several programming languages into several others. It is also able to translate several metasyntax notations, such as EBNF and ABNF.

The online version of this translator is written in JavaScript, but an experimental version is also being written in Prolog.

This is some JavaScript code:

function add(a,b){
        var g = [3,4,5];
        return a+b+(g[0])+(g.length);

function divide(a,b){
        return a/b;

and this is the Java code that it generates:

public static Object add(Object a,Object b){
        Object g=new ArrayList<>(Arrays.asList(3,4,5));
        return a+b+(g.get(0))+(g.length);
public static Object divide(Object a,Object b){
        return a/b;

How to use the online translator

This translator can convert many languages into many others:

  • JavaScript to Prolog%7B%5Cn%20%20%20%20return%20%5B%5C%22dog%5C%22%2C%5C%22horse%5C%22%2C%5C%22cat%5C%22%5D.indexOf(thing)%20!%3D%3D%20-1%3B%5Cn%7D%22%2C%22inputLang%22%3A%22javascript%22%2C%22outputLang%22%3A%22prolog%22%7D)
  • C to C#%7B%5Cn%20%20%20%20return%20a%20%2B%20b%3B%5Cn%7D%22%2C%22inputLang%22%3A%22c%22%2C%22outputLang%22%3A%22c%23%22%7D)
  • PHP to Python%7B%5Cn%20%20%20%20return%20%24a%2B%24b%3B%5Cn%7D%22%2C%22inputLang%22%3A%22php%22%2C%22outputLang%22%3A%22python%22%7D)
  • Lua to Perl%20%5Cn%20%20%20%20return%20a%2Bb%5Cnend%22%2C%22inputLang%22%3A%22lua%22%2C%22outputLang%22%3A%22perl%22%7D)
  • C to Haskell%7B%5Cn%20%20%20%20return%20a%20%2B%20b%3B%5Cn%7D%22%2C%22inputLang%22%3A%22c%22%2C%22outputLang%22%3A%22haskell%22%7D)

How to use the Prolog translator

The Prolog translator is still unfinished and experimental. You can install the package by typing pack_install(transpiler) in the SWI-Prolog console. Now, you can use the translator to convert JavaScript source code into Lua:

:- use_module(library(transpiler)).
:- set_prolog_flag(double_quotes,chars).
:- initialization(main).

main :-
        translate("function add(a,b){return a + b;}",javascript,lua,X),

How to extend the Prolog translator

A limited number of translation rules are provided here, but you can easily add your own rules to transpiler.pl. This is a simplified version of one of its translation rules, implementing the sine function:

%The type of this expression is double.
parentheses_expr(Data,double,sin(Var1_)) -->
                %The parameter of the sine function can be an integer or double.
                Var1 = expr(Data,double,Var1_)

To-do list

Here is an automatically-generated list of features that have not yet been implemented in this translator.

Other planned features:

  • Try to translate markup languages, similar to Pandoc.
  • Try to convert SVG into other vector graphics formats
  • Try to convert X3D into other vector graphics formats
  • Simultaneous editing of two programming languages in two text areas

Similar projects

There are several other source-to-source compilers and code generators that are similar to this one.

JTransc compiles Java, Kotlin, and Scala into several other programming languages. Pandoc is a universal document converter

This universal code generator is one example.

Contents of pack "transpiler"

Pack contains 137 files holding a total of 3.1M bytes.