forked from soplang/soplang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
123 lines (102 loc) · 3.79 KB
/
main.py
File metadata and controls
123 lines (102 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python3
# ======================================================
# Soplang - The Somali Programming Language
# Main entry point for both shell and file execution
# ======================================================
import argparse
import os
import sys
from src.core.lexer import Lexer
from src.core.parser import Parser
from src.core.version import VERSION
from src.runtime.interpreter import Interpreter
from src.runtime.shell import SoplangShell
def main():
"""
Main entry point for Soplang.
This function handles starting the Soplang environment:
- If run with a filename argument, it executes that file
- If run with flags, it processes the appropriate action
- If run without arguments, it launches the interactive shell
Usage:
python main.py # Start interactive shell
python main.py filename.sop # Execute a Soplang file
python main.py -e 1 # Run example number 1
python main.py -c 'qor("Hello")' # Execute code snippet
python main.py -v # Display version information
"""
# Setup command line argument parser
parser = argparse.ArgumentParser(description="Soplang Programming Language")
parser.add_argument(
"-v",
"--version",
action="store_true",
help="Display Soplang version information",
)
parser.add_argument("-f", "--file", metavar="FILE", help="Execute a Soplang file")
parser.add_argument(
"-e", "--example", metavar="N", type=int, help="Run example program number N"
)
parser.add_argument(
"-i",
"--interactive",
action="store_true",
help="Start interactive shell after executing file",
)
parser.add_argument(
"-c", "--command", metavar="CODE", help="Execute Soplang code snippet"
)
parser.add_argument("filename", nargs="?", help="Soplang file to execute")
# Parse arguments
args = parser.parse_args()
# Create shell instance
shell = SoplangShell()
# Display version information if requested
if args.version:
print("Soplang - The Somali Programming Language")
print(f"Version: {VERSION}")
print("Website: https://www.soplang.org/")
print("License: MIT")
return 0
# Execute code snippet if provided
if args.command:
# No decorative header - just execute the code directly
shell.execute_code(args.command)
return 0
# Run example if requested
if args.example is not None:
# Load example list
shell.list_examples("")
if not shell.last_examples_list:
return 1
if args.example < 1 or args.example > len(shell.last_examples_list):
print(
f"\033[31mInvalid example number. Choose between 1 and {len(shell.last_examples_list)}\033[0m"
)
return 1
# Run the example
example_file = shell.last_examples_list[args.example - 1]
example_path = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
"examples",
example_file,
)
shell.run_file(example_path)
# Start interactive shell afterward if requested
if args.interactive:
shell.run()
return 0
# Handle file if provided (either through --file or positional argument)
filename = args.file or args.filename
if filename:
# Remove redundant "Running file" message as it's handled in run_file
shell.run_file(filename)
# Start interactive shell afterward if requested
if args.interactive:
shell.run()
return 0
# No specific command given, start interactive shell
shell.run()
return 0
if __name__ == "__main__":
sys.exit(main())