Skip to content
Snippets Groups Projects
Commit b75f8abd authored by Andreas Klöckner's avatar Andreas Klöckner
Browse files

Fortran parsing improvements

parent 072372c3
No related branches found
No related tags found
No related merge requests found
......@@ -26,7 +26,8 @@ from loopy.diagnostic import LoopyError
def f2loopy(source, free_form=True, strict=True,
pre_transform_code=None, use_c_preprocessor=False,
pre_transform_code=None, pre_transform_code_context=None,
use_c_preprocessor=False,
file_name="<floopy code>"):
if use_c_preprocessor:
try:
......@@ -60,9 +61,10 @@ def f2loopy(source, free_form=True, strict=True,
analyze=False, ignore_comments=False)
from loopy.frontend.fortran.translator import F2LoopyTranslator
f2loopy = F2LoopyTranslator()
f2loopy = F2LoopyTranslator(file_name)
f2loopy(tree)
return f2loopy.make_kernels(pre_transform_code=pre_transform_code)
return f2loopy.make_kernels(pre_transform_code=pre_transform_code,
pre_transform_code_context=pre_transform_code_context)
# vim: foldmethod=marker
......@@ -214,7 +214,7 @@ def remove_common_indentation(lines):
# {{{ translator
class F2LoopyTranslator(FTreeWalkerBase):
def __init__(self):
def __init__(self, filename):
FTreeWalkerBase.__init__(self)
self.scope_stack = []
......@@ -234,6 +234,8 @@ class F2LoopyTranslator(FTreeWalkerBase):
self.transform_code_lines = []
self.filename = filename
def add_expression_instruction(self, lhs, rhs):
scope = self.scope_stack[-1]
......@@ -331,7 +333,7 @@ class F2LoopyTranslator(FTreeWalkerBase):
tp = self.dtype_from_stmt(node)
for name, shape in self.parse_dimension_specs(node.entity_decls):
for name, shape in self.parse_dimension_specs(node, node.entity_decls):
if shape is not None:
assert name not in scope.dim_map
scope.dim_map[name] = shape
......@@ -350,7 +352,7 @@ class F2LoopyTranslator(FTreeWalkerBase):
def map_Dimension(self, node):
scope = self.scope_stack[-1]
for name, shape in self.parse_dimension_specs(node.items):
for name, shape in self.parse_dimension_specs(node, node.items):
if shape is not None:
assert name not in scope.dim_map
scope.dim_map[name] = shape
......@@ -369,7 +371,7 @@ class F2LoopyTranslator(FTreeWalkerBase):
for name, data in node.stmts:
name, = name
assert name not in scope.data
scope.data[name] = [self.parse_expr(i) for i in data]
scope.data[name] = [self.parse_expr(node, i) for i in data]
return []
......@@ -399,7 +401,7 @@ class F2LoopyTranslator(FTreeWalkerBase):
scope = self.scope_stack[-1]
lhs = scope.process_expression_for_loopy(
self.parse_expr(node.variable))
self.parse_expr(node, node.variable))
from pymbolic.primitives import Subscript, Call
if isinstance(lhs, Call):
raise TranslationError("function call (to '%s') on left hand side of"
......@@ -411,7 +413,7 @@ class F2LoopyTranslator(FTreeWalkerBase):
scope.use_name(lhs_name)
rhs = scope.process_expression_for_loopy(self.parse_expr(node.expr))
rhs = scope.process_expression_for_loopy(self.parse_expr(node, node.expr))
self.add_expression_instruction(lhs, rhs)
......@@ -425,9 +427,7 @@ class F2LoopyTranslator(FTreeWalkerBase):
raise NotImplementedError("save")
def map_Line(self, node):
#from warnings import warn
#warn("Encountered a 'line': %s" % node)
raise NotImplementedError
pass
def map_Program(self, node):
raise NotImplementedError
......@@ -467,7 +467,7 @@ class F2LoopyTranslator(FTreeWalkerBase):
cond_var = var(cond_name)
self.add_expression_instruction(
cond_var, self.parse_expr(node.expr))
cond_var, self.parse_expr(node, node.expr))
self.conditions.append(cond_name)
......@@ -489,6 +489,7 @@ class F2LoopyTranslator(FTreeWalkerBase):
loop_var = loop_var.strip()
scope.use_name(loop_var)
loop_bounds = self.parse_expr(
node,
loop_bounds, min_precedence=self.expr_parser._PREC_FUNC_ARGS)
if len(loop_bounds) == 2:
......@@ -627,12 +628,16 @@ class F2LoopyTranslator(FTreeWalkerBase):
# }}}
def make_kernels(self, pre_transform_code=None):
def make_kernels(self, pre_transform_code=None, pre_transform_code_context=None):
kernel_names = [
sub.subprogram_name
for sub in self.kernels]
proc_dict = {}
if pre_transform_code_context is None:
proc_dict = {}
else:
proc_dict = pre_transform_code_context.copy()
proc_dict["lp"] = lp
proc_dict["np"] = np
......
......@@ -24,6 +24,8 @@ THE SOFTWARE.
import re
from loopy.diagnostic import LoopyError
class FTreeWalkerBase(object):
def __init__(self):
......@@ -53,13 +55,13 @@ class FTreeWalkerBase(object):
r"^(?P<name>[_0-9a-zA-Z]+)"
"(\((?P<shape>[-+*0-9:a-zA-Z, \t]+)\))?$")
def parse_dimension_specs(self, dim_decls):
def parse_dimension_specs(self, node, dim_decls):
def parse_bounds(bounds_str):
start_end = bounds_str.split(":")
assert 1 <= len(start_end) <= 2
return [self.parse_expr(s) for s in start_end]
return [self.parse_expr(node, s) for s in start_end]
for decl in dim_decls:
entity_match = self.ENTITY_RE.match(decl)
......@@ -81,8 +83,13 @@ class FTreeWalkerBase(object):
# {{{ expressions
def parse_expr(self, expr_str, **kwargs):
return self.expr_parser(expr_str, **kwargs)
def parse_expr(self, node, expr_str, **kwargs):
try:
return self.expr_parser(expr_str, **kwargs)
except Exception as e:
raise LoopyError(
"Error parsing expression '%s' on line %d of '%s': %s"
% (expr_str, node.item.span[0], self.filename, str(e)))
# }}}
......
[flake8]
ignore = E126,E127,E128,E123,E226,E241,E242,E265
ignore = E126,E127,E128,E123,E226,E241,E242,E265,N802
max-line-length=85
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment