Skip to content
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

feat: Add new data types for LiteralInt8, LiteralInt16, LiteralIn64 #24

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
"source": [
"module = astx.Module()\n",
"\n",
"a = astx.Variable(name=\"a\", type_=astx.Int32, value=astx.Int32Literal(1))\n",
"b = astx.Variable(name=\"b\", type_=astx.Int32, value=astx.Int32Literal(2))\n",
"c = astx.Variable(name=\"c\", type_=astx.Int32, value=astx.Int32Literal(4))\n",
"a = astx.Variable(name=\"a\", type_=astx.Int32, value=astx.LiteralInt32(1))\n",
"b = astx.Variable(name=\"b\", type_=astx.Int32, value=astx.LiteralInt32(2))\n",
"c = astx.Variable(name=\"c\", type_=astx.Int32, value=astx.LiteralInt32(4))\n",
"\n",
"lit_1 = astx.Int32Literal(1)\n",
"lit_1 = astx.LiteralInt32(1)\n",
"\n",
"basic_op = lit_1 + b - a * c / a + (b - a / a)\n",
"\n",
Expand Down
10 changes: 8 additions & 2 deletions src/astx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@
Int8,
Int16,
Int32,
Int32Literal,
Int64,
Integer,
Literal,
LiteralInt8,
LiteralInt16,
LiteralInt32,
LiteralInt64,
Number,
SignedInteger,
)
Expand Down Expand Up @@ -102,7 +105,10 @@ def get_version() -> str:
"Int8",
"Int16",
"Int32",
"Int32Literal",
"LiteralInt8",
"LiteralInt16",
"LiteralInt32",
"LiteralInt64",
"Int64",
"Integer",
"Literal",
Expand Down
54 changes: 51 additions & 3 deletions src/astx/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,16 +215,64 @@ def get_struct(self) -> ReprStruct:


@public
class Int32Literal(Literal):
"""Int32Literal data type class."""
class LiteralInt8(Literal):
"""LiteralInt8 data type class."""

value: int

def __init__(
self, value: int, loc: SourceLocation = SourceLocation(0, 0)
) -> None:
"""Initialize Int32Literal."""
"""Initialize LiteralInt8."""
super().__init__(loc)
self.value = value
self.type_ = Int8
self.loc = loc


@public
class LiteralInt16(Literal):
"""LiteralInt16 data type class."""

value: int

def __init__(
self, value: int, loc: SourceLocation = SourceLocation(0, 0)
) -> None:
"""Initialize LiteralInt16."""
super().__init__(loc)
self.value = value
self.type_ = Int16
self.loc = loc


@public
class LiteralInt32(Literal):
"""LiteralInt32 data type class."""

value: int

def __init__(
self, value: int, loc: SourceLocation = SourceLocation(0, 0)
) -> None:
"""Initialize LiteralInt32."""
super().__init__(loc)
self.value = value
self.type_ = Int32
self.loc = loc


@public
class LiteralInt64(Literal):
"""LiteralInt64 data type class."""

value: int

def __init__(
self, value: int, loc: SourceLocation = SourceLocation(0, 0)
) -> None:
"""Initialize LiteralInt64."""
super().__init__(loc)
self.value = value
self.type_ = Int64
self.loc = loc
10 changes: 5 additions & 5 deletions tests/test_blocks.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
"""Module for testing different kind of ASTx blocks."""
from astx.blocks import Block, Module
from astx.datatypes import Int32, Int32Literal
from astx.datatypes import Int32, LiteralInt32
from astx.operators import BinaryOp
from astx.variables import Variable


def test_block() -> None:
"""Test ASTx block."""
block = Block()
var_a = Variable("a", Int32, Int32Literal(1))
var_b = Variable("b", Int32, Int32Literal(2))
var_a = Variable("a", Int32, LiteralInt32(1))
var_b = Variable("b", Int32, LiteralInt32(2))
sum_op = BinaryOp(op_code="+", lhs=var_a, rhs=var_b)
block.append(sum_op)


def test_module() -> None:
"""Test ASTx module."""
module = Module()
var_a = Variable("a", Int32, Int32Literal(1))
var_b = Variable("b", Int32, Int32Literal(2))
var_a = Variable("a", Int32, LiteralInt32(1))
var_b = Variable("b", Int32, LiteralInt32(2))
sum_op = BinaryOp(op_code="+", lhs=var_a, rhs=var_b)
module.append(sum_op)
10 changes: 5 additions & 5 deletions tests/test_callables.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"""Test callable ASTx objects."""
from astx.blocks import Block
from astx.callables import Function, FunctionPrototype
from astx.datatypes import Int32, Int32Literal
from astx.datatypes import Int32, LiteralInt32
from astx.modifiers import ScopeKind, VisibilityKind
from astx.variables import Variable


def test_function_creation_with_no_modifiers() -> None:
"""Test function creation with no modifiers."""
var_a = Variable("a", type_=Int32, value=Int32Literal(1))
var_b = Variable("b", type_=Int32, value=Int32Literal(1))
var_a = Variable("a", type_=Int32, value=LiteralInt32(1))
var_b = Variable("b", type_=Int32, value=LiteralInt32(1))
proto = FunctionPrototype(
name="add",
args=[var_a, var_b],
Expand All @@ -21,8 +21,8 @@ def test_function_creation_with_no_modifiers() -> None:

def test_function_creation_with_modifiers() -> None:
"""Test function creation with modifiers."""
var_a = Variable("a", type_=Int32, value=Int32Literal(1))
var_b = Variable("b", type_=Int32, value=Int32Literal(1))
var_a = Variable("a", type_=Int32, value=LiteralInt32(1))
var_b = Variable("b", type_=Int32, value=LiteralInt32(1))
proto = FunctionPrototype(
name="add",
args=[var_a, var_b],
Expand Down
10 changes: 5 additions & 5 deletions tests/test_datatypes.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
"""Tests for data types."""
from astx.datatypes import Int32, Int32Literal
from astx.datatypes import Int32, LiteralInt32
from astx.operators import BinaryOp
from astx.variables import Variable


def test_variable_i32() -> None:
"""Test variable i32."""
var_a = Variable("a", Int32, value=Int32Literal(1))
var_b = Variable("b", Int32, value=Int32Literal(1))
var_a = Variable("a", Int32, value=LiteralInt32(1))
var_b = Variable("b", Int32, value=LiteralInt32(1))
BinaryOp(op_code="+", lhs=var_a, rhs=var_b)


def test_literal_i32() -> None:
"""Test literal i32."""
lit_a = Int32Literal(value=1)
lit_b = Int32Literal(value=2)
lit_a = LiteralInt32(value=1)
lit_b = LiteralInt32(value=2)
BinaryOp(op_code="+", lhs=lit_a, rhs=lit_b)
22 changes: 11 additions & 11 deletions tests/test_flows.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
"""Tests for control flow statements."""
from astx.blocks import Block
from astx.datatypes import Int32, Int32Literal
from astx.datatypes import Int32, LiteralInt32
from astx.flows import ForCountLoop, ForRangeLoop, If
from astx.operators import BinaryOp, UnaryOp
from astx.variables import Variable


def test_if() -> None:
"""Test `if` statement."""
op = BinaryOp(op_code=">", lhs=Int32Literal(1), rhs=Int32Literal(2))
op = BinaryOp(op_code=">", lhs=LiteralInt32(1), rhs=LiteralInt32(2))
then_block = Block()
If(condition=op, then=then_block)


def test_if_else() -> None:
"""Test `if`/`else` statement."""
cond = BinaryOp(op_code=">", lhs=Int32Literal(1), rhs=Int32Literal(2))
cond = BinaryOp(op_code=">", lhs=LiteralInt32(1), rhs=LiteralInt32(2))
then_block = Block()
else_block = Block()
If(condition=cond, then=then_block, else_=else_block)


def test_for_range() -> None:
"""Test `For Range Loop` statement."""
var_a = Variable("a", type_=Int32, value=Int32Literal(-1))
start = Int32Literal(1)
end = Int32Literal(10)
step = Int32Literal(1)
var_a = Variable("a", type_=Int32, value=LiteralInt32(-1))
start = LiteralInt32(1)
end = LiteralInt32(10)
step = LiteralInt32(1)
body = Block()
body.append(Int32Literal(2))
body.append(LiteralInt32(2))
ForRangeLoop(variable=var_a, start=start, end=end, step=step, body=body)


def test_for_count() -> None:
"""Test `For Count Loop` statement."""
var_a = Variable("a", type_=Int32, value=Int32Literal(0))
cond = BinaryOp(op_code="<", lhs=var_a, rhs=Int32Literal(10))
var_a = Variable("a", type_=Int32, value=LiteralInt32(0))
cond = BinaryOp(op_code="<", lhs=var_a, rhs=LiteralInt32(10))
update = UnaryOp(op_code="++", operand=var_a)
body = Block()
body.append(Int32Literal(2))
body.append(LiteralInt32(2))
ForCountLoop(initializer=var_a, condition=cond, update=update, body=body)
10 changes: 5 additions & 5 deletions tests/test_operators.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Module for testing operators."""
import pytest

from astx.datatypes import Int32Literal
from astx.datatypes import LiteralInt32
from astx.operators import BinaryOp, UnaryOp

lit_1 = Int32Literal(1)
lit_2 = Int32Literal(2)
lit_3 = Int32Literal(3)
lit_1 = LiteralInt32(1)
lit_2 = LiteralInt32(2)
lit_3 = LiteralInt32(3)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -38,5 +38,5 @@ def test_binary_op(explicit: BinaryOp, implicit: BinaryOp) -> None:

def test_unary_op() -> None:
"""Test unary operator."""
lit_a = Int32Literal(1)
lit_a = LiteralInt32(1)
UnaryOp(op_code="+", operand=lit_a)