Skip to content

Commit ab3f883

Browse files
authored
Update main.py
1 parent ec023d0 commit ab3f883

File tree

1 file changed

+64
-31
lines changed

1 file changed

+64
-31
lines changed

hackerc/main.py

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,32 @@
88
from rich.table import Table
99
from rich.syntax import Syntax
1010
from rich.text import Text
11-
from parser import parse_hacker_file
11+
from hacker_parser import parse_hacker_file # Changed from parser to hacker_parser
1212
from repl import run_repl
13+
import pygments.lexers.special # Required for bundling
1314

1415
console = Console()
1516
VERSION = "0.0.1"
16-
HACKER_DIR = os.path.expanduser("~/.hacker-lang")
17+
HACKER_DIR = os.path.expanduser("~/.hackeros/hacker-lang") # Updated path as per user
1718
BIN_DIR = os.path.join(HACKER_DIR, "bin")
1819

20+
1921
def ensure_hacker_dir():
2022
os.makedirs(BIN_DIR, exist_ok=True)
2123
os.makedirs(os.path.join(HACKER_DIR, "libs"), exist_ok=True)
2224

25+
2326
def display_welcome():
2427
banner = Text("Hacker Lang", style="bold magenta")
2528
banner.append("\nSimple scripting for Debian-based Linux", style="italic cyan")
2629
banner.append(f"\nVersion {VERSION}", style="bold blue")
2730
console.print(Panel(banner, expand=False))
2831
help_command(show_banner=False)
2932

33+
34+
# Fixed signatures: all functions take (file_path, verbose) or (verbose)
3035
def run_command(file_path, verbose=False):
31-
deps, libs, vars, cmds, includes, errors = parse_hacker_file(file_path, verbose)
36+
deps, libs, vars_dict, cmds, includes, errors = parse_hacker_file(file_path, verbose)
3237
if errors:
3338
console.print(Panel("\n".join(errors), title="Syntax Errors", style="bold red"))
3439
return False
@@ -37,13 +42,12 @@ def run_command(file_path, verbose=False):
3742
temp_sh.write('#!/bin/bash\n')
3843
temp_sh.write('set -e\n')
3944

40-
for var, value in vars.items():
45+
for var, value in vars_dict.items():
4146
temp_sh.write(f'export {var}="{value}"\n')
4247

4348
for dep in deps:
44-
check_cmd = f"command -v {dep} &> /dev/null || (sudo apt update && sudo apt install -y {dep})"
45-
if check_cmd and dep != "sudo":
46-
temp_sh.write(f"{check_cmd}\n")
49+
if dep != "sudo":
50+
temp_sh.write(f"command -v {dep} &> /dev/null || (sudo apt update && sudo apt install -y {dep})\n")
4751

4852
for include in includes:
4953
lib_path = os.path.join(HACKER_DIR, "libs", include, "main.hacker")
@@ -62,7 +66,7 @@ def run_command(file_path, verbose=False):
6266
console.print(Panel(f"Running script from {file_path}", title="Hacker Lang Run", style="bold green"))
6367
try:
6468
env = os.environ.copy()
65-
env.update(vars)
69+
env.update(vars_dict)
6670
subprocess.check_call(['bash', temp_sh_path], env=env)
6771
console.print("[bold green]Execution successful![/bold green]")
6872
return True
@@ -72,8 +76,9 @@ def run_command(file_path, verbose=False):
7276
finally:
7377
os.unlink(temp_sh_path)
7478

75-
def compile_command(file_path, output, verbose=False):
76-
deps, libs, vars, cmds, includes, errors = parse_hacker_file(file_path, verbose)
79+
80+
def compile_command(file_path, output=None, verbose=False):
81+
deps, libs, vars_dict, cmds, includes, errors = parse_hacker_file(file_path, verbose)
7782
if errors:
7883
console.print(Panel("\n".join(errors), title="Syntax Errors", style="bold red"))
7984
return False
@@ -83,7 +88,7 @@ def compile_command(file_path, output, verbose=False):
8388

8489
bin_path = os.path.join(BIN_DIR, "hacker-compiler")
8590
if not os.path.exists(bin_path):
86-
console.print("[bold red]hacker-compiler not found in ~/.hacker-lang/bin/.[/bold red]")
91+
console.print("[bold red]hacker-compiler not found in ~/.hackeros/hacker-lang/bin/.[/bold red]")
8792
return False
8893

8994
console.print(Panel(f"Compiling {file_path} to {output}", title="Hacker Lang Compile", style="bold blue"))
@@ -98,15 +103,17 @@ def compile_command(file_path, output, verbose=False):
98103
console.print(f"[bold red]Compilation failed with code {e.returncode}[/bold red]")
99104
return False
100105

106+
101107
def check_command(file_path, verbose=False):
102108
console.print(Panel(f"Checking syntax of {file_path}", title="Hacker Lang Check", style="bold cyan"))
103-
deps, libs, vars, cmds, includes, errors = parse_hacker_file(file_path, verbose)
109+
deps, libs, vars_dict, cmds, includes, errors = parse_hacker_file(file_path, verbose)
104110
if errors:
105111
console.print(Panel("\n".join(errors), title="Syntax Errors", style="bold red"))
106112
return False
107113
console.print("[bold green]Syntax check passed![/bold green]")
108114
return True
109115

116+
110117
def init_command(file_path, verbose=False):
111118
if os.path.exists(file_path):
112119
console.print(f"[bold red]File {file_path} already exists![/bold red]")
@@ -139,6 +146,7 @@ def init_command(file_path, verbose=False):
139146
console.print(f"[bold red]Failed to create {file_path}: {e}[/bold red]")
140147
return False
141148

149+
142150
def clean_command(verbose=False):
143151
console.print(Panel("Cleaning temporary files", title="Hacker Lang Clean", style="bold yellow"))
144152
temp_dir = tempfile.gettempdir()
@@ -155,10 +163,11 @@ def clean_command(verbose=False):
155163
console.print(f"[bold green]Cleaned {count} temporary files[/bold green]")
156164
return True
157165

166+
158167
def install_command(libname, verbose=False):
159168
bin_path = os.path.join(BIN_DIR, "hacker-library")
160169
if not os.path.exists(bin_path):
161-
console.print("[bold red]hacker-library not found in ~/.hacker-lang/bin/.[/bold red]")
170+
console.print("[bold red]hacker-library not found in ~/.hackeros/hacker-lang/bin/.[/bold red]")
162171
return False
163172

164173
cmd = ['node', bin_path, 'install', libname]
@@ -172,10 +181,11 @@ def install_command(libname, verbose=False):
172181
console.print(f"[bold red]Install failed with code {e.returncode}[/bold red]")
173182
return False
174183

184+
175185
def update_command(verbose=False):
176186
bin_path = os.path.join(BIN_DIR, "hacker-library")
177187
if not os.path.exists(bin_path):
178-
console.print("[bold red]hacker-library not found in ~/.hacker-lang/bin/.[/bold red]")
188+
console.print("[bold red]hacker-library not found in ~/.hackeros/hacker-lang/bin/.[/bold red]")
179189
return False
180190

181191
cmd = ['node', bin_path, 'update']
@@ -189,9 +199,11 @@ def update_command(verbose=False):
189199
console.print(f"[bold red]Update failed with code {e.returncode}[/bold red]")
190200
return False
191201

202+
192203
def version_command():
193204
console.print(Panel(f"Hacker Lang CLI version {VERSION}", title="Version", style="bold blue"))
194205

206+
195207
def help_command(show_banner=True):
196208
if show_banner:
197209
console.print(Panel("Hacker Lang CLI - Simple scripting for Debian-based Linux", title="Welcome", style="bold magenta"))
@@ -236,6 +248,7 @@ def help_command(show_banner=True):
236248
line_numbers=True
237249
))
238250

251+
239252
def main():
240253
ensure_hacker_dir()
241254
parser = argparse.ArgumentParser(
@@ -245,56 +258,76 @@ def main():
245258
parser.add_argument('--verbose', action='store_true', help='Enable verbose output')
246259
subparsers = parser.add_subparsers(dest='command')
247260

261+
# run
248262
run_parser = subparsers.add_parser('run', help='Run .hacker file')
249263
run_parser.add_argument('file', help='Path to .hacker file')
250-
run_parser.set_defaults(func=run_command)
264+
run_parser.set_defaults(func=lambda file, verbose=False: run_command(file, verbose))
251265

266+
# compile
252267
compile_parser = subparsers.add_parser('compile', help='Compile to binary')
253268
compile_parser.add_argument('file', help='Path to .hacker file')
254269
compile_parser.add_argument('-o', '--output', help='Output binary path')
255-
compile_parser.set_defaults(func=compile_command)
270+
compile_parser.set_defaults(func=lambda file, output=None, verbose=False: compile_command(file, output, verbose))
256271

272+
# check
257273
check_parser = subparsers.add_parser('check', help='Check syntax')
258274
check_parser.add_argument('file', help='Path to .hacker file')
259-
check_parser.set_defaults(func=check_command)
275+
check_parser.set_defaults(func=lambda file, verbose=False: check_command(file, verbose))
260276

277+
# init
261278
init_parser = subparsers.add_parser('init', help='Create template')
262279
init_parser.add_argument('file', help='Path to .hacker file')
263-
init_parser.set_defaults(func=init_command)
280+
init_parser.set_defaults(func=lambda file, verbose=False: init_command(file, verbose))
264281

282+
# clean
265283
clean_parser = subparsers.add_parser('clean', help='Clean temp files')
266-
clean_parser.set_defaults(func=clean_command)
284+
clean_parser.set_defaults(func=lambda verbose=False: clean_command(verbose))
267285

286+
# install
268287
install_parser = subparsers.add_parser('install', help='Install library')
269288
install_parser.add_argument('libname', help='Library name')
270-
install_parser.set_defaults(func=install_command)
289+
install_parser.set_defaults(func=lambda libname, verbose=False: install_command(libname, verbose))
271290

291+
# update
272292
update_parser = subparsers.add_parser('update', help='Update libraries')
273-
update_parser.set_defaults(func=update_command)
293+
update_parser.set_defaults(func=lambda verbose=False: update_command(verbose))
274294

295+
# repl
275296
repl_parser = subparsers.add_parser('repl', help='Start REPL')
276-
repl_parser.set_defaults(func=lambda v: run_repl(console, verbose=v))
297+
repl_parser.set_defaults(func=lambda verbose=False: run_repl(console, verbose))
277298

299+
# version
278300
version_parser = subparsers.add_parser('version', help='Show version')
279-
version_parser.set_defaults(func=version_command)
301+
version_parser.set_defaults(func=lambda: version_command())
280302

303+
# help
281304
help_parser = subparsers.add_parser('help', help='Show help')
282-
help_parser.set_defaults(func=help_command)
305+
help_parser.set_defaults(func=lambda: help_command())
283306

284307
args = parser.parse_args()
308+
285309
if not args.command:
286310
display_welcome()
287311
sys.exit(0)
288312

289-
success = True
290-
if args.command in ['run', 'compile', 'check', 'init']:
291-
success = args.func(args.file, args.output if 'output' in args else None, args.verbose)
292-
elif args.command == 'install':
293-
success = args.func(args.libname, args.verbose)
294-
elif args.command in ['clean', 'update', 'version', 'help', 'repl']:
295-
success = args.func(args.verbose)
313+
# Call function with correct arguments
314+
try:
315+
if args.command == 'compile':
316+
success = args.func(args.file, getattr(args, 'output', None), args.verbose)
317+
elif args.command in ['run', 'check', 'init']:
318+
success = args.func(args.file, args.verbose)
319+
elif args.command == 'install':
320+
success = args.func(args.libname, args.verbose)
321+
elif args.command in ['clean', 'update', 'repl']:
322+
success = args.func(args.verbose)
323+
else:
324+
success = args.func()
325+
except Exception as e:
326+
console.print(f"[bold red]Error: {e}[/bold red]")
327+
success = False
296328

297329
sys.exit(0 if success else 1)
298330

331+
299332
if __name__ == '__main__':
300333
main()

0 commit comments

Comments
 (0)