-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpnil.py
64 lines (52 loc) · 1.45 KB
/
pnil.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#! /usr/bin/python
# -*- Mode: Python -*-
# -*- coding: UTF-8 -*-
# Copyright (C) 2009 by Artur Ventura
#
# File: pnil.py
# Time-stamp: Tue Sep 15 15:02:12 2009
#
# Author: Artur Ventura
#
import compiler
import sys
from pprint import pprint
from copy import copy
nodeDispatcher = {}
def register(node,handler):
nodeDispatcher[node] = handler
def dispatch(node):
if node.__class__.__name__ not in nodeDispatcher:
raise Exception("node '%s' has not a disptacher associated"% node.__class__.__name__ )
else:
return nodeDispatcher[node.__class__.__name__](node)
def respondsTo(string):
return lambda x: register(string,x); return x
@respondsTo("Return")
def returnDispatcher (node):
return "(return %s)" % dispatch(node.value)
@respondsTo("Mul")
def mulDispatcher(node):
return "(* %s %s)" % (dispatch(node.left), dispatch(node.right))
@respondsTo("Name")
def nameDisptacher(node):
return node.name
@respondsTo("Const")
def nameDisptacher(node):
return node.value
@respondsTo("Function")
def funcDisp(node):
result = ""
for stmt in node.code:
result = dispatch(stmt)
return "(defun %s (%s) %s)" % (node.name, reduce(lambda x,y: "%s %s" % (x,y), node.argnames), result)
def main():
s = sys.argv[1]
ast = compiler.parseFile(s)
if ast.doc:
o = copy(ast.doc)
print ";; " + o.replace("\n","\n;; ")
for i in ast.node:
print dispatch(i)
if __name__ == "__main__":
main()