2017-10-12 9 views
0

James Kyle의 The Super Tiny Compiler를 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); 
} 

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) 파이썬에? 감사합니다.

Here's a link to the Tiny Compiler code

+1

그들이하는 일을 이해하려면, The Super Tiny Compiler에 대해 더 읽고,'enter'와'exit'가 어떻게 정의되는지 살펴 보길 권합니다. 파이썬으로 번역 할 때, 그것들은 두 개의 노드를 취하는 단지 방법 일뿐입니다. Python으로 메소드를 작성할 수 있다면 번역하는 것이 간단합니다. –

+0

입력 및 종료가 Super Tiny Compiler에 정의되어 있다고 생각하지 않습니다. 나는 그들이 D3 JavaScript Library에서 왔다고 믿는다. –

+1

@MarcoLugo D3에서 AST에 적용 할 수있는 방문자를 정의하는 이유는 무엇입니까? D3은 데이터 시각화 라이브러리입니다. – sepp2k

답변

0

당신은 다음 파일에서 "4 tranformer.js"을 찾을 수 있습니다. enterexit은 객체 methods의 메소드 인 단지 visitor입니다. 이 코드 평화주의 :

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

을 우리가 단지 methods 객체가 방법을 exit 또는 enter이 있는지 확인하고 만약 그렇다면 그들에게 전화 게시 된 코드에서.

+0

감사합니다! 나는 traverser가 변압기에서 특별히 호출되었다는 것을 몰랐습니다. –