Browse code
first
Ed L authored on 24/05/2011 23:55:22
Showing 2 changed files
Showing 2 changed files
2 | 2 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,116 @@ |
1 |
+class Variable(object): |
|
2 |
+ def __init__(self, name): |
|
3 |
+ self.name = name |
|
4 |
+ def __lshift__(self, value): |
|
5 |
+ return Assignment(self, value) |
|
6 |
+ def __str__(self): |
|
7 |
+ result = self.name |
|
8 |
+ return result |
|
9 |
+ def __repr__(self): |
|
10 |
+ return 'Variable: %s' % self |
|
11 |
+ def __getattr__(self, name): |
|
12 |
+ if name.startswith('__') or name.endswith('__'): raise AttributeError |
|
13 |
+ return Variable('.'.join([self.name, name])) |
|
14 |
+ def __call__(self, *args): |
|
15 |
+ return FuncCall(self, *args) |
|
16 |
+ |
|
17 |
+class FuncCall(object): |
|
18 |
+ def __init__(self, name, *args): |
|
19 |
+ self.name = str(name) |
|
20 |
+ self.args = ', '.join(str(x) for x in args) |
|
21 |
+ def __repr__(self): |
|
22 |
+ return 'FuncCall: %s(%s)' % (self.name, self.args) |
|
23 |
+ def __str__(self): |
|
24 |
+ return '%s(%s)' % (self.name, self.args) |
|
25 |
+ |
|
26 |
+ |
|
27 |
+class Expression(object): |
|
28 |
+ def __init__(self, expr): |
|
29 |
+ self.expr = expr |
|
30 |
+ def __repr__(self): |
|
31 |
+ return 'Expression: %s' % self.expr |
|
32 |
+ def __rshift__(self, args): |
|
33 |
+ if not hasattr(args, '__iter__'): args = (args,) |
|
34 |
+ return self.apply(*args) |
|
35 |
+ def apply(self, *args): |
|
36 |
+ return self.expr % args |
|
37 |
+ |
|
38 |
+class Assignment(object): |
|
39 |
+ template = 'var %s = %s' |
|
40 |
+ def __init__(self, name, value): |
|
41 |
+ self.name = name |
|
42 |
+ self.value = value |
|
43 |
+ def __repr__(self): |
|
44 |
+ return 'Assignment: %s, %s' % (self.name, self.value) |
|
45 |
+ def __str__(self): |
|
46 |
+ return self.template % (self.name, self.value) |
|
47 |
+ |
|
48 |
+class Return(object): |
|
49 |
+ template = 'return %s' |
|
50 |
+ def __init__(self, var): |
|
51 |
+ self.var = var |
|
52 |
+ def __str__(self): |
|
53 |
+ return self.template % self.var |
|
54 |
+ def __repr__(self): |
|
55 |
+ return 'Return: %s' % self |
|
56 |
+ |
|
57 |
+class Block(object): |
|
58 |
+ template = '{\n%s\n}' |
|
59 |
+ def __init__(self, *lines): |
|
60 |
+ self.lines = lines |
|
61 |
+ def __repr__(self): return 'Block: %d lines' % len(self.lines) |
|
62 |
+ def __str__(self): |
|
63 |
+ return self.template % ';\n'.join(str(x) for x in self.lines) |
|
64 |
+ |
|
65 |
+class Closure(object): |
|
66 |
+ template = '''function(%s)''' |
|
67 |
+ def __init__(self, args, *code): |
|
68 |
+ self.args = ', '.join(str(x) for x in args) |
|
69 |
+ self.code = Block(*code) |
|
70 |
+ def __str__(self): |
|
71 |
+ out = ['(', |
|
72 |
+ self.template % self.args, |
|
73 |
+ str(self.code), |
|
74 |
+ ')' |
|
75 |
+ ] |
|
76 |
+ return '\n'.join(out) |
|
77 |
+ def __repr__(self): |
|
78 |
+ return 'Closure: %s' % (self.template % self.args) |
|
79 |
+ def __call__(self, *args): |
|
80 |
+ return FuncCall(self, *args) |
|
81 |
+ |
|
82 |
+class Field(object): |
|
83 |
+ template = '''%s: %s''' |
|
84 |
+ def __init__(self, name, closure): |
|
85 |
+ self.name = name |
|
86 |
+ self.closure = closure |
|
87 |
+ def __repr__(self): |
|
88 |
+ return 'Method: %s' % self.name |
|
89 |
+ def __str__(self): |
|
90 |
+ return self.template % (self.name, self.closure) |
|
91 |
+ |
|
92 |
+class Object(object): |
|
93 |
+ template = '''{\n%s\n}''' |
|
94 |
+ def __init__(self, *methods): |
|
95 |
+ self.methods = methods |
|
96 |
+ def __repr__(self): |
|
97 |
+ return 'Object at %x: %d methods' % (id(self), len(self.methods)) |
|
98 |
+ def __str__(self): |
|
99 |
+ methods = ',\n'.join(str(x) for x in self.methods) |
|
100 |
+ return self.template % methods |
|
101 |
+ |
|
102 |
+widg = Variable('widg') |
|
103 |
+jq = Variable('$') |
|
104 |
+this = Variable('this') |
|
105 |
+arguments = Variable('arguments') |
|
106 |
+print Closure((jq,), |
|
107 |
+ widg << jq.widget('"ui.captionator"', Object( |
|
108 |
+ Field('options', Object()), |
|
109 |
+ Field('_create', Closure(())), |
|
110 |
+ Field('destroy', Closure(())), |
|
111 |
+ Field('_setOption', Closure(('option', 'value'), |
|
112 |
+ jq.Widget.prototype._setOption.apply( this, arguments ) |
|
113 |
+ )) |
|
114 |
+ )), |
|
115 |
+ Return(widg) |
|
116 |
+)('jQuery') |