-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
73 lines (53 loc) · 2.93 KB
/
parser.py
File metadata and controls
73 lines (53 loc) · 2.93 KB
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
from __future__ import annotations
def parser(token_list: list["Token"]) -> tuple[list["Instruction"], dict[str, int]]:
token_lst: list["Token"] = token_list.copy()
instruction_list: list["Instruction"] = []
jump_table: dict[str, int] = {}
list_pointer: int = 0
while list_pointer < len(token_lst):
current_opcode: str = ""
if token_lst[list_pointer].type in ("OPCODE", "LABEL"):
if token_lst[list_pointer].type == "OPCODE":
current_opcode = token_lst[list_pointer].value
elif token_lst[list_pointer].type == "LABEL":
jump_table[token_lst[list_pointer].value] = len(instruction_list)
list_pointer += 1
continue
list_pointer += 1
current_args: list[str | int | float] = []
current_types: list[str] = []
while list_pointer < len(token_lst) and token_lst[list_pointer].type not in ("OPCODE", "LABEL"):
if token_lst[list_pointer].type == "INT":
current_args.append(int(token_lst[list_pointer].value))
current_types.append("int")
list_pointer += 1
elif token_lst[list_pointer].type == "FLOAT":
current_args.append(float(token_lst[list_pointer].value))
current_types.append("float")
list_pointer += 1
elif token_lst[list_pointer].type == "REGISTER":
current_args.append(token_list[list_pointer].value)
current_types.append("register")
list_pointer += 1
elif token_list[list_pointer].type == "CHAR":
current_args.append(token_list[list_pointer].value)
current_types.append("char")
list_pointer += 1
elif token_list[list_pointer].type == "STRING":
current_args.append(token_list[list_pointer].value)
current_types.append("string")
list_pointer += 1
elif token_lst[list_pointer].type == "IDENT":
current_args.append(token_lst[list_pointer].value)
current_types.append("ident")
list_pointer += 1
elif token_lst[list_pointer].type == "MESSAGE":
current_args.append("'" + token_lst[list_pointer].value + "'")
current_types.append("message")
list_pointer += 1
else:
break
instruction_list.append(Instruction(opcode = current_opcode, args = current_args, arg_types = current_types))
return instruction_list, jump_table
from Token import Token
from Instruction import Instruction