forked from oils-for-unix/oils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta.py
187 lines (132 loc) · 4.09 KB
/
meta.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/python
"""
meta.py
Another "thin waist" of the interpreter. It can be happen at compile time!
We are following the code <-> data pattern, and this is the "data" module.
id_kind and asdl are the "code" modules.
Usage:
from osh.meta import Id, Kind, ast, ID_SPEC
"""
import posix
from core import id_kind
_BOOTSTRAP_LEVEL = int(posix.environ.get('BOOTSTRAP_LEVEL', '3'))
# TODO: Get rid of this hack.
def _AssignTypes(src_module, dest_module):
"""For generated code."""
for name in dir(src_module):
if not name.startswith('__'):
v = getattr(src_module, name)
setattr(dest_module, name, v)
class Id(object):
"""Token and op type.
The evaluator must consider all Ids.
NOTE: We add a bunch of class attributes that are INSTANCES of this class,
e.g. Id.Lit_Chars.
"""
def __init__(self, enum_value):
self.enum_value = enum_value
def __repr__(self):
return IdName(self)
class Kind(object):
"""A coarser version of Id, used to make parsing decisions."""
# TODO: The Kind type should be folded into ASDL. It can't print itself,
# which is inconsistent with Id.
pass
class _AsdlModule(object):
"""Dummy object to copy attributes onto."""
pass
_ID_TO_KIND = {} # int -> Kind
def LookupKind(id_):
return _ID_TO_KIND[id_.enum_value]
_ID_NAMES = {} # int -> string
def IdName(id_):
return _ID_NAMES[id_.enum_value]
# Keep one instance of each Id, to save memory and enable comparison by
# OBJECT IDENTITY.
# Do NOT create any any more instances of them! Always used IdInstance().
# TODO: Fold Id into ASDL, which will enforce uniqueness?
_ID_INSTANCES = {} # int -> Id
def IdInstance(i):
return _ID_INSTANCES[i]
#
# Instantiate osh/types.asdl
#
types = _AsdlModule()
from _devbuild.gen import types_asdl
_AssignTypes(types_asdl, types)
# Id -> bool_arg_type_e
BOOL_ARG_TYPES = {} # type: dict
# Used by test_builtin.py
TEST_UNARY_LOOKUP = {}
TEST_BINARY_LOOKUP = {}
TEST_OTHER_LOOKUP = {}
#
# Add attributes to Id and Kind
#
ID_SPEC = id_kind.IdSpec(Id, Kind,
_ID_NAMES, _ID_INSTANCES, _ID_TO_KIND,
BOOL_ARG_TYPES)
id_kind.AddKinds(ID_SPEC)
id_kind.AddBoolKinds(ID_SPEC, Id, types.bool_arg_type_e) # must come second
# NOTE: Dependency on the types module here. This is the root cause of the
# _BOOTSTRAP_LEVEL hack.
id_kind.SetupTestBuiltin(Id, Kind, ID_SPEC,
TEST_UNARY_LOOKUP, TEST_BINARY_LOOKUP,
TEST_OTHER_LOOKUP,
types.bool_arg_type_e)
# Debug
_kind_sizes = ID_SPEC.kind_sizes
#
# Instantiate osh/osh.asdl
#
if _BOOTSTRAP_LEVEL > 1:
ast = _AsdlModule()
from _devbuild.gen import osh_asdl
_AssignTypes(osh_asdl, ast)
#
# Instantiate core/runtime.asdl
#
if _BOOTSTRAP_LEVEL > 2:
runtime = _AsdlModule()
from _devbuild.gen import runtime_asdl
_AssignTypes(runtime_asdl, runtime)
#
# Redirect Tables associated with IDs
#
# These might be osh specific.
#
REDIR_DEFAULT_FD = {
# filename
Id.Redir_Less: 0, # cat <input.txt means cat 0<input.txt
Id.Redir_Great: 1,
Id.Redir_DGreat: 1,
Id.Redir_Clobber: 1,
Id.Redir_LessGreat: 1, # TODO: What does echo <>foo do?
# bash &> and &>>
Id.Redir_AndGreat: 1,
Id.Redir_AndDGreat: 1,
# descriptor
Id.Redir_GreatAnd: 1, # echo >&2 means echo 1>&2
Id.Redir_LessAnd: 0, # echo <&3 means echo 0<&3, I think
Id.Redir_TLess: 0, # here word
# here docs included
Id.Redir_DLess: 0,
Id.Redir_DLessDash: 0,
}
redir_arg_type_e = types.redir_arg_type_e
REDIR_ARG_TYPES = {
# filename
Id.Redir_Less: redir_arg_type_e.Path,
Id.Redir_Great: redir_arg_type_e.Path,
Id.Redir_DGreat: redir_arg_type_e.Path,
Id.Redir_Clobber: redir_arg_type_e.Path,
Id.Redir_LessGreat: redir_arg_type_e.Path,
# bash &> and &>>
Id.Redir_AndGreat: redir_arg_type_e.Path,
Id.Redir_AndDGreat: redir_arg_type_e.Path,
# descriptor
Id.Redir_GreatAnd: redir_arg_type_e.Desc,
Id.Redir_LessAnd: redir_arg_type_e.Desc,
Id.Redir_TLess: redir_arg_type_e.Here, # here word
# note: here docs aren't included
}