-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathkinds.py
executable file
·78 lines (55 loc) · 1.38 KB
/
kinds.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/python
"""
kinds.py
"""
import sys
import ast
import cStringIO
# Task:
# - Schema compiler?
# - Special case: Generate a named class with variable number of variables
NAMES = ['x', 'y', 'z', 'w']
def GenClassLexical(name, n):
"""After reader, before lexer."""
f = cStringIO.StringIO()
assert n <= len(NAMES), n
# Note syntax errors:
print >>f, 'class %s:' % name
print >>f, ' def __init__(self):'
for i in xrange(n):
print >>f, ' self.%s = 0' % NAMES[i]
return f.getvalue()
def GenClassParser(name, n):
"""After lexer, before parser."""
# Use tokenize module? Construct them manually? But Python doesn't really
# have access to its own tokenizer.
# 1. create tokens.
# 2. invoke parser
# 3. run compiler module
pass
def GenClassAst(name, n):
"""After parser, before compiler."""
print ast
# 1. create ast data structures?
# 2. run compiler module?
pass
def GenClassBytecode(name, n):
"""After compiler, before runtime."""
# BUILD_CLASS ? Use opcode module?
# compile()?
pass
def GenClassReflection(name, n):
#
cls = type(name, [], {})
cls.__init__ = constructor
def main(argv):
exec GenClassLexical('Point', 2)
exec GenClassLexical('Quad', 4)
print Point()
print Quad()
if __name__ == '__main__':
try:
main(sys.argv)
except RuntimeError as e:
print >>sys.stderr, 'FATAL: %s' % e
sys.exit(1)