mirror of
https://gitlab.com/scemama/irpf90.git
synced 2024-12-22 04:13:33 +01:00
Improved pased_text
This commit is contained in:
parent
fa48e8966f
commit
e1fb434bc8
@ -265,7 +265,7 @@ def create_irpf90_files():
|
|||||||
import os
|
import os
|
||||||
if command_line.do_run:
|
if command_line.do_run:
|
||||||
def is_irpf90_file(filename):
|
def is_irpf90_file(filename):
|
||||||
return filename.endswith(".irp.f")
|
return filename.endswith(".irp.f") and filename.startswith("p")
|
||||||
result = filter ( is_irpf90_file, os.listdir(os.getcwd()) )
|
result = filter ( is_irpf90_file, os.listdir(os.getcwd()) )
|
||||||
return result
|
return result
|
||||||
irpf90_files = create_irpf90_files()
|
irpf90_files = create_irpf90_files()
|
||||||
|
@ -6,6 +6,7 @@ from variables import variables
|
|||||||
from preprocessed_text import preprocessed_text
|
from preprocessed_text import preprocessed_text
|
||||||
from subroutines import subroutines
|
from subroutines import subroutines
|
||||||
import regexps
|
import regexps
|
||||||
|
import error
|
||||||
|
|
||||||
def find_variables_in_line(line):
|
def find_variables_in_line(line):
|
||||||
assert isinstance(line,Line)
|
assert isinstance(line,Line)
|
||||||
@ -17,6 +18,32 @@ def find_variables_in_line(line):
|
|||||||
result.append(var.name)
|
result.append(var.name)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def find_subroutine_in_line(line):
|
||||||
|
assert isinstance(line,Call)
|
||||||
|
buffer = regexps.re_string.sub('',line.text)
|
||||||
|
result = None
|
||||||
|
for v in subroutines.keys():
|
||||||
|
sub = subroutines[v]
|
||||||
|
if sub.regexp.search(buffer) is not None:
|
||||||
|
result = sub.name
|
||||||
|
break
|
||||||
|
assert result is not None
|
||||||
|
return result
|
||||||
|
|
||||||
|
def check_touch(vars,main_vars):
|
||||||
|
def fun(main_var):
|
||||||
|
if main_var not in variables:
|
||||||
|
error.fail(line,"Variable %s unknown"%(main_var,))
|
||||||
|
x = variables[main_var]
|
||||||
|
return [main_var]+x.others
|
||||||
|
all_others = flatten( map(fun,main_vars) )
|
||||||
|
all_others.sort()
|
||||||
|
if len(all_others) == len(vars):
|
||||||
|
for x,y in zip(vars,all_others):
|
||||||
|
if x != y:
|
||||||
|
message = "The following entities should be touched:\n"
|
||||||
|
message = "\n".join([message]+map(lambda x: "- %s"%(x,),all_others))
|
||||||
|
error.fail(line,message)
|
||||||
|
|
||||||
def get_parsed_text():
|
def get_parsed_text():
|
||||||
result = []
|
result = []
|
||||||
@ -50,7 +77,52 @@ def get_parsed_text():
|
|||||||
elif isinstance(line,Provide):
|
elif isinstance(line,Provide):
|
||||||
l = line.text.split()[1:]
|
l = line.text.split()[1:]
|
||||||
varlist += l
|
varlist += l
|
||||||
temp_result.append( (l,line) )
|
temp_result.append( (l,Simple_line(line.i,"!%s"%(line.text),line.filename)) )
|
||||||
|
elif isinstance(line,Call):
|
||||||
|
sub = find_subroutine_in_line(line)
|
||||||
|
if subroutines[sub].touches == []:
|
||||||
|
t = Simple_line
|
||||||
|
else:
|
||||||
|
t = Provide_all
|
||||||
|
temp_result.append( ([],t(line.i,line.text,line.filename)) )
|
||||||
|
elif isinstance(line,Free):
|
||||||
|
vars = line.text.split()
|
||||||
|
if len(vars) < 2:
|
||||||
|
error.fail(line,"Syntax error")
|
||||||
|
vars = vars[1:]
|
||||||
|
result.append( ([],Simple_line(line.i,"!%s"%(line.text),line.filename)) )
|
||||||
|
for var in vars:
|
||||||
|
temp_result.append( ([],Simple_line(line.i," call free_%s"%var,
|
||||||
|
line.filename)) )
|
||||||
|
elif isinstance(line,Touch):
|
||||||
|
vars = line.text.split()
|
||||||
|
if len(vars) < 2:
|
||||||
|
error.fail(line,"Syntax error")
|
||||||
|
vars = vars[1:]
|
||||||
|
def fun(x):
|
||||||
|
if x not in variables:
|
||||||
|
error.fail(line,"Variable %s unknown"%(x,))
|
||||||
|
main = variables[x].same_as
|
||||||
|
if main is None:
|
||||||
|
main = x
|
||||||
|
return main
|
||||||
|
main_vars = make_single( map(fun, vars) )
|
||||||
|
check_touch(vars,main_vars)
|
||||||
|
txt = " ".join(vars)
|
||||||
|
result = [ ([],Simple_line(line.i,"!",line.filename)),
|
||||||
|
([],Simple_line(line.i,"! >>> TOUCH %s"%(txt,),line.filename)) ]
|
||||||
|
def fun(x):
|
||||||
|
if x not in variables:
|
||||||
|
error.fail(line,"Variable %s unknown"%(x,))
|
||||||
|
return [ ([],Simple_line(line.i," call touch_%s"%(x,),line.filename)),
|
||||||
|
([],Simple_line(line.i," %s_is_built = .True."%(x,),line.filename)) ]
|
||||||
|
result += map( fun, main_vars )
|
||||||
|
def fun(x):
|
||||||
|
if x not in variables:
|
||||||
|
error.fail(line,"Variable %s unknown"%(x,))
|
||||||
|
return [ ([],Simple_line(line.i," %s_is_built = .True."%(x,),line.filename)) ]
|
||||||
|
result += map( fun, main_vars )
|
||||||
|
result += [ ([],Provide_all(line.i,"! <<< END TOUCH",line.filename)) ]
|
||||||
else:
|
else:
|
||||||
l = find_variables_in_line(line)
|
l = find_variables_in_line(line)
|
||||||
varlist += l
|
varlist += l
|
||||||
|
@ -206,9 +206,7 @@ def form(text):
|
|||||||
re2 = re.compile(r"^\s*[!#]")
|
re2 = re.compile(r"^\s*[!#]")
|
||||||
re3 = re.compile(r"^\s*[^ 0-9]+")
|
re3 = re.compile(r"^\s*[^ 0-9]+")
|
||||||
for line in text:
|
for line in text:
|
||||||
if isinstance(line,Empty_line) or \
|
if type(line) in [ Empty_line, Doc, Openmp ]:
|
||||||
isinstance(line,Doc) or \
|
|
||||||
isinstance(line,Openmp):
|
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
if len(line.text) > 5:
|
if len(line.text) > 5:
|
||||||
@ -237,8 +235,7 @@ def remove_comments(text,form):
|
|||||||
|
|
||||||
if form == Free_form:
|
if form == Free_form:
|
||||||
for line in text:
|
for line in text:
|
||||||
if isinstance(line,Openmp) or \
|
if type(line) in [ Openmp, Doc] :
|
||||||
isinstance(line,Doc):
|
|
||||||
result.append(line)
|
result.append(line)
|
||||||
elif isinstance(line,Empty_line):
|
elif isinstance(line,Empty_line):
|
||||||
pass
|
pass
|
||||||
@ -252,8 +249,7 @@ def remove_comments(text,form):
|
|||||||
return result
|
return result
|
||||||
else:
|
else:
|
||||||
for line in text:
|
for line in text:
|
||||||
if isinstance(line,Openmp) or \
|
if type(line) in [ Openmp, Doc ]:
|
||||||
isinstance(line,Doc):
|
|
||||||
result.append(line)
|
result.append(line)
|
||||||
elif isinstance(line,Empty_line):
|
elif isinstance(line,Empty_line):
|
||||||
pass
|
pass
|
||||||
@ -513,8 +509,7 @@ def process_old_style_do(text):
|
|||||||
def change_matching_enddo(begin,number):
|
def change_matching_enddo(begin,number):
|
||||||
for i in range(begin+1,len(text)):
|
for i in range(begin+1,len(text)):
|
||||||
line = text[i]
|
line = text[i]
|
||||||
if isinstance(line,Continue) or \
|
if type(line) in [Continue,Enddo]:
|
||||||
isinstance(line,Enddo):
|
|
||||||
buffer = line.text.split()
|
buffer = line.text.split()
|
||||||
if buffer[0] == number:
|
if buffer[0] == number:
|
||||||
text[i] = Enddo(line.i," enddo",line.filename)
|
text[i] = Enddo(line.i," enddo",line.filename)
|
||||||
@ -586,13 +581,9 @@ endif'''
|
|||||||
def check_begin_end(text):
|
def check_begin_end(text):
|
||||||
'''Checks x...endx consistence'''
|
'''Checks x...endx consistence'''
|
||||||
|
|
||||||
def filter_line(line):
|
filter_line = lambda line: type(line) in [ Do, Enddo, If, Endif, \
|
||||||
for type in [ Do, Enddo, If, Endif, Begin_provider, End_provider, \
|
Begin_provider, End_provider, \
|
||||||
Subroutine, Function, End, Begin_doc, End_doc ]:
|
Subroutine, Function, End, Begin_doc, End_doc ]
|
||||||
if isinstance(line,type):
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
text = filter(filter_line, text)
|
text = filter(filter_line, text)
|
||||||
|
|
||||||
d = { 'do' : Do, 'enddo': Enddo,
|
d = { 'do' : Do, 'enddo': Enddo,
|
||||||
@ -610,8 +601,7 @@ def check_begin_end(text):
|
|||||||
level -= 1
|
level -= 1
|
||||||
if level == 0:
|
if level == 0:
|
||||||
return True
|
return True
|
||||||
elif isinstance(line,End) or \
|
elif type(line) in [End, End_provider]:
|
||||||
isinstance(line,End_provider):
|
|
||||||
break
|
break
|
||||||
error.fail(text[begin],"Missing 'end%s'"%(x,))
|
error.fail(text[begin],"Missing 'end%s'"%(x,))
|
||||||
|
|
||||||
@ -620,9 +610,8 @@ def check_begin_end(text):
|
|||||||
line = text[i]
|
line = text[i]
|
||||||
if isinstance(line,x):
|
if isinstance(line,x):
|
||||||
return
|
return
|
||||||
for t in [ Subroutine, Function, Begin_provider ]:
|
if type(line) in [ Subroutine, Function, Begin_provider ]:
|
||||||
if isinstance(line,t):
|
error.fail(text[begin],"Subroutine/Function/Provider is not closed")
|
||||||
error.fail(text[begin],"Subroutine/Function/Provider is not closed")
|
|
||||||
error.fail(text[begin],"Subroutine/Function/Provider is not closed")
|
error.fail(text[begin],"Subroutine/Function/Provider is not closed")
|
||||||
|
|
||||||
|
|
||||||
@ -679,9 +668,7 @@ def move_to_top(text,t):
|
|||||||
begin = -1
|
begin = -1
|
||||||
for i in range(len(text)):
|
for i in range(len(text)):
|
||||||
line = text[i]
|
line = text[i]
|
||||||
if isinstance(line,Begin_provider) or \
|
if type(line) in [ Begin_provider, Subroutine, Function ]:
|
||||||
isinstance(line,Subroutine) or \
|
|
||||||
isinstance(line,Function):
|
|
||||||
begin = i
|
begin = i
|
||||||
elif isinstance(line,t):
|
elif isinstance(line,t):
|
||||||
text.pop(i)
|
text.pop(i)
|
||||||
|
@ -10,7 +10,7 @@ class Sub(object):
|
|||||||
def __init__(self,text):
|
def __init__(self,text):
|
||||||
assert isinstance(text,list)
|
assert isinstance(text,list)
|
||||||
assert len(text) > 0
|
assert len(text) > 0
|
||||||
assert isinstance(text[0],Subroutine) or isinstance(text[0],Function)
|
assert type(text[0]) in [Subroutine, Function]
|
||||||
self.text = text
|
self.text = text
|
||||||
|
|
||||||
############################################################
|
############################################################
|
||||||
|
20
src/util.py
20
src/util.py
@ -44,8 +44,22 @@ def make_single(l):
|
|||||||
d[x] = True
|
d[x] = True
|
||||||
return d.keys()
|
return d.keys()
|
||||||
|
|
||||||
|
def flatten(l):
|
||||||
|
if isinstance(l,list):
|
||||||
|
result = []
|
||||||
|
for i in range(len(l)):
|
||||||
|
elem = l[i]
|
||||||
|
result += flatten(elem)
|
||||||
|
return result
|
||||||
|
else:
|
||||||
|
return [l]
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print build_dim([])
|
a = 0
|
||||||
print build_dim(['a'])
|
print flatten(a)
|
||||||
print build_dim(['a','b'])
|
a = []
|
||||||
|
print flatten(a)
|
||||||
|
a = [1,2,3,4]
|
||||||
|
print flatten(a)
|
||||||
|
a = [1,2,3,[4,5,6,[7,8,9],10,],11,12,[13,14],15,16]
|
||||||
|
print flatten(a)
|
||||||
|
@ -46,8 +46,7 @@ class Variable(object):
|
|||||||
def others(self):
|
def others(self):
|
||||||
if '_others' not in self.__dict__:
|
if '_others' not in self.__dict__:
|
||||||
result = []
|
result = []
|
||||||
def f(l):
|
f = lambda l: type(l) in [Begin_provider, Cont_provider]
|
||||||
return isinstance(l,Begin_provider) or isinstance(l,Cont_provider)
|
|
||||||
lines = filter(f, self.text)
|
lines = filter(f, self.text)
|
||||||
for line in lines:
|
for line in lines:
|
||||||
buffer = line.text.replace(']',',').split(',')
|
buffer = line.text.replace(']',',').split(',')
|
||||||
@ -128,8 +127,7 @@ class Variable(object):
|
|||||||
############################################################
|
############################################################
|
||||||
def line(self):
|
def line(self):
|
||||||
if '_line' not in self.__dict__:
|
if '_line' not in self.__dict__:
|
||||||
def f(l):
|
f = lambda l: type(l) in [Begin_provider, Cont_provider]
|
||||||
return isinstance(l,Begin_provider) or isinstance(l,Cont_provider)
|
|
||||||
lines = filter(f, self.text)
|
lines = filter(f, self.text)
|
||||||
for line in lines:
|
for line in lines:
|
||||||
buffer = line.text.replace(']',',').split(',')
|
buffer = line.text.replace(']',',').split(',')
|
||||||
|
Loading…
Reference in New Issue
Block a user