-
Notifications
You must be signed in to change notification settings - Fork 0
/
subleqp.py
84 lines (76 loc) · 2.69 KB
/
subleqp.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Subleq+ master program
# Copyright (C) 2022 McChuck
# Released under GNU General Public License
# See LICENSE for more details.
# Many thanks to Chris Lloyd (github.com/cjrl) and Lawrence Woodman for inspiration and examples.
# Check out https://techtinkering.com/articles/subleq-a-one-instruction-set-computer/
# And especially https://techtinkering.com/2009/05/15/improving-the-standard-subleq-oisc-architecture/
# And maybe watch https://www.youtube.com/watch?v=FvwcRaE9yxc
import sys
import os
from subleqp_parser import SubleqpParser
from subleqp_vm import SubleqpVM
try:
from getch import getch, getche # Linux
except ImportError:
from msvcrt import getch, getche # Windows
def Write_slc(slc_file, mem):
pc = 0
maxpc = len(mem) - 1
while pc <= maxpc:
a = mem[pc]
b = 0
c = 0
if pc+1 <= maxpc:
b = mem[pc+1]
if pc+2 <= maxpc:
c = mem[pc+2]
slc_file.write('{} {} {}\n'.format(a, b, c))
pc += 3
def Subleqp(args):
try:
sla_name = args[0]
slc_name = None
if len(args) > 1:
slc_name = args[1]
parser = SubleqpParser()
mem = []
with open(sla_name, "r") as sla_file:
raw = sla_file.read()
mem = parser.parse(raw)
sla_file.close()
if slc_name != None:
with open(slc_name, "w") as slc_file:
Write_slc(slc_file, mem)
slc_file.close()
SubleqpVM.execute(mem)
except(ValueError, IndexError):
print("I just don't know what went wrong!\n")
sla_file.close()
def main(args):
try:
print()
if len(args) == 1:
Subleqp(args)
elif len(args) == 2:
if os.path.isfile(args[1]):
print(args[1], "exists. Overwrite? ", end="", flush=True)
answer = getche()
if answer in ["y", "Y"]:
print()
print(args[1], "replaced \n\n", flush=True)
Subleqp(args)
else:
print()
print(args[1], "retained \n\n", flush=True)
Subleqp([args[0]])
else:
print("creating", args[1], "\n\n", flush=True)
Subleqp(args)
else:
print("usage: python subleqp.py infile.sla [outfile.slc]\n")
except FileNotFoundError:
print("< *Peter_Lorre* >\nYou eediot! What were you theenking?\nTry it again, but thees time with a valid file name!\n</ *Peter_Lorre* >\n")
print("usage: python subleqp.py infile.sla [outfile.slc]\n")
if __name__ == '__main__':
main(sys.argv[1:])