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

Reviews

No reviews. Create the first review!.

Details by download location

VersionSHA1#DownloadsURL
0.111c9e6806401c840759acf9918c402ed8b99296e7https://github.com/jarble/transpiler.git
177363303931bbd3353a95f06e29827d0a265d761https://github.com/jarble/transpiler.git
1df6be304f5351723f199b7ed55c8b81f416b9e65https://github.com/jarble/transpiler.git
2a1c90e2b7a0ba69ca577b9af9230eb5eec1003712https://github.com/jarble/transpiler.git
2b036e9d6964c90d1e2281db8db4e742727353932https://github.com/jarble/transpiler/archive/v0.1.zip
2fd47e8056c688821f0d56761ec058a298f6be861https://github.com/jarble/transpiler.git
30421ac3a32d55f235e89723517af0dbc5e7ff452https://github.com/jarble/transpiler.git
336467a68f7611b3a68ac5b7ef36a0dac3740bf41https://github.com/jarble/transpiler.git
3f13033ca4399d797f637b09942ccc70bb9eca743https://github.com/jarble/transpiler.git
426d9f6eb52697a6e9387337eb085992bb77a3e41https://github.com/jarble/transpiler.git
5b9cbdf976108d5b3e5260ad992f6b88c850151d1https://github.com/jarble/transpiler.git
5f0bed7ac03c9a594d10510543d608b5f345d7d93https://github.com/jarble/transpiler.git
807b9b25453c3e33b1668c05554d222685e3607a1https://github.com/jarble/transpiler.git
873fb8e46d8a3d2454d49aa54677fdd9903886fe4https://github.com/jarble/transpiler.git
989a84b41a23db0ba3680893ce2aeac45f6b3b3143https://github.com/jarble/transpiler/archive/v0.1.zip
9e2a5d6eac13e8a77fecbb8b03d5356df0fc94771https://github.com/jarble/transpiler.git
a5011afa3876b304bbca685975defd78a325eb551https://github.com/jarble/transpiler.git
a9b7e7c42876b98f55e789a5523993eb295c4a281https://github.com/jarble/transpiler.git
b7e263292ac19042d0f07df84a44f2f02b100ad78https://github.com/jarble/transpiler.git
ba7c537d1c36552e4cf2896c832c12560a4cdcb21https://github.com/jarble/transpiler.git
bd753575241c231faaaa04b0b5b92dc0811289bf11https://github.com/jarble/transpiler.git
e29323955d6264605ceeeea2deb68c63d6ed48c72https://github.com/jarble/transpiler.git
feba96c898365107f24e690cbf0ad9b30e6ede943https://github.com/jarble/transpiler.git

This transpiler is no longer maintained; you should probably use Extism instead.

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

Universal-transpiler

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

Universal-transpiler was written as an experimental "proof-of-concept," so it can only translate relatively simple programs. The translation is not always 100% accurate, but I hope it will still be useful.

The online version of this translator%7B%5Cn%5Ctreturn%20Math.round(a%2B0.5)%3B%5Cn%5Ct%7D%5Cn%5Ctpublic%20static%20int%20floor(double%20a)%7B%5Cn%5Ctreturn%20Math.floor(a)%3B%5Cn%5Ct%7D%5Cn%5Ctpublic%20static%20int%20ceil(double%20a)%7B%5Cn%5Ctreturn%20Math.ceil(0.5)%3B%5Cn%5Ct%7D%5Cn%5Cn%5Ctpublic%20class%20math%7B%5Cn%5Ctpublic%20static%20double%20sin(double%20a)%7B%5Cn%5Ctreturn%20Math.sin(a)%3B%5Cn%5Ct%7D%5Cn%5Ctpublic%20static%20double%20pow(double%20a%2Cdouble%20b)%7B%5Cn%5Ctreturn%20Math.pow(a%2Cb)%3B%5Cn%5Ct%7D%5Cn%5Ctpublic%20static%20double%20cos(double%20a)%7B%5Cn%5Ctreturn%20Math.cos(a)%3B%5Cn%5Ct%7D%5Cn%5Ctpublic%20static%20double%20tan(double%20a)%7B%5Cn%5Ctreturn%20Math.tan(a)%3B%5Cn%5Ct%7D%5Cn%5Ctpublic%20static%20double%20asin(double%20a)%7B%5Cn%5Ctreturn%20Math.asin(a)%3B%5Cn%5Ct%7D%5Cn%5Ctpublic%20static%20double%20acos(double%20a)%7B%5Cn%5Ctreturn%20Math.acos(a)%3B%5Cn%5Ct%7D%5Cn%5Ctpublic%20static%20double%20atan(double%20a)%7B%5Cn%5Ctreturn%20Math.atan(a)%3B%5Cn%5Ct%7D%5Cn%5Ct%7D%5Cn%7D%22%2C%22inputLang%22%3A%22java%22%2C%22outputLang%22%3A%22c%23%22%7D) is written in JavaScript, but an experimental version is also being written in Prolog.

A major goal of this project is to translate TypeScript and JavaScript to other languages that compile to C or native code. For example, it's possible to translate a subset of TypeScript to Zig%3Anumber%7B%5Cnreturn%20a%20%2B%20b%3B%5Cn%7D%3B%22%2C%22inputLang%22%3A%22typescript%22%2C%22outputLang%22%3A%22zig%22%7D):

var a = {a1:1,b:2};

var b = function(a1:number,b:number):number{
return a + b;
};

This is the compiler's output:

var a=.{.a1=1,.b=2};
var b=struct{fn function(a1:f64,b:f64)f64{
    return a+b;}}.function;

How to use the online translator

Some supported features

  • Anonymous functions%7Breturn%20a%20%2B%20b%3B%7D%3B%22%2C%22inputLang%22%3A%22javascript%22%2C%22outputLang%22%3A%22lua%22%7D)
  • Foreach loops%5Cn%7B%5Cn%20%20%20%20%20%20%20%20int%20max_val%20%3D%20numbers%5B0%5D%3B%5Cn%20%20%20%20%20%20%20%20for%20(int%20num%20%3A%20numbers)%5Cn%20%20%20%20%20%20%20%20%7B%5Cn%20%20%20%20%20%20%20%20%20%20%20%20if%20(num%20%3E%20max_val)%5Cn%20%20%20%20%20%20%20%20%20%20%20%20%7B%5Cn%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20max_val%20%3D%20num%3B%5Cn%20%20%20%20%20%20%20%20%20%20%20%20%7D%5Cn%20%20%20%20%20%20%20%20%7D%5Cn%20%20%20%20return%20max_val%3B%5Cn%7D%22%2C%22inputLang%22%3A%22java%22%2C%22outputLang%22%3A%22typescript%22%7D)
  • Structs
  • Input and output parameters%7B%5Cnb%20%2B%3D%201%3B%5Cna%20%3D%20c%20%2B%20b%3B%5Cn%7D%20%22%2C%22inputLang%22%3A%22glsl%22%2C%22outputLang%22%3A%22c%23%22%7D)
  • Mathematical functions%7B%5Cnint%20c%20%3D%204%252%3B%5Cndouble%20b%20%3D%204.0%3B%5Cnfloat%20a%20%3D%20pow(1.0%2C2.0)%2Bfloor(1.0)%2Bexp(1.0)%2Bround(1.5)%2Bceil(1.0)%2Bcos(1.0)%2Bsin(1.0)%2Btan(1.0)%2Basin(1.0)%2Bacos(1.0)%2Batan(1.0)%2Bsqrt(2.0)%2Babs(-4.0)%3B%5Cnreturn%20log10(a)%3B%5Cn%7D%22%2C%22inputLang%22%3A%22c%22%2C%22outputLang%22%3A%22vala%22%7D)
  • Interfaces%3B%5Cn%20%20public%20String%20Age%20()%3B%5Cn%7D%22%2C%22inputLang%22%3A%22java%22%2C%22outputLang%22%3A%22c%23%22%7D)
  • Boolean operators%7B%5Cnif(a%20!%3D%20true%20%7C%7C%20b%20%3D%3D%20false)%7B%5Cnreturn%20!a%3B%5Cn%7D%5Cnelse%7B%5Cnreturn%20a%20%7C%7C%20b%20%26%26%20c%3B%5Cn%7D%5Cn%7D%22%2C%22inputLang%22%3A%22c%22%2C%22outputLang%22%3A%22MiniZinc%22%7D)
  • Generics and templates%5Cn%7B%5Cn%20%20%20%20return%20(x%20%3E%20y)%20%3F%20x%20%3A%20y%3B%5Cn%7D%22%2C%22inputLang%22%3A%22c%2B%2B%22%2C%22outputLang%22%3A%22typescript%22%7D) (in several languages)
  • Arrays
  • [Foreach loops](file:///C:/Users/jarbl/Dropbox/All%20source%20code%20goes%20here%20-%20don't%20put%20this%20folder%20inside%20any%20other%20folder/Prolog%20projects/universal-transpiler/javascript/js_transpiler/test_parser.html#%7B%22inputText%22%3A%22public%20static%20int%20max_number(int%5B%5D%20num)%5Cn%7B%5Cn%20%20%20%20%20%20%20%20int%20max_val%20%3D%20numbers%5B0%5D%3B%5Cn%20%20%20%20%20%20%20%20for%20(int%20num%20%3A%20numbers)%5Cn%20%20%20%20%20%20%20%20%7B%5Cn%20%20%20%20%20%20%20%20%20%20%20%20if%20(num%20%3E%20max_val)%5Cn%20%20%20%20%20%20%20%20%20%20%20%20%7B%5Cn%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20maxSoFar%20%3D%20num%3B%5Cn%20%20%20%20%20%20%20%20%20%20%20%20%7D%5Cn%20%20%20%20%20%20%20%20%7D%5Cn%20%20%20%20return%20max_val%3B%5Cn%7D%22%2C%22inputLang%22%3A%22java%22%2C%22outputLang%22%3A%22javascript%22%7D) 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) or PHP%20%5Cn%20%20%20%20return%20a..b%5Cnend%22%2C%22inputLang%22%3A%22lua%22%2C%22outputLang%22%3A%22php%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)
  • C# to Fortran%7B%5Cn%20%20%20%20return%20a%2Bb%3B%5Cn%7D%22%2C%22inputLang%22%3A%22c%23%22%2C%22outputLang%22%3A%22fortran%22%7D)
  • Java to OCaml%7B%5Cn%20%20%20%20%20%20%20%20return%20a%20%2B%20b%3B%5Cn%20%20%20%20%7D%5Cn%7D%22%2C%22inputLang%22%3A%22java%22%2C%22outputLang%22%3A%22ocaml%22%7D) or [GLSL][1]

Constraint programming and automated reasoning

Universal-transpiler is able to generate code in several constraint programming languages and computer algebra systems, including MiniZinc%7B%5Cnreturn%20a%20%2B%20b%3B%5Cn%7D%22%2C%22inputLang%22%3A%22c%22%2C%22outputLang%22%3A%22minizinc%22%7D), Maxima%7B%5Cnreturn%20a%20%2B%20b%3B%5Cn%7D%22%2C%22inputLang%22%3A%22c%22%2C%22outputLang%22%3A%22maxima%22%7D), Sage%7B%5Cnreturn%20a%20%2B%20b%3B%5Cn%7D%22%2C%22inputLang%22%3A%22c%22%2C%22outputLang%22%3A%22sage%22%7D), Algebrite%2BMath.sqrt(2)%3B%22%2C%22inputLang%22%3A%22javascript%22%2C%22outputLang%22%3A%22algebrite%22%7D), and Axiom%2BMath.sqrt(2)%3B%22%2C%22inputLang%22%3A%22javascript%22%2C%22outputLang%22%3A%22axiom%22%7D). Some languages can also be translated into the SMT-LIB%7B%5Cnreturn%20a%20%2B%20b%3B%5Cn%7D%22%2C%22inputLang%22%3A%22c%22%2C%22outputLang%22%3A%22smt-lib%22%7D), TPTP%7B%5Cnreturn%20a%3Eb%3B%5Cn%7D%22%2C%22inputLang%22%3A%22javascript%22%2C%22outputLang%22%3A%22tptp%22%7D), Coq%7B%5Cnreturn%20a%20%2B%20b%3B%5Cn%7D%22%2C%22inputLang%22%3A%22c%22%2C%22outputLang%22%3A%22coq%22%7D), Isabelle/HOL)%22%2C%22inputLang%22%3A%22haskell%22%2C%22outputLang%22%3A%22isabelle%2Fhol%22%7D), and alt-ergo%7B%5Cn%20%20%20%20return%20a%3Eb%3B%5Cn%7D%22%2C%22inputLang%22%3A%22c%22%2C%22outputLang%22%3A%22alt-ergo%22%7D) languages for automated theorem proving. As an experimental feature, it also converts a subset of Prolog into the PDDL%20%3A-%20%5Cn%20%20%20%20is_awake(A)%3Bis_asleep(B).%22%2C%22inputLang%22%3A%22prolog%22%2C%22outputLang%22%3A%22pddl%22%7D) automated planning language.

Similarly, it can translate constraint handing rules from Prolog into CLIPS%20%3D%3D%3E%20%5Cn%20%20%20%20is_awake(A)%3Bis_asleep(B).%22%2C%22inputLang%22%3A%22prolog%22%2C%22outputLang%22%3A%22clips%22%7D) and vice-versa%20%3D%3E%20(assert%20(or%20(is_awake%20%3FA)%20(is_asleep%20%3FB))))%22%2C%22inputLang%22%3A%22clips%22%2C%22outputLang%22%3A%22prolog%22%7D).

Ontology languages

Universal-transpiler can also translate programming languages into the KIF%7B%5Cnreturn%20a%20%2B%20b%3B%5Cn%7D%22%2C%22inputLang%22%3A%22c%22%2C%22outputLang%22%3A%22kif%22%7D) ontology language.

Generating parsers with universal-transpiler

Universal-transpiler can also translate various grammar notations, such as jison, marpa, peg.js, and nearley.

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),
        atom_chars(Y,X),
        writeln(Y).

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_)
        },
langs_to_output(Data,sin,[
['java','javascript']:
        ("Math",ws,".",ws,"sin",ws,"(",ws,Var1,ws,")"),
['lua','python']:
        ("math",python_ws,".",python_ws,"sin",python_ws,"(",python_ws,Var1,python_ws,")"),
]).

Other planned features:

  • Add a translator for lens languages such as Augeas and Boomerang
  • Simplify and refactor the code generator using string interpolation
  • Converting SQL to Linq and vice-versa
  • Simultaneous editing of two programming languages in two text areas
  • Translate list comprehensions from other languages into Prolog.
  • 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

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.

[1]: https://jarble.github.io/transpiler/javascript/js_transpiler/test_parser.html#%7B%22inputText%22%3A%22public%20class%20Rectangle%7B%5Cn%20%20%20%20public%20double%20width%3B%5Cn%20%20%20%20public%20double%20height%3B%5Cn%20%20%20%20public%20Rectangle(double%20width%2C%20double%20height)%7B%5Cn%20%20%20%20%20%20%20%20this.width%20%3D%20width%3B%5Cn%20%20%20%20%20%20%20%20this.height%20%3D%20height%3B%5Cn%20%20%20%20%7D%5Cn%20%20%20%20public%20double%20area()%7B%5Cn%20%20%20%20%20%20%20%20return%20this.width*this.height%3B%5Cn%20%20%20%20%7D%5Cn%20%20%20%20public%20double%20perimeter()%7B%5Cn%20%20%20%20%20%20%20%20return%20(this.width%2Bthis.height)*2.0%3B%5Cn%20%20%20%20%7D%5Cn%7D%22%2C%22inputLang%22%3A%22java%22%2C%22outputLang%22%3A%22glsl%22%7D

Contents of pack "transpiler"

Pack contains 287 files holding a total of 21.4M bytes.