Skip to content

Allow custom ExternArgument in extern declaration #83

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

Merged
merged 6 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions oqpy/subroutines.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def wrapper(

def declare_extern(
name: str,
args: list[tuple[str, ast.ClassicalType]],
args: list[tuple[str, ast.ClassicalType | ast.ExternArgument]],
return_type: Optional[ast.ClassicalType] = None,
annotations: Sequence[str | tuple[str, str]] = (),
) -> Callable[..., OQFunctionCall]:
Expand All @@ -194,7 +194,7 @@ def declare_extern(
arg_types = list(zip(*(args)))[1] if args else []
extern_decl = ast.ExternDeclaration(
ast.Identifier(name),
[ast.ExternArgument(type=t) for t in arg_types],
[ast.ExternArgument(type=t) if isinstance(t, ast.ClassicalType) else t for t in arg_types],
return_type,
)
extern_decl.annotations = make_annotations(annotations)
Expand Down Expand Up @@ -236,7 +236,7 @@ def call_extern(*call_args: AstConvertible, **call_kwargs: AstConvertible) -> OQ

def declare_waveform_generator(
name: str,
argtypes: list[tuple[str, ast.ClassicalType]],
argtypes: list[tuple[str, ast.ClassicalType | ast.ExternArgument]],
annotations: Sequence[str | tuple[str, str]] = (),
) -> Callable[..., OQFunctionCall]:
"""Create a function which generates waveforms using a specified name and argument signature."""
Expand Down
17 changes: 17 additions & 0 deletions tests/test_directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,20 @@ def test_declare_extern():
# Test an extern with no input and no output
fire_bazooka = declare_extern("fire_bazooka", [])

# Test an extern with readonly array
print_array = declare_extern(
"print_array",
[
(
"arr",
ast.ExternArgument(
type=ast.ArrayReferenceType(int32, ast.IntegerLiteral(1)),
access=ast.AccessControl["readonly"],
),
),
],
)

f = oqpy.FloatVar(name="f", init_expression=0.0)
i = oqpy.IntVar(name="i", init_expression=5)

Expand All @@ -1032,6 +1046,7 @@ def test_declare_extern():
program.set(i, time())
program.do_expression(set_global_voltage(i))
program.do_expression(fire_bazooka())
program.do_expression(print_array([0, 1, 2]))

expected = textwrap.dedent(
"""
Expand All @@ -1041,13 +1056,15 @@ def test_declare_extern():
extern time() -> int[32];
extern set_voltage(int[32]);
extern fire_bazooka();
extern print_array(readonly array[int[32], #dim=1]);
float[64] f = 0.0;
int[32] i = 5;
f = sqrt(f);
f = arctan(f, f);
i = time();
set_voltage(i);
fire_bazooka();
print_array({0, 1, 2});
"""
).strip()

Expand Down