Introduced variables

Version:1.1.2
This commit is contained in:
Anthony Scemama 2009-09-04 15:11:42 +02:00
parent 5f6950fd7c
commit 7f8767ee5e
7 changed files with 272 additions and 50 deletions

View File

@ -23,9 +23,7 @@ class CommandLine(object):
self.executable_name = self.argv[0]
def defined(self):
try:
d = self._defined
except AttributeError:
if '_defined' not in self.__dict__:
self._defined = []
for o,a in self.opts:
if o in [ "-D", options['D'][0] ]:
@ -79,9 +77,7 @@ Options:
t = """
def do_$LONG(self):
try:
x = self._do_$LONG
except AttributeError:
if '_do_$LONG' not in self.__dict__:
self._do_$LONG = False
for o,a in self.opts:
if o in ("-$SHORT", "--$LONG"):
@ -95,9 +91,7 @@ do_$LONG = property(fget=do_$LONG)
exec t.replace("$LONG",long).replace("$SHORT",short)
def do_run(self):
try:
x = self._do_run
except AttributeError:
if '_do_run' not in self.__dict__:
self._do_run = not (self.do_version or self.do_init)
return self._do_run
do_run = property(fget=do_run)

View File

@ -11,20 +11,23 @@ Error:
-----
"""
print message, '\n'
print "file %s ; line %d :\n %s"%(line.filename,line.i,line.text)
if line is not None:
print "file %s ; line %d :\n %s"%(line.filename,line.i,line.text)
sys.exit(1)
######################################################################
def warn(line,message):
assert isinstance(line,Line)
print """
if line is not None:
print """
Warning:
-------
"""
if line.i > 0:
print message, '\n'
print "file %s, line %d:\n %s"%(line.filename,line.i,line.text)
print message, '\n'
else:
print "Warning: %s"%(message)
######################################################################

View File

@ -257,7 +257,18 @@ class Provide_all (Line):
return "%20s:%5d : %s"%("Provide_all",self.i,self.text)
######################################################################
def create_irpf90_files():
result = []
from command_line import command_line
import os
if command_line.do_run:
def is_irpf90_file(filename):
return filename.endswith(".irp.f")
result = filter ( is_irpf90_file, os.listdir(os.getcwd()) )
return result
irpf90_files = create_irpf90_files()

View File

@ -323,7 +323,7 @@ def irp_simple_statements(text):
result = [
Simple_line(i,"!",f),
Simple_line(i,"! >>> %s"%(txt,),f ),
Provide_all(i," call %ser_%s('%s')"%(rw,variable,num),f),
Provide_all(i," call %ser_%s('%s')"%(rw,variable,num),f),
Simple_line(i,"! >>> END %s "%(txt,),f ),
Simple_line(line.i,"!",f),
]
@ -340,7 +340,7 @@ def irp_simple_statements(text):
def process_return(line):
assert isinstance(line,Return)
if command_line.do_assert or command_line.do_debug:
newline = Simple_line(line.i," call irp_leave(irp_here)",line.filename)
newline = Simple_line(line.i," call irp_leave(irp_here)",line.filename)
result = [newline, line]
else:
result = [ line ]
@ -357,8 +357,8 @@ def irp_simple_statements(text):
matches = [ match.group(1).strip(), match.group(3).strip() ]
for m in matches:
if not(m.isdigit() or ("'" in m) or (m == "")):
result.append ( Simple_line (line.i, " print *, '%s = ', %s"%(m,m), line.filename) )
result.append ( Simple_line (line.i, " print *, ''", line.filename) )
result.append ( Simple_line (line.i, " print *, '%s = ', %s"%(m,m), line.filename) )
result.append ( Simple_line (line.i, " print *, ''", line.filename) )
return result
def process_assert(line):
@ -374,14 +374,14 @@ def irp_simple_statements(text):
result = [
Simple_line(i, "!", f),
Simple_line(i, "! >>> %s"%(txt,), f),
If (i, " if (.not.%s) then"%(condition,), f),
Simple_line(i, " call irp_trace", f),
Simple_line(i, " print *, irp_here//': Assert failed:'", f),
Simple_line(i, " print *, ' file: %s, line: %d'"%(f,i), f),
Simple_line(i, " print *, '%s'"%(condition_str,), f),
If (i, " if (.not.%s) then"%(condition,), f),
Simple_line(i, " call irp_trace", f),
Simple_line(i, " print *, irp_here//': Assert failed:'", f),
Simple_line(i, " print *, ' file: %s, line: %d'"%(f,i), f),
Simple_line(i, " print *, '%s'"%(condition_str,), f),
] + debug_conditions(line) + [
Simple_line(i, " stop 1", f),
Endif (i, " endif", f),
Simple_line(i, " stop 1", f),
Endif (i, " endif", f),
Simple_line(i, "! <<< END %s"%(txt,), f),
Simple_line(i, "!", f)
]
@ -395,7 +395,7 @@ def irp_simple_statements(text):
i = line.i
f = line.filename
result = [
Simple_line(i," call irp_leave(irp_here)", f),
Simple_line(i," call irp_leave(irp_here)", f),
line
]
else:
@ -415,10 +415,10 @@ def irp_simple_statements(text):
i = line.i
f = line.filename
result = [ line,
Declaration(i," character*(%d), parameter :: irp_here = '%s'"%(length,varname), f) ]
Declaration(i," character*(%d), parameter :: irp_here = '%s'"%(length,varname), f) ]
if command_line.do_assert or command_line.do_debug:
result += [
Simple_line(i," call irp_enter(irp_here)", f),
Simple_line(i," call irp_enter(irp_here)", f),
]
return result
@ -438,10 +438,10 @@ def irp_simple_statements(text):
i = line.i
f = line.filename
result = [ line,
Declaration(i," character*(%d), parameter :: irp_here = '%s'"%(length,subname), f) ]
Declaration(i," character*(%d), parameter :: irp_here = '%s'"%(length,subname), f) ]
if command_line.do_assert or command_line.do_debug:
result += [
Simple_line(i," call irp_enter(irp_here)", f),
Simple_line(i," call irp_enter(irp_here)", f),
]
return result
@ -459,10 +459,10 @@ def irp_simple_statements(text):
i = line.i
f = line.filename
result = [ line,
Declaration(i," character*(%d), parameter :: irp_here = '%s'"%(length,subname), f) ]
Declaration(i," character*(%d), parameter :: irp_here = '%s'"%(length,subname), f) ]
if command_line.do_assert or command_line.do_debug:
result += [
Simple_line(i," call irp_enter(irp_here)", f),
Simple_line(i," call irp_enter(irp_here)", f),
]
return result
@ -511,7 +511,7 @@ def change_includes(text):
try:
file = open(filename,'r')
file.close()
result += preprocessed_text(filename)
result += create_preprocessed_text(filename)
#result += get_text(file.readlines(), filename)
except IOError:
result.append(line)
@ -531,7 +531,7 @@ def process_old_style_do(text):
isinstance(line,Enddo):
buffer = line.text.split()
if buffer[0] == number:
text[i] = Enddo(line.i," enddo",line.filename)
text[i] = Enddo(line.i," enddo",line.filename)
return
error.fail(text[begin],"Old-style do loops should end with 'continue' or 'end do'")
@ -543,7 +543,7 @@ def process_old_style_do(text):
if buffer[1].isdigit():
number = buffer.pop(1)
change_matching_enddo(i,number)
line.text = " "+" ".join(buffer)
line.text = " ".join(buffer)
result.append(line)
return result
@ -591,7 +591,7 @@ endif'''
f = line.filename
result.append( If(i,"%s then"%(test,),f) )
result += get_type(i,f,code,False)[0]
result.append( Endif(i," endif",f) )
result.append( Endif(i," endif",f) )
else:
result.append(line)
return result
@ -705,7 +705,7 @@ def move_to_top(text,t):
return text
######################################################################
def preprocessed_text(filename):
def create_preprocessed_text(filename):
file = open(filename,"r")
lines = file.readlines()
file.close()
@ -724,13 +724,21 @@ def preprocessed_text(filename):
result = move_to_top(result,Use)
return result
if __name__ == '__main__':
txt = preprocessed_text('testfile.irp.f')
check_begin_end(txt)
for line in txt:
print line
txt = preprocessed_text('testfile_fixed.irp.f')
check_begin_end(txt)
for line in txt:
print line
######################################################################
preprocessed_text = []
for filename in irpf90_files:
result = create_preprocessed_text(filename)
check_begin_end(result)
preprocessed_text.append( (filename, result) )
######################################################################
def debug():
for filename, txt in preprocessed_text:
print "=== "+filename+" ==="
for line in txt:
print line
print irpf90_files
if __name__ == '__main__':
debug()

View File

@ -1,5 +1,8 @@
#!/usr/bin/python
def strip(x):
return x.strip()
def same_file(filename,txt):
assert isinstance(filename,str)
assert isinstance(txt,list)
@ -17,8 +20,13 @@ def same_file(filename,txt):
return False
return True
def build_dim(dim):
if len(dim) == 0:
return ""
else:
return "(%s)"%( ",".join(dim) )
if __name__ == '__main__':
txt = open('/etc/passwd','r').readlines()
print same_file('/etc/passwd',txt)
print same_file('/etc/group',txt)
print same_file('/etc/passwd-',txt)
print build_dim([])
print build_dim(['a'])
print build_dim(['a','b'])

168
src/variable.py Normal file
View File

@ -0,0 +1,168 @@
#!/usr/bin/python
from irpf90_t import *
from util import *
import error
class Variable(object):
############################################################
def __init__(self,text,name = None):
assert isinstance(text,list)
assert len(text) > 0
assert isinstance(text[0],Line)
self.text = text
if name is not None:
self._name = name.lower()
############################################################
def name(self):
'''Name is lowercase'''
if '_name' not in self.__dict__:
buffer = None
for line in self.text:
if isinstance(line,Begin_provider):
buffer = line.text.replace(']',',').split(',')
break
assert buffer is not None
if len(buffer) < 3:
error.fail(line, "Error in Begin_provider line")
self._name = buffer[1].strip().lower()
return self._name
name = property(name)
############################################################
def doc(self):
if '_doc' not in self.__dict__:
def f(l): return
buffer = filter(lambda l:isinstance(l,Doc), self.text)
self._doc = map(lambda l: l.text[1:], buffer)
if buffer == []:
error.warn(None,"Variable %s is not documented"%(self.name))
return self._doc
doc = property(doc)
############################################################
def others(self):
if '_others' not in self.__dict__:
result = []
def f(l):
return isinstance(l,Begin_provider) or isinstance(l,Cont_provider)
lines = filter(f, self.text)
for line in lines:
buffer = line.text.replace(']',',').split(',')
if len(buffer) < 3:
error.fail(line,"Syntax Error")
buffer = buffer[1].strip().lower()
result.append(buffer)
result.remove(self.name)
self._others = result
return self._others
others = property(others)
############################################################
def same_as(self):
if '_same_as' not in self.__dict__:
if isinstance(self.line,Begin_provider):
result = None
else:
buffer = self.text[0].text.replace(']',',').split(',')
if len(buffer) < 3:
error.fail(line,"Syntax Error")
result = buffer[1].strip().lower()
self._same_as = result
return self._same_as
same_as = property(same_as)
############################################################
def allocate(self):
if '_allocate' not in self.__dict__:
from variables import variables
def f(var):
return variables[var].dim != []
self._allocate = filter ( f, self.others + [self.name] )
return self._allocate
allocate = property(allocate)
############################################################
def dim(self):
if '_dim' not in self.__dict__:
line = self.line.text
buffer = line.replace(']','').split(',',2)
if len(buffer) == 2:
self._dim = []
else:
buffer = buffer[2].strip()[1:-1].split(',')
self._dim = map(strip,buffer)
return self._dim
dim = property(dim)
############################################################
def type(self):
if '_type' not in self.__dict__:
line = self.line.text
buffer = line.split(',')[0]
buffer = buffer.split('[')[1].strip()
if self.dim != '':
buffer = "%s, allocatable"%(buffer)
self._type = buffer
return self._type
type = property(type)
############################################################
def fmodule(self):
if '_fmodule' not in self.__dict__:
self._fmodule = self.line.filename.replace('.irp.f','_mod')
return self._fmodule
fmodule = property(fmodule)
############################################################
def regexp(self):
if '_regexp' not in self.__dict__:
import re
self._regexp = re.compile( \
r"^.*[^a-z0-9'\"_]+%s([^a-z0-9_]|$)"%(self.name),re.I)
return self._regexp
regexp = property(regexp)
############################################################
def line(self):
if '_line' not in self.__dict__:
def f(l):
return isinstance(l,Begin_provider) or isinstance(l,Cont_provider)
lines = filter(f, self.text)
for line in lines:
buffer = line.text.replace(']',',').split(',')
if len(buffer) < 3:
error.fail(line,"Syntax Error")
buffer = buffer[1].strip().lower()
if self.name == buffer:
self._line = line
break
assert '_line' in self.__dict__
return self._line
line = property(line)
############################################################
def needs(self):
if '_needs' not in self.__dict__:
self._needs = None
return self._needs
needs = property(needs)
############################################################
def header(self):
if '_header' not in self.__dict__:
name = self.name
self._header = [
" %s :: %s %s"%(self.type, name, build_dim(self.dim) ),
" logical :: %s_is_built = .False."%(name),
]
return self._header
header = property(header)
######################################################################
if __name__ == '__main__':
from preprocessed_text import preprocessed_text
from variables import variables
print variables['elec_fitcusp_lapl'].doc

30
src/variables.py Normal file
View File

@ -0,0 +1,30 @@
#!/usr/bin/python
from variable import *
from irpf90_t import *
def create_variables():
from preprocessed_text import preprocessed_text
result = {}
for filename, text in preprocessed_text:
buffer = []
inside = False
for line in text:
if isinstance(line,Begin_provider):
inside = True
if inside:
buffer.append(line)
if isinstance(line,End_provider):
inside = False
v = Variable(buffer)
result[v.name] = v
for other in v.others:
result[other] = Variable(buffer,other)
buffer = []
return result
variables = create_variables()
if __name__ == '__main__':
for v in variables.keys():
print v