OILS / asdl / target_lang_test.py View on Github | oilshell.org

115 lines, 25 significant
1#!/usr/bin/env python2
2"""target_lang_test.py: test out MyPy-compatible code we generated
3
4Similar to mycpp/demo/target_lang.cc
5
6List of types that could be containers:
7
8syntax.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
32runtime.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
48value.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
68from typing import List, Dict
69
70
71class word_t(object):
72 pass
73
74#class CompoundWord('List[int]'):
75#class CompoundWord(word_t, 'List[int]'):
76
77#class CompoundWord(List[int]):
78class CompoundWord(word_t, List[int]):
79 pass
80
81class value_t(object):
82 pass
83
84class Dict_(value_t, Dict[str, value_t]):
85 pass
86
87class List_(value_t, List[value_t]):
88 pass
89
90
91def main():
92 # type: () -> None
93
94 #print(dir(collections))
95
96 # Wow this works
97 c = CompoundWord()
98
99 c.append(42)
100 print(c)
101 print('len %d' % len(c))
102
103 d = Dict_()
104
105 d['key'] = d
106 print(d)
107 print(len(d))
108
109 mylist = List_()
110 print(mylist)
111 print(len(mylist))
112
113
114if __name__ == '__main__':
115 main()