Skip to content

Commit

Permalink
Fix to specify module and test case
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisIsKing committed Oct 2, 2024
1 parent 12a6aea commit 003972d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
22 changes: 5 additions & 17 deletions jac/jaclang/runtimelib/machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,31 +151,19 @@ def update_walker(
logger.warning(f"Module {module_name} not found in loaded modules.")
return ()

def spawn_node(
self,
node_name: str,
attributes: Optional[dict] = None,
) -> NodeArchitype:
def spawn_node(self, node_name: str, attributes: dict = {}, module_name: str = '__main__') -> NodeArchitype:
"""Spawn a node instance of the given node_name with attributes."""
node_class = self.get_architype("__main__", node_name)
node_class = self.get_architype(module_name, node_name)
if isinstance(node_class, type) and issubclass(node_class, NodeArchitype):
if attributes is None:
attributes = {}
node_instance = node_class(**attributes)
return node_instance
else:
raise ValueError(f"Node {node_name} not found.")

def spawn_walker(
self,
walker_name: str,
attributes: Optional[dict] = None,
) -> WalkerArchitype:
def spawn_walker(self, walker_name: str, attributes: dict = {}, module_name: str = '__main__') -> WalkerArchitype:
"""Spawn a walker instance of the given walker_name."""
walker_class = self.get_architype("__main__", walker_name)
walker_class = self.get_architype(module_name, walker_name)
if isinstance(walker_class, type) and issubclass(walker_class, WalkerArchitype):
if attributes is None:
attributes = {}
walker_instance = walker_class(**attributes)
return walker_instance
else:
Expand Down Expand Up @@ -241,4 +229,4 @@ def get_bytecode(
if result.ir.gen.py_bytecode is not None:
return marshal.loads(result.ir.gen.py_bytecode)
else:
return None
return None
30 changes: 19 additions & 11 deletions jac/jaclang/tests/fixtures/dynamic_architype.jac
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import:py from jaclang.runtimelib.machine {JacMachine}
import:jac from bar {Item}

node test_node {
has value:int;
}

walker test_walker {
can visit_nodes with `root entry {
visit [-->](`?test_node);
visit [-->];
}
}

walker child_walker :test_walker: {}

node test_node {
has value:int;

can print_value with test_walker entry {
print("Value:", f'{self.value}');
can print_value with test_node | Item entry {
print("Value:", f'{here.value}');
}
}

walker child_walker :test_walker: {}

with entry {
for value in range(1, 4) {
root ++> test_node(value=value);
root ++> Item(value=value);
}
node_obj = JacMachine.get().spawn_node('test_node', {'value': 0});
walker_obj = JacMachine.get().spawn_walker('child_walker');
node_obj = JacMachine.get().spawn_node(node_name='test_node', attributes={'value': 0}, module_name='__main__');
walker_obj = JacMachine.get().spawn_walker(walker_name='child_walker', module_name='__main__');
external_node = JacMachine.get().spawn_node(node_name='Item', attributes={'value': 0}, module_name='bar');
root ++> external_node;
root ++> node_obj;
print("Spawned Node:", node_obj);
print("Spawned Walker:", walker_obj);
print("Spawned External node:", external_node);
root spawn walker_obj;
}

0 comments on commit 003972d

Please sign in to comment.