Skip to content

Commit

Permalink
refactor: Change AST types from class type to instance (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
xmnlab authored Nov 28, 2024
1 parent 3daf628 commit 1997e75
Show file tree
Hide file tree
Showing 25 changed files with 328 additions and 487 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ import astx

# Define a simple function `add(x, y): return x + y`
args = astx.Arguments(
astx.Argument(name="x", type_=astx.Int32),
astx.Argument(name="y", type_=astx.Int32),
astx.Argument(name="x", type_=astx.Int32()),
astx.Argument(name="y", type_=astx.Int32()),
)
fn_body = astx.Block()
fn_body.append(
Expand Down
40 changes: 26 additions & 14 deletions docs/tutorials/context.ipynb

Large diffs are not rendered by default.

24 changes: 18 additions & 6 deletions docs/tutorials/fibonacci.ipynb

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions docs/tutorials/for-loop.ipynb

Large diffs are not rendered by default.

34 changes: 23 additions & 11 deletions docs/tutorials/functions.ipynb

Large diffs are not rendered by default.

189 changes: 42 additions & 147 deletions docs/tutorials/get-started.ipynb

Large diffs are not rendered by default.

46 changes: 29 additions & 17 deletions docs/tutorials/literals.ipynb

Large diffs are not rendered by default.

26 changes: 19 additions & 7 deletions docs/tutorials/variables.ipynb

Large diffs are not rendered by default.

18 changes: 14 additions & 4 deletions src/astx/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from abc import abstractmethod
from enum import Enum
from hashlib import sha256
from typing import ClassVar, Dict, List, Optional, Type, Union, cast
from typing import ClassVar, Dict, List, Optional, Union, cast

from typeguard import typechecked

Expand Down Expand Up @@ -306,7 +306,17 @@ class Expr(AST):
nbytes: int = 0


ExprType: TypeAlias = Type[Expr]
@public
@typechecked
class ExprType(Expr):
"""ExprType expression class."""

nbytes: int = 0

def get_struct(self, simplified: bool = False) -> ReprStruct:
"""Return a structure that represents the node object."""
return {"Type": self.__class__.__name__}


PrimitivesStruct: TypeAlias = Union[
int,
Expand Down Expand Up @@ -340,7 +350,7 @@ def get_struct(self, simplified: bool = False) -> ReprStruct:

@public
@typechecked
class DataType(Expr):
class DataType(ExprType):
"""AST main expression class."""

type_: ExprType
Expand All @@ -356,7 +366,7 @@ def __init__(
self.name = f"temp_{DataType._tmp_id}"
DataType._tmp_id += 1
# set it as a generic data type
self.type_: ExprType = DataType
self.type_: ExprType = ExprType()
self.parent = parent

def __str__(self) -> str:
Expand Down
15 changes: 9 additions & 6 deletions src/astx/callables.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
ASTNodes,
DataType,
Expr,
ExprType,
ReprStruct,
SourceLocation,
StatementType,
Undefined,
)
from astx.blocks import Block
from astx.datatypes import AnyType
from astx.modifiers import MutabilityKind, ScopeKind, VisibilityKind
from astx.variables import Variable

Expand All @@ -33,13 +33,13 @@ class Argument(Variable):

mutability: MutabilityKind
name: str
type_: ExprType
type_: DataType
default: Expr

def __init__(
self,
name: str,
type_: ExprType,
type_: DataType,
mutability: MutabilityKind = MutabilityKind.constant,
default: Expr = UNDEFINED,
loc: SourceLocation = NO_SOURCE_LOCATION,
Expand All @@ -54,7 +54,7 @@ def __init__(

def __str__(self) -> str:
"""Return a string that represents the object."""
type_ = self.type_.__name__
type_ = self.type_.__class__.__name__
return f"Argument[{self.name}, {type_}]"

def get_struct(self, simplified: bool = False) -> ReprStruct:
Expand Down Expand Up @@ -97,11 +97,13 @@ class FunctionCall(DataType):

fn: Function
args: Iterable[DataType]
type_: DataType = AnyType()

def __init__(
self,
fn: Function,
args: Iterable[DataType],
type_: DataType = AnyType(),
loc: SourceLocation = NO_SOURCE_LOCATION,
parent: Optional[ASTNodes] = None,
) -> None:
Expand All @@ -110,6 +112,7 @@ def __init__(
self.fn = fn
self.args = args
self.kind = ASTKind.CallKind
self.type_ = type_

def __str__(self) -> str:
"""Return a string representation of the object."""
Expand Down Expand Up @@ -144,15 +147,15 @@ class FunctionPrototype(StatementType):

name: str
args: Arguments
return_type: ExprType
return_type: AnyType
scope: ScopeKind
visibility: VisibilityKind

def __init__(
self,
name: str,
args: Arguments,
return_type: ExprType,
return_type: AnyType,
scope: ScopeKind = ScopeKind.global_,
visibility: VisibilityKind = VisibilityKind.public,
loc: SourceLocation = NO_SOURCE_LOCATION,
Expand Down
Loading

0 comments on commit 1997e75

Please sign in to comment.