Repository navigation
Expand file tree
/
Copy pathijpc.py
More file actions
315 lines (249 loc) · 9.53 KB
/
Copy pathijpc.py
File metadata and controls
315 lines (249 loc) · 9.53 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#/ijp python compiled
import subprocess, sys
from rich.console import Console
from rich.syntax import Syntax
# Python target to run from
PYTHON_TARGET = sys.executable
# Get arguments
agrs = sys.argv
# Try to find filename, compile_py
try:
filename = agrs[1]
compile_py = agrs[2]
print(filename, compile_py)
# If not, show main screen
except Exception:
print("""usage: IJP [-h] ijp_file compilation_py
VERSION INFO: Varson Alpha Next 1
positional arguments:
ijp_file What file to read IJP code from
compilation_py Compilation target, what file to compile to?
optional arguments:
-h, --help show this help message and exit
-v --version show version info
-d --docs load mkdocs server for documentation
""")
sys.exit()
# Read code
code = open(filename, "r")
codes = code.readlines()
# Define imports to compile to when you say import tkall
imports = "from tkinter import *\nfrom tkinter import colorchooser\nfrom tkinter import messagebox\nfrom tkinter import filedialog\nfrom tkinter import ttk"
"""
DIFFERENCES
write("hello world") --> print("hello world")
fun name(): --> def name():
imp --> import
fr --> from
write --> print
tksuper root title geometry color
#/ijp python --> #/ijp python compiled
OOP --> hell
"""
lines = []
console = Console()
# Errors
class error:
def __init__(self, type, line_num, content) -> None:
# Assign values
self.type = type
line_num = line_num
self.content = content
# When initalizer contains python
def invalidLangError(type, line_num, content):
error_line = f"{type} on line {line_num}: {content}"
console.rule(f"[italic blue]{type} + Tips")
console.print(f"[red bold]{error_line}")
console.print('[italic blue]You can use [italic green]#/ijp python[italic blue] [italic red](or "py" or "python" based on what the python command is on your laptop)[italic blue] to help you fix this error.')
sys.exit()
def MissingArgument(type, content):
error_line = f"{type}: {content}"
console.rule(f"[italic blue]{type} + Tips")
console.print(f"[red bold]{error_line}")
console.print('[italic blue]Please define a [italic green]variable name and "=" and content[italic blue] [italic red]for example: var name = "Joe"')
sys.exit()
def tksuper(type, content):
error_line = f"{type}: {content}"
console.rule(f"[italic blue]{type} + Tips")
console.print(f"[red bold]{error_line}")
console.print('[italic green]Example: tksuper root title geometry color')
sys.exit()
# Check for space
def check_space(string):
# counter
count = 0
# loop for search each index
for i in range(0, len(string)):
# Check each char
# is blank or not
if string[i] == " ":
count += 1
return count
# Parser
def parse(line):
# remove whitespace at begining
line_whitespace = line.lstrip()
# print --> write
if line_whitespace.startswith("write(") == True:
if line.endswith(")") == True: line = line.replace(f"write", "print", 1)
# def --> fun
elif line_whitespace.startswith("fun ") == True:
if line.endswith(":") == True: line = line.replace("fun ", "def ", 1)
# class --> OOP
elif line_whitespace.startswith("OOP") == True:
if line.endswith(":") == True: line = line.replace("OOP", "class", 1)
# print("") --> sp
elif line_whitespace == "sepr":
line = line.replace('sepr', "print('')")
# Allow init
elif line_whitespace.startswith("init(") == True:
if line.endswith(":") == True: line = line.replace("init", "def __init__")
# import --> imp
elif line_whitespace.startswith("imp ") == True:
if line == "imp tkall": line = line.replace(line, imports)
line = line.replace("imp", "import", 1)
# from --> fr
elif line.startswith("fr ") == True:
line = line.replace("fr", "from", 1)
# strings
elif line_whitespace.startswith("str") == True:
# Convert line into list
line_list = line.split(" ")
line_list = [x for x in line_list if x != ""]
try:
# Check for type
types = line_list[0]
name = line_list[1]
identifier = line_list[2]
content = " ".join(line_list[3:])
#print(types, name, identifier, content)
#print(f"Type:{types}\nName:{name}\nIdentifier:{identifier}\nContent:{content}")
prev = f"str {name} = {content}"
new = f"{name} {identifier} str({content})"
line = line.replace(prev, new, 1)
except Exception:
error.MissingArgument("Missing name, identifier, content", line)
# integers
elif line_whitespace.startswith("int") == True:
# Convert line into list
line_list = line.split(" ")
line_list = [x for x in line_list if x != ""]
try:
# Check for type
types = line_list[0]
name = line_list[1]
identifier = line_list[2]
content = " ".join(line_list[3:])
#print(types, name, identifier, content)
#print(f"Type:{types}\nName:{name}\nIdentifier:{identifier}\nContent:{content}")
prev = f"int {name} = {content}"
new = f"{name} {identifier} int({content})"
line = line.replace(prev, new, 1)
except Exception:
error.MissingArgument("Missing name, identifier, content", line)
# floats
elif line_whitespace.startswith("float") == True:
# Convert line into list
line_list = line.split(" ")
line_list = [x for x in line_list if x != ""]
try:
# Check for type
types = line_list[0]
name = line_list[1]
identifier = line_list[2]
content = " ".join(line_list[3:])
#print(types, name, identifier, content)
#print(f"Type:{types}\nName:{name}\nIdentifier:{identifier}\nContent:{content}")
prev = f"float {name} = {content}"
new = f"{name} {identifier} float({content})"
line = line.replace(prev, new, 1)
except Exception:
error.MissingArgument("Missing name, identifier, content", line)
# bool
elif line_whitespace.startswith("bool") == True:
# Convert line into list
line_list = line.split(" ")
line_list = [x for x in line_list if x != ""]
try:
# Check for type
types = line_list[0]
name = line_list[1]
identifier = line_list[2]
content = " ".join(line_list[3:])
#print(types, name, identifier, content)
#print(f"Type:{types}\nName:{name}\nIdentifier:{identifier}\nContent:{content}")
prev = f"{types} {name} = {content}"
new = f"{name} {identifier} bool({content})"
line = line.replace(prev, new, 1)
except Exception:
error.MissingArgument("Missing name, identifier, content", line)
# Check if line startswith init:
elif line_whitespace.startswith("init(") == True:
line = line.replace("init(", "def __init__(:", 1)
# check if line starts with onexec:
elif line_whitespace.startswith("onexec:") == True:
# Remove onexec:
line = line.replace('onexec:', 'if __name__ == "__main__":', 1)
# Check if line ends with :
if line.endswith(":") == True:
# Remove :
line = line.replace(":", "", 1)
# Add to onexec
onexec.append(line)
#tksuper
elif line_whitespace.startswith("tksuper "):
# Convert line to a list
lists = line.split(" ")
try:
# get root, title, size, color
var_root = lists[1]
var_title = lists[2]
var_size = lists[3]
var_color = lists[4]
line = f'''{var_root} = Tk()
root.title({var_title})
root.geometry({var_size})
root.configure(bg={var_color})'''
except Exception:
line_num = lists[0]
error.TkSuper("Rw, WindowTitle, WindowSize, and WindowColor missing", line)
line = line.replace("#/ijp python", "#/ijp python compiled")
lines.append(line)
#print(line)
# Loop through code
for line in codes:
line = line.replace("\n", "")
parse(line)
# Create new file in directory called compile_py.py
with open(compile_py, "w") as file:
# Clear all text
file.seek(0)
file.truncate()
file.close()
# Write to compile.property
for x in lines:
files = open(compile_py, "a")
files.write(f"{x}\n")
files.close()
# Print compiled code to terminal
my_code = open(compile_py, "r")
my_codes = my_code.read()
my_code.close()
# Output code
def output_code():
syntax = Syntax(my_codes, "python", theme="monokai", line_numbers=True)
console.print(syntax)
# Run python compile.py
def run_code():
subprocess.run([PYTHON_TARGET, compile_py])
if __name__ == '__main__':
# Check first line
if codes[0] == "#/ijp python\n": pass
elif codes[0] != "#/ijp python\n":
error.invalidLangError("Invalid IJP code", "1", codes[0])
sys.exit()
try:
run_code()
pass
except Exception as pyerror:
print(pyerror)