Skip to content
Closed
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
4 changes: 3 additions & 1 deletion src/irx/builders/llvmliteir.py
Original file line number Diff line number Diff line change
Expand Up @@ -3423,7 +3423,9 @@ def visit(self, node: astx.VariableDeclaration) -> None:
type_str = node.type_.__class__.__name__.lower()

# Emit the initializer
if node.value is not None:
if node.value is not None and not isinstance(
node.value, astx.Undefined
):
self.visit(node.value)
init_val = self.result_stack.pop()
if init_val is None:
Expand Down
60 changes: 60 additions & 0 deletions tests/test_binary_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,63 @@ def test_binary_op_logical_and_or(
module.block.append(main_fn)

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


@pytest.mark.parametrize(
"a_val, b_val, op, true_label, false_label",
[
(2.0, 3.0, "<=", "le_true", "le_false"),
(3.0, 2.0, ">=", "ge_true", "ge_false"),
(2.0, 2.0, "==", "eq_true", "eq_false"),
(1.0, 2.0, "!=", "ne_true", "ne_false"),
],
)
@pytest.mark.parametrize("builder_class", [LLVMLiteIR])
def test_binary_op_float_comparison(
builder_class: type[Builder],
a_val: float,
b_val: float,
op: str,
true_label: str,
false_label: str,
) -> None:
"""
title: Test Float32 comparison operators cover fcmp_ordered paths.
parameters:
builder_class:
type: type[Builder]
a_val:
type: float
b_val:
type: float
op:
type: str
true_label:
type: str
false_label:
type: str
"""
builder = builder_class()
module = builder.module()

cond = astx.BinaryOp(
op_code=op,
lhs=astx.LiteralFloat32(a_val),
rhs=astx.LiteralFloat32(b_val),
)
then_blk = astx.Block()
then_blk.append(PrintExpr(astx.LiteralUTF8String(true_label)))
else_blk = astx.Block()
else_blk.append(PrintExpr(astx.LiteralUTF8String(false_label)))
if_stmt = astx.IfStmt(condition=cond, then=then_blk, else_=else_blk)

proto = astx.FunctionPrototype(
name="main", args=astx.Arguments(), return_type=astx.Int32()
)
block = astx.Block()
block.append(if_stmt)
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=true_label)
4 changes: 4 additions & 0 deletions tests/test_if_stmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
(astx.Int16, astx.LiteralInt16),
(astx.Int8, astx.LiteralInt8),
(astx.Int64, astx.LiteralInt64),
(astx.Float32, astx.LiteralFloat32),
(astx.Float64, astx.LiteralFloat64),
],
)
@pytest.mark.parametrize(
Expand Down Expand Up @@ -94,6 +96,8 @@ def test_if_else_stmt(
(astx.Int32, astx.LiteralInt32),
(astx.Int16, astx.LiteralInt16),
(astx.Int8, astx.LiteralInt8),
(astx.Float32, astx.LiteralFloat32),
(astx.Float64, astx.LiteralFloat64),
],
)
@pytest.mark.parametrize(
Expand Down
78 changes: 66 additions & 12 deletions tests/test_variable_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,62 +7,116 @@

from irx.builders.base import Builder
from irx.builders.llvmliteir import LLVMLiteIR
from irx.system import PrintExpr

from .conftest import check_result


@pytest.mark.parametrize(
"int_type, literal_type",
"var_type, literal_type, expected_output",
[
(astx.Int32, astx.LiteralInt32),
(astx.Int16, astx.LiteralInt16),
(astx.Int8, astx.LiteralInt8),
(astx.Int64, astx.LiteralInt64),
(astx.Int32, astx.LiteralInt32, "42"),
(astx.Int16, astx.LiteralInt16, "42"),
(astx.Int8, astx.LiteralInt8, "42"),
(astx.Int64, astx.LiteralInt64, "42"),
(astx.Float32, astx.LiteralFloat32, "42.000000"),
(astx.Float64, astx.LiteralFloat64, "42.000000"),
],
)
@pytest.mark.parametrize(
"builder_class",
[
# ("translate", "test_variable_assignment.ll"),
LLVMLiteIR,
],
)
def test_variable_assignment(
builder_class: type[Builder],
int_type: type,
var_type: type,
literal_type: type,
expected_output: str,
) -> None:
"""
title: Test VariableAssignment by reassigning and returning.
parameters:
builder_class:
type: type[Builder]
int_type:
var_type:
type: type
literal_type:
type: type
expected_output:
type: str
"""
builder = builder_class()
module = builder.module()

decl = astx.InlineVariableDeclaration(
name="x",
type_=int_type(),
type_=var_type(),
value=literal_type(10),
mutability=astx.MutabilityKind.mutable,
)
assignment = astx.VariableAssignment(name="x", value=literal_type(42))

proto = astx.FunctionPrototype(
name="main", args=astx.Arguments(), return_type=int_type()
name="main", args=astx.Arguments(), return_type=astx.Int32()
)
fn_block = astx.Block()
fn_block.append(decl)
fn_block.append(assignment)
fn_block.append(astx.FunctionReturn(astx.Identifier("x")))
fn_block.append(PrintExpr(astx.Identifier("x")))
fn_block.append(astx.FunctionReturn(astx.LiteralInt32(0)))
fn_main = astx.FunctionDef(prototype=proto, body=fn_block)

module.block.append(fn_main)

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


@pytest.mark.parametrize(
"var_type, expected_output",
[
(astx.Int8, "0"),
(astx.Int16, "0"),
(astx.Int32, "0"),
(astx.Int64, "0"),
(astx.Float32, "0.000000"),
(astx.Float64, "0.000000"),
],
)
@pytest.mark.parametrize("builder_class", [LLVMLiteIR])
def test_variable_declaration_no_initializer(
builder_class: type[Builder],
var_type: type,
expected_output: str,
) -> None:
"""
title: Test VariableDeclaration without an initializer defaults to zero.
parameters:
builder_class:
type: type[Builder]
var_type:
type: type
expected_output:
type: str
"""
builder = builder_class()
module = builder.module()

decl = astx.VariableDeclaration(
name="x",
type_=var_type(),
)

proto = astx.FunctionPrototype(
name="main", args=astx.Arguments(), return_type=astx.Int32()
)
fn_block = astx.Block()
fn_block.append(decl)
fn_block.append(PrintExpr(astx.Identifier("x")))
fn_block.append(astx.FunctionReturn(astx.LiteralInt32(0)))
fn_main = astx.FunctionDef(prototype=proto, body=fn_block)

module.block.append(fn_main)

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