@@ -4,6 +4,7 @@ class machine:
44
55 def __init__ (self ):
66 self .regs = defaultdict (int )
7+ self .lbls = defaultdict (int )
78 self .cmds = [0 ]
89 self .ptr = 1
910
@@ -18,7 +19,17 @@ def T(self, m, n):
1819
1920 def J (self , m , n , q ):
2021 if self .regs [m ] == self .regs [n ]:
21- self .ptr = q - 1
22+ if type (q ) == str :
23+ self .ptr = self .lbls [q ] - 1
24+ else :
25+ self .ptr = q - 1
26+
27+ def CheckLabel (self , l ):
28+ lbl = l .strip ()
29+ return len (lbl ) and lbl [0 ] == '@' and lbl [1 :]
30+
31+ def SaveLabel (self , lbl , ptr ):
32+ self .lbls ['@' + lbl .strip ()] = ptr
2233
2334 def DeleteComments (self , l ):
2435 hsh = l .find ('#' )
@@ -28,9 +39,13 @@ def DeleteComments(self, l):
2839
2940 def LoadProg (self , file ):
3041 with open (file , 'r' ) as f :
42+ ptr = 1
3143 for l in f :
32- if s := self .DeleteComments (l ):
44+ if lbl := self .CheckLabel (l ):
45+ self .SaveLabel (lbl , ptr )
46+ elif s := self .DeleteComments (l ):
3347 self .cmds .append (f'self.{ s } ' )
48+ ptr += 1
3449
3550 def SetRegs (self , reg ):
3651 self .regs .update (reg )
@@ -50,6 +65,7 @@ def __str__(self):
5065
5166if __name__ == '__main__' :
5267 m = machine ()
53- m .LoadProg ('examples/mult.txt' )
54- m .SetRegs ({'x' :3 , 'a' : 5 })
55- m .Run ()
68+ m .LoadProg ('examples/mod3.txt' )
69+ m .SetRegs ({'a' : 12 })
70+ m .Run ()
71+ print (m .lbls )
0 commit comments