-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.yaka
233 lines (206 loc) · 7.18 KB
/
main.yaka
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
# Standard libs
import libs.c
import libs.strings.refs as r
import libs.strings.array as sarr
import libs.console
import libs.io
import libs.os
import libs.os.path as op
import libs.fileformats.ini
# --
import dtraverse as dt
import dialogs
import webui as w
directive ccode """
static void* ye_data = NULL;
void* ye_get_state() { return ye_data; }
void ye_set_state(void* data) { ye_data = data; }
"""
directive apply_nativedefine
class State:
yaksha: sr
exe: sr
@nativedefine("ye_get_state")
def get_state() -> State:
pass
@nativedefine("ye_set_state")
def set_state(data: State) -> None:
pass
@native
def append_char(s: sr, character: int) -> str:
ccode """size_t length = yk__bstr_len(nn__s);
size_t new_length = length + 1;
yk__sds result = yk__sdsnewlen(yk__bstr_get_reference(nn__s), length);
result = yk__sdsgrowzero(result, new_length);
result[length] = nn__character;
return result"""
def escape_js_string(s: sr) -> str:
length = len(s)
result: str = ""
cur: int = 0
for (i = 0; i < length; i += 1):
cur = charat(s, i)
if cur == cast("int", c.char!{"\""}):
result += "\\\""
elif cur == cast("int", c.char!{"\\"}):
result += "\\\\"
elif cur == cast("int", c.char!{"\b"}):
result += "\\b"
elif cur == cast("int", c.char!{"\f"}):
result += "\\f"
elif cur == cast("int", c.char!{"\n"}):
result += "\\n"
elif cur == cast("int", c.char!{"\r"}):
result += "\\r"
elif cur == cast("int", c.char!{"\t"}):
result += "\\t"
else:
result = append_char(result, cur)
return result
def file_entries_to_json(entries: Array[dt.Entry]) -> str:
current_dir = os.cwd()
json: str = "{\"cwd\": \""
json += escape_js_string(current_dir)
json += "\", \"files\": ["
counter = 0
for i in entries:
json += "{"
json += "\"name\": \"" + escape_js_string(i.name) + "\","
json += "\"type\": \"" + iif(i.is_dir, "d", "f") + "\""
json += "}"
if counter < len(entries) - 1:
json += ","
counter += 1
json += "]}"
return json
def get_compilation_errors(event: w.Event) -> None:
println("compiling ...")
state: State = get_state()
compiler: str = state.yaksha
arguments: Array[str] = array("str", compiler, "compile", "-d", "main.yaka")
defer sarr.del_str_array(arguments)
result: os.ProcessResult = os.run(arguments)
w.return_string(event, cast("c.CStr", result.output))
defer os.del_process_result(result)
def list_files(event: w.Event) -> None:
f = dt.listdir(".")
json: str = file_entries_to_json(f)
w.return_string(event, cast("c.CStr", json))
def get_doc_json(event: w.Event) -> None:
state: State = get_state()
exe_path: sr = state.exe
doc_path: sr = op.join(exe_path, "frontend/docs.json")
doc: str = io.readfile(doc_path)
w.return_string(event, cast("c.CStr", doc))
def get_local_doc_json(event: w.Event) -> None:
println("compiling ...")
state: State = get_state()
compiler: str = state.yaksha
arguments: Array[str] = array("str", compiler, "dump", "-a", "-i", "-p", "main.yaka")
defer sarr.del_str_array(arguments)
result: os.ProcessResult = os.run(arguments)
w.return_string(event, cast("c.CStr", result.output))
defer os.del_process_result(result)
def click_file(event: w.Event) -> None:
path_cstr: c.CStr = w.get_string(event)
path: sr = r.wrap_cstr_z(path_cstr)
content = io.readfile(path)
w.return_string(event, cast("c.CStr", content))
def save_file(event: w.Event) -> None:
path_cstr: c.CStr = w.get_string_at(event, cast("c.Size", 0))
path: sr = r.wrap_cstr_z(path_cstr)
content_cstr: c.CStr = w.get_string_at(event, cast("c.Size", 1))
content: sr = r.wrap_cstr_z(content_cstr)
success: bool = io.writefile(path, content)
w.return_string(event, iif(success, c.cstr!{"OK"}, c.cstr!{"Failed to save file"}))
def show_open_folder_dialog(event: w.Event) -> None:
path = os.cwd()
selected: c.CStr = dialogs.select_folder_dialog(c.cstr!{"YakshaEditor"}, cast("c.CStr",path))
if selected == None:
w.return_string(event, c.cstr!{""})
else:
w.return_string(event, selected)
def explore(event: w.Event) -> None:
path = os.cwd()
program: str
if os.is_windows():
program = os.which("explorer")
elif os.is_macos():
program = os.which("open")
else:
program = os.which("xdg-open")
if program == "":
w.return_string(event, c.cstr!{"Failed to locate file explorer"})
return
program += " \""
program += path
program += "\""
println("running: " + program)
c.system(cast("c.CStr", program))
def create_new_file(event: w.Event) -> None:
default_filename: c.CStr = c.cstr!{"newfile.yaka"}
filename = dialogs.input_box(c.cstr!{"YakshaEditor"}, c.cstr!{"Enter file name"}, default_filename)
if filename == None:
w.return_string(event, c.cstr!{""})
return
path: sr = r.wrap_cstr_z(filename)
success: bool = io.writefile(path, "")
w.return_string(event, iif(success, filename, c.cstr!{""}))
def change_folder(event: w.Event) -> None:
path_cstr: c.CStr = w.get_string(event)
path: sr = r.wrap_cstr_z(path_cstr)
success: bool = os.chdir(path)
if not success:
w.return_string(event, c.cstr!{"Failed to change directory"})
else:
w.return_string(event, c.cstr!{"OK"})
def get_yaksha_binary(exe_path: sr) -> str:
ini_path: str = op.join(exe_path, "settings.ini")
from_which = os.which("yaksha")
# if settings.ini does not exist, try to find yaksha in PATH
if not op.exists(ini_path):
return from_which
data = ini.from_str(io.readfile(ini_path))
defer ini.del_ini(data)
path = ini.get(data, "compiler", "yaksha")
if len(path) > 0 and op.executable(path):
return path
return from_which
def main() -> int:
exe_path: str = os.exe_path()
yaksha_bin: str = get_yaksha_binary(exe_path)
print("exe_path = ")
println(exe_path)
state = State()
state.yaksha = yaksha_bin
state.exe = exe_path
defer del state
set_state(state)
mw = w.new_window()
println("created window")
if state.yaksha == "":
println("failed to find yaksha binary")
return -1
else:
println("yaksha binary found at: " + state.yaksha)
frontend: str = op.join(exe_path, "frontend")
w.set_root_folder(mw, cast("c.CStr", frontend))
w.show(mw, c.cstr!{"index.html"})
# bind events
w.bind(mw, c.cstr!{"listfiles"}, list_files)
w.bind(mw, c.cstr!{"clickfile"}, click_file)
w.bind(mw, c.cstr!{"savefile"}, save_file)
w.bind(mw, c.cstr!{"cd"}, change_folder)
w.bind(mw, c.cstr!{"openfolder"}, show_open_folder_dialog)
w.bind(mw, c.cstr!{"explore"}, explore)
w.bind(mw, c.cstr!{"newfile"}, create_new_file)
w.bind(mw, c.cstr!{"getdocjson"}, get_doc_json)
w.bind(mw, c.cstr!{"getlocaljson"}, get_local_doc_json)
w.bind(mw, c.cstr!{"compile"}, get_compilation_errors)
# exit
println("waiting ... ")
w.wait()
println("done")
w.clean()
println("cleaned")
return 0