no python files to be committed in isafunctions
[openpower-isa.git] / src / openpower / decoder / pseudo / functionreader.py
1 # Reads OpenPOWER ISA pages from http://libre-soc.org/openpower/isafunctions
2 """OpenPOWER ISA function reader
3
4 reads markdown files looking for indented code blocks
5 """
6
7 from collections import OrderedDict
8 from copy import copy
9 import os
10
11
12 def get_isafn_dir():
13 fdir = os.path.abspath(os.path.dirname(__file__))
14 fdir = os.path.split(fdir)[0]
15 fdir = os.path.split(fdir)[0]
16 fdir = os.path.split(fdir)[0]
17 fdir = os.path.split(fdir)[0]
18 print (fdir)
19 return os.path.join(fdir, "openpower", "isafunctions")
20
21
22 class ISAFunctions:
23
24 def __init__(self):
25 self.fns = OrderedDict()
26 for pth in os.listdir(os.path.join(get_isafn_dir())):
27 print("examining", get_isafn_dir(), pth)
28 if "swp" in pth:
29 continue
30 if not pth.endswith(".mdwn"):
31 print ("warning, file not .mdwn, skipping", pth)
32 continue
33 self.read_file(pth)
34
35 def read_file(self, fname):
36 pagename = fname.split('.')[0]
37 fname = os.path.join(get_isafn_dir(), fname)
38 with open(fname) as f:
39 lines = f.readlines()
40
41 # set up dict with current page name
42 d = {'page': pagename}
43
44 # line-by-line lexer/parser, quite straightforward: pops one
45 # line off the list and checks it. nothing complicated needed,
46 # all sections are mandatory so no need for a full LALR parser.
47
48 l = lines.pop(0).rstrip() # get first line
49 while lines:
50 print(l)
51 # look for HTML comment, if starting, skip line.
52 # XXX this is braindead! it doesn't look for the end
53 # so please put ending of comments on one line:
54 # <!-- line 1 comment -->
55 # <!-- line 2 comment -->
56 if l.startswith('<!--'):
57 # print ("skipping comment", l)
58 l = lines.pop(0).rstrip() # get next line
59 continue
60
61 # Ignore blank lines before the first #
62 if len(l) == 0:
63 l = lines.pop(0).rstrip() # get next line
64 continue
65
66 # expect get heading
67 assert l.startswith('#'), ("# not found in line '%s'" % l)
68 d['desc'] = l[1:].strip()
69
70 # any lines not starting with space, ignore
71 while True:
72 l = lines.pop(0).rstrip()
73 print ("examining", repr(l))
74 if l.startswith(" "):
75 break
76 if l.startswith('<!--'):
77 continue
78
79 # get pseudocode
80 li = [l[4:]] # first line detected with 4-space
81 while lines:
82 l = lines.pop(0).rstrip()
83 print ("examining", repr(l))
84 if len(l) == 0:
85 li.append(l)
86 continue
87 if l.startswith('<!--'):
88 continue
89 assert l.startswith(' '), ("4spcs not found in line %s" % l)
90 l = l[4:] # lose 4 spaces
91 li.append(l)
92 d['pcode'] = '\n'.join(li)
93 break
94
95 self.fns[pagename] = d
96
97 def pprint(self):
98 for k, v in self.fns.items():
99 print("# %s %s" % (k, v['desc']))
100 print(v['pcode'])
101 print()
102
103
104 if __name__ == '__main__':
105 isa = ISAFunctions()
106 isa.pprint()