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

[Chore] Semtable raised into JacProgram #596

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions jaclang/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def run(
elif filename.endswith(".jir"):
with open(filename, "rb") as f:
JacMachine(base).attach_program(
JacProgram(mod_bundle=pickle.load(f), bytecode=None)
JacProgram(mod_bundle=pickle.load(f), bytecode=None, semtable=None)
)
jac_import(
target=mod,
Expand Down Expand Up @@ -145,7 +145,7 @@ def get_object(
elif filename.endswith(".jir"):
with open(filename, "rb") as f:
JacMachine(base).attach_program(
JacProgram(mod_bundle=pickle.load(f), bytecode=None)
JacProgram(mod_bundle=pickle.load(f), bytecode=None, semtable=None)
)
jac_import(
target=mod,
Expand Down Expand Up @@ -261,7 +261,7 @@ def enter(
elif filename.endswith(".jir"):
with open(filename, "rb") as f:
JacMachine(base).attach_program(
JacProgram(mod_bundle=pickle.load(f), bytecode=None)
JacProgram(mod_bundle=pickle.load(f), bytecode=None, semtable=None)
)
ret_module = jac_import(
target=mod,
Expand Down
21 changes: 13 additions & 8 deletions jaclang/compiler/passes/main/registry_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,20 @@ def enter_module(self, node: ast.Module) -> None:
def exit_module(self, node: ast.Module) -> None:
"""Save registry for each module."""
module_name = node.name
module_dir = os.path.join(
os.path.abspath(os.path.dirname(node.source.file_path)), Con.JAC_GEN_DIR
)
# module_dir = os.path.join(
# os.path.abspath(os.path.dirname(node.source.file_path)), Con.JAC_GEN_DIR
# )
module_path = node.source.file_path
# print(os.path.dirname(node.source.file_path))
try:
os.makedirs(module_dir, exist_ok=True)
with open(
os.path.join(module_dir, f"{module_name}.registry.pkl"), "wb"
) as f:
pickle.dump(node.registry, f)
# os.makedirs(module_dir, exist_ok=True)
# with open(
# os.path.join(module_dir, f"{module_name}.registry.pkl"), "wb"
# ) as f:
# pickle.dump(node.registry, f)
from jaclang.runtimelib.machine import JacMachine

JacMachine.get().get_semtable(module_path, node.registry)
except Exception as e:
self.warning(f"Can't save registry for {module_name}: {e}")
self.modules_visited.pop()
Expand Down
4 changes: 2 additions & 2 deletions jaclang/compiler/tests/test_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TestLoader(TestCase):
def test_import_basic_python(self) -> None:
"""Test basic self loading."""
JacMachine(self.fixture_abs_path(__file__)).attach_program(
JacProgram(mod_bundle=None, bytecode=None)
JacProgram(mod_bundle=None, bytecode=None, semtable=None)
)
(h,) = jac_import("fixtures.hello_world", base_path=__file__)
self.assertEqual(h.hello(), "Hello World!") # type: ignore
Expand All @@ -24,7 +24,7 @@ def test_import_basic_python(self) -> None:
def test_modules_correct(self) -> None:
"""Test basic self loading."""
JacMachine(self.fixture_abs_path(__file__)).attach_program(
JacProgram(mod_bundle=None, bytecode=None)
JacProgram(mod_bundle=None, bytecode=None, semtable=None)
)
jac_import("fixtures.hello_world", base_path=__file__)
self.assertIn(
Expand Down
4 changes: 3 additions & 1 deletion jaclang/plugin/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,9 @@ def jac_import(

jac_machine = JacMachine.get(base_path)
if not jac_machine.jac_program:
jac_machine.attach_program(JacProgram(mod_bundle=None, bytecode=None))
jac_machine.attach_program(
JacProgram(mod_bundle=None, bytecode=None, semtable=None)
)

if lng == "py":
import_result = PythonImporter(JacMachine.get()).run_import(spec)
Expand Down
15 changes: 13 additions & 2 deletions jaclang/runtimelib/machine.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Jac Machine module."""

from __future__ import annotations

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


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -63,6 +65,11 @@ def get_bytecode(
)
return None

def get_semtable(self, module_path: str, semtable: SemRegistry | None) -> None:
"""Update semtable on the attached JacProgram."""
if self.jac_program and semtable:
self.jac_program.semtable[module_path] = semtable

def load_module(self, module_name: str, module: types.ModuleType) -> None:
"""Load a module into the machine."""
self.loaded_modules[module_name] = module
Expand Down Expand Up @@ -122,11 +129,15 @@ class JacProgram:
"""Class to hold the mod_bundle and bytecode for Jac modules."""

def __init__(
self, mod_bundle: Optional[Module], bytecode: Optional[dict[str, bytes]]
self,
mod_bundle: Optional[Module],
bytecode: Optional[dict[str, bytes]],
semtable: Optional[dict[SemRegistry]],
) -> None:
"""Initialize the JacProgram object."""
self.mod_bundle = mod_bundle
self.bytecode = bytecode or {}
self.semtable = semtable or {}

def get_bytecode(
self,
Expand Down
11 changes: 11 additions & 0 deletions jaclang/tests/fixtures/registry.jac
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,14 @@ with entry {
einstein_age += 1;
einstein = Person('Albert Einstein', einstein_age);
}


::py::
from jaclang.runtimelib.machine import JacMachine

semtable = JacMachine.get().jac_program.semtable
print(semtable)

::py::


2 changes: 1 addition & 1 deletion jaclang/tests/test_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def setUp(self) -> None:
"""Set up test."""
SUPER_ROOT_ANCHOR.edges.clear()
JacMachine(self.fixture_abs_path("./")).attach_program(
JacProgram(mod_bundle=None, bytecode=None)
JacProgram(mod_bundle=None, bytecode=None, semtable=None)
)
return super().setUp()

Expand Down
Loading