-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber.py
executable file
·102 lines (81 loc) · 2.05 KB
/
number.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/python3
# number.py takes a non-numbered BASIC program with LABELs i
# and outputs a nicely numbered file
import re
import sys
def wline(s):
if outfile:
outfile.write(s)
else:
print(s)
def wtmp(s):
tmpfile.write(s)
def usage():
print("usage : ")
print(sys.argv[0]+" <input file> [output file]")
print("")
print(" output file is optional - will print to stdout otherwise")
filename=""
outfilename=""
outfile = 0
tmpfile = 0
if len(sys.argv[1:]) == 0:
usage()
quit()
filename=sys.argv[1]
print("Process file : "+filename)
if len(sys.argv[2:]) != 0:
outfilename=sys.argv[2]
print("Write to : "+outfilename)
outfile=open(outfilename,"w")
tmpfilename = "/tmp/renum.tmp"
tmpfile = open(tmpfilename,"w")
infile = open(filename, "r")
if not infile:
if outfile:
outfile.close()
if tmpfile:
tmpfile.close()
print("Failed to open file "+filename)
quit()
lnum=10
pat_proc = re.compile("DEF PROC[a-zA-Z]+")
pat_fn = re.compile("DEF FN[a-zA-Z]+")
pat_empty = re.compile("^\s*$")
pat_label = re.compile("^(LABEL\w+):")
pat_labelref = re.compile("(LABEL\w+)(?!:)\s")
pat_sep = re.compile ("REM\s*---")
labs = {}
# Pass 1 write tmp file
for fline in infile.readlines():
if pat_empty.match(fline):
continue
if pat_proc.search(fline) or pat_fn.search(fline) or pat_sep.search(fline):
r=(lnum+100) % 100
lnum=(lnum+100-r)
wtmp("\n")
m = re.match(pat_label, fline)
if m:
labs.update({m.group(1): str(lnum)})
wtmp(str(lnum) +" REM " + fline)
else:
wtmp(str(lnum) +" " + fline)
lnum=lnum+10
tmpfile.close()
# Pass 2, sort out references to labels
print("Labels:")
for l,n in labs.items():
print(l,n)
tmpfile = open(tmpfilename,"r")
for tline in tmpfile.readlines():
m = re.search(pat_labelref, tline)
if m:
wline(tline.replace(m.group(1),labs[m.group(1)]))
else:
wline(tline)
# Close everything
infile.close()
if outfile:
outfile.close()
if tmpfile:
tmpfile.close()