This repository has been archived by the owner on Aug 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
build_database_with_returns.py
239 lines (205 loc) · 9.23 KB
/
build_database_with_returns.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
"""
This script is used to write `sqf/dababase.py`, that contains all valid SQF expressions.
It reads a file from here:
https://raw.githubusercontent.com/intercept/intercept/master/src/client/headers/client/sqf_pointers_declaration.hpp
"""
import urllib.request
from sqf.interpreter_types import ForType, IfType, SwitchType, WhileType, TryType, WithType
from sqf.types import Code, Array, Boolean, Number, Type, Nothing, Anything, String, Namespace, \
Object, Config, Script, Control, Group, Display, Side, Task, Location, NetObject, DiaryReport, TeamMember
# The mapping of SQF types to our types
STRING_TO_TYPE = {
'array': Array,
'scalar': Number,
'bool': Boolean,
'code': Code,
'string': String,
'text': String,
'namespace': Namespace,
'config': Config,
'location': Location,
'object': Object,
'group': Group,
'member': TeamMember, # team_member gets split
'control': Control,
'display': Display,
'exception': TryType,
'for': ForType,
'if': IfType,
'switch': SwitchType,
'while': WhileType,
'with': WithType,
'side': Side,
'task': Task,
'script': Script,
'nan': Number,
'nothing': Nothing,
'netobject': NetObject,
'any': Type,
'diary': DiaryReport # diary_record gets split
}
# the argument the type is initialized with
TYPE_TO_INIT_ARGS = {
Namespace: "'missionNamespace'",
}
# The return type "ANY" means that we do not know it, so it is Nothing()
STRING_TO_TYPE_RETURN = STRING_TO_TYPE.copy()
STRING_TO_TYPE_RETURN['any'] = Anything
WRONG_RETURN_TYPES = {
'attachedto': Object,
'getclientstatenumber': Number,
'handgunmagazine': Array,
'ammoonpylon': Anything
}
def _parse_type_names(type_names):
# Alternative types separated by _ char
types_names = type_names.split('_')
# Never care about NaN type (covered by scalar)
if 'nan' in types_names:
types_names.remove('nan')
# Remove parts of types that also get split
if 'team' in types_names:
types_names.remove('team')
if 'record' in types_names:
types_names.remove('record')
return types_names
def _parse_return_type_names(return_type_names):
return_type_names = _parse_type_names(return_type_names)
if len(return_type_names) > 1 and 'nothing' in return_type_names:
return_type_names.remove('nothing')
if len(return_type_names) > 1:
return_type_name = 'any'
else:
return_type_name = return_type_names[0]
return STRING_TO_TYPE_RETURN[return_type_name]
url = 'https://raw.githubusercontent.com/intercept/intercept/master/src/' \
'client/headers/client/sqf_pointers_declaration.hpp'
data = urllib.request.urlopen(url).read().decode('utf-8').split('\n')
expressions = []
for line in data:
if not line.startswith('static '):
continue
sections = line.split('__')
num_sections = len(sections)
if num_sections not in [4, 5, 6]:
print('Could\'t read line: ', line)
continue
# Name always comes first
op_name = sections[1]
# Return type always comes last (some operators have incorrect values for whatever reason)
if op_name in WRONG_RETURN_TYPES:
return_type = WRONG_RETURN_TYPES[op_name]
else:
return_type = _parse_return_type_names(sections[num_sections-1][:-1])
# Adds any relevant initialization argument for the return type
init_code = ''
# Number of sections allows us to classify the operation
if num_sections == 6:
if return_type in TYPE_TO_INIT_ARGS:
init_code = ', action=lambda lhs, rhs, i: %s' % TYPE_TO_INIT_ARGS[return_type]
for lhs_type_name in _parse_type_names(sections[2]):
lhs_type = STRING_TO_TYPE[lhs_type_name]
for rhs_type_name in _parse_type_names(sections[3]):
rhs_type = STRING_TO_TYPE[rhs_type_name]
expression = 'BinaryExpression(' \
'{lhs_type}, ' \
'Keyword(\'{keyword}\'), ' \
'{rhs_type}, {return_type}{init_code})'.format(
lhs_type=lhs_type.__name__,
keyword=op_name,
rhs_type=rhs_type.__name__,
return_type=return_type.__name__,
init_code=init_code
)
expressions.append(expression)
elif num_sections == 5:
if return_type in TYPE_TO_INIT_ARGS:
init_code = ', action=lambda rhs, i: %s' % TYPE_TO_INIT_ARGS[return_type]
for rhs_type_name in _parse_type_names(sections[2]):
rhs_type = STRING_TO_TYPE[rhs_type_name]
expression = 'UnaryExpression(' \
'Keyword(\'{keyword}\'), ' \
'{rhs_type}, {return_type}{init_code})'.format(
keyword=op_name,
rhs_type=rhs_type.__name__,
return_type=return_type.__name__,
init_code=init_code
)
expressions.append(expression)
else:
if return_type in TYPE_TO_INIT_ARGS:
init_code = ', action=lambda i: %s' % TYPE_TO_INIT_ARGS[return_type]
expression = 'NullExpression(' \
'Keyword(\'{keyword}\'), ' \
'{return_type}{init_code})'.format(
keyword=op_name,
return_type=return_type.__name__,
init_code=init_code
)
expressions.append(expression)
preamble = r'''# This file is generated automatically by `build_database.py`. Change it there.
from sqf.expressions import BinaryExpression, UnaryExpression, NullExpression
from sqf.types import Keyword, Type, Nothing, Anything, String, Code, Array, Number, Boolean, Namespace, \
Object, Config, Script, Control, Group, Display, Side, Task, Location, NetObject, DiaryReport, TeamMember
from sqf.interpreter_types import WhileType, \
ForType, SwitchType, IfType, TryType, WithType'''
# Expressions that use symbols are hardcoded since they aren't present in the parsed file
symbols = r'''
EXPRESSIONS = [
BinaryExpression(Array, Keyword('#'), Number, Anything),
BinaryExpression(Number, Keyword('!='), Number, Boolean),
BinaryExpression(String, Keyword('!='), String, Boolean),
BinaryExpression(Object, Keyword('!='), Object, Boolean),
BinaryExpression(Group, Keyword('!='), Group, Boolean),
BinaryExpression(Side, Keyword('!='), Side, Boolean),
BinaryExpression(String, Keyword('!='), String, Boolean),
BinaryExpression(Config, Keyword('!='), Config, Boolean),
BinaryExpression(Display, Keyword('!='), Display, Boolean),
BinaryExpression(Control, Keyword('!='), Control, Boolean),
BinaryExpression(TeamMember, Keyword('!='), TeamMember, Boolean),
BinaryExpression(NetObject, Keyword('!='), NetObject, Boolean),
BinaryExpression(Task, Keyword('!='), Task, Boolean),
BinaryExpression(Location, Keyword('!='), Location, Boolean),
BinaryExpression(Number, Keyword('%'), Number, Number),
BinaryExpression(Boolean, Keyword('&&'), Boolean, Boolean),
BinaryExpression(Boolean, Keyword('&&'), Code, Boolean),
BinaryExpression(Number, Keyword('*'), Number, Number),
BinaryExpression(Number, Keyword('+'), Number, Number),
BinaryExpression(String, Keyword('+'), String, String),
BinaryExpression(Array, Keyword('+'), Array, Array),
BinaryExpression(Number, Keyword('-'), Number, Number),
BinaryExpression(Array, Keyword('-'), Array, Array),
BinaryExpression(Number, Keyword('/'), Number, Number),
BinaryExpression(Config, Keyword('/'), String, Config),
BinaryExpression(SwitchType, Keyword(':'), Code, Nothing),
BinaryExpression(Number, Keyword('<'), Number, Boolean),
BinaryExpression(Number, Keyword('<='), Number, Boolean),
BinaryExpression(Number, Keyword('=='), Number, Boolean),
BinaryExpression(String, Keyword('=='), String, Boolean),
BinaryExpression(Object, Keyword('=='), Object, Boolean),
BinaryExpression(Group, Keyword('=='), Group, Boolean),
BinaryExpression(Side, Keyword('=='), Side, Boolean),
BinaryExpression(String, Keyword('=='), String, Boolean),
BinaryExpression(Config, Keyword('=='), Config, Boolean),
BinaryExpression(Display, Keyword('=='), Display, Boolean),
BinaryExpression(Control, Keyword('=='), Control, Boolean),
BinaryExpression(TeamMember, Keyword('=='), TeamMember, Boolean),
BinaryExpression(NetObject, Keyword('=='), NetObject, Boolean),
BinaryExpression(Task, Keyword('=='), Task, Boolean),
BinaryExpression(Location, Keyword('=='), Location, Boolean),
BinaryExpression(Number, Keyword('>'), Number, Boolean),
BinaryExpression(Number, Keyword('>='), Number, Boolean),
BinaryExpression(Config, Keyword('>>'), String, Config),
BinaryExpression(Number, Keyword('^'), Number, Number),
BinaryExpression(Boolean, Keyword('||'), Boolean, Boolean),
BinaryExpression(Boolean, Keyword('||'), Code, Boolean),
UnaryExpression(Keyword('!'), Boolean, Boolean),
UnaryExpression(Keyword('+'), Number, Number),
UnaryExpression(Keyword('+'), Array, Array),
UnaryExpression(Keyword('-'), Number, Number),
'''
with open('sqf/database.py', 'w') as f:
f.write(preamble + '\n\n')
f.write(symbols + ' ')
f.write(',\n '.join(expressions))
f.write('\n]\n')