Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor & cleanup Grammar.field_index #75

Open
BenWeber42 opened this issue Jan 29, 2021 · 0 comments
Open

Refactor & cleanup Grammar.field_index #75

BenWeber42 opened this issue Jan 29, 2021 · 0 comments
Labels
large Requires large effort/time med-prio Medium priority issue/task tech-debt Technical debt that is overdue

Comments

@BenWeber42
Copy link
Contributor

The Grammar.field_index has become way too complex. It's a key piece for the location semantics, but its complexity hinders development severely:

dusk/dusk/grammar.py

Lines 509 to 594 in d6bbfaf

@transform(
OneOf(
Tuple(
elts=FixedList(
Capture(OneOf(Compare, Name)).to("hindex"),
Capture(expr).to("vindex"),
),
ctx=Load,
),
# FIXME: ensure built-ins (like `Edge`) aren't _shadowed_ by variables
# TODO: hardcoded string
Capture(OneOf(Compare, name(OneOf("Edge", "Cell", "Vertex")))).to("hindex"),
Capture(expr).to("vindex"),
None,
)
)
def field_index(self, field: DuskField, vindex=None, hindex=None):
voffset, vbase = (
self.relative_vertical_offset(vindex) if vindex is not None else (0, None)
)
include_center, hindex = (
self.location_chain(hindex) if hindex is not None else (False, None)
)
if not self.ctx.location.in_neighbor_iteration:
if hindex is not None:
raise DuskSyntaxError(
f"Invalid horizontal index for field '{field.sir.name}' "
"outside of neighbor iteration!"
)
return make_unstructured_offset(False), voffset, vbase
neighbor_chain = self.ctx.location.current_neighbor_iteration.chain
field_dimension = self.ctx.location.get_field_dimension(field.sir)
if hindex is not None and not self.ctx.location.is_dense(field_dimension):
raise DuskSyntaxError(
f"Invalid horizontal offset for field '{field.sir.name}',"
"sparse fields do not take an offset (only non-offset read makes sense)!"
)
# TODO: `vindex` is _non-sensical_ if the field is 2d
# TODO: we should check that `field_dimension` is valid for
# the current neighbor iteration(s?)
if hindex is None:
if self.ctx.location.is_dense(field_dimension):
if self.ctx.location.is_ambiguous(neighbor_chain):
raise DuskSyntaxError(
f"Field '{field.sir.name}' requires a horizontal index "
"inside of ambiguous neighbor iteration!"
)
return (
make_unstructured_offset(field_dimension[0] == neighbor_chain[-1]),
voffset,
vbase,
)
else:
return make_unstructured_offset(False), voffset, vbase
# TODO: check if `hindex` is valid for this field's location type
if (
self.ctx.location.current_neighbor_iteration.include_center
!= include_center
):
raise DuskSyntaxError(
f"Invalid horizontal offset for field '{field.sir.name}'! "
"inconsistent center inclusion"
)
if len(hindex) == 1:
if neighbor_chain[0] != hindex[0]:
raise DuskSyntaxError(
f"Invalid horizontal offset for field '{field.sir.name}'!"
)
return make_unstructured_offset(False), voffset, vbase
if hindex != neighbor_chain:
raise DuskSyntaxError(
f"Invalid horizontal offset for field '{field.sir.name}'!"
)
return make_unstructured_offset(True), voffset, vbase

@BenWeber42 BenWeber42 added med-prio Medium priority issue/task tech-debt Technical debt that is overdue large Requires large effort/time labels Jan 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
large Requires large effort/time med-prio Medium priority issue/task tech-debt Technical debt that is overdue
Projects
None yet
Development

No branches or pull requests

1 participant