2017-10-12 115 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

謝謝!我沒有意識到移動器在變壓器中被專門調用。 –