From 31547aac084977f77326588114e1a38c0dde0356 Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Fri, 4 Sep 2009 18:37:10 +0200 Subject: [PATCH] Added subroutines Version:1.1.3 --- src/preprocessed_text.py | 20 +++-------- src/subroutine.py | 72 ++++++++++++++++++++++++++++++++++++++++ src/subroutines.py | 29 ++++++++++++++++ src/util.py | 13 ++++++++ src/variable.py | 2 +- src/version.py | 2 +- 6 files changed, 120 insertions(+), 18 deletions(-) create mode 100644 src/subroutine.py create mode 100644 src/subroutines.py diff --git a/src/preprocessed_text.py b/src/preprocessed_text.py index 98a3bf4..ac7a55a 100644 --- a/src/preprocessed_text.py +++ b/src/preprocessed_text.py @@ -4,6 +4,7 @@ from irpf90_t import * from regexps import * import error from command_line import command_line +from util import * # Local regular expressions re_endif = re.compile("end\s+if") @@ -424,16 +425,7 @@ def irp_simple_statements(text): def process_subroutine(line): assert isinstance(line,Subroutine) - buffer = line.text.split('(') - if len(buffer) > 1: - buffer = " ".join(buffer[:-1]) - else: - buffer = buffer[0] - buffer = buffer.lower().split() - if len(buffer) != 2: - print buffer - error.fail(line,"Error in Subroutine statement") - subname = buffer[1] + subname = find_subname(line) length = len(subname) i = line.i f = line.filename @@ -449,12 +441,8 @@ def irp_simple_statements(text): assert isinstance(line,Function) buffer = line.text.split('(') if (len(buffer) < 2): - error.fail(line,"Error in Function statement") - buffer = " ".join(buffer[:-1]) - buffer = buffer.lower().split() - if len(buffer) < 2: - error.fail(line,"Error in Function statement") - subname = buffer[-1] + error.fail(line,"Syntax error") + subname = find_subname(line) length = len(subname) i = line.i f = line.filename diff --git a/src/subroutine.py b/src/subroutine.py new file mode 100644 index 0000000..78a5c5a --- /dev/null +++ b/src/subroutine.py @@ -0,0 +1,72 @@ +#!/usr/bin/python + +from irpf90_t import * +from util import * +import error + +class Sub(object): + + ############################################################ + def __init__(self,text): + assert isinstance(text,list) + assert len(text) > 0 + assert isinstance(text[0],Subroutine) or isinstance(text[0],Function) + self.text = text + + ############################################################ + def name(self): + '''Name is lowercase''' + if '_name' not in self.__dict__: + self._name = find_subname(self.line) + 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,"Subroutine %s is not documented"%(self.name)) + return self._doc + doc = property(doc) + + ############################################################ + def line(self): + if '_line' not in self.__dict__: + self._line = self.text[0] + return self._line + line = property(line) + + ############################################################ + def touches(self): + if '_touches' not in self.__dict__: + self._touches = filter(lambda x: isinstance(x,Touch),self.text) != [] + return self._touches + + ############################################################ + 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 calls(self): + if '_calls' not in self.__dict__: + buffer = filter(lambda x: isinstance(x,Call),self.text) + self._calls = [] + for line in buffer: + sub = line.text.split('(',1)[0].split()[1] + self._calls.append(sub) + return self._calls + calls = property(calls) + +###################################################################### +if __name__ == '__main__': + from preprocessed_text import preprocessed_text + from subroutines import subroutines + print subroutines['invert'].calls diff --git a/src/subroutines.py b/src/subroutines.py new file mode 100644 index 0000000..77c8cc9 --- /dev/null +++ b/src/subroutines.py @@ -0,0 +1,29 @@ +#!/usr/bin/python + +from subroutine import * +from irpf90_t import * + +def create_subroutines(): + from preprocessed_text import preprocessed_text + result = {} + for filename, text in preprocessed_text: + buffer = [] + inside = False + for line in text: + if isinstance(line,Subroutine): + inside = True + if inside: + buffer.append(line) + if isinstance(line,End): + if inside: + v = Sub(buffer) + result[v.name] = v + buffer = [] + inside = False + return result + +subroutines = create_subroutines() + +if __name__ == '__main__': + for v in subroutines.keys(): + print v diff --git a/src/util.py b/src/util.py index 3ae9534..0b4aa71 100644 --- a/src/util.py +++ b/src/util.py @@ -26,6 +26,19 @@ def build_dim(dim): else: return "(%s)"%( ",".join(dim) ) + +def find_subname(line): + buffer = line.text.split('(') + if len(buffer) > 1: + buffer = " ".join(buffer[:-1]) + else: + buffer = buffer[0] + buffer = buffer.lower().split() + if len(buffer) < 2: + error.fail(line,"Syntax Error") + return buffer[-1] + + if __name__ == '__main__': print build_dim([]) print build_dim(['a']) diff --git a/src/variable.py b/src/variable.py index db4aa7b..9b0cc73 100644 --- a/src/variable.py +++ b/src/variable.py @@ -10,7 +10,7 @@ class Variable(object): def __init__(self,text,name = None): assert isinstance(text,list) assert len(text) > 0 - assert isinstance(text[0],Line) + assert isinstance(text[0],Begin_provider) self.text = text if name is not None: self._name = name.lower() diff --git a/src/version.py b/src/version.py index e673db0..a67d063 100644 --- a/src/version.py +++ b/src/version.py @@ -1 +1 @@ -version = "1.1.2" +version = "1.1.3"