Skip to content

Commit

Permalink
refactor: Change AST types from class type to instance
Browse files Browse the repository at this point in the history
  • Loading branch information
xmnlab committed Nov 27, 2024
1 parent 3daf628 commit c4fa854
Show file tree
Hide file tree
Showing 21 changed files with 143 additions and 259 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
12 changes: 6 additions & 6 deletions docs/tutorials/context.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@
"module = astx.Module()\n",
"\n",
"# Declare variables 'a', 'b', and 'c' with initial values\n",
"decl_a = astx.VariableDeclaration(name=\"a\", type_=astx.Int32, value=astx.LiteralInt32(1))\n",
"decl_b = astx.VariableDeclaration(name=\"b\", type_=astx.Int32, value=astx.LiteralInt32(2))\n",
"decl_c = astx.VariableDeclaration(name=\"c\", type_=astx.Int32, value=astx.LiteralInt32(1))\n",
"decl_a = astx.VariableDeclaration(name=\"a\", type_=astx.Int32(), value=astx.LiteralInt32(1))\n",
"decl_b = astx.VariableDeclaration(name=\"b\", type_=astx.Int32(), value=astx.LiteralInt32(2))\n",
"decl_c = astx.VariableDeclaration(name=\"c\", type_=astx.Int32(), value=astx.LiteralInt32(1))\n",
"\n",
"# Reassign a new value to variable 'c'\n",
"assign_c = astx.VariableAssignment(name=\"c\", value=astx.LiteralInt32(3))\n",
Expand Down Expand Up @@ -182,9 +182,9 @@
"main_fn = astx.Function(prototype=main_proto, body=main_block, parent=module)\n",
"\n",
"# Declare variables 'a', 'b', and 'c' with initial values\n",
"decl_a = astx.VariableDeclaration(name=\"a\", type_=astx.Int32, value=astx.LiteralInt32(1), parent=main_block)\n",
"decl_b = astx.VariableDeclaration(name=\"b\", type_=astx.Int32, value=astx.LiteralInt32(2), parent=main_block)\n",
"decl_c = astx.VariableDeclaration(name=\"c\", type_=astx.Int32, value=astx.LiteralInt32(1), parent=main_block)\n",
"decl_a = astx.VariableDeclaration(name=\"a\", type_=astx.Int32(), value=astx.LiteralInt32(1), parent=main_block)\n",
"decl_b = astx.VariableDeclaration(name=\"b\", type_=astx.Int32(), value=astx.LiteralInt32(2), parent=main_block)\n",
"decl_c = astx.VariableDeclaration(name=\"c\", type_=astx.Int32(), value=astx.LiteralInt32(1), parent=main_block)\n",
"\n",
"# Reassign a new value to variable 'c'\n",
"assign_c = astx.VariableAssignment(name=\"c\", value=astx.LiteralInt32(3), parent=main_block)\n",
Expand Down
4 changes: 2 additions & 2 deletions docs/tutorials/for-loop.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"# Declare a loop variable\n",
"decl_a = astx.InlineVariableDeclaration(\n",
" \"a\",\n",
" type_=astx.Int32,\n",
" type_=astx.Int32(),\n",
")\n",
"\n",
"# Create a block for loop body\n",
Expand Down Expand Up @@ -141,7 +141,7 @@
],
"source": [
"# Declare and initialize the loop variable\n",
"decl_a = astx.InlineVariableDeclaration(\"a\", type_=astx.Int32, value=astx.LiteralInt32(0))\n",
"decl_a = astx.InlineVariableDeclaration(\"a\", type_=astx.Int32(), value=astx.LiteralInt32(0))\n",
"var_a = astx.Variable(\"a\")\n",
"\n",
"# Create a block for loop body\n",
Expand Down
6 changes: 3 additions & 3 deletions docs/tutorials/functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@
],
"source": [
"# Define function arguments\n",
"arg_a = astx.Argument(name=\"a\", type_=astx.Int32)\n",
"arg_b = astx.Argument(name=\"b\", type_=astx.Int32)\n",
"arg_c = astx.Argument(name=\"c\", type_=astx.Int32)\n",
"arg_a = astx.Argument(name=\"a\", type_=astx.Int32())\n",
"arg_b = astx.Argument(name=\"b\", type_=astx.Int32())\n",
"arg_c = astx.Argument(name=\"c\", type_=astx.Int32())\n",
"\n",
"# Create ASTx Variable objects\n",
"a = astx.Variable(name=\"a\")\n",
Expand Down
6 changes: 3 additions & 3 deletions docs/tutorials/get-started.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@
"module = astx.Module()\n",
"\n",
"# Declare variables\n",
"decl_a = astx.VariableDeclaration(name=\"a\", type_=astx.Int32, value=astx.LiteralInt32(1))\n",
"decl_b = astx.VariableDeclaration(name=\"b\", type_=astx.Int32, value=astx.LiteralInt32(2))\n",
"decl_c = astx.VariableDeclaration(name=\"c\", type_=astx.Int32, value=astx.LiteralInt32(4))\n",
"decl_a = astx.VariableDeclaration(name=\"a\", type_=astx.Int32(), value=astx.LiteralInt32(1))\n",
"decl_b = astx.VariableDeclaration(name=\"b\", type_=astx.Int32(), value=astx.LiteralInt32(2))\n",
"decl_c = astx.VariableDeclaration(name=\"c\", type_=astx.Int32(), value=astx.LiteralInt32(4))\n",
"\n",
"a = astx.Variable(name=\"a\")\n",
"b = astx.Variable(name=\"b\")\n",
Expand Down
6 changes: 3 additions & 3 deletions docs/tutorials/variables.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@
"module = astx.Module()\n",
"\n",
"# Declare variables 'a', 'b', and 'c' with initial values\n",
"decl_a = astx.VariableDeclaration(name=\"a\", type_=astx.Int32, value=astx.LiteralInt32(1))\n",
"decl_b = astx.VariableDeclaration(name=\"b\", type_=astx.Int32, value=astx.LiteralInt32(2))\n",
"decl_c = astx.VariableDeclaration(name=\"c\", type_=astx.Int32, value=astx.LiteralInt32(4))\n",
"decl_a = astx.VariableDeclaration(name=\"a\", type_=astx.Int32(), value=astx.LiteralInt32(1))\n",
"decl_b = astx.VariableDeclaration(name=\"b\", type_=astx.Int32(), value=astx.LiteralInt32(2))\n",
"decl_c = astx.VariableDeclaration(name=\"c\", type_=astx.Int32(), value=astx.LiteralInt32(4))\n",
"\n",
"# Reassign a new value to variable 'c'\n",
"assign_c = astx.VariableAssignment(name=\"c\", value=astx.LiteralInt32(3))\n",
Expand Down
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
2 changes: 1 addition & 1 deletion src/astx/callables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading

0 comments on commit c4fa854

Please sign in to comment.