Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce import_cwd #163

Merged
merged 2 commits into from
Jun 1, 2024
Merged
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__pycache__
.venv
.vscode
hello.txt # Used by a test
hello.txt
.ruff_cache
.mypy_cache
radon.html
Expand Down
3 changes: 2 additions & 1 deletion core/builtin_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ def run(
entry_pos: Optional[Position] = None,
return_result: bool = False,
hide_paths: bool = False,
import_cwd: Optional[str] = None,
):
from core.interpreter import Interpreter # Lazy import

Expand All @@ -557,7 +558,7 @@ def run(
interpreter = Interpreter()
# context = Context('<program>')
# context.symbol_table = global_symbol_table
context = Context("<program>", context, entry_pos)
context = Context("<program>", context, entry_pos, import_cwd=import_cwd)
if context.parent is None:
context.symbol_table = global_symbol_table
else:
Expand Down
10 changes: 4 additions & 6 deletions core/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,11 @@ def visit_ImportNode(self, node: ImportNode, context: Context) -> RTResult[Value
module_file = module_name.split("/")[-1]
module_path = os.path.dirname(os.path.realpath(module_name))

global CURRENT_DIR
# if CURRENT_DIR is None:
CURRENT_DIR = module_path

module_name = os.path.join(CURRENT_DIR, module_file)
module_name = os.path.join(context.get_import_cwd(), module_file)
else:
# For STDLIB modules
module_name = os.path.join(BASE_DIR, "stdlib", f"{module_name}.rn")
module_path = os.path.join(BASE_DIR, "stdlib")
module_name = os.path.join(module_path, f"{module_name}.rn")

with open(module_name, "r") as f:
script = f.read()
Expand All @@ -241,6 +238,7 @@ def visit_ImportNode(self, node: ImportNode, context: Context) -> RTResult[Value
symbol_table = create_global_symbol_table()
new_ctx = Context(module_name, context, node.pos_start)
new_ctx.symbol_table = symbol_table
new_ctx.import_cwd = module_path
_, error, should_exit = run(module_name, script, context=new_ctx)

if error:
Expand Down
7 changes: 7 additions & 0 deletions core/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1754,3 +1754,10 @@ class Context:
parent: Optional[Context] = None
parent_entry_pos: Optional[Position] = None
symbol_table: SymbolTable = field(default_factory=SymbolTable)
import_cwd: Optional[str] = "."

def get_import_cwd(self) -> str:
if self.import_cwd is not None:
return self.import_cwd
assert self.parent is not None, "Root context must always have import_cwd"
return self.parent.get_import_cwd()
4 changes: 2 additions & 2 deletions examples/files.rn
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

f = File("examples/files.rn")
contents = f.read()
const f = File("examples/files.rn")
const contents = f.read()
f.close()

print(contents)
Expand Down
File renamed without changes.
8 changes: 3 additions & 5 deletions radon.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,10 @@ def main(argv: list[str]) -> None:
pos = Position(0, 0, 0, "<argv>", "<argv>")
base_core.global_symbol_table.set("argv", base_core.radonify(argv, pos, pos, Context("<global>")))
if source_file is not None:
head, tail = os.path.split(source_file)
if head != "":
os.chdir(head)
with open(tail, "r") as f:
head, _ = os.path.split(source_file)
with open(source_file, "r") as f:
source = f.read()
(result, error, should_exit) = base_core.run(source_file, source)
(result, error, should_exit) = base_core.run(source_file, source, import_cwd=head)

if error:
print(error.as_string())
Expand Down