nac3core: toplevel, fix for class constructors + spelling errors #263

Closed
wylited wants to merge 2 commits from iss221_noinit into master

Allows for classes without __init__ constructors.
Also corrects some spelling, (contructor -> constructor).
fix #221.

Allows for classes without `__init__` constructors. Also corrects some spelling, (contructor -> constructor). fix #221.
wylited added 1 commit 2022-04-13 10:38:23 +08:00
wylited added 1 commit 2022-04-13 10:58:31 +08:00
sb10q reviewed 2022-04-13 14:21:51 +08:00
@ -223,4 +223,1 @@
let mut class_method_index_offset = 0;
let init_id = "__init__".into();
let exception_id = "Exception".into();
// TODO: Fix this hack. We will generate constructor for classes that inherit

@pca006132 What is the purpose of this hack?

@pca006132 What is the purpose of this hack?

register_top_level will return Result<(StrRef, DefinitionId, Option<Type>), String>, where the third element of the tuple is the type of the function or constructor of the class (if there is any). If the class does not have a constructor, the third element will be None to avoid adding this to the symbol table. If the class inherits from Exception, we will need to generate a default constructor for it.

`register_top_level` will return `Result<(StrRef, DefinitionId, Option<Type>), String>`, where the third element of the tuple is the type of the function or constructor of the class (if there is any). If the class does not have a constructor, the third element will be None to avoid adding this to the symbol table. If the class inherits from `Exception`, we will need to generate a default constructor for it.

We will generate or we are generating? I think this is already implemented?

We will generate or we are generating? I think this is already implemented?

#32 (comment)

Related: if __init__ exists and is not decorated with @kernel or @portable, attempting to create the object on the device should fail with a clear error message.

Maybe we should document this clearer. This PR will make this code compile:

from min_artiq import *

@nac3
class Test:
    def __init__(self):
        pass

@nac3
class Demo:
    core: KernelInvariant[Core]

    def __init__(self):
        self.core = Core()

    @kernel
    def run(self):
        test = Test()


if __name__ == "__main__":
    Demo().run()
https://git.m-labs.hk/M-Labs/nac3/issues/32#issuecomment-2891 > Related: if `__init__` exists and is not decorated with `@kernel` or `@portable`, attempting to create the object on the device should fail with a clear error message. Maybe we should document this clearer. This PR will make this code compile: ```python from min_artiq import * @nac3 class Test: def __init__(self): pass @nac3 class Demo: core: KernelInvariant[Core] def __init__(self): self.core = Core() @kernel def run(self): test = Test() if __name__ == "__main__": Demo().run() ```
wylited closed this pull request 2022-04-13 15:55:30 +08:00

Closed this pull request due to issues working with git so i just opened a new branch with the changes.

to be honest though, I don't know what reason there was to be checking if the class had a constructor or not, and only if it does have a constructor should it have an associated type.

Closed this pull request due to issues working with git so i just opened a new branch with the changes. to be honest though, I don't know what reason there was to be checking if the class had a constructor or not, and only if it does have a constructor should it have an associated type.

Pull request closed

Sign in to join this conversation.
No reviewers
No Milestone
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: M-Labs/nac3#263
There is no content yet.