-
Notifications
You must be signed in to change notification settings - Fork 2
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: Add a unified definition system #179
Conversation
@@ -0,0 +1,116 @@ | |||
import ast |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the main file laying out the compilation fow for definitions
guppylang/definition/declaration.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file was mostly copied from the old guppylang/declared.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file was mostly copied from guppylang/checker/func_checker.py
and guppylang/compiler/func_compiler.py
guppylang/definition/value.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file was mostly copied from guppylang/checker/core.py
and guppylang/compiler/core.py
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #179 +/- ##
==========================================
+ Coverage 90.55% 90.67% +0.11%
==========================================
Files 41 46 +5
Lines 4595 4717 +122
==========================================
+ Hits 4161 4277 +116
- Misses 434 440 +6 ☔ View full report in Codecov by Sentry. |
guppylang/prelude/builtins.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, we replace the old bool(x)
, int(x)
etc functions with a __new__(x)
constructor
return with_loc(node, GlobalName(id=x, def_id=defn.id)), defn.ty | ||
# For types, we return their `__new__` constructor | ||
case TypeDef() as defn if constr := self.ctx.globals.get_instance_func( | ||
defn, "__new__" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So for Qubit
, this is doing a QAlloc
whenever we find a name? I'm not 100% sure when I should expect to find Name
s that stand for types here. The obvious example is x = Qubit()
- I hope everything else is covered by stmt_checker.py
(like AnnAssign and type signatures on functions)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is also int(x)
which takes everthing that implements __int__
, same for float(x)
and bool(x)
.
Before, these were defined as functions, but now the names is already taken by the type.
Python handles this in the same way: int
is a class/type and "calling" a class executes the constructor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but I'm only worried about the Qubit
case because adding unnecessary QAlloc
s (when checking type signatrues for example) will mess up any qubit resource estimates!
However, I'm fairly satisfied we're doing the right thing here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see. Yes, Name
nodes in types are handled by different code in tys.parsing
Definition
ABC for all user-defined things on module-level. Currently, these are functions, types, and type vars.guppylang.definition
Globals
container is restructured accordingly.int(x)
,bool(x)
,qubit()
, etc. functions are replaced with calls to the__new__
constructor of the type.ParsableDef -> CheckableDef -> CompilableDef -> CompiledDef
RawDef < ParsedDef < CheckedDef < CompiledDef
Closes #175 and also closes #170 (as a drive-by)