Did you know ... | Search Documentation: |
Pack transpiler -- README.md |
This transpiler is no longer maintained; you should probably use Extism instead.
![Build Status](https://travis-ci.org/jarble/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;
indexOf(thing)
%20!%3D%3D%20-1%3B%5Cn%7D%22%2C%22inputLang%22%3A%22javascript%22%2C%22outputLang%22%3A%22prolog%22%7D)
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).
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.
Universal-transpiler can also translate various grammar notations, such as jison, marpa, peg.js, and nearley.
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).
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,")"), ]).
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.