Skip to content

Commit 0dc8817

Browse files
committed
jump labels
1 parent 8245879 commit 0dc8817

File tree

5 files changed

+78
-31
lines changed

5 files changed

+78
-31
lines changed

examples/mod3.txt

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
1+
# mod = x mod 3
2+
13
Z('mod')
24
Z('k')
3-
J('k', 'a', 12)
4-
S('k')
5-
J('k', 'a', 11)
6-
S('k')
7-
J('k', 'a', 10)
8-
S('k')
9-
J(1,1,3)
5+
@loop
6+
J('k', 'a', '@m1')
7+
S('k')
8+
J('k', 'a', '@m2')
9+
S('k')
10+
J('k', 'a', '@end')
11+
S('k')
12+
J(1,1,'@loop')
13+
@m2
1014
S('mod')
15+
@m1
1116
S('mod')
12-
Z('end')
17+
@end
18+
19+
# explanation
20+
#while 1:
21+
# if k == a:
22+
# break
23+
# k += 1
24+
# if k == a:
25+
# break
26+
# k += 1
27+
# if k == a:
28+
# break
29+
# k += 1
30+
# div += 1

examples/mult.txt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
# x = x * a
2+
13
Z('i1')
24
T('x', 'x0')
35
Z('x')
4-
J('i1', 'a', 11)
6+
@loop
7+
J('i1', 'a', '@end')
58
Z('i2')
69
S('i1')
10+
@innerloop
711
S('x')
812
S('i2')
9-
J('i2', 'x0', 4)
10-
J('x', 'x', 7)
11-
Z('end')
13+
J('i2', 'x0', '@loop')
14+
J('x', 'x', '@innerloop')
15+
@end

examples/xdiva.txt

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
Z('i')
2-
J('i', 'a', 7)
3-
J('k', 'x', 9)
4-
S('k')
5-
S('i')
6-
J(1, 1, 2)
7-
S('div')
8-
J(1, 1, 1)
9-
Z('end')
1+
# div = x // a
2+
3+
4+
@loop
5+
Z('i') # i = 0
6+
@innerloop
7+
J('i', 'a', '@div') # if i == a goto div
8+
J('k', 'x', '@end') # if k == x goto end
9+
S('k') # k += 1
10+
S('i') # i += 1
11+
J(1, 1, '@innerloop') # goto innerloop
12+
@div
13+
S('div') # div += 1
14+
J(1, 1, '@loop') # goto loop
15+
@end

examples/xminusa.txt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
J('a', 'x', 5)
2-
S('a')
3-
S('sub')
4-
J(1, 1, 1)
5-
Z('end')
1+
# sub = x - a
2+
3+
@start
4+
J('a', 'x', '@end') # if a == x goto end
5+
S('a') # a += 1
6+
S('sub') # sub += 1
7+
J(1, 1, '@start') # goto start
8+
@end

rmach.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

5166
if __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

Comments
 (0)