-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbols.py
64 lines (57 loc) · 1.74 KB
/
symbols.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
"""
This module contains three functions that handle initialization of
the symbol table, handling symbols and variables
"""
def initialize():
"""
This function initializes the symbol table with te language's inbuilt symbols.
These symbols include, registers 0 to 15 identified by the symbol R0-R15, KBD, SCREEN, SP,LCL.ARG, THIS,THAT.
:return: dictionary containing the symbol name as the key and te numerical value/address of the register
"""
return {
"R0": 0,
"R1": 1,
"R2": 2,
"R3": 3,
"R4": 4,
"R5": 5,
"R6": 6,
"R7": 7,
"R8": 8,
"R9": 9,
"R10": 10,
"R11": 11,
"R12": 12,
"R13": 13,
"R14": 14,
"R15": 15,
"SP": 0,
"LCL": 1,
"ARG": 2,
"THIS": 3,
"THAT": 4,
"SCREEN": 16384,
"KBD": 24576
}
def labels(line):
"""
This function check whether the current line is a symbol and removes the parenthesis
surrounding the label
:param line: current line being processed
:return: the label name without the parenthesis.
"""
if line.startswith("("):
line = line.replace("(", "").replace(")", "")
return line
def variables(line, symbol_table):
"""
This function checks whether the current line is a variable declaration
and then adds it to the symbol table if it does not already exist.
:param line: the current line of code being processed
:param symbol_table: the symbol table
:return: the variable name if it des not already exist in the symbol table, else returns none
"""
if line.startswith("@"):
line = line.replace("@", "")
if line not in symbol_table:
return line