1 | // MIT License |
2 | |
3 | // Copyright (c) 2017 Vadim Grigoruk @nesbox // grigoruk@gmail.com |
4 | |
5 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
6 | // of this software and associated documentation files (the "Software"), to deal |
7 | // in the Software without restriction, including without limitation the rights |
8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
9 | // copies of the Software, and to permit persons to whom the Software is |
10 | // furnished to do so, subject to the following conditions: |
11 | |
12 | // The above copyright notice and this permission notice shall be included in all |
13 | // copies or substantial portions of the Software. |
14 | |
15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
21 | // SOFTWARE. |
22 | |
23 | #include "core/core.h" |
24 | |
25 | // Moonscript requires Lua |
26 | #if defined(TIC_BUILD_WITH_LUA) |
27 | |
28 | #include "lua_api.h" |
29 | |
30 | #if defined(TIC_BUILD_WITH_MOON) |
31 | |
32 | static const char _ms_loadstring[] = "_ms_loadstring" ; |
33 | |
34 | static inline bool isalnum_(char c) {return isalnum(c) || c == '_';} |
35 | |
36 | #include "moonscript.h" |
37 | |
38 | #define MOON_CODE(...) #__VA_ARGS__ |
39 | |
40 | static const char* execute_moonscript_src = MOON_CODE( |
41 | local fn, err = require('moonscript.base').loadstring(...) |
42 | |
43 | if not fn then |
44 | error(err) |
45 | end |
46 | return fn() |
47 | ); |
48 | |
49 | static void setloaded(lua_State* l, char* name) |
50 | { |
51 | s32 top = lua_gettop(l); |
52 | lua_getglobal(l, "package" ); |
53 | lua_getfield(l, -1, "loaded" ); |
54 | lua_getfield(l, -1, name); |
55 | if (lua_isnil(l, -1)) { |
56 | lua_pop(l, 1); |
57 | lua_pushvalue(l, top); |
58 | lua_setfield(l, -2, name); |
59 | } |
60 | |
61 | lua_settop(l, top); |
62 | } |
63 | |
64 | static void evalMoonscript(tic_mem* tic, const char* code) { |
65 | tic_core* core = (tic_core*)tic; |
66 | lua_State* lua = core->currentVM; |
67 | |
68 | lua_getglobal(lua, _ms_loadstring); |
69 | |
70 | lua_pushstring(lua, code); |
71 | if (lua_pcall(lua, 1, 1, 0) != LUA_OK) |
72 | { |
73 | const char* msg = lua_tostring(lua, -1); |
74 | if (msg) |
75 | { |
76 | core->data->error(core->data->data, msg); |
77 | } |
78 | } |
79 | } |
80 | |
81 | static bool initMoonscript(tic_mem* tic, const char* code) |
82 | { |
83 | tic_core* core = (tic_core*)tic; |
84 | closeLua(tic); |
85 | |
86 | lua_State* lua = core->currentVM = luaL_newstate(); |
87 | lua_open_builtins(lua); |
88 | |
89 | luaopen_lpeg(lua); |
90 | setloaded(lua, "lpeg" ); |
91 | |
92 | initLuaAPI(core); |
93 | |
94 | { |
95 | lua_State* moon = lua; |
96 | |
97 | lua_settop(moon, 0); |
98 | |
99 | if (luaL_loadbuffer(moon, (const char *)moonscript_lua, moonscript_lua_len, "moonscript.lua" ) != LUA_OK) |
100 | { |
101 | core->data->error(core->data->data, "failed to load moonscript.lua" ); |
102 | return false; |
103 | } |
104 | |
105 | lua_call(moon, 0, 0); |
106 | |
107 | if (luaL_loadbuffer(moon, execute_moonscript_src, strlen(execute_moonscript_src), "execute_moonscript" ) != LUA_OK) |
108 | { |
109 | core->data->error(core->data->data, "failed to load moonscript compiler" ); |
110 | return false; |
111 | } |
112 | |
113 | lua_setglobal(lua, _ms_loadstring); |
114 | lua_getglobal(lua, _ms_loadstring); |
115 | |
116 | lua_pushstring(moon, code); |
117 | if (lua_pcall(moon, 1, 1, 0) != LUA_OK) |
118 | { |
119 | const char* msg = lua_tostring(moon, -1); |
120 | |
121 | if (msg) |
122 | { |
123 | core->data->error(core->data->data, msg); |
124 | return false; |
125 | } |
126 | } |
127 | } |
128 | |
129 | return true; |
130 | } |
131 | |
132 | static const char* const MoonKeywords [] = |
133 | { |
134 | "false" , "true" , "nil" , "local" , "return" , |
135 | "break" , "continue" , "for" , "while" , |
136 | "if" , "else" , "elseif" , "unless" , "switch" , |
137 | "when" , "and" , "or" , "in" , "do" , |
138 | "not" , "super" , "try" , "catch" , |
139 | "with" , "export" , "import" , "then" , |
140 | "from" , "class" , "extends" , "new" , "using" , |
141 | }; |
142 | |
143 | static const tic_outline_item* getMoonOutline(const char* code, s32* size) |
144 | { |
145 | enum{Size = sizeof(tic_outline_item)}; |
146 | |
147 | *size = 0; |
148 | |
149 | static tic_outline_item* items = NULL; |
150 | |
151 | if(items) |
152 | { |
153 | free(items); |
154 | items = NULL; |
155 | } |
156 | |
157 | const char* ptr = code; |
158 | |
159 | while(true) |
160 | { |
161 | static const char FuncString[] = "=->" ; |
162 | |
163 | ptr = strstr(ptr, FuncString); |
164 | |
165 | if(ptr) |
166 | { |
167 | const char* end = ptr; |
168 | |
169 | ptr += sizeof FuncString - 1; |
170 | |
171 | while(end >= code && !isalnum_(*end)) end--; |
172 | |
173 | const char* start = end; |
174 | |
175 | for (const char* val = start-1; val >= code && (isalnum_(*val)); val--, start--); |
176 | |
177 | if(end > start) |
178 | { |
179 | items = realloc(items, (*size + 1) * Size); |
180 | |
181 | items[*size].pos = start; |
182 | items[*size].size = (s32)(end - start + 1); |
183 | |
184 | (*size)++; |
185 | } |
186 | } |
187 | else break; |
188 | } |
189 | |
190 | return items; |
191 | } |
192 | |
193 | tic_script_config MoonSyntaxConfig = |
194 | { |
195 | .id = 13, |
196 | .name = "moon" , |
197 | .fileExtension = ".moon" , |
198 | .projectComment = "--" , |
199 | { |
200 | .init = initMoonscript, |
201 | .close = closeLua, |
202 | .tick = callLuaTick, |
203 | .boot = callLuaBoot, |
204 | |
205 | .callback = |
206 | { |
207 | .scanline = callLuaScanline, |
208 | .border = callLuaBorder, |
209 | .menu = callLuaMenu, |
210 | }, |
211 | }, |
212 | |
213 | .getOutline = getMoonOutline, |
214 | .eval = evalMoonscript, |
215 | |
216 | .blockCommentStart = NULL, |
217 | .blockCommentEnd = NULL, |
218 | .blockCommentStart2 = NULL, |
219 | .blockCommentEnd2 = NULL, |
220 | .blockStringStart = NULL, |
221 | .blockStringEnd = NULL, |
222 | .singleComment = "--" , |
223 | .blockEnd = NULL, |
224 | |
225 | .keywords = MoonKeywords, |
226 | .keywordsCount = COUNT_OF(MoonKeywords), |
227 | }; |
228 | |
229 | #endif /* defined(TIC_BUILD_WITH_MOON) */ |
230 | |
231 | #endif /* defined(TIC_BUILD_WITH_LUA) */ |
232 | |