]>
gcc.gnu.org Git - gcc.git/blob - gcc/m2/tools-src/array2index.py
1ed1ed3ccfcaa22044a3d4f9bd59680c9cc6421d
3 # array2index.py utility to convert static arrays into a dynamic equivalent.
5 # Copyright (C) 2011-2021 Free Software Foundation, Inc.
7 # This file is part of GNU Modula-2.
9 # GNU Modula-2 is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3, or (at your option)
14 # GNU Modula-2 is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with GNU Modula-2; see the file COPYING. If not, write to the
21 # Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
32 lines
= [] # global copy of the input lines of text.
36 # printf - keeps C programmers happy :-)
39 def printf (format
, *args
):
40 print(str(format
) % args
, end
=' ')
44 # fatal - displays a message and exits
47 def fatal (format
, *args
):
48 print(str(format
) % args
, end
=' ')
59 # putNext - pushes, i, to be the next line read when
68 # getNext - returns the next line of input text.
83 # isEof - return True if <eof> is seen.
91 # isProcedure - return True if 'PROCEDURE' is seen.
96 return (len(l
)>0) and (l
[0]=='PROCEDURE')
100 # isVar - return True if 'VAR' is seen.
105 return (len(l
)>0) and (l
[0]=='VAR')
109 # isBegin - return True if 'BEGIN' is seen.
114 return (len(l
)>0) and (l
[0]=='BEGIN')
118 # isEnd - return True if the 'END' to a procedure is seen.
122 return (len(i
)>3) and (i
[0:3]=='END')
126 # getVarIndent - return the variable indent (where ':' occurs)
129 def getVarIndent (l
):
139 # getMaxIndent - return the maximum variable indent (where ':' occurs)
142 def getMaxIndent (v
):
151 # setVarIndent - sets the indentation to, n, for each variable declared.
154 def setVarIndent (v
, n
):
159 w
+= [l
[0:i
] + (' '*(n
-i
)) + l
[i
:]]
165 # adjustVar - adds the dictionary contents to the variable list.
168 def adjustVar (v
, d
):
175 v
= h
+ [' pCall: PtrToCallFrame ;\n'] + t
177 v
= h
+ [' pSym: PtrToSymbol ;\n'] + t
181 v
= [h
] + [' pCall: PtrToCallFrame ;\n'] + v
[1:]
183 v
= [h
] + [' pSym: PtrToSymbol ;\n'] + v
[1:]
184 v
= setVarIndent(v
, getMaxIndent(v
))
188 # getIndent - returns the number of spaces before the text.
201 # scanStatements - returns the statements in a procedure after they
202 # have been transformed to use an index rather than
206 def scanStatements ():
207 debug(scanStatements
)
212 x
= i
.find('Symbols[')
217 print("indexing ", i
[y
:z
])
220 j
+= 'pSym := GetPsym(%s) ;\n' % i
[y
:z
]
222 i
= i
[0:x
]+'pSym^'+i
[z
+1:]
224 x
= i
.find('ScopeCallFrame[')
229 print("indexing ", i
[y
:z
])
232 j
+= 'pCall := GetPcall(%s) ;\n' % i
[y
:z
]
234 i
= i
[0:x
]+'pCall^'+i
[z
+1:]
241 # scanVar - returns the list of variable declarations.
247 while not isBegin(i
):
254 # scanProcedure - returns the list of modified lines within
258 def scanProcedure ():
259 debug("scanProcedure")
263 while (not isEnd(i
)):
269 s
, d
= scanStatements()
274 # const, type, comment
277 fatal("internal error")
281 # scanLines - scans a list of lines for each procedure
288 lines
= l
+ ['<eof>']
301 # Usage - display a single line summarising usage.
305 print("array2index.py [-h][-o outputfile] inputfile")
309 # collectArgs - return inputfile, outputfile.
317 optlist
, list = getopt
.getopt(sys
.argv
[1:], ':ho:')
325 except getopt
.GetoptError
:
328 return inputfile
, outputfile
332 # getFiles - given two strings, input, and, output, open and
333 # return their two respective files.
336 def getFiles (inputfile
, outputfile
):
340 if os
.path
.exists(inputfile
):
341 ip
= open(inputfile
, 'r')
343 fatal('cannot open file %s', inputfile
)
347 op
= open(outputfile
, 'w')
352 # main - the main procedure
357 inputfile
, outputfile
= collectArgs()
358 ip
, op
= getFiles(inputfile
, outputfile
)
359 op
.writelines(scanLines(ip
.readlines()))
This page took 0.051561 seconds and 4 git commands to generate.