-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsl.py
50 lines (38 loc) · 1.49 KB
/
dsl.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
from highord import *
# FIXME: lots of bugs
# ...for instance: this[0].equals(this[1]), where this[1] means this[0][1] in outer scope
# ...that isn't what we except
class DSLBuilder:
def __init__(self, *, other=None, func=None, terminal=True):
if other is None:
self.__method_chain = []
else:
self.__method_chain = other.__method_chain + [func]
self.__terminal = terminal
def __call__(self, obj):
chain_len = len(self.__method_chain)
if chain_len:
func = self.__method_chain[chain_len - 1]
if not self.__terminal:
func = func(lambda t: t)
for i in range(chain_len - 1):
func = self.__method_chain[chain_len - i - 2](func)
return func(obj)
else:
return obj
def __getattr__(self, item):
return DSLBuilder(other=self, func=get_attr(item), terminal=False)
def __getitem__(self, item):
return DSLBuilder(other=self, func=get_item(item), terminal=False)
def __eq__(self, other):
return self.__make_wrapper(equals(other), called=True)
def __make_wrapper(self, func, *, called=False):
if called:
return DSLBuilder(other=self, func=func)
else:
def wrapper(*args):
return DSLBuilder(other=self, func=func(*args))
return wrapper
pass
# and some others method for dsl-builder
this = DSLBuilder()