This repository was archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
expression typeinfo implemented #586
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,17 +4,20 @@ | |
|
||
import ast | ||
import os | ||
from types import MethodType | ||
|
||
from jaclang.compiler.absyntree import AstNode | ||
from jaclang.compiler.passes import Pass | ||
from jaclang.compiler.passes.main.fuse_typeinfo_pass import ( | ||
FuseTypeInfoPass, | ||
) | ||
from jaclang.utils.helpers import pascal_to_snake | ||
|
||
import mypy.build as myb | ||
import mypy.checkexpr as mycke | ||
import mypy.errors as mye | ||
import mypy.fastparse as myfp | ||
import mypy.nodes as mypy_nodes | ||
from mypy.build import BuildSource | ||
from mypy.build import BuildSourceSet | ||
from mypy.build import FileSystemCache | ||
|
@@ -29,6 +32,55 @@ | |
from mypy.semanal_main import semantic_analysis_for_scc | ||
|
||
|
||
# All the expression nodes of mypy. | ||
EXPRESSION_NODES = ( | ||
mypy_nodes.AssertTypeExpr, | ||
mypy_nodes.AssignmentExpr, | ||
mypy_nodes.AwaitExpr, | ||
mypy_nodes.BytesExpr, | ||
mypy_nodes.CallExpr, | ||
mypy_nodes.CastExpr, | ||
mypy_nodes.ComparisonExpr, | ||
mypy_nodes.ComplexExpr, | ||
mypy_nodes.ConditionalExpr, | ||
mypy_nodes.DictionaryComprehension, | ||
mypy_nodes.DictExpr, | ||
mypy_nodes.EllipsisExpr, | ||
mypy_nodes.EnumCallExpr, | ||
mypy_nodes.Expression, | ||
mypy_nodes.FloatExpr, | ||
mypy_nodes.GeneratorExpr, | ||
mypy_nodes.IndexExpr, | ||
mypy_nodes.IntExpr, | ||
mypy_nodes.LambdaExpr, | ||
mypy_nodes.ListComprehension, | ||
mypy_nodes.ListExpr, | ||
mypy_nodes.MemberExpr, | ||
mypy_nodes.NamedTupleExpr, | ||
mypy_nodes.NameExpr, | ||
mypy_nodes.NewTypeExpr, | ||
mypy_nodes.OpExpr, | ||
mypy_nodes.ParamSpecExpr, | ||
mypy_nodes.PromoteExpr, | ||
mypy_nodes.RefExpr, | ||
mypy_nodes.RevealExpr, | ||
mypy_nodes.SetComprehension, | ||
mypy_nodes.SetExpr, | ||
mypy_nodes.SliceExpr, | ||
mypy_nodes.StarExpr, | ||
mypy_nodes.StrExpr, | ||
mypy_nodes.SuperExpr, | ||
mypy_nodes.TupleExpr, | ||
mypy_nodes.TypeAliasExpr, | ||
mypy_nodes.TypedDictExpr, | ||
mypy_nodes.TypeVarExpr, | ||
mypy_nodes.TypeVarTupleExpr, | ||
mypy_nodes.UnaryExpr, | ||
mypy_nodes.YieldExpr, | ||
mypy_nodes.YieldFromExpr, | ||
) | ||
|
||
|
||
mypy_to_jac_node_map: dict[ | ||
tuple[int, int | None, int | None, int | None], list[AstNode] | ||
] = {} | ||
|
@@ -87,63 +139,45 @@ def __init__( | |
"""Override to mypy expression checker for direct AST pass through.""" | ||
super().__init__(tc, msg, plugin, per_line_checking_time_ns) | ||
|
||
def visit_list_expr(self, e: mycke.ListExpr) -> mycke.Type: | ||
"""Type check a list expression [...].""" | ||
out = super().visit_list_expr(e) | ||
FuseTypeInfoPass.node_type_hash[e] = out | ||
return out | ||
|
||
def visit_set_expr(self, e: mycke.SetExpr) -> mycke.Type: | ||
"""Type check a set expression {...}.""" | ||
out = super().visit_set_expr(e) | ||
FuseTypeInfoPass.node_type_hash[e] = out | ||
return out | ||
|
||
def visit_tuple_expr(self, e: myfp.TupleExpr) -> myb.Type: | ||
"""Type check a tuple expression (...).""" | ||
out = super().visit_tuple_expr(e) | ||
FuseTypeInfoPass.node_type_hash[e] = out | ||
return out | ||
|
||
def visit_dict_expr(self, e: myfp.DictExpr) -> myb.Type: | ||
"""Type check a dictionary expression {...}.""" | ||
out = super().visit_dict_expr(e) | ||
FuseTypeInfoPass.node_type_hash[e] = out | ||
return out | ||
|
||
def visit_list_comprehension(self, e: myfp.ListComprehension) -> myb.Type: | ||
"""Type check a list comprehension.""" | ||
out = super().visit_list_comprehension(e) | ||
FuseTypeInfoPass.node_type_hash[e] = out | ||
return out | ||
|
||
def visit_set_comprehension(self, e: myfp.SetComprehension) -> myb.Type: | ||
"""Type check a set comprehension.""" | ||
out = super().visit_set_comprehension(e) | ||
FuseTypeInfoPass.node_type_hash[e] = out | ||
return out | ||
|
||
def visit_generator_expr(self, e: myfp.GeneratorExpr) -> myb.Type: | ||
"""Type check a generator expression.""" | ||
out = super().visit_generator_expr(e) | ||
FuseTypeInfoPass.node_type_hash[e] = out | ||
return out | ||
|
||
def visit_dictionary_comprehension( | ||
self, e: myfp.DictionaryComprehension | ||
) -> myb.Type: | ||
"""Type check a dict comprehension.""" | ||
out = super().visit_dictionary_comprehension(e) | ||
FuseTypeInfoPass.node_type_hash[e] = out | ||
return out | ||
|
||
def visit_member_expr( | ||
self, e: myfp.MemberExpr, is_lvalue: bool = False | ||
) -> myb.Type: | ||
"""Type check a member expr.""" | ||
out = super().visit_member_expr(e, is_lvalue) | ||
FuseTypeInfoPass.node_type_hash[e] = out | ||
return out | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All the |
||
# For every expression there, create attach a method on this instance (self) named "enter_expr()" | ||
for expr_node in EXPRESSION_NODES: | ||
method_name = "visit_" + pascal_to_snake(expr_node.__name__) | ||
|
||
# We call the super() version of the method so ensure the parent class has the method or else continue. | ||
if not hasattr(mycke.ExpressionChecker, method_name): | ||
continue | ||
|
||
# If the method already overriden then don't override it again here. Continue. Note that the method exists | ||
# on the parent class and if it's also exists on this class and it's a different object that means it's | ||
# overrident method. | ||
if getattr(mycke.ExpressionChecker, method_name) != getattr( | ||
ExpressionChecker, method_name | ||
): | ||
continue | ||
|
||
# Since the "closure" function bellow captures the method name inside it, we cannot use it directly as the | ||
# "method_name" variable is used inside a loop and by the time the closure close the "method_name" value, | ||
# it'll be changed by the loop, so we need another method ("make_closure") to persist the value. | ||
def make_closure(method_name: str): # noqa: ANN201 | ||
def closure( | ||
self: ExpressionChecker, | ||
e: mycke.Expression, | ||
*args, # noqa: ANN002 | ||
**kwargs, # noqa: ANN003 | ||
) -> mycke.Type: | ||
# Ignore B023 here since we bind loop variable properly but flake8 raise a false alarm | ||
# (in some version of it), a bug in flake8 (https://github.com/PyCQA/flake8-bugbear/issues/269). | ||
out = getattr(mycke.ExpressionChecker, method_name)( # noqa: B023 | ||
self, e, *args, **kwargs | ||
) | ||
FuseTypeInfoPass.node_type_hash[e] = out | ||
return out | ||
|
||
return closure | ||
|
||
# Attach the new "visit_expr()" method to this instance. | ||
method = make_closure(method_name) | ||
setattr(self, method_name, MethodType(method, self)) | ||
|
||
|
||
class State(myb.State): | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
These deleted methods are dynamically added above in init