Skip to content

Commit

Permalink
Updated files
Browse files Browse the repository at this point in the history
  • Loading branch information
Thamirawaran authored and Thamirawaran committed Dec 30, 2024
1 parent 1942ca1 commit 69f4528
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
46 changes: 30 additions & 16 deletions jac/jaclang/compiler/passes/main/import_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""

import ast as py_ast
import marshal
import os
import pathlib
from typing import Optional
Expand Down Expand Up @@ -463,23 +464,36 @@ def __load_builtins(self) -> None:
from jaclang.compiler.passes.main import PyastBuildPass

assert isinstance(self.ir, ast.Module)

file_to_raise = str(
pathlib.Path(os.path.dirname(__file__)).parent.parent.parent
/ "vendor"
/ "mypy"
/ "typeshed"
/ "stdlib"
/ "builtins.pyi"
base_dir = pathlib.Path(os.path.dirname(__file__)).parent.parent.parent
jac_gen_dir = (
base_dir / "vendor" / "mypy" / "typeshed" / "stdlib" / "__jac__gen__"
)
with open(file_to_raise, "r", encoding="utf-8") as f:
file_source = f.read()
mod = PyastBuildPass(
input_ir=ast.PythonModuleAst(
py_ast.parse(file_source),
orig_src=ast.JacSource(file_source, file_to_raise),
),
).ir
mod_file_path = jac_gen_dir / "builtins_mod.jbc"
jac_gen_dir.mkdir(parents=True, exist_ok=True)
if mod_file_path.exists():
print(f"Loading `mod` from {mod_file_path}")
with open(mod_file_path, "rb") as mod_file:
mod = marshal.load(mod_file)
else:
file_to_raise = str(
pathlib.Path(os.path.dirname(__file__)).parent.parent.parent
/ "vendor"
/ "mypy"
/ "typeshed"
/ "stdlib"
/ "builtins.pyi"
)
with open(file_to_raise, "r", encoding="utf-8") as f:
file_source = f.read()
mod = PyastBuildPass(
input_ir=ast.PythonModuleAst(
py_ast.parse(file_source),
orig_src=ast.JacSource(file_source, file_to_raise),
),
).ir
with open(mod_file_path, "wb") as mod_file:
marshal.dump(mod, mod_file)
print(f"`mod` has been stored in {mod_file_path}")
mod.parent = self.ir
SubNodeTabPass(input_ir=mod, prior=self)
SymTabBuildPass(input_ir=mod, prior=self)
Expand Down
17 changes: 17 additions & 0 deletions jac/jaclang/compiler/passes/main/sub_node_tab_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
pass and is not required for any other pass to work.
"""

import marshal
import os
import pickle
from copy import copy

import jaclang.compiler.absyntree as ast
Expand All @@ -18,6 +21,7 @@ def enter_node(self, node: ast.AstNode) -> None:
"""Table builder."""
super().enter_node(node)
node._sub_node_tab = {} # clears on entry
self.dumped_modules: dict = {}

def exit_node(self, node: ast.AstNode) -> None:
"""Table builder."""
Expand All @@ -34,3 +38,16 @@ def exit_node(self, node: ast.AstNode) -> None:
node._sub_node_tab[type(i)].append(i)
else:
node._sub_node_tab[type(i)] = [i]
if isinstance(node, ast.Module) and node.mod_deps:
self.dump_module(node)

def dump_module(self, mod: ast.Module) -> None:
"""Dump module dependencies."""
full_path = next(iter(mod.mod_deps))
folder_path = os.path.join(os.path.dirname(full_path), "__jac_gen__")
os.makedirs(folder_path, exist_ok=True)
bc_file = os.path.join(folder_path, mod.mod_deps[full_path].name + ".jbc")
for i in mod.mod_deps:
self.dumped_modules[i] = pickle.dumps(mod.mod_deps[i])
with open(bc_file, "wb") as f:
marshal.dump(self.dumped_modules, f)
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ with entry {
g = fool_me();
c4: pygame_mock.color.Color = color.Color(g.CONST_VALUE2);
a: argparse.ArgumentParser = argparse.ArgumentParser();
WIN_HEIGHT = 600;
WIN_WIDTH = 800;
print("Hello", 1, b);
pygame_mock.display.set_mode(WIN_WIDTH, WIN_HEIGHT);
print(os.path.isfile("/jj"));
Expand Down

0 comments on commit 69f4528

Please sign in to comment.