Skip to content

Commit 4b0667a

Browse files
authored
Update repl.py
1 parent ab3f883 commit 4b0667a

File tree

1 file changed

+29
-38
lines changed

1 file changed

+29
-38
lines changed

hackerc/repl.py

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,19 @@
33
import tempfile
44
from rich.console import Console
55
from rich.prompt import Prompt
6-
from parser import parse_hacker_file
6+
from rich.panel import Panel
7+
from hacker_parser import parse_hacker_file # Changed from parser to hacker_parser
78

89
def run_repl(console, verbose=False):
910
console.print(Panel("Hacker Lang REPL - Type 'exit' to quit", title="REPL", style="bold magenta"))
10-
console.print("Supports: // (system deps), # (libs/include), @ (vars), = (loops), ? (conditionals), & (background), > (cmds), [ ... ], ! (comments)")
11+
console.print("Supports: // (deps), # (libs), @ (vars), = (loop), ? (if), & (bg), > (cmd), [ ] (config), ! (comment)")
1112

1213
lines = []
1314
in_config = False
1415

1516
while True:
1617
try:
17-
if in_config:
18-
prompt = "CONFIG> "
19-
else:
20-
prompt = "> "
18+
prompt = "CONFIG> " if in_config else "> "
2119
line = Prompt.ask(prompt, console=console).strip()
2220

2321
if line.lower() == 'exit':
@@ -27,9 +25,9 @@ def run_repl(console, verbose=False):
2725
in_config = True
2826
lines.append(line)
2927
continue
30-
elif line == ']':
28+
if line == ']':
3129
if not in_config:
32-
console.print("[bold red]Error: Closing ] without opening [[/bold red]")
30+
console.print("[bold red]Error: Closing ] without [[/bold red]")
3331
continue
3432
in_config = False
3533
lines.append(line)
@@ -38,55 +36,48 @@ def run_repl(console, verbose=False):
3836
lines.append(line)
3937

4038
if not in_config and line and not line.startswith('!'):
41-
with tempfile.NamedTemporaryFile(mode='w+', suffix='.hacker', delete=False) as temp_hacker:
42-
temp_hacker.write('\n'.join(lines) + '\n')
43-
temp_hacker_path = temp_hacker.name
39+
with tempfile.NamedTemporaryFile(mode='w+', suffix='.hacker', delete=False) as f:
40+
f.write('\n'.join(lines) + '\n')
41+
temp_path = f.name
4442

45-
deps, libs, vars, cmds, includes, errors = parse_hacker_file(temp_hacker_path, verbose, console)
46-
os.unlink(temp_hacker_path)
43+
deps, libs, vars_dict, cmds, includes, errors = parse_hacker_file(temp_path, verbose, console)
44+
os.unlink(temp_path)
4745

4846
if errors:
4947
console.print(Panel("\n".join(errors), title="REPL Errors", style="bold red"))
5048
continue
5149

52-
with tempfile.NamedTemporaryFile(mode='w+', suffix='.sh', delete=False) as temp_sh:
53-
temp_sh.write('#!/bin/bash\n')
54-
temp_sh.write('set -e\n')
55-
56-
for var, value in vars.items():
57-
temp_sh.write(f'export {var}="{value}"\n')
58-
50+
with tempfile.NamedTemporaryFile(mode='w+', suffix='.sh', delete=False) as f:
51+
f.write('#!/bin/bash\nset -e\n')
52+
for k, v in vars_dict.items():
53+
f.write(f'export {k}="{v}"\n')
5954
for dep in deps:
6055
if dep != "sudo":
61-
temp_sh.write(f"command -v {dep} &> /dev/null || (sudo apt update && sudo apt install -y {dep})\n")
62-
63-
for include in includes:
64-
lib_path = os.path.join(os.path.expanduser("~/.hacker-lang"), "libs", include, "main.hacker")
56+
f.write(f"command -v {dep} || (sudo apt update && sudo apt install -y {dep})\n")
57+
for inc in includes:
58+
lib_path = os.path.join(os.path.expanduser("~/.hackeros/hacker-lang"), "libs", inc, "main.hacker") # Updated path
6559
if os.path.exists(lib_path):
66-
temp_sh.write(f"# Included from {include}\n")
67-
with open(lib_path, 'r') as lib_file:
68-
temp_sh.write(lib_file.read() + "\n")
69-
60+
f.write(f"# include {inc}\n")
61+
with open(lib_path) as lf:
62+
f.write(lf.read() + "\n")
7063
for cmd in cmds:
71-
temp_sh.write(f"{cmd}\n")
72-
73-
temp_sh_path = temp_sh.name
74-
75-
os.chmod(temp_sh_path, 0o755)
64+
f.write(cmd + "\n")
65+
sh_path = f.name
7666

67+
os.chmod(sh_path, 0o755)
7768
try:
7869
env = os.environ.copy()
79-
env.update(vars)
80-
output = subprocess.check_output(['bash', temp_sh_path], env=env, text=True, stderr=subprocess.STDOUT)
81-
if output:
70+
env.update(vars_dict)
71+
output = subprocess.check_output(['bash', sh_path], env=env, text=True, stderr=subprocess.STDOUT)
72+
if output.strip():
8273
console.print(Panel(output.strip(), title="Output", style="bold green"))
8374
except subprocess.CalledProcessError as e:
8475
console.print(Panel(e.output.strip(), title="Error", style="bold red"))
8576
finally:
86-
os.unlink(temp_sh_path)
77+
os.unlink(sh_path)
8778

8879
except KeyboardInterrupt:
89-
console.print("\n[bold yellow]Use 'exit' to quit REPL[/bold yellow]")
80+
console.print("\n[bold yellow]Use 'exit' to quit[/bold yellow]")
9081

9182
console.print("[bold green]REPL exited[/bold green]")
9283
return True

0 commit comments

Comments
 (0)