this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #1670 from joshgoebel/moonscript_sep_file

(chore) move moonscript into separate C file

authored by

Vadim Grigoruk and committed by
GitHub
666705db a54f4b1a

+219 -187
+1
CMakeLists.txt
··· 302 302 ${TIC80CORE_DIR}/core/sound.c 303 303 ${TIC80CORE_DIR}/api/js.c 304 304 ${TIC80CORE_DIR}/api/lua.c 305 + ${TIC80CORE_DIR}/api/moonscript.c 305 306 ${TIC80CORE_DIR}/api/wren.c 306 307 ${TIC80CORE_DIR}/api/squirrel.c 307 308 ${TIC80CORE_DIR}/tic.c
+10 -185
src/api/lua.c
··· 1394 1394 return 0; 1395 1395 } 1396 1396 1397 - static void lua_open_builtins(lua_State *lua) 1397 + void lua_open_builtins(lua_State *lua) 1398 1398 { 1399 1399 static const luaL_Reg loadedlibs[] = 1400 1400 { ··· 1415 1415 } 1416 1416 } 1417 1417 1418 - static void initAPI(tic_core* core) 1418 + void initLuaAPI(tic_core* core) 1419 1419 { 1420 1420 #define API_FUNC_DEF(name, ...) {lua_ ## name, #name}, 1421 1421 static const struct{lua_CFunction func; const char* name;} ApiItems[] = {TIC_API_LIST(API_FUNC_DEF)}; ··· 1428 1428 registerLuaFunction(core, lua_loadfile, "loadfile"); 1429 1429 } 1430 1430 1431 - static void closeLua(tic_mem* tic) 1431 + void closeLua(tic_mem* tic) 1432 1432 { 1433 1433 tic_core* core = (tic_core*)tic; 1434 1434 ··· 1448 1448 lua_State* lua = core->currentVM = luaL_newstate(); 1449 1449 lua_open_builtins(lua); 1450 1450 1451 - initAPI(core); 1451 + initLuaAPI(core); 1452 1452 1453 1453 { 1454 1454 lua_State* lua = core->currentVM; ··· 1500 1500 return status; 1501 1501 } 1502 1502 1503 - static void callLuaTick(tic_mem* tic) 1503 + void callLuaTick(tic_mem* tic) 1504 1504 { 1505 1505 tic_core* core = (tic_core*)tic; 1506 1506 ··· 1522 1522 } 1523 1523 } 1524 1524 1525 - static void callLuaScanlineName(tic_mem* tic, s32 row, void* data, const char* name) 1525 + void callLuaScanlineName(tic_mem* tic, s32 row, void* data, const char* name) 1526 1526 { 1527 1527 tic_core* core = (tic_core*)tic; 1528 1528 lua_State* lua = core->currentVM; ··· 1540 1540 } 1541 1541 } 1542 1542 1543 - static void callLuaScanline(tic_mem* tic, s32 row, void* data) 1543 + void callLuaScanline(tic_mem* tic, s32 row, void* data) 1544 1544 { 1545 1545 callLuaScanlineName(tic, row, data, SCN_FN); 1546 1546 ··· 1548 1548 callLuaScanlineName(tic, row, data, "scanline"); 1549 1549 } 1550 1550 1551 - static void callLuaBorder(tic_mem* tic, s32 row, void* data) 1551 + void callLuaBorder(tic_mem* tic, s32 row, void* data) 1552 1552 { 1553 1553 callLuaScanlineName(tic, row, data, BDR_FN); 1554 1554 } 1555 1555 1556 - static void callLuaOverline(tic_mem* tic, void* data) 1556 + void callLuaOverline(tic_mem* tic, void* data) 1557 1557 { 1558 1558 tic_core* core = (tic_core*)tic; 1559 1559 lua_State* lua = core->currentVM; ··· 1693 1693 return &LuaSyntaxConfig; 1694 1694 } 1695 1695 1696 - #if defined(TIC_BUILD_WITH_MOON) 1697 - 1698 - #include "moonscript.h" 1699 - 1700 - #define MOON_CODE(...) #__VA_ARGS__ 1701 - 1702 - static const char* execute_moonscript_src = MOON_CODE( 1703 - local fn, err = require('moonscript.base').loadstring(...) 1704 - 1705 - if not fn then 1706 - error(err) 1707 - end 1708 - return fn() 1709 - ); 1710 - 1711 - static void setloaded(lua_State* l, char* name) 1712 - { 1713 - s32 top = lua_gettop(l); 1714 - lua_getglobal(l, "package"); 1715 - lua_getfield(l, -1, "loaded"); 1716 - lua_getfield(l, -1, name); 1717 - if (lua_isnil(l, -1)) { 1718 - lua_pop(l, 1); 1719 - lua_pushvalue(l, top); 1720 - lua_setfield(l, -2, name); 1721 - } 1722 - 1723 - lua_settop(l, top); 1724 - } 1725 - 1726 - static bool initMoonscript(tic_mem* tic, const char* code) 1727 - { 1728 - tic_core* core = (tic_core*)tic; 1729 - closeLua(tic); 1730 - 1731 - lua_State* lua = core->currentVM = luaL_newstate(); 1732 - lua_open_builtins(lua); 1733 - 1734 - luaopen_lpeg(lua); 1735 - setloaded(lua, "lpeg"); 1736 - 1737 - initAPI(core); 1738 - 1739 - { 1740 - lua_State* moon = core->currentVM; 1741 - 1742 - lua_settop(moon, 0); 1743 - 1744 - if (luaL_loadbuffer(moon, (const char *)moonscript_lua, moonscript_lua_len, "moonscript.lua") != LUA_OK) 1745 - { 1746 - core->data->error(core->data->data, "failed to load moonscript.lua"); 1747 - return false; 1748 - } 1749 - 1750 - lua_call(moon, 0, 0); 1751 - 1752 - if (luaL_loadbuffer(moon, execute_moonscript_src, strlen(execute_moonscript_src), "execute_moonscript") != LUA_OK) 1753 - { 1754 - core->data->error(core->data->data, "failed to load moonscript compiler"); 1755 - return false; 1756 - } 1757 - 1758 - lua_pushstring(moon, code); 1759 - if (lua_pcall(moon, 1, 1, 0) != LUA_OK) 1760 - { 1761 - const char* msg = lua_tostring(moon, -1); 1762 - 1763 - if (msg) 1764 - { 1765 - core->data->error(core->data->data, msg); 1766 - return false; 1767 - } 1768 - } 1769 - } 1770 - 1771 - return true; 1772 - } 1773 - 1774 - static const char* const MoonKeywords [] = 1775 - { 1776 - "false", "true", "nil", "local", "return", 1777 - "break", "continue", "for", "while", 1778 - "if", "else", "elseif", "unless", "switch", 1779 - "when", "and", "or", "in", "do", 1780 - "not", "super", "try", "catch", 1781 - "with", "export", "import", "then", 1782 - "from", "class", "extends", "new", "using", 1783 - }; 1784 - 1785 - static const tic_outline_item* getMoonOutline(const char* code, s32* size) 1786 - { 1787 - enum{Size = sizeof(tic_outline_item)}; 1788 - 1789 - *size = 0; 1790 - 1791 - static tic_outline_item* items = NULL; 1792 - 1793 - if(items) 1794 - { 1795 - free(items); 1796 - items = NULL; 1797 - } 1798 - 1799 - const char* ptr = code; 1800 - 1801 - while(true) 1802 - { 1803 - static const char FuncString[] = "=->"; 1804 - 1805 - ptr = strstr(ptr, FuncString); 1806 - 1807 - if(ptr) 1808 - { 1809 - const char* end = ptr; 1810 - 1811 - ptr += sizeof FuncString - 1; 1812 - 1813 - while(end >= code && !isalnum_(*end)) end--; 1814 - 1815 - const char* start = end; 1816 - 1817 - for (const char* val = start-1; val >= code && (isalnum_(*val)); val--, start--); 1818 - 1819 - if(end > start) 1820 - { 1821 - items = realloc(items, (*size + 1) * Size); 1822 - 1823 - items[*size].pos = start; 1824 - items[*size].size = (s32)(end - start + 1); 1825 - 1826 - (*size)++; 1827 - } 1828 - } 1829 - else break; 1830 - } 1831 - 1832 - return items; 1833 - } 1834 - 1835 - tic_script_config MoonSyntaxConfig = 1836 - { 1837 - .name = "moon", 1838 - .fileExtension = ".moon", 1839 - .projectComment = "--", 1840 - .init = initMoonscript, 1841 - .close = closeLua, 1842 - .tick = callLuaTick, 1843 - .callback = 1844 - { 1845 - .scanline = callLuaScanline, 1846 - .border = callLuaBorder, 1847 - .overline = callLuaOverline, 1848 - }, 1849 - 1850 - .getOutline = getMoonOutline, 1851 - .eval = NULL, 1852 - 1853 - .blockCommentStart = NULL, 1854 - .blockCommentEnd = NULL, 1855 - .blockCommentStart2 = NULL, 1856 - .blockCommentEnd2 = NULL, 1857 - .blockStringStart = NULL, 1858 - .blockStringEnd = NULL, 1859 - .singleComment = "--", 1860 - 1861 - .keywords = MoonKeywords, 1862 - .keywordsCount = COUNT_OF(MoonKeywords), 1863 - }; 1864 - 1865 - const tic_script_config* get_moon_script_config() 1866 - { 1867 - return &MoonSyntaxConfig; 1868 - } 1869 - 1870 - #endif /* defined(TIC_BUILD_WITH_MOON) */ 1871 1696 1872 1697 #if defined(TIC_BUILD_WITH_FENNEL) 1873 1698 ··· 1893 1718 lua_State* lua = core->currentVM = luaL_newstate(); 1894 1719 lua_open_builtins(lua); 1895 1720 1896 - initAPI(core); 1721 + initLuaAPI(core); 1897 1722 1898 1723 { 1899 1724 lua_State* fennel = core->currentVM;
+20
src/api/lua_api.h
··· 1 + #ifndef lua_api_h 2 + 3 + #include <stdlib.h> 4 + #include <string.h> 5 + #include <lua.h> 6 + #include <lauxlib.h> 7 + #include <lualib.h> 8 + #include <ctype.h> 9 + 10 + extern void callLuaTick(tic_mem* tic); 11 + extern void callLuaScanlineName(tic_mem* tic, s32 row, void* data, const char* name); 12 + extern void callLuaScanline(tic_mem* tic, s32 row, void* data); 13 + extern void callLuaBorder(tic_mem* tic, s32 row, void* data); 14 + extern void callLuaOverline(tic_mem* tic, void* data); 15 + extern void closeLua(tic_mem* tic); 16 + extern void callLuaTick(tic_mem* tic); 17 + extern void lua_open_builtins(lua_State *lua); 18 + 19 + #define lua_api_h 20 + #endif
+186
src/api/moonscript.c
··· 1 + #include "core/core.h" 2 + 3 + // Moonscript requires Lua 4 + #if defined(TIC_BUILD_WITH_LUA) 5 + 6 + #include "lua_api.h" 7 + 8 + #if defined(TIC_BUILD_WITH_MOON) 9 + 10 + static inline bool isalnum_(char c) {return isalnum(c) || c == '_';} 11 + 12 + #include "moonscript.h" 13 + 14 + #define MOON_CODE(...) #__VA_ARGS__ 15 + 16 + static const char* execute_moonscript_src = MOON_CODE( 17 + local fn, err = require('moonscript.base').loadstring(...) 18 + 19 + if not fn then 20 + error(err) 21 + end 22 + return fn() 23 + ); 24 + 25 + static void setloaded(lua_State* l, char* name) 26 + { 27 + s32 top = lua_gettop(l); 28 + lua_getglobal(l, "package"); 29 + lua_getfield(l, -1, "loaded"); 30 + lua_getfield(l, -1, name); 31 + if (lua_isnil(l, -1)) { 32 + lua_pop(l, 1); 33 + lua_pushvalue(l, top); 34 + lua_setfield(l, -2, name); 35 + } 36 + 37 + lua_settop(l, top); 38 + } 39 + 40 + static bool initMoonscript(tic_mem* tic, const char* code) 41 + { 42 + tic_core* core = (tic_core*)tic; 43 + closeLua(tic); 44 + 45 + lua_State* lua = core->currentVM = luaL_newstate(); 46 + lua_open_builtins(lua); 47 + 48 + luaopen_lpeg(lua); 49 + setloaded(lua, "lpeg"); 50 + 51 + initLuaAPI(core); 52 + 53 + { 54 + lua_State* moon = core->currentVM; 55 + 56 + lua_settop(moon, 0); 57 + 58 + if (luaL_loadbuffer(moon, (const char *)moonscript_lua, moonscript_lua_len, "moonscript.lua") != LUA_OK) 59 + { 60 + core->data->error(core->data->data, "failed to load moonscript.lua"); 61 + return false; 62 + } 63 + 64 + lua_call(moon, 0, 0); 65 + 66 + if (luaL_loadbuffer(moon, execute_moonscript_src, strlen(execute_moonscript_src), "execute_moonscript") != LUA_OK) 67 + { 68 + core->data->error(core->data->data, "failed to load moonscript compiler"); 69 + return false; 70 + } 71 + 72 + lua_pushstring(moon, code); 73 + if (lua_pcall(moon, 1, 1, 0) != LUA_OK) 74 + { 75 + const char* msg = lua_tostring(moon, -1); 76 + 77 + if (msg) 78 + { 79 + core->data->error(core->data->data, msg); 80 + return false; 81 + } 82 + } 83 + } 84 + 85 + return true; 86 + } 87 + 88 + static const char* const MoonKeywords [] = 89 + { 90 + "false", "true", "nil", "local", "return", 91 + "break", "continue", "for", "while", 92 + "if", "else", "elseif", "unless", "switch", 93 + "when", "and", "or", "in", "do", 94 + "not", "super", "try", "catch", 95 + "with", "export", "import", "then", 96 + "from", "class", "extends", "new", "using", 97 + }; 98 + 99 + static const tic_outline_item* getMoonOutline(const char* code, s32* size) 100 + { 101 + enum{Size = sizeof(tic_outline_item)}; 102 + 103 + *size = 0; 104 + 105 + static tic_outline_item* items = NULL; 106 + 107 + if(items) 108 + { 109 + free(items); 110 + items = NULL; 111 + } 112 + 113 + const char* ptr = code; 114 + 115 + while(true) 116 + { 117 + static const char FuncString[] = "=->"; 118 + 119 + ptr = strstr(ptr, FuncString); 120 + 121 + if(ptr) 122 + { 123 + const char* end = ptr; 124 + 125 + ptr += sizeof FuncString - 1; 126 + 127 + while(end >= code && !isalnum_(*end)) end--; 128 + 129 + const char* start = end; 130 + 131 + for (const char* val = start-1; val >= code && (isalnum_(*val)); val--, start--); 132 + 133 + if(end > start) 134 + { 135 + items = realloc(items, (*size + 1) * Size); 136 + 137 + items[*size].pos = start; 138 + items[*size].size = (s32)(end - start + 1); 139 + 140 + (*size)++; 141 + } 142 + } 143 + else break; 144 + } 145 + 146 + return items; 147 + } 148 + 149 + tic_script_config MoonSyntaxConfig = 150 + { 151 + .name = "moon", 152 + .fileExtension = ".moon", 153 + .projectComment = "--", 154 + .init = initMoonscript, 155 + .close = closeLua, 156 + .tick = callLuaTick, 157 + .callback = 158 + { 159 + .scanline = callLuaScanline, 160 + .border = callLuaBorder, 161 + .overline = callLuaOverline, 162 + }, 163 + 164 + .getOutline = getMoonOutline, 165 + .eval = NULL, 166 + 167 + .blockCommentStart = NULL, 168 + .blockCommentEnd = NULL, 169 + .blockCommentStart2 = NULL, 170 + .blockCommentEnd2 = NULL, 171 + .blockStringStart = NULL, 172 + .blockStringEnd = NULL, 173 + .singleComment = "--", 174 + 175 + .keywords = MoonKeywords, 176 + .keywordsCount = COUNT_OF(MoonKeywords), 177 + }; 178 + 179 + const tic_script_config* get_moon_script_config() 180 + { 181 + return &MoonSyntaxConfig; 182 + } 183 + 184 + #endif /* defined(TIC_BUILD_WITH_MOON) */ 185 + 186 + #endif /* defined(TIC_BUILD_WITH_LUA) */
+1 -1
src/api/squirrel.c
··· 1556 1556 sq_newclosure(vm, squirrel_errorHandler, 0); 1557 1557 sq_seterrorhandler(vm); 1558 1558 1559 - initAPI(core); 1559 + initLuaAPI(core); 1560 1560 1561 1561 { 1562 1562 HSQUIRRELVM vm = core->currentVM;
+1 -1
src/api/wren.c
··· 1489 1489 1490 1490 WrenVM* vm = core->currentVM = wrenNewVM(&config); 1491 1491 1492 - initAPI(core); 1492 + initLuaAPI(core); 1493 1493 1494 1494 if (wrenInterpret(core->currentVM, "main", code) != WREN_RESULT_SUCCESS) 1495 1495 {