|
135 | 135 |
|
136 | 136 | while "<" in line and ">" in line:
|
137 | 137 | equation = line[line.find("<") + 1:line.find(">")]
|
138 |
| - new_equation = line[line.find("<") + 1:line.find(">")] |
| 138 | + """new_equation = line[line.find("<") + 1:line.find(">")] |
139 | 139 | while "{" in new_equation and "}" in new_equation:
|
140 | 140 | variable = new_equation[new_equation.find("{") + 1:new_equation.find("}")]
|
141 | 141 | new_equation = new_equation.replace("{"+variable+"}", variable)
|
| 142 | + line = line.replace("<"+equation+">", "{"+new_equation+"}")""" |
| 143 | + new_equation = equation.replace("{", "") |
| 144 | + new_equation = new_equation.replace("}", "") |
142 | 145 | line = line.replace("<"+equation+">", "{"+new_equation+"}")
|
143 | 146 |
|
| 147 | + while len(re.findall("\{[a-zA-Z]*\[len\]\}", line)) > 0 and re.findall("\{[a-zA-Z]*\[len\]\}", line)[0] in line: |
| 148 | + variable = line[line.find("{") + 1:line.find("[")] |
| 149 | + full_var = re.findall("\{[a-zA-Z]*\[len\]\}", line)[0] |
| 150 | + line = line.replace(str(full_var), "{len("+variable+")}") |
| 151 | + while "[{" in line and "}]" in line: |
| 152 | + variable = line[line.find("[") + 1:line.find("]")] |
| 153 | + full_var = line[line.find("{") + 1:line.find("[")] |
| 154 | + variable = variable.replace("{", "", 1) |
| 155 | + variable = variable.replace("}", "", 1) |
| 156 | + if variable == "len": |
| 157 | + line = line.replace("{"+full_var+"[{"+variable+"}]}", "{len("+full_var+")}") |
| 158 | + else: |
| 159 | + line = line.replace("[{"+variable+"}]", "["+variable+"]", 1) |
| 160 | + |
144 | 161 | if line.startswith("for"):
|
145 | 162 | indentation_required += 1
|
146 | 163 | line = remove_from_string(line, ["{", "}"])
|
|
230 | 247 | var_type = 'int'
|
231 | 248 | elif line.startswith(":float"):
|
232 | 249 | var_type = 'float'
|
| 250 | + elif line.startswith(":list"): |
| 251 | + var_type = 'list' |
233 | 252 | else:
|
234 | 253 | var_type = 'str'
|
235 | 254 | line = line.replace(":"+var_type, "", 1)
|
|
256 | 275 | if line.startswith(" "):
|
257 | 276 | line = line.replace(" ", "", 1)
|
258 | 277 |
|
259 |
| - line = line.split(" ") # Result : [name, "=", content] |
| 278 | + line = line.split(" ") # Result : [name, var_operator, content] |
260 | 279 |
|
261 | 280 | if var_parameters is not None:
|
262 | 281 | line.pop(0)
|
|
294 | 313 | f"The variable \"{variable}\" is not existing or has been declared later in the code.")
|
295 | 314 | do_regroup = False
|
296 | 315 |
|
| 316 | + elif var_type == "list": |
| 317 | + # Recombination of line vars in a single line |
| 318 | + for i in range(3, len(line)): |
| 319 | + line[2] = line[2] + " " + line[i] |
| 320 | + |
| 321 | + # Initializing list elements |
| 322 | + list_elements = [] |
| 323 | + if "list." not in line[2]: |
| 324 | + while "[" in line[2] and "]" in line[2]: |
| 325 | + errors_count = 0 |
| 326 | + variable = line[2][line[2].find("[") + 1:line[2].find("]")] |
| 327 | + debug("other", lineno(), f"List element {variable} found.") |
| 328 | + try: |
| 329 | + line[2] = line[2].replace("[" + variable + "]", "") |
| 330 | + except KeyError: |
| 331 | + error(line_numbers, "ArgumentError", |
| 332 | + f"An error occured while trying to parse the list \"{line[0]}\".") |
| 333 | + errors_count += 1 |
| 334 | + if errors_count >= 10: |
| 335 | + print( |
| 336 | + f"\n\n{bcolors.FAIL}Debug has chosen to stop this program due to too many errors. Sorry for the inconvenicence.{bcolors.ENDC}") |
| 337 | + line_numbers = len(code_lines) |
| 338 | + break |
| 339 | + list_elements.append(variable) |
| 340 | + # Dememorizing 'variable' |
| 341 | + variable = None |
| 342 | + |
| 343 | + line[2] = list_elements |
| 344 | + var_type = "list" |
| 345 | + do_regroup = False |
| 346 | + elif line[2].startswith("list.add"): |
| 347 | + line[2] = line[2].replace("list.add ", "", 1) |
| 348 | + line[2] = remove_suffix(line[2], line[2].endswith("\n")) |
| 349 | + line = line[0]+".append(f\""+line[2]+"\")" |
| 350 | + do_regroup = False |
| 351 | + elif line[2].startswith("list.insert"): |
| 352 | + line[2] = line[2].replace("list.insert ", "", 1) |
| 353 | + index = re.findall("\d*", str(line[2]))[0] |
| 354 | + line[2] = line[2].replace(index+" ", "", 1) |
| 355 | + line[2] = remove_suffix(line[2], line[2].endswith("\n")) |
| 356 | + line = line[0]+".insert("+index+", f\""+line[2]+"\")" |
| 357 | + do_regroup = False |
| 358 | + elif line[2].startswith("list.remove"): |
| 359 | + line[2] = line[2].replace("list.remove ", "", 1) |
| 360 | + index = re.findall("\d*", str(line[2]))[0] |
| 361 | + line = line[0]+".pop("+index+")" |
| 362 | + do_regroup = False |
| 363 | + |
297 | 364 | else:
|
298 | 365 | try:
|
299 | 366 | if "." not in str(line[2]):
|
|
306 | 373 | except ValueError:
|
307 | 374 | line[2] = str(line[2])
|
308 | 375 |
|
| 376 | + |
309 | 377 | if do_regroup:
|
310 | 378 | for i in range(3, len(line)):
|
311 | 379 | line[2] += " "+line[i]
|
312 | 380 |
|
313 |
| - if not isinstance(line[2], bool): |
| 381 | + |
| 382 | + if not isinstance(line[2], bool) and not isinstance(line[2], list): |
314 | 383 | try:
|
315 | 384 | line[2] = int(line[2])
|
316 | 385 | except ValueError:
|
|
319 | 388 | except ValueError:
|
320 | 389 | pass
|
321 | 390 |
|
322 |
| - if isinstance(line[2], str) and not line[2].startswith("input") and not line[2].startswith("randint"): |
| 391 | + |
| 392 | + if isinstance(line[2], str) and not line[2].startswith("input") and not line[2].startswith("randint") and not isinstance(line, str): |
323 | 393 | line[2] = remove_suffix(line[2], line[2].endswith("\n"))
|
324 | 394 | line[2] = "f\""+line[2]+"\""
|
325 | 395 |
|
|
337 | 407 | elif var_action == "ceil":
|
338 | 408 | line[2] = f"ceil({line[2]})"
|
339 | 409 |
|
340 |
| - if var_type is not None: |
341 |
| - line = line[0] + " = " + var_type + "(" + str(line[2]) + ")" |
| 410 | + if ("append" in line or "insert" in line or "pop" in line) and var_type == "list": |
| 411 | + line = line |
| 412 | + elif var_type == "list": |
| 413 | + line = line[0] + " " + line[1] + " " + str(line[2]) |
| 414 | + elif var_type is not None: |
| 415 | + line = line[0] + " " + line[1] + " " + var_type + "(" + str(line[2]) + ")" |
342 | 416 | else:
|
343 |
| - line = line[0] + " = " + str(line[2]) |
| 417 | + line = line[0] + " " + line[1] + " " + str(line[2]) |
344 | 418 |
|
345 | 419 | elif line.startswith("pause"):
|
346 | 420 | line = line.replace("pause ", "", 1)
|
|
0 commit comments