Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
153 changes: 148 additions & 5 deletions tests/test_binary_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ def test_binary_op_literals(
],
)
@pytest.mark.parametrize(
"action,expected_file",
"action,expected_file,expected_output",
[
# ("translate", "test_binary_op_basic.ll"),
("build", ""),
# ("translate", "test_binary_op_basic.ll", ""),
("build", "", "2"),
],
)
@pytest.mark.parametrize(
Expand All @@ -88,6 +88,7 @@ def test_binary_op_literals(
def test_binary_op_basic(
action: str,
expected_file: str,
expected_output: str,
builder_class: Type[Builder],
int_type: type,
literal_type: type,
Expand All @@ -99,6 +100,8 @@ def test_binary_op_basic(
type: str
expected_file:
type: str
expected_output:
type: str
builder_class:
type: Type[Builder]
int_type:
Expand Down Expand Up @@ -143,12 +146,18 @@ def test_binary_op_basic(
main_block.append(decl_a)
main_block.append(decl_b)
main_block.append(decl_c)
main_block.append(basic_op)
decl_tmp = astx.VariableDeclaration(
"tmp", type_=int_type(), value=basic_op
)
main_block.append(decl_tmp)
main_block.append(PrintExpr(astx.LiteralUTF8String("2")))
main_block.append(astx.FunctionReturn(literal_type(0)))
main_fn = astx.FunctionDef(prototype=main_proto, body=main_block)

module.block.append(main_fn)
check_result(action, builder, module, expected_file)
check_result(
action, builder, module, expected_file, expected_output=expected_output
)


@pytest.mark.parametrize("builder_class", [LLVMLiteIR])
Expand Down Expand Up @@ -248,3 +257,137 @@ def test_binary_op_logical_and_or(
module.block.append(main_fn)

check_result("build", builder, module, expected_output=expect)


def test_literal_int8() -> None:
"""
title: Test LiteralInt8 visitor.
"""
builder = LLVMLiteIR()
module = builder.module()

decl = astx.InlineVariableDeclaration(
name="b",
type_=astx.Int8(),
value=astx.LiteralInt8(42),
mutability=astx.MutabilityKind.mutable,
)

proto = astx.FunctionPrototype(
name="main", args=astx.Arguments(), return_type=astx.Int32()
)
block = astx.Block()
block.append(decl)
block.append(PrintExpr(astx.LiteralUTF8String("42")))
block.append(astx.FunctionReturn(astx.LiteralInt32(0)))
fn = astx.FunctionDef(prototype=proto, body=block)
module.block.append(fn)

check_result("build", builder, module, expected_output="42")


def test_literal_int64() -> None:
"""
title: Test LiteralInt64 visitor.
"""
builder = LLVMLiteIR()
module = builder.module()

decl = astx.InlineVariableDeclaration(
name="big",
type_=astx.Int64(),
value=astx.LiteralInt64(1000000),
mutability=astx.MutabilityKind.mutable,
)

proto = astx.FunctionPrototype(
name="main", args=astx.Arguments(), return_type=astx.Int32()
)
block = astx.Block()
block.append(decl)
block.append(PrintExpr(astx.LiteralUTF8String("1000000")))
block.append(astx.FunctionReturn(astx.LiteralInt32(0)))
fn = astx.FunctionDef(prototype=proto, body=block)
module.block.append(fn)

check_result("build", builder, module, expected_output="1000000")


def test_int_equality() -> None:
"""
title: Test integer == comparison (line 1085).
"""
builder = LLVMLiteIR()
module = builder.module()

decl_a = astx.InlineVariableDeclaration(
name="a",
type_=astx.Int32(),
value=astx.LiteralInt32(5),
mutability=astx.MutabilityKind.mutable,
)
decl_b = astx.InlineVariableDeclaration(
name="b",
type_=astx.Int32(),
value=astx.LiteralInt32(5),
mutability=astx.MutabilityKind.mutable,
)

cond = astx.BinaryOp("==", astx.Identifier("a"), astx.Identifier("b"))
then_block = astx.Block()
then_block.append(astx.FunctionReturn(astx.LiteralInt32(1)))
else_block = astx.Block()
else_block.append(astx.FunctionReturn(astx.LiteralInt32(0)))
if_stmt = astx.IfStmt(condition=cond, then=then_block, else_=else_block)

proto = astx.FunctionPrototype(
name="main", args=astx.Arguments(), return_type=astx.Int32()
)
block = astx.Block()
block.append(decl_a)
block.append(decl_b)
block.append(if_stmt)
fn = astx.FunctionDef(prototype=proto, body=block)
module.block.append(fn)

check_result("build", builder, module, expected_output="1")


def test_int_inequality() -> None:
"""
title: Test integer != comparison (line 1108).
"""
builder = LLVMLiteIR()
module = builder.module()

decl_a = astx.InlineVariableDeclaration(
name="a",
type_=astx.Int32(),
value=astx.LiteralInt32(1),
mutability=astx.MutabilityKind.mutable,
)
decl_b = astx.InlineVariableDeclaration(
name="b",
type_=astx.Int32(),
value=astx.LiteralInt32(2),
mutability=astx.MutabilityKind.mutable,
)

cond = astx.BinaryOp("!=", astx.Identifier("a"), astx.Identifier("b"))
then_block = astx.Block()
then_block.append(astx.FunctionReturn(astx.LiteralInt32(1)))
else_block = astx.Block()
else_block.append(astx.FunctionReturn(astx.LiteralInt32(0)))
if_stmt = astx.IfStmt(condition=cond, then=then_block, else_=else_block)

proto = astx.FunctionPrototype(
name="main", args=astx.Arguments(), return_type=astx.Int32()
)
block = astx.Block()
block.append(decl_a)
block.append(decl_b)
block.append(if_stmt)
fn = astx.FunctionDef(prototype=proto, body=block)
module.block.append(fn)

check_result("build", builder, module, expected_output="1")
Loading
Loading