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 #569 from Jaseci-Labs/architype_context
Browse files Browse the repository at this point in the history
Initial Commit for resolving architype
  • Loading branch information
marsninja authored Aug 28, 2024
2 parents e94ec96 + afa4259 commit 39cc924
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
40 changes: 40 additions & 0 deletions jaclang/runtimelib/machine.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Jac Machine module."""

import inspect
import marshal
import os
import sys
Expand All @@ -9,8 +10,10 @@
from jaclang.compiler.absyntree import Module
from jaclang.compiler.compile import compile_jac
from jaclang.compiler.constant import Constants as Con
from jaclang.runtimelib.architype import EdgeArchitype, NodeArchitype, WalkerArchitype
from jaclang.utils.log import logging


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -59,6 +62,43 @@ def load_module(self, module_name: str, module: types.ModuleType) -> None:
self.loaded_modules[module_name] = module
sys.modules[module_name] = module

def list_modules(self) -> list[str]:
"""List all loaded modules."""
return list(self.loaded_modules.keys())

def list_walkers(self, module_name: str) -> list[str]:
"""List all walkers in a specific module."""
module = self.loaded_modules.get(module_name)
if module:
walkers = []
for name, obj in inspect.getmembers(module):
if isinstance(obj, type) and issubclass(obj, WalkerArchitype):
walkers.append(name)
return walkers
return []

def list_nodes(self, module_name: str) -> list[str]:
"""List all nodes in a specific module."""
module = self.loaded_modules.get(module_name)
if module:
nodes = []
for name, obj in inspect.getmembers(module):
if isinstance(obj, type) and issubclass(obj, NodeArchitype):
nodes.append(name)
return nodes
return []

def list_edges(self, module_name: str) -> list[str]:
"""List all edges in a specific module."""
module = self.loaded_modules.get(module_name)
if module:
nodes = []
for name, obj in inspect.getmembers(module):
if isinstance(obj, type) and issubclass(obj, EdgeArchitype):
nodes.append(name)
return nodes
return []


class JacProgram:
"""Class to hold the mod_bundle and bytecode for Jac modules."""
Expand Down
34 changes: 34 additions & 0 deletions jaclang/tests/fixtures/bar.jac
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Define a simple node type called `Item`
node Item {
has value: int = 0;
}

# Define an edge type called `Link`
edge Link {}

# Define the `bar` walker
walker bar_walk {
has count: int = 0;

# Start walking from the root node or an Item node
can start with `root | Item entry {
here ++> Item();
if self.count < 5 {
visit [-->];
} else {
"Created 5 items." |> print;
disengage;
}
}

# Walk over Item nodes and update their values
can walk with Item entry {
here.value = self.count;
f"Item value: {here.value}" |> print;
self.count += 1;
visit [-->] else {
"Finished walking over all items." |> print;
disengage;
}
}
}
44 changes: 44 additions & 0 deletions jaclang/tests/fixtures/foo.jac
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import:py from jaclang.plugin.feature, JacFeature as Jac;
import:jac from bar, bar_walk;


# Test runner to initialize the walker
can test_run {
# Print the loaded modules
modules = Jac.context().jac_machine.list_modules();
"Loaded Modules:" |> print;
for mod_name in modules {
f"Module: {mod_name}" |> print;
}
# Print walkers
walkers = Jac.context().jac_machine.list_walkers(mod_name);
if walkers{
f"Walkers in {mod_name}:" |> print;
for walker in walkers{
f" - Walker: {walker}" |> print;
}
}

# Print nodes
nodes = Jac.context().jac_machine.list_nodes(mod_name);
if nodes{
f"Nodes in {mod_name}:" |> print;
for node in nodes{
f" - Node: {node}" |> print;
}
}
# Print edges
edges = Jac.context().jac_machine.list_edges(mod_name);
if edges{
f"Edges in {mod_name}:" |> print;
for edge in edges{
f" - Edge: {edge}" |> print;
}
}
root spawn bar_walk();
}

# Define the entry point to run the test
with entry {
test_run();
}
31 changes: 31 additions & 0 deletions jaclang/tests/test_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,3 +946,34 @@ def test_cls_method(self) -> None:
self.assertEqual("MyClass", stdout_value[0])
self.assertEqual("Hello, World1! Hello, World2!", stdout_value[1])
self.assertEqual("Hello, World! Hello, World22!", stdout_value[2])

def test_list_methods(self) -> None:
"""Test list_modules, list_walkers, list_nodes, and list_edges."""
captured_output = io.StringIO()
sys.stdout = captured_output

jac_import("foo", base_path=self.fixture_abs_path("."))

sys.stdout = sys.__stdout__
stdout_value = captured_output.getvalue()

self.assertIn(
"Module: jaclang.tests.fixtures.foo",
stdout_value,
)
self.assertIn(
"Module: jaclang.tests.fixtures.bar",
stdout_value,
)
self.assertIn(
"Walkers in jaclang.tests.fixtures.bar:\n - Walker: bar_walk",
stdout_value,
)
self.assertIn(
"Nodes in jaclang.tests.fixtures.bar:\n - Node: Item", stdout_value
)
self.assertIn(
"Edges in jaclang.tests.fixtures.bar:\n - Edge: Link", stdout_value
)
self.assertIn("Item value: 0", stdout_value)
self.assertIn("Created 5 items.", stdout_value)

0 comments on commit 39cc924

Please sign in to comment.