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

feat: Build for loops and comprehensions #68

Merged
merged 6 commits into from
Jan 16, 2024
Merged

Conversation

mark-koch
Copy link
Collaborator

  • Refactor the ast_util.py file and provide a new template_replace method to easily create AST nodes.
  • Build for loops and list comprehensions into CFGs using this template API

Copy link
Contributor

@cqc-alec cqc-alec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

illegals = find_nodes(is_illegal_in_list_comp, node)
if illegals:
raise GuppyError(
"Expression is not supported inside a list comprehension", illegals[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we maybe include all of them and not just the zeroth?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice, but at the moment we can only report a single location that is highlighted in the error message (see for example here)

@ss2165 ss2165 added this to the Release v0.1 milestone Jan 10, 2024
"""
it = make_var(next(tmp_vars), g.iter)
b = make_var(next(tmp_vars), g.iter)
[gen.iter_assign, gen.hasnext_assign, gen.next_assign] = cast(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this example a lot less convincing a use of template_replace than the last one. I imagine something like

gen.iter_assign = make_assignment(it, MakeIter(value=g.iter, origin_node=node))
gen.hasnext_assign = make_assignment( (b, it), with_loc(it, IterHasNext(value=it)) )
gen.next_assign = make_assignment( (x, it), with_loc(it, IterNext(value-=it)) )

which seems a whole lot shorter and with a whole lot easier to see what ends up where without having to thread the value through kwarg-variable into the template. Now I'm sure that the practice may not be as easy as my hypothetical make_assignment (to a tuple of variables!!), but....how much worse? I'm not convinced this template_replace really pulls its weight?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to a make_assign helper function 👍

@mark-koch mark-koch requested a review from acl-cqc January 15, 2024 17:41
@mark-koch mark-koch self-assigned this Jan 16, 2024
replacements: Mapping[str, ast.AST | Sequence[ast.AST]]
default_loc: ast.AST

def __init__(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note I think you could just make this an @dataclass and avoid the constructor. (@dataclass(eq=False, frozen=True) or similar if you want to)

Copy link
Contributor

@acl-cqc acl-cqc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I hadn't thought about the extra with_locs required, but still looks good to me, thanks :-)

@mark-koch mark-koch merged commit e1e4da3 into feat/lists Jan 16, 2024
1 check passed
@mark-koch mark-koch deleted the lists/build branch January 16, 2024 12:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants