-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
370 lines (338 loc) · 12 KB
/
config.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
"""
Terminology:
- Word: A semantic unit made up of alphanumeric characters (and any other characters you choose to allow). Words must be broken up by whitespace or symbols
- Symbol: A semantic unit that translates to an internal name and consists of non-word characters. Words and symbols both translate to internal names
- Internal name: Identifies a unique token. Words and symbols both map to internal names, which in turn get turned into tokens
You can have multiple symbols and/or words map to the same internal name
"""
from shared import LanguageError
# (Whitespaces basically)
IGNORE = (" ", "\t", "\n", "\r")
STRING_DELIMITERS = ('"', "'", "`")
# Used to determine what operations you can do between/to an expression
LANGUAGE_TYPES = ("integer", "float", "string", "boolean", "function", "array", "null")
# Characters that can't be used in symbols (the list implicitly includes all characters that are allowed in words)
RESERVED_CHARS = IGNORE + STRING_DELIMITERS
# Characters that can be used in words (in addition to alphanumerics)
OK_IN_WORD = ("_",)
# Translates symbols to internal names
SYMBOLS = {
".": "dot",
",": "comma",
":": "assign",
"(": "open_paren",
")": "close_paren",
"[": "array_start",
"]": "array_end",
"{": "block_start",
"}": "block_end",
"+": "plus",
"+=": "plus_equal",
"-": "minus",
"-=": "minus_equal",
"*": "times",
"*=": "times_equal",
"/": "over",
"//": "over_int",
"/=": "over_equal",
"==": "equals",
"<": "lessthan",
"<=": "lessthan_equal",
">": "greaterthan",
">=": "greaterthan_equal",
"!": "not",
"!=": "not_equal",
"&&": "and",
"&=": "and_equal",
"||": "or",
"|=": "or_equal",
"^": "power",
"^=": "power_equal",
"%": "mod",
"%=": "mod_equal",
"@": "from",
"#": "comment",
}
# Translates words to internal names
# Words in this list can't be used as identifiers (they're reserved)
RESERVED_WORDS = {
"as": "as",
"true": "true",
"false": "false",
"if": "if",
"else": "else",
"elif": "elif",
"while": "while",
"for": "for",
"return": "return",
"print": "print",
"then": "then",
"error": "error",
"fn": "function_keyword",
"typeof": "typeof",
"like": "stop_word",
"umm": "stop_word",
"um": "stop_word",
"uh": "stop_word",
"plus": "plus",
"minus": "minus",
"times": "times",
"of": "times",
"over": "over",
"mod": "mod",
"power": "power",
"and": "and",
"or": "or",
"import": "import",
"from": "from",
"push": "push",
"return": "return",
"null": "null",
"secret": "comment",
}
# Now we define the uses of different internal names
# Follow the pattern IDENTIFIER ASSIGNMENT_OPERATOR (any word or symbol that maps to it) EXPRESSION
# The identifier gets updated using the expression
ASSIGNMENT_OPERATORS = (
"plus_equal",
"minus_equal",
"times_equal",
"over_equal",
"over_int_equal",
"power_equal",
"mod_equal",
"and_equal",
"or_equal",
"assign",
"push",
)
# The function that gets called on the values of the identifier and expression
# a is the value of the identifier before the assignment
# b is the value of the expression
# returns a tuple of the new value and the type of the new value
ASSIGNMENT_OPERATIONS = {
"plus_equal": lambda a, b: (a.value + b.value, a.type),
"minus_equal": lambda a, b: (a.value - b.value, a.type),
"times_equal": lambda a, b: (a.value * b.value, a.type),
"over_equal": lambda a, b: (a.value / b.value, a.type),
"over_int_equal": lambda a, b: (a.value // b.value, a.type),
"mod_equal": lambda a, b: (a.value % b.value, a.type),
"power_equal": lambda a, b: (a.value**b.value, a.type),
"and_equal": lambda a, b: (a.value and b.value, "boolean"),
"or_equal": lambda a, b: (a.value or b.value, "boolean"),
"push": lambda a, b: ([*a.value, b], "array"),
# Just for symmetry (lexer pre-checks), but it should never be called bc assign is a special case (a doesn't need to be initialized)
"assign": None,
}
# What internal types we support for each assignment operator
# A tuple of the identifier's start type and the expression's type
ASSIGNMENT_OPERATOR_TYPE_SUPPORT = {
# We don't need to check compatibility for a plain assignment
"assign": None,
"plus_equal": [("integer", "integer"), ("float", "float"), ("string", "string"), ("array", "array")],
"minus_equal": [("integer", "integer"), ("float", "float")],
"times_equal": [("integer", "integer"), ("float", "float")],
"over_equal": [("float", "float")],
"over_int_equal": [("integer", "integer")],
"mod_equal": [("integer", "integer"), ("float", "float")],
"power_equal": [("integer", "integer"), ("float", "float")],
"and_equal": [("boolean", "boolean")],
"or_equal": [("boolean", "boolean")],
"push": [("array", i) for i in LANGUAGE_TYPES]
}
# Same old deal, but for binary operators
# Binary operators have an expression on either side
# And create a new expression using the two expressions's values and an operaion
BINARY_OPERATORS = (
"plus",
"minus",
"times",
"over",
"mod",
"power",
"and",
"or",
"equals",
"not_equal",
"lessthan",
"lessthan_equal",
"greaterthan",
"greaterthan_equal",
"as",
"from",
"over_int",
)
# Functions used by config (for operations) AND nodes (internally)
def careful_from_array(index: int, array: list) -> tuple:
try:
return array[index].value, array[index].type
except IndexError:
raise LanguageError(f"Index {index} out of bounds for array of length {len(array)}", array[0].line, array[0].col, array[0].program)
def as_type(evaluated_expr, end_type: str):
start_type = evaluated_expr.type
if not end_type in LANGUAGE_TYPE_CONVERSIONS[start_type]:
raise LanguageError(f"Cannot convert {start_type} to {end_type}", evaluated_expr.line, evaluated_expr.col, evaluated_expr.program)
return (LANGUAGE_TYPE_CONVERSIONS[start_type][end_type](evaluated_expr.value), end_type)
BINARY_OPERATIONS = {
"plus": lambda a, b: (a.value + b.value, a.type),
"minus": lambda a, b: (a.value - b.value, a.type),
"times": lambda a, b: (a.value * b.value, a.type),
"over": lambda a, b: (a.value / b.value, a.type),
"over_int": lambda a, b: (a.value // b.value, a.type),
"mod": lambda a, b: (a.value % b.value, a.type),
"power": lambda a, b: (a.value**b.value, a.type),
"and": lambda a, b: (a.value and b.value, "boolean"),
"minus": lambda a, b: (a.value - b.value, a.type),
"or": lambda a, b: (a.value or b.value, "boolean"),
"equals": lambda a, b: (a.value == b.value, "boolean"),
"not_equal": lambda a, b: (a.value != b.value, "boolean"),
"lessthan": lambda a, b: (a.value < b.value, "boolean"),
"lessthan_equal": lambda a, b: (a.value <= b.value, "boolean"),
"greaterthan": lambda a, b: (a.value > b.value, "boolean"),
"greaterthan_equal": lambda a, b: (a.value >= b.value, "boolean"),
"from": lambda a, b: careful_from_array(a.value, b.value),
"as": lambda a, b: as_type(a, b.value),
}
# Tuple of (left type, right type)
BINARY_OPERATOR_TYPE_SUPPORT = {
"plus": [("float", "float"), ("integer", "integer"), ("string", "string"), ("array", "array")],
"minus": [("float", "float"), ("integer", "integer")],
"times": [("float", "float"), ("integer", "integer")],
"over": [("float", "float")],
"over_int": [("integer", "integer")],
"mod": [("float", "float"), ("integer", "integer")],
"power": [("float", "float"), ("integer", "integer")],
"and": [("boolean", "boolean")],
"or": [("boolean", "boolean")],
"equals": [("string", "string"), ("boolean", "boolean"), ("integer", "integer"), ("float", "float")],
"not_equal": [("float", "float"), ("integer", "integer"), ("string", "string"), ("boolean", "boolean")],
"lessthan": [("float", "float"), ("integer", "integer")],
"lessthan_equal": [("float", "float"), ("integer", "integer")],
"greaterthan": [("float", "float"), ("integer", "integer")],
"greaterthan_equal": [("float", "float"), ("integer", "integer")],
"as": [(t, "string") for t in LANGUAGE_TYPES],
"from": [("integer", "array")]
}
# Since you can have multiple operations in an expression, we need to know each operation's precedence
# The higher the precedence, the earlier the operation is evaluated
BINARY_OPERATOR_PRECEDENCE = {
"plus": 1,
"minus": 1,
"times": 2,
"over": 2,
"over_int": 2,
"mod": 2,
"power": 3,
"and": 0,
"or": 0,
"equals": 1,
"not_equal": 1,
"lessthan": 1,
"lessthan_equal": 1,
"greaterthan": 1,
"greaterthan_equal": 1,
"as": 0,
"from": 0
}
# Same thing here, but for unary operators
# Which go in front of a term (after any binary operators) and apply to just it
UNARY_OPERATORS = (
"minus",
"not",
"typeof",
"stop_word",
)
UNARY_OPERATIONS = {
"minus": lambda a: (-a.value, a.type),
"not": lambda a: (not a.value, a.type),
"typeof": lambda a: (a.type, "string"),
"stop_word": lambda a: (a.value, a.type),
}
UNARY_OPERATOR_TYPE_SUPPORT = {
"minus": ["integer", "float"],
"not": ["boolean"],
"typeof": LANGUAGE_TYPES,
"stop_word": LANGUAGE_TYPES,
}
# How to convert one type('s internal `value`) to another type (also internal `value`)
LANGUAGE_TYPE_CONVERSIONS = {
"float": {
"integer": lambda x: int(x),
"string": lambda x: str(x),
"boolean": lambda x: x != 0,
"float": lambda x: x,
},
"string": {
"string": lambda x: x,
"boolean": lambda x: x != "",
"array": lambda x: [i for i in x],
},
"boolean": {
"integer": lambda x: 1 if x else 0,
"float": lambda x: 1.0 if x else 0.0,
"string": lambda x: "true" if x else "false",
"boolean": lambda x: x,
},
"function": {
"string": lambda x: str(x),
},
"array": {
"array": lambda x: x,
"string": lambda x: "[" + ", ".join(as_type(i, "string")[0] for i in x) + "]",
},
"integer": {
"integer": lambda x: x,
"float": lambda x: float(x),
"string": lambda x: str(x),
"boolean": lambda x: x != 0,
},
"null": {
"null": lambda x: None,
"string": lambda x: "null",
"boolean": lambda x: False,
"integer": lambda x: 0,
"float": lambda x: 0.0,
"array": lambda x: [],
}
}
# Check if the internal value is valid for a certain type of expression
LANGUAGE_TYPE_CHECKS = {
"integer": lambda x: isinstance(x, int),
"float": lambda x: isinstance(x, float),
"string": lambda x: isinstance(x, str),
"boolean": lambda x: isinstance(x, bool),
"array": lambda x: isinstance(x, list),
# When functions are eval-ed, they just return themselves so we can check "type". Since Function is defined in a daughter file, we can't just do a literal typecheck
"function": lambda x: x.type == "function",
"null": lambda x: x is None,
}
# First value is the language's equivalent of True, second is False
BOOLS = ("true", "false")
# Keywords are used by the parser for statements
# (Just by convention, we technically don't have to use keywords per se for statements)
KEYWORDS = (
"if",
"else",
"elif",
"while",
"for",
"return",
"print",
"then",
"error",
"function_keyword",
"import",
"return",
)
# A made-up category for tokens that don't fit into any other category
MISC_SYMBOLS = ("comma", "dot", "block_start", "block_end", "array_start", "array_end")
OPEN_BRACKETS = ("open_paren",)
CLOSE_BRACKETS = ("close_paren",)
# Maps a bracket internal name to its type (brackets of a type can only be closed by a closing bracket of the same type)
BRACKET_TYPES = {
"open_paren": "paren",
"close_paren": "paren",
}
# Things to import before running any (top-level) program
# Import statements basically inject the code in a given file into the top-level program where they're used
CORE_IMPORTS = ("corelib.dlg",)