-
Notifications
You must be signed in to change notification settings - Fork 4
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
ClassDeclStmt
/ClassDefStmt
- Declares and defines a class.
#90
Labels
Comments
xmnlab
changed the title
Sep 18, 2024
ClassDeclarationStmt
- Declares a class.ClassDeclarationStmt
/ClassDefinitionStmt
- Declares a class.
xmnlab
changed the title
Sep 18, 2024
ClassDeclarationStmt
/ClassDefinitionStmt
- Declares a class.ClassDeclarationStmt
/ClassDefinitionStmt
- Declares and defines a class.
xmnlab
changed the title
Nov 22, 2024
ClassDeclarationStmt
/ClassDefinitionStmt
- Declares and defines a class.ClassDeclStmt
/ClassDefStmt
- Declares and defines a class.
|
add a new attribute:
|
let's try to not overthink too much about that .. let's keep it simple for now and we can improve that later. This is some recommendation from gpt with some changes mine @public
@typechecked
class ClassDefStmt(ClassDeclStmt):
"""AST class for class definition, including attributes and methods."""
attributes: Iterable[VariableDeclaration]
methods: Iterable[Function]
body: Block
def __init__(
self,
name: str,
bases: Optional[List[Expr]] = None,
decorators: Optional[List[Expr]] = None,
visibility: VisibilityKind = VisibilityKind.public,
is_abstract: bool = False,
metaclass: Optional[Expr] = None,
attributes: Iterable[VariableDeclaration] = [],
methods: Iterable[Function] = [],
loc: SourceLocation = NO_SOURCE_LOCATION,
parent: Optional[ASTNodes] = None,
) -> None:
super().__init__(
name=name,
bases=bases,
decorators=decorators,
visibility=visibility,
is_abstract=is_abstract,
metaclass=metaclass,
loc=loc,
parent=parent,
)
self.attributes = attributes if attributes is not None else []
self.methods = methods if methods is not None else []
# Construct body as a block containing attributes and methods
self.body = Block(name=f"{name}_body")
for attr in self.attributes:
self.body.append(attr)
for method in self.methods:
self.body.append(method)
self.kind = ASTKind.ClassDefinitionKind
def __str__(self) -> str:
class_decl_str = super().__str__()
if not self.body.nodes:
body_str = " pass"
else:
body_str = "\n ".join(str(stmt) for stmt in self.body.nodes)
return f"{class_decl_str}:\n {body_str}"
def get_struct(self, simplified: bool = False) -> ReprStruct:
value = super().get_struct(simplified)
value["attributes"] = [attr.get_struct(simplified) for attr in self.attributes]
value["methods"] = [method.get_struct(simplified) for method in self.methods]
value["body"] = self.body.get_struct(simplified)
key = f"ClassDefinition[{self.name}]"
return self._prepare_struct(key, value, simplified) |
21 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
from gpt:
Understanding the Existing Structure
From the modules you've shared, we can summarize the key points:
Base Classes and Enums:
AST
: The root class for all AST nodes (astx/base.py
).StatementType
: Inherits fromAST
, used for statements.Expr
: Inherits fromAST
, used for expressions.ASTKind
: An enumeration of different AST node kinds.Modules:
astx.base
: Contains base classes and fundamental definitions.astx.callables
: Contains function-related classes likeFunction
,FunctionPrototype
, etc.astx.variables
: Contains variable-related classes likeVariable
,VariableDeclaration
.astx.blocks
: Contains theBlock
class for sequences of statements.astx.modifiers
: Contains enums forVisibilityKind
,MutabilityKind
, andScopeKind
.astx.datatypes
: Contains data type classes and operations.astx.packages
: ContainsModule
,Package
, andProgram
classes for broader scopes.astx.flows
,astx.symbol_table
,astx.viz
, etc.Conventions:
StatementType
.Expr
.ASTKind
enum.VisibilityKind
,MutabilityKind
, andScopeKind
are used to specify attributes.Designing the Class Declaration and Definition Classes
Given your existing structure, we'll proceed to:
ASTKind
.astx/classes.py
to contain class-related AST nodes.ClassDeclarationStmt
andClassDefinitionStmt
classes, inheriting fromStatementType
.Expr
,ASTNodes
,Block
, and modifiers.astx/__init__.py
to include the new module and classes.1. Updating
ASTKind
EnumIn
astx/base.py
, add new kinds for the class statements:2. Creating
astx/classes.py
ModuleCreate a new module
astx/classes.py
to contain class-related AST nodes.Explanation:
StatementType
as class declarations are statements.name
: The class name.bases
: List of base classes (Expr
instances).decorators
: List of decorators (Expr
instances).visibility
: Access modifier fromVisibilityKind
.is_abstract
: Indicates if the class is abstract.metaclass
: Optional metaclass (Expr
instance).__str__
: Provides a readable string representation.get_struct
: Returns a structured representation for serialization.3. Defining
ClassDefinitionStmt
Explanation:
ClassDeclarationStmt
, adding thebody
attribute.body
: ABlock
containing the class body (list of statements).__str__
: Extends the string representation to include the class body.get_struct
: Extends the structured representation to include the body.4. Updating
astx/__init__.py
Add the new module and classes to your package's
__init__.py
:5. Ensuring Integration with Existing Structures
VisibilityKind
and other modifiers fromastx.modifiers
.Expr
and its subclasses forbases
,decorators
, andmetaclass
.Block
fromastx.blocks
for the class body.Example Usage
1. Creating a Class Declaration
Output:
2. Creating a Class Definition
Output:
Testing and Validation
ClassDeclarationStmt
andClassDefinitionStmt
with various configurations.astx
framework.astx.viz
to visualize the AST of the class definitions.Documentation
classes
module and provide usage examples.Conclusion
By adding
ClassDeclarationStmt
andClassDefinitionStmt
classes to yourastx
module, you've extended your AST framework to support class declarations and definitions. These classes integrate seamlessly with your existing codebase, following your established conventions and utilizing existing components.Next Steps
The text was updated successfully, but these errors were encountered: