Transpiler and compiler

风格不统一 提交于 2019-12-10 16:08:28

问题


I was wondering between the transpiler and compiler.

For example, I have a language('let's call it foo') and it will be transpiled to javascript.

foo -----transpiled-----> javascript

However, is foo limited under javascript?

Such as: "JavaScript cannot write to files on the server without the help of a server side script"

foo ----x----> write to files on the server without the help of a server side script

If so, is it possible to exit from the limit of javascript?

Such as making foo able to write to file itself.

foo ---------> write to files on the server

Note: What i am asking for is explanation and why and so on, NOT THE CODES!

Note again: Is it possible for it to exit the limits with additional libraries?

Edit: So, if i added another library from another language like python, will it help to exit the limits?


回答1:


You CAN'T exit the limits of the destination language. You could, however, create a compatibility layer that emulates the missing features and provide the functionality somehow.

In your example, if the foo internal function writefile() is the one that writes files, you could make a library that provides a writefile function that uses ajax to store the files to the server (or a cookie or localstore, etc). The original foo script wouldn't have to be changed for it to work.

And this is precisely one of the main parts about writing a transpiler: You not only have to translate the language, but you also have to emulate the missing features.

(you can also opt to disable the missing features)




回答2:


Transpiler is a type of compiler that takes the source code of a programming language as its input and outputs the source code into another programming language.

So in output you have file with source on destinationa language, for your sample its javascript, and this file will be executed, so if you create it manualy on javascript it was same result.

so answer on your question: no its not possible exit the limits destinations language, because in the end you can execute program on this language



来源:https://stackoverflow.com/questions/20771808/transpiler-and-compiler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!