diff --git a/nac3artiq/demo/module.py b/nac3artiq/demo/module.py new file mode 100644 index 00000000..58f92450 --- /dev/null +++ b/nac3artiq/demo/module.py @@ -0,0 +1,26 @@ +from min_artiq import * +from numpy import int32 + +# Global Variable Definition +X: Kernel[int32] = 1 + +# TopLevelFunction Defintion +@kernel +def display_X(): + print_int32(X) + +# TopLevel Class Definition +@nac3 +class A: + @kernel + def __init__(self): + self.set_x(1) + + @kernel + def set_x(self, new_val: int32): + global X + X = new_val + + @kernel + def get_X(self) -> int32: + return X diff --git a/nac3artiq/demo/module_support.py b/nac3artiq/demo/module_support.py index a863b380..78ef6565 100644 --- a/nac3artiq/demo/module_support.py +++ b/nac3artiq/demo/module_support.py @@ -1,7 +1,5 @@ from min_artiq import * -import tests.string_attribute_issue337 as issue337 -import tests.support_class_attr_issue102 as issue102 -import tests.global_variables as global_variables +import module as module_definition @nac3 class TestModuleSupport: @@ -13,17 +11,16 @@ class TestModuleSupport: @kernel def run(self): # Accessing classes - issue337.Demo().run() - obj = issue102.Demo() - obj.attr3 = 3 + obj = module_definition.A() + obj.get_X() + obj.set_x(2) # Calling functions - global_variables.inc_X() - global_variables.display_X() + module_definition.display_X() # Updating global variables - global_variables.X = 9 - global_variables.display_X() + module_definition.X = 9 + module_definition.display_X() if __name__ == "__main__": TestModuleSupport().run() \ No newline at end of file diff --git a/nac3artiq/demo/tests/global_variables.py b/nac3artiq/demo/tests/global_variables.py deleted file mode 100644 index ac0e0cf0..00000000 --- a/nac3artiq/demo/tests/global_variables.py +++ /dev/null @@ -1,14 +0,0 @@ -from min_artiq import * -from numpy import int32 - -X: Kernel[int32] = 1 - -@rpc -def display_X(): - print_int32(X) - -@kernel -def inc_X(): - global X - X += 1 - diff --git a/nac3artiq/demo/tests/string_attribute_issue337.py b/nac3artiq/demo/tests/string_attribute_issue337.py deleted file mode 100644 index c0b36ed6..00000000 --- a/nac3artiq/demo/tests/string_attribute_issue337.py +++ /dev/null @@ -1,18 +0,0 @@ -from min_artiq import * -from numpy import int32 - -@nac3 -class Demo: - attr1: Kernel[str] - attr2: Kernel[int32] - - @kernel - def __init__(self): - self.attr2 = 32 - self.attr1 = "SAMPLE" - - @kernel - def run(self): - print_int32(self.attr2) - self.attr1 - diff --git a/nac3artiq/demo/tests/support_class_attr_issue102.py b/nac3artiq/demo/tests/support_class_attr_issue102.py deleted file mode 100644 index 0482e3f1..00000000 --- a/nac3artiq/demo/tests/support_class_attr_issue102.py +++ /dev/null @@ -1,37 +0,0 @@ -from min_artiq import * -from numpy import int32 - -@nac3 -class Demo: - attr1: KernelInvariant[int32] = 2 - attr2: int32 = 4 - attr3: Kernel[int32] - - @kernel - def __init__(self): - self.attr3 = 8 - -@nac3 -class NAC3Devices: - core: KernelInvariant[Core] - attr4: KernelInvariant[int32] = 16 - - def __init__(self): - self.core = Core() - - @kernel - def run(self): - Demo.attr1 # Supported - # Demo.attr2 # Field not accessible on Kernel - # Demo.attr3 # Only attributes can be accessed in this way - # Demo.attr1 = 2 # Attributes are immutable - - self.attr4 # Attributes can be accessed within class - - obj = Demo() - obj.attr1 # Attributes can be accessed by class objects - - NAC3Devices.attr4 # Attributes accessible for classes without __init__ - -if __name__ == "__main__": - NAC3Devices().run() diff --git a/nac3standalone/demo/src/class_attributes.py b/nac3standalone/demo/src/class_attributes.py new file mode 100644 index 00000000..b58958fa --- /dev/null +++ b/nac3standalone/demo/src/class_attributes.py @@ -0,0 +1,35 @@ +@extern +def output_int32(x: int32): + ... + +@extern +def output_strln(x: str): + ... + + +class A: + a: int32 = 1 + b: int32 + c: str = "test" + d: str + + def __init__(self): + self.b = 2 + self.d = "test" + + output_int32(self.a) # Attributes can be accessed within class + + +def run() -> int32: + output_int32(A.a) # Attributes can be directly accessed with class name + # A.b # Only attributes can be accessed in this way + # A.a = 2 # Attributes are immutable + + obj = A() + output_int32(obj.a) # Attributes can be accessed by class objects + + output_strln(obj.c) + output_strln(obj.d) + + return 0 +