2017-10-12 117 views
0

我试图将由James Kyle编写的Super Tiny Compiler从JavaScript翻译成Python。翻译JavaScript输入和退出Python的方法

但我无法理解什么进入和从JavaScript退出方法做:

// 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); 
} 

我怎样才能将这两种方法翻译)

// 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)进入Python? 谢谢。

Here's a link to the Tiny Compiler code

+1

为了理解他们做了什么,我建议阅读关于超级小编译器的更多内容,并看看如何定义“进入”和“退出”。至于翻译成Python,它们只是需要两个节点的方法。只要你可以用Python编写方法,翻译它们就很简单。 –

+0

我不认为进入和退出是在Super Tiny Compiler中定义的。我相信他们来自D3 JavaScript库。 –

+1

@MarcoLugo为什么D3会定义可应用于AST的访问者? D3是一个数据可视化库。 – sepp2k

回答

0

你会发现它的下一个文件“四tranformer.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,并呼吁他们如果这样做的代码。

+0

谢谢!我没有意识到移动器在变压器中被专门调用。 –