-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
241 lines (191 loc) · 6.28 KB
/
main.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import streamlit as st
from lexer import StarshipLexer
from parser import StarshipParser
from interpreter import StarshipRuntime
from errors import StarshipError
FACTORIAL_EXAMPLE = """MISSION: FactorialCalculator
CARGO:
number = 5 as METRIC
result = 1 as METRIC
counter = 1 as METRIC
FLIGHT_PLAN:
1. ORBIT number TIMES:
2. BOOST result with counter INTO result
3. DOCK counter with 1 INTO counter
4. BEAM result to DISPLAY
END_MISSION"""
QUANTUM_EXAMPLE = """MISSION: RandomGenerator
CARGO:
results = [] as CONSTELLATION
iterations = 2 as METRIC
current = 0 as METRIC
QUANTUM:
random_range = UNCERTAIN(1, 100)
quantum_state = UNCERTAIN(-1, 1)
FLIGHT_PLAN:
1. ORBIT iterations TIMES:
2. EXTRACT random_range INTO current
3. APPEND current TO results
4. BEAM current to DISPLAY
5. BEAM "Quantum states stabilized." to DISPLAY
6. ORBIT iterations TIMES:
7. EXTRACT quantum_state INTO current
8. BOOST current with 100 INTO current
9. BEAM current to DISPLAY
END_MISSION"""
ARRAY_EXAMPLE = """MISSION: ArrayManipulator
CARGO:
numbers = [1, 2, 3, 4, 5] as CONSTELLATION
squares = [] as CONSTELLATION
sum = 0 as METRIC
temp = 0 as METRIC
index = 0 as METRIC
FLIGHT_PLAN:
1. BEAM "Original array:" to DISPLAY
2. BEAM numbers to DISPLAY
3. EXTRACT numbers[0] INTO temp
4. BOOST temp with temp INTO temp
5. APPEND temp TO squares
6. EXTRACT numbers[1] INTO temp
7. BOOST temp with temp INTO temp
8. APPEND temp TO squares
9. EXTRACT numbers[2] INTO temp
10. BOOST temp with temp INTO temp
11. APPEND temp TO squares
12. EXTRACT numbers[3] INTO temp
13. BOOST temp with temp INTO temp
14. APPEND temp TO squares
15. EXTRACT numbers[4] INTO temp
16. BOOST temp with temp INTO temp
17. APPEND temp TO squares
18. BEAM "Squared array:" to DISPLAY
19. BEAM squares to DISPLAY
20. EXTRACT squares[0] INTO temp
21. DOCK sum with temp INTO sum
22. EXTRACT squares[1] INTO temp
23. DOCK sum with temp INTO sum
24. EXTRACT squares[2] INTO temp
25. DOCK sum with temp INTO sum
26. EXTRACT squares[3] INTO temp
27. DOCK sum with temp INTO sum
28. EXTRACT squares[4] INTO temp
29. DOCK sum with temp INTO sum
30. BEAM "Sum of squares:" to DISPLAY
31. BEAM sum to DISPLAY
END_MISSION"""
def run_starship_program(code):
try:
lexer = StarshipLexer(code)
tokens = lexer.tokenize()
parser = StarshipParser(tokens)
ast = parser.parse()
runtime = StarshipRuntime()
runtime.execute(ast)
output = ["🚀 Mission completed successfully!"]
output.extend(runtime.output_buffer)
print(output)
return "\n\n".join(str(line) for line in output)
except StarshipError as e:
return f"🚨 MISSION FAILURE at line {e.line}: {e.message}"
except Exception as e:
return f"🔥 Critical system failure: {str(e)}"
def main():
st.title("🚀 Starship Language Text Editor")
example_choice = st.selectbox(
"Choose an example program:",
[
"Custom Program",
"Factorial Calculator",
"Random Generator (Quantum)",
"Array Manipulator",
],
)
if example_choice == "Factorial Calculator":
initial_code = FACTORIAL_EXAMPLE
elif example_choice == "Random Generator (Quantum)":
initial_code = QUANTUM_EXAMPLE
elif example_choice == "Array Manipulator":
initial_code = ARRAY_EXAMPLE
else:
initial_code = """MISSION: FactorialCalculator
CARGO:
# Add variables here
[OPTIONAL] QUANTUM:
# Add uncertain variables here
FLIGHT_PLAN:
# Add numbered steps here
END_MISSION"""
code = st.text_area("Code Editor", initial_code, height=400)
if st.button("Launch Mission"):
st.write("### Output:")
output = run_starship_program(code)
if "Mission completed successfully" in output:
st.success(output)
else:
st.error(output)
st.sidebar.header("Quick Reference")
st.sidebar.subheader("Basic Commands")
st.sidebar.code(
"""
BEAM value to DISPLAY # Print output
EXTRACT source INTO target # Get/assign value
APPEND value TO array # Add to array
"""
)
st.sidebar.subheader("Math Operations")
st.sidebar.code(
"""
BOOST x with y INTO z # Multiply: z = x * y
DOCK x with y INTO z # Add: z = x + y
UNDOCK x with y INTO z # Subtract: z = x - y
SPLIT x with y INTO z # Divide: z = x / y
"""
)
st.sidebar.subheader("Array Operations")
st.sidebar.code(
"""
array[index] # Access array element
EXTRACT array[0] INTO x # Get array element
APPEND value TO array # Add to end of array
array = [] as CONSTELLATION # Create empty array
array = [1,2,3] as CONSTELLATION # Create with values
"""
)
st.sidebar.subheader("Variable Declaration")
st.sidebar.code(
"""
x = 0 as METRIC # Number (int/float)
msg = "hello" as SIGNAL # String
arr = [] as CONSTELLATION # Array
"""
)
st.sidebar.subheader("Control Flow")
st.sidebar.code(
"""
ORBIT count TIMES: # Loop count times
<numbered steps> # Steps must be numbered
"""
)
st.sidebar.subheader("Quantum Operations")
st.sidebar.code(
"""
QUANTUM:
x = UNCERTAIN(min, max) # Random float
EXTRACT x INTO target # Get random value
"""
)
st.sidebar.subheader("Program Structure")
st.sidebar.code(
"""
MISSION: name
CARGO: # Variables
<declarations>
[QUANTUM:] # Optional quantum vars
<declarations>
FLIGHT_PLAN: # Instructions
<numbered steps>
END_MISSION
"""
)
if __name__ == "__main__":
main()