Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #185 from Jaseci-Labs/plugin_refactor
Browse files Browse the repository at this point in the history
refactor: breaking out architype plugin interface
  • Loading branch information
marsninja committed Jan 31, 2024
2 parents 5b9dabb + 0cb5dc1 commit a437b70
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 24 deletions.
4 changes: 2 additions & 2 deletions jaclang/compiler/passes/main/pyast_gen_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,11 +486,11 @@ def exit_architype(self, node: ast.Architype) -> None:
value=self.sync(
ast3.Name(id=Con.JAC_FEATURE.value, ctx=ast3.Load())
),
attr="make_architype",
attr=f"make_{node.arch_type.value}",
ctx=ast3.Load(),
)
),
args=[self.sync(ast3.Constant(value=node.arch_type.value))],
args=[],
keywords=[
self.sync(
ast3.keyword(
Expand Down
102 changes: 89 additions & 13 deletions jaclang/plugin/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class JacFeatureDefaults:

@staticmethod
@hookimpl
def make_architype(
arch_type: str, on_entry: list[DSFunc], on_exit: list[DSFunc]
def make_obj(
on_entry: list[DSFunc], on_exit: list[DSFunc]
) -> Callable[[type], type]:
"""Create a new architype."""

Expand All @@ -43,18 +43,94 @@ def decorator(cls: Type[ArchBound]) -> Type[ArchBound]:
cls = dataclass(eq=False)(cls)
for i in on_entry + on_exit:
i.resolve(cls)
arch_cls = Architype
if not issubclass(cls, arch_cls):
cls = type(cls.__name__, (cls, arch_cls), {})
cls._jac_entry_funcs_ = on_entry
cls._jac_exit_funcs_ = on_exit
inner_init = cls.__init__

@wraps(inner_init)
def new_init(self: ArchBound, *args: object, **kwargs: object) -> None:
inner_init(self, *args, **kwargs)
arch_cls.__init__(self)

cls.__init__ = new_init
return cls

return decorator

@staticmethod
@hookimpl
def make_node(
on_entry: list[DSFunc], on_exit: list[DSFunc]
) -> Callable[[type], type]:
"""Create a obj architype."""

def decorator(cls: Type[ArchBound]) -> Type[ArchBound]:
"""Decorate class."""
cls = dataclass(eq=False)(cls)
for i in on_entry + on_exit:
i.resolve(cls)
arch_cls = NodeArchitype
if not issubclass(cls, arch_cls):
cls = type(cls.__name__, (cls, arch_cls), {})
cls._jac_entry_funcs_ = on_entry
cls._jac_exit_funcs_ = on_exit
inner_init = cls.__init__

@wraps(inner_init)
def new_init(self: ArchBound, *args: object, **kwargs: object) -> None:
inner_init(self, *args, **kwargs)
arch_cls.__init__(self)

cls.__init__ = new_init
return cls

return decorator

@staticmethod
@hookimpl
def make_edge(
on_entry: list[DSFunc], on_exit: list[DSFunc]
) -> Callable[[type], type]:
"""Create a edge architype."""

match arch_type:
case "obj":
arch_cls = Architype
case "node":
arch_cls = NodeArchitype
case "edge":
arch_cls = EdgeArchitype
case "walker":
arch_cls = WalkerArchitype
case _:
raise TypeError("Invalid archetype type")
def decorator(cls: Type[ArchBound]) -> Type[ArchBound]:
"""Decorate class."""
cls = dataclass(eq=False)(cls)
for i in on_entry + on_exit:
i.resolve(cls)
arch_cls = EdgeArchitype
if not issubclass(cls, arch_cls):
cls = type(cls.__name__, (cls, arch_cls), {})
cls._jac_entry_funcs_ = on_entry
cls._jac_exit_funcs_ = on_exit
inner_init = cls.__init__

@wraps(inner_init)
def new_init(self: ArchBound, *args: object, **kwargs: object) -> None:
inner_init(self, *args, **kwargs)
arch_cls.__init__(self)

cls.__init__ = new_init
return cls

return decorator

@staticmethod
@hookimpl
def make_walker(
on_entry: list[DSFunc], on_exit: list[DSFunc]
) -> Callable[[type], type]:
"""Create a walker architype."""

def decorator(cls: Type[ArchBound]) -> Type[ArchBound]:
"""Decorate class."""
cls = dataclass(eq=False)(cls)
for i in on_entry + on_exit:
i.resolve(cls)
arch_cls = WalkerArchitype
if not issubclass(cls, arch_cls):
cls = type(cls.__name__, (cls, arch_cls), {})
cls._jac_entry_funcs_ = on_entry
Expand Down
31 changes: 25 additions & 6 deletions jaclang/plugin/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,32 @@ class JacFeature:
EdgeDir: Type[EdgeDir] = EdgeDir

@staticmethod
def make_architype(
arch_type: str, on_entry: list[DSFunc], on_exit: list[DSFunc]
def make_obj(
on_entry: list[DSFunc], on_exit: list[DSFunc]
) -> Callable[[type], type]:
"""Create a new architype."""
return JacFeature.pm.hook.make_architype(
arch_type=arch_type, on_entry=on_entry, on_exit=on_exit
)
"""Create a obj architype."""
return JacFeature.pm.hook.make_obj(on_entry=on_entry, on_exit=on_exit)

@staticmethod
def make_node(
on_entry: list[DSFunc], on_exit: list[DSFunc]
) -> Callable[[type], type]:
"""Create a node architype."""
return JacFeature.pm.hook.make_node(on_entry=on_entry, on_exit=on_exit)

@staticmethod
def make_edge(
on_entry: list[DSFunc], on_exit: list[DSFunc]
) -> Callable[[type], type]:
"""Create a edge architype."""
return JacFeature.pm.hook.make_edge(on_entry=on_entry, on_exit=on_exit)

@staticmethod
def make_walker(
on_entry: list[DSFunc], on_exit: list[DSFunc]
) -> Callable[[type], type]:
"""Create a walker architype."""
return JacFeature.pm.hook.make_walker(on_entry=on_entry, on_exit=on_exit)

@staticmethod
def create_test(test_fun: Callable) -> Callable:
Expand Down
30 changes: 27 additions & 3 deletions jaclang/plugin/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,34 @@ class JacFeatureSpec:

@staticmethod
@hookspec(firstresult=True)
def make_architype(
arch_type: str, on_entry: list[DSFunc], on_exit: list[DSFunc]
def make_obj(
on_entry: list[DSFunc], on_exit: list[DSFunc]
) -> Callable[[type], type]:
"""Create a new architype."""
"""Create a obj architype."""
raise NotImplementedError

@staticmethod
@hookspec(firstresult=True)
def make_node(
on_entry: list[DSFunc], on_exit: list[DSFunc]
) -> Callable[[type], type]:
"""Create a node architype."""
raise NotImplementedError

@staticmethod
@hookspec(firstresult=True)
def make_edge(
on_entry: list[DSFunc], on_exit: list[DSFunc]
) -> Callable[[type], type]:
"""Create a edge architype."""
raise NotImplementedError

@staticmethod
@hookspec(firstresult=True)
def make_walker(
on_entry: list[DSFunc], on_exit: list[DSFunc]
) -> Callable[[type], type]:
"""Create a walker architype."""
raise NotImplementedError

@staticmethod
Expand Down

0 comments on commit a437b70

Please sign in to comment.