从 JavaScript 到 Python 代码的转换?

发布于 2024-08-23 03:45:43 字数 1539 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萧瑟寒风 2024-08-30 03:45:43

您可以使用 Js2Py 将 JavaScript 翻译为 Python。它支持整个 JavaScript,您可以使用它来翻译大型 JavaScript 模块,例如 esprima.js(JavaScript 6 解析器)。


Short demo:

>>> import js2py
>>> f = js2py.eval_js( "function $(a) {return a + arguments[1]}" )
>>> f
function $(a) { [python code] }
>>> f(1, 2, 3)
3

这就是翻译后的函数内部的样子(相当难看):

>>> print js2py.translate_js( "function $(a) {return a + arguments[1]}" )
from js2py.pyjs import *
var = Scope( JS_BUILTINS )
set_global_object(var)
# Code follows:
var.registers([u'
])
@Js
def PyJsHoistedNonPyName(a, this, arguments, var=var):
    var = Scope({u'a':a, u'this':this, u'arguments':arguments}, var)
    var.registers([u'a'])
    return (var.get(u'a')+var.get(u'arguments').get(u'1'))
PyJsHoistedNonPyName.func_name = u'

var.put(u'
, PyJsHoistedNonPyName)

You can translate JavaScript to Python using Js2Py. It supports whole JavaScript and you can use it to translate large JavaScript modules like esprima.js (a JavaScript 6 parser).


Short demo:

>>> import js2py
>>> f = js2py.eval_js( "function $(a) {return a + arguments[1]}" )
>>> f
function $(a) { [python code] }
>>> f(1, 2, 3)
3

This is how the translated function looks like internally (it's rather ugly):

>>> print js2py.translate_js( "function $(a) {return a + arguments[1]}" )
from js2py.pyjs import *
var = Scope( JS_BUILTINS )
set_global_object(var)
# Code follows:
var.registers([u'
])
@Js
def PyJsHoistedNonPyName(a, this, arguments, var=var):
    var = Scope({u'a':a, u'this':this, u'arguments':arguments}, var)
    var.registers([u'a'])
    return (var.get(u'a')+var.get(u'arguments').get(u'1'))
PyJsHoistedNonPyName.func_name = u'

var.put(u'
, PyJsHoistedNonPyName)
稚气少女 2024-08-30 03:45:43

几(4)年后现在更新

(几乎肯定)可以做到这一点;尽管肯定不是使用正则表达式。
我建议未来的读者查看@Piotr Dabkowski 的答案
或者其他一些答案。 (我不知道没有尝试过)


原始答案

嗯,这是一个很难的问题。
编译器的定义是从高级语言翻译成低级语言。
例如 python 到机器代码。
或者java到javascript(谷歌在某个地方有一个相当著名的编译器 - 它使谷歌文档更容易制作)
Python 到 javascript 的编译器比比皆是。
从技术上讲,javascript 到 python 是一个反编译器。 (afaik)

我发现了一些关于 javascript-python 转换器的猜测这里: 按照步骤进行操作。它主要讲述的是如何做到这一点不会太难。
我找不到任何东西,但这并不意味着它不存在。

正则表达式不适合,正则表达式仅适合常规语言。
编程语言通常不是常规语言。请参阅

Updated

Now several (4) years later this (almost certainly) can be done; though certainly not with RegEx.
I suggest future readers look to @Piotr Dabkowski's answer..
Or some of the other answers. (I don't know having not tried them)


Original Answer

Hm this is a hard one.
The definition of a compiler is translates from a higher level language to a lower level language.
eg python to machine-code.
or java to javascript (google has a rather famous compiler for this somewhere - its' what makes google doc easier to make)
Python to javascript compilers abound.
technically javascript to python would be a decompiler. (afaik)

I found some speculation about a javascript-python converter here: follow the tread through. it mostly speaks of how it wouldn't be too hard to do.
I can't find anything , but that doesn't mean it's no out there.

Regex is not suitable, regex is suitable only for regular languages.
programming languages are not normally regular languages. see this

贱贱哒 2024-08-30 03:45:43

这个答案可能晚了大约 2 年,但是 js 怎么样 -> CoffeeScript -> Python?如果你使用 http://js2.coffee/ 之类的东西来转换为 cs,那么缩进和列表之类的东西就是已经为你做好了。

This answer might be about 2 years late but how about js -> CoffeeScript -> python? If you use something like http://js2.coffee/ to convert to cs, then things like indentation and lists are already done for you.

心房敞 2024-08-30 03:45:43

如果您所要求的是将 javascript 中的一些正则表达式转换为 Python 等效项,那么正则表达式的基础知识大多是相当标准的。查看 Python re 模块文档。查看您所使用的内容是否有记录。您还感兴趣的是页面。

如果您正在谈论将 javascript 代码转换为等效的 Python 代码,那么最好的转换器就是。了解Python,然后手动转换它们。没有什么比人类更好的了。循环、变量、数组等编程结构非常常见和标准,您将立即知道如何使用它们。

If what you are asking is converting a few regexs in javascript to Python equivalent, the basics of regular expressions are mostly pretty standard. check out the Python re module doc. See if what you are using is documented. Also of interest to you is this page.

If you are talking about converting javascript code to Python equivalent, the best converter is you. Learn about Python, and then convert them manually. there is nothing else better than the human being. Programming constructs like loops, variables, arrays are pretty common and standard you will recognize instantly how to use them straight away.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文