1 | #!/usr/bin/env python2
|
2 | """target_lang_test.py: test out MyPy-compatible code we generated
|
3 |
|
4 | Similar to mycpp/demo/target_lang.cc
|
5 |
|
6 | List of types that could be containers:
|
7 |
|
8 | syntax.asdl:
|
9 |
|
10 | CompoundWord = (List[word_part] parts)
|
11 |
|
12 | word_part =
|
13 | ...
|
14 | | BracedTuple(List[CompoundWord] words)
|
15 |
|
16 | condition =
|
17 | Shell(List[command] commands) # if false; true; then echo hi; fi
|
18 |
|
19 | pat =
|
20 | Else
|
21 | | Words(List[word] words)
|
22 | | YshExprs(List[expr] exprs)
|
23 |
|
24 | command =
|
25 | | CommandList(List[command] children)
|
26 |
|
27 | re =
|
28 | | Seq(List[re] children)
|
29 | | Alt(List[re] children)
|
30 |
|
31 |
|
32 | runtime.asdl
|
33 |
|
34 | part_value =
|
35 | String %Piece
|
36 |
|
37 | # "$@" or "${a[@]}" # never globbed or split (though other shells
|
38 | # split them)
|
39 | | Array(List[str] strs)
|
40 | | ExtGlob(List[part_value] part_vals)
|
41 |
|
42 | wait_status =
|
43 | | Pipeline(List[int] codes)
|
44 |
|
45 | trace =
|
46 | External(List[str] argv) # sync, needs argv (command.Simple or 'command')
|
47 |
|
48 | value.asdl
|
49 |
|
50 | value =
|
51 | | Str(str s)
|
52 |
|
53 | # Maybe we can add global singletons value::Bool::true and
|
54 | # value::Bool::false or something? They are pointers.
|
55 | | Bool(bool b)
|
56 |
|
57 | # Not sure if we can do anything about these. Probably should for Int and
|
58 | # Float
|
59 | | Int(BigInt i)
|
60 | | Float(float f)
|
61 |
|
62 | | BashAssoc(Dict[str, str] d)
|
63 | | List(List[value] items)
|
64 | | Dict(Dict[str, value] d)
|
65 | | Frame(Dict[str, Cell] frame)
|
66 |
|
67 | Note that this must work:
|
68 |
|
69 | with tagswitch(node) as case:
|
70 | pass
|
71 |
|
72 | I think it just needs _type_tag, and then the C++ version will use the GC
|
73 | header that's BEFORE the object.
|
74 | """
|
75 |
|
76 | from typing import List, Dict
|
77 |
|
78 |
|
79 | class word_t(object):
|
80 | pass
|
81 |
|
82 |
|
83 | #class CompoundWord('List[int]'):
|
84 | #class CompoundWord(word_t, 'List[int]'):
|
85 |
|
86 |
|
87 | #class CompoundWord(List[int]):
|
88 | class CompoundWord(word_t, List[int]):
|
89 | pass
|
90 |
|
91 |
|
92 | class value_t(object):
|
93 | pass
|
94 |
|
95 |
|
96 | class Dict_(value_t, Dict[str, value_t]):
|
97 | pass
|
98 |
|
99 |
|
100 | class List_(value_t, List[value_t]):
|
101 | pass
|
102 |
|
103 |
|
104 | def main():
|
105 | # type: () -> None
|
106 |
|
107 | #print(dir(collections))
|
108 |
|
109 | # Wow this works
|
110 | c = CompoundWord()
|
111 |
|
112 | c.append(42)
|
113 | print(c)
|
114 | print('len %d' % len(c))
|
115 |
|
116 | d = Dict_()
|
117 |
|
118 | d['key'] = d
|
119 | print(d)
|
120 | print(len(d))
|
121 |
|
122 | mylist = List_()
|
123 | print(mylist)
|
124 | print(len(mylist))
|
125 |
|
126 |
|
127 | if __name__ == '__main__':
|
128 | main()
|