@@ -17,13 +17,13 @@ def __init__(self, program):
17
17
self .warnings = []
18
18
self .program .compiler = self
19
19
self .addCommand = self .program .add
20
-
20
+
21
21
def getPC (self ):
22
22
return len (self .program .code )
23
-
23
+
24
24
def getIndex (self ):
25
25
return self .index
26
-
26
+
27
27
# Move the index along
28
28
def next (self ):
29
29
self .index += 1
@@ -33,47 +33,47 @@ def getToken(self):
33
33
if self .index >= len (self .tokens ):
34
34
FatalError (self , 'Premature end of script' )
35
35
return self .tokens [self .index ].token
36
-
36
+
37
37
# Get the next token
38
38
def nextToken (self ):
39
39
self .index += 1
40
40
return self .getToken ()
41
-
41
+
42
42
def peek (self ):
43
43
try :
44
44
return self .tokens [self .index + 1 ].token
45
45
except :
46
46
return None
47
-
47
+
48
48
# Get a value
49
49
def getValue (self ):
50
50
return self .value .compileValue ()
51
-
51
+
52
52
# Get the next value
53
53
def nextValue (self ):
54
54
self .index += 1
55
55
return self .value .compileValue ()
56
-
56
+
57
57
# Get a constant
58
58
def getConstant (self , token ):
59
59
self .index += 1
60
60
return self .value .compileConstant (token )
61
-
61
+
62
62
# Get a condition
63
63
def getCondition (self ):
64
64
return self .condition .compileCondition ()
65
-
65
+
66
66
# Get the next condition
67
67
def nextCondition (self ):
68
68
self .index += 1
69
69
return self .condition .compileCondition ()
70
-
70
+
71
71
def tokenIs (self , value ):
72
72
return self .getToken () == value
73
-
73
+
74
74
def nextIs (self , value ):
75
75
return self .nextToken () == value
76
-
76
+
77
77
def getCommandAt (self , pc ):
78
78
return self .program .code [pc ]
79
79
@@ -91,15 +91,15 @@ def nextIsSymbol(self):
91
91
92
92
def rewindTo (self , index ):
93
93
self .index = index
94
-
94
+
95
95
def getLino (self ):
96
96
if self .index >= len (self .tokens ):
97
97
return 0
98
98
return self .tokens [self .index ].lino
99
-
99
+
100
100
def warning (self , message ):
101
101
self .warnings .append (message )
102
-
102
+
103
103
def showWarnings (self ):
104
104
for warning in self .warnings :
105
105
print (f'Line { self .getLino () + 1 } : { warning } ' )
@@ -112,20 +112,24 @@ def getSymbolRecord(self):
112
112
symbolRecord ['used' ] = True
113
113
return symbolRecord
114
114
return None
115
-
115
+
116
116
def compileLabel (self , command ):
117
117
return self .compileSymbol (command , self .getToken (), False )
118
118
119
119
def compileVariable (self , command , valueHolder = False ):
120
120
return self .compileSymbol (command , self .nextToken (), valueHolder )
121
121
122
122
def compileSymbol (self , command , name , valueHolder ):
123
- if hasattr (self .symbols , name ):
124
- FatalError (self , f'{ self .code [self .pc ].lino } : Duplicate symbol name "{ name } "' )
123
+ try :
124
+ v = self .symbols [name ]
125
+ except :
126
+ v = None
127
+ if v :
128
+ FatalError (self , f'Duplicate symbol name "{ name } "' )
125
129
return False
126
130
self .symbols [name ] = self .getPC ()
127
131
command ['type' ] = 'symbol'
128
- command ['isValueHolder ' ] = valueHolder
132
+ command ['valueHolder ' ] = valueHolder
129
133
command ['name' ] = name
130
134
command ['elements' ] = 1
131
135
command ['index' ] = 0
@@ -138,7 +142,7 @@ def compileSymbol(self, command, name, valueHolder):
138
142
# Compile the current token
139
143
def compileToken (self ):
140
144
token = self .getToken ()
141
- # print(token)
145
+ # print(f'Compile { token}' )
142
146
if not token :
143
147
return False
144
148
mark = self .getIndex ()
0 commit comments