Closed xmnlab closed 1 month ago
@xmnlab , would it be possible for you to put here quick examples of what the output of the visit method of the transpiler for these classes should be? Thank you!
@apkrelling python doesn't have this import or import-from features as expressions, but we can workaround that.
just for the record some other programming languages would offer that (from gpt):
but with python we could do it with __import__
:
# Using __import__() as an expression
module = __import__('module_name')
for the import-from, maybe you could do something like this:
name = getattr(__import__('module_name', fromlist=['name']), 'name')
in the case of multiple imports
name1, name2 = (
getattr(__import__('module_name', fromlist=['name1']), 'name1'),
getattr(__import__('module_name', fromlist=['name2']), 'name2'),
)
Hello @xmnlab ,
about the AST ascii viz showing the wrong node names:
The node names that will show on the AST ascii viz are the keys
of the get_struct dictionary. The way we are currently designing the AliasExpr
, ImportStmt
, ImportFromStmt
, ImportExpr
, and ImportFomExpr
classes, the names that we want as node names are the values. So the get_struct method would need to be modified to make them keys, not values.
See the example below:
The ascii viz problem is solved on PR #122 .
from gpt:
Understanding the Requirements
ImportExpr
: Represents the expression version of animport
statement.ImportFromExpr
: Represents the expression version of afrom ... import ...
statement.In your AST framework:
Expr
or an expression-derived class.StatementType
or a class derived from it.AST
.Use Cases for
ImportExpr
andImportFromExpr
In some programming languages, importing can be performed at runtime within expressions. Examples include:
importlib
module: Allows dynamic imports.import()
: Returns a promise that resolves to the module.In such cases, the import operation is an expression that can be used within other expressions or assigned to variables.
Designing
ImportExpr
andImportFromExpr
ClassesInheritance Hierarchy
ImportExpr
andImportFromExpr
should inherit fromExpr
or a subclass ofExpr
.Class Definitions
ImportExpr
Expr
.ImportFromExpr
Expr
.from ... import ...
operation as an expression.Defining the Classes
1. Updating
ASTKind
EnumFirst, we'll need to add new kinds to the
ASTKind
enum for these expressions.2. Implementing
ImportExpr
ClassExplanation:
Expr
, as it's an expression.module
: The name of the module to import.alias
: An optional alias name.__str__
: Provides a readable string representation.get_struct
: Returns the AST structure.3. Implementing
ImportFromExpr
ClassExplanation:
Expr
.module
: The module to import from (can beNone
for relative imports).names
: A list of names to import.aliases
: A dictionary mapping names to their aliases.level
: The level of relative import.__str__
: Provides a readable string representation.get_struct
: Returns the AST structure.Considerations
1. Type Annotations
type_
attribute to represent the resulting type of the expression.ModuleType
, whereas importing a specific name might have a different type.2. Dynamic Imports
import('module')
in JavaScript), you may need to handle asynchronous behavior or promises.3. Integration with Existing Classes
AliasExpr
class, you might reuse it within these expressions.ImportFromExpr
, you could have a list ofAliasExpr
instances instead of simple strings.Using
AliasExpr
inImportFromExpr
:Example Usage
1. Importing a Module as an Expression
2. Importing Specific Names from a Module
Conclusion
By designing
ImportExpr
andImportFromExpr
as subclasses ofExpr
, you can represent import operations as expressions within your AST framework. This allows for dynamic importing and integration with other expressions in the language.Next Steps
type_
attribute for these expressions to represent the types they yield.Additional Considerations
1. Error Handling
2. Contextual Semantics
3. Runtime Behavior