【问题标题】:Translate JavaScript enter and exit methods into Python将 JavaScript 进入和退出方法翻译成 Python
【发布时间】:2017-10-12 16:13:08
【问题描述】:

我正在尝试将 James Kyle 的 The Super Tiny Compiler 从 JavaScript 翻译成 Python。

但我无法理解 JavaScript 的进入和退出方法的作用:

1)

// If there is an `enter` method for this node type we'll call it with the
// `node` and its `parent`.
if (methods && methods.enter) {
  methods.enter(node, parent);
}

2)

// If there is an `exit` method for this node type we'll call it with the
// `node` and its `parent`.
if (methods && methods.exit) {
  methods.exit(node, parent);
}

如何将这两种方法翻译成 Python? 谢谢。

Here's a link to the Tiny Compiler code

【问题讨论】:

  • 要了解它们的作用,我建议阅读有关 The Super Tiny Compiler 的更多信息,并查看如何定义 enterexit。至于翻译成 Python,它们只是需要两个节点的方法。只要你能用 Python 编写方法,翻译它们就很简单了。
  • 我不认为进入和退出是在 Super Tiny 编译器中定义的。我相信它们来自 D3 JavaScript 库。
  • @MarcoLugo 为什么 D3 会定义可以应用于 AST 的访问者? D3 是一个数据可视化库。

标签: javascript python compiler-construction translate


【解决方案1】:

您将在下一个文件“4-transformer.js”中找到它。 enterexit 只是 visitor 中对象 methods 的方法。注意这段代码的和平:

// We start by testing for the existence of a method on the visitor with a
// matching `type`.
let methods = visitor[node.type];

在您发布的代码中,我们只是检查methods 对象是否具有exitenter 方法,如果有则调用它们。

【讨论】:

  • 谢谢!我没有意识到在transformer中专门调用了遍历器。
猜你喜欢
  • 2018-09-09
  • 1970-01-01
  • 2012-08-22
  • 2023-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-07
  • 2017-10-12
相关资源
最近更新 更多