this repo has no description
0
fork

Configure Feed

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

Merge pull request #1671 from joshgoebel/moonscript_sep_file_part2

fixup: move moon script and fennel into sep files

authored by

Vadim Grigoruk and committed by
GitHub
de735517 474dc3cb

+198 -186
+1
CMakeLists.txt
··· 303 303 ${TIC80CORE_DIR}/api/js.c 304 304 ${TIC80CORE_DIR}/api/lua.c 305 305 ${TIC80CORE_DIR}/api/moonscript.c 306 + ${TIC80CORE_DIR}/api/fennel.c 306 307 ${TIC80CORE_DIR}/api/wren.c 307 308 ${TIC80CORE_DIR}/api/squirrel.c 308 309 ${TIC80CORE_DIR}/tic.c
+194
src/api/fennel.c
··· 1 + #include "core/core.h" 2 + 3 + // Fennel requires Lua 4 + #if defined(TIC_BUILD_WITH_LUA) 5 + 6 + #include "lua_api.h" 7 + 8 + #if defined(TIC_BUILD_WITH_FENNEL) 9 + 10 + #include "fennel.h" 11 + 12 + #define FENNEL_CODE(...) #__VA_ARGS__ 13 + 14 + static const char* execute_fennel_src = FENNEL_CODE( 15 + local fennel = require("fennel") 16 + debug.traceback = fennel.traceback 17 + local opts = {filename="game", allowedGlobals = false} 18 + local src = ... 19 + if(src:find("\n;; strict: true")) then opts.allowedGlobals = nil end 20 + local ok, msg = pcall(fennel.eval, src, opts) 21 + if(not ok) then return msg end 22 + ); 23 + 24 + static bool initFennel(tic_mem* tic, const char* code) 25 + { 26 + tic_core* core = (tic_core*)tic; 27 + closeLua(tic); 28 + 29 + lua_State* lua = core->currentVM = luaL_newstate(); 30 + lua_open_builtins(lua); 31 + 32 + initLuaAPI(core); 33 + 34 + { 35 + lua_State* fennel = core->currentVM; 36 + 37 + lua_settop(fennel, 0); 38 + 39 + if (luaL_loadbuffer(fennel, (const char *)loadfennel_lua, 40 + loadfennel_lua_len, "fennel.lua") != LUA_OK) 41 + { 42 + core->data->error(core->data->data, "failed to load fennel compiler"); 43 + return false; 44 + } 45 + 46 + lua_call(fennel, 0, 0); 47 + 48 + if (luaL_loadbuffer(fennel, execute_fennel_src, strlen(execute_fennel_src), "execute_fennel") != LUA_OK) 49 + { 50 + core->data->error(core->data->data, "failed to load fennel compiler"); 51 + return false; 52 + } 53 + 54 + lua_pushstring(fennel, code); 55 + lua_call(fennel, 1, 1); 56 + const char* err = lua_tostring(fennel, -1); 57 + 58 + if (err) 59 + { 60 + core->data->error(core->data->data, err); 61 + return false; 62 + } 63 + } 64 + 65 + return true; 66 + } 67 + 68 + static const char* const FennelKeywords [] = 69 + { 70 + "lua", "hashfn","macro", "macros", "macroexpand", "macrodebug", 71 + "do", "values", "if", "when", "each", "for", "fn", "lambda", "partial", 72 + "while", "set", "global", "var", "local", "let", "tset", "doto", "match", 73 + "or", "and", "true", "false", "nil", "not", "not=", "length", "set-forcibly!", 74 + "rshift", "lshift", "bor", "band", "bnot", "bxor", "pick-values", "pick-args", 75 + ".", "..", "#", "...", ":", "->", "->>", "-?>", "-?>>", "$", "with-open" 76 + }; 77 + 78 + static const tic_outline_item* getFennelOutline(const char* code, s32* size) 79 + { 80 + enum{Size = sizeof(tic_outline_item)}; 81 + 82 + *size = 0; 83 + 84 + static tic_outline_item* items = NULL; 85 + 86 + if(items) 87 + { 88 + free(items); 89 + items = NULL; 90 + } 91 + 92 + const char* ptr = code; 93 + 94 + while(true) 95 + { 96 + static const char FuncString[] = "(fn "; 97 + 98 + ptr = strstr(ptr, FuncString); 99 + 100 + if(ptr) 101 + { 102 + ptr += sizeof FuncString - 1; 103 + 104 + const char* start = ptr; 105 + const char* end = start; 106 + 107 + while(*ptr) 108 + { 109 + char c = *ptr; 110 + 111 + if(c == ' ' || c == '\t' || c == '\n' || c == '[') 112 + { 113 + end = ptr; 114 + break; 115 + } 116 + 117 + ptr++; 118 + } 119 + 120 + if(end > start) 121 + { 122 + items = realloc(items, (*size + 1) * Size); 123 + 124 + items[*size].pos = start; 125 + items[*size].size = (s32)(end - start); 126 + 127 + (*size)++; 128 + } 129 + } 130 + else break; 131 + } 132 + 133 + return items; 134 + } 135 + 136 + static void evalFennel(tic_mem* tic, const char* code) { 137 + tic_core* core = (tic_core*)tic; 138 + lua_State* fennel = core->currentVM; 139 + 140 + lua_settop(fennel, 0); 141 + 142 + if (luaL_loadbuffer(fennel, execute_fennel_src, strlen(execute_fennel_src), "execute_fennel") != LUA_OK) 143 + { 144 + core->data->error(core->data->data, "failed to load fennel compiler"); 145 + } 146 + 147 + lua_pushstring(fennel, code); 148 + lua_call(fennel, 1, 1); 149 + const char* err = lua_tostring(fennel, -1); 150 + 151 + if (err) 152 + { 153 + core->data->error(core->data->data, err); 154 + } 155 + } 156 + 157 + tic_script_config FennelSyntaxConfig = 158 + { 159 + .name = "fennel", 160 + .fileExtension = ".fnl", 161 + .projectComment = ";;", 162 + .init = initFennel, 163 + .close = closeLua, 164 + .tick = callLuaTick, 165 + .callback = 166 + { 167 + .scanline = callLuaScanline, 168 + .border = callLuaBorder, 169 + .overline = callLuaOverline, 170 + }, 171 + 172 + .getOutline = getFennelOutline, 173 + .eval = evalFennel, 174 + 175 + .blockCommentStart = NULL, 176 + .blockCommentEnd = NULL, 177 + .blockCommentStart2 = NULL, 178 + .blockCommentEnd2 = NULL, 179 + .blockStringStart = NULL, 180 + .blockStringEnd = NULL, 181 + .singleComment = ";", 182 + 183 + .keywords = FennelKeywords, 184 + .keywordsCount = COUNT_OF(FennelKeywords), 185 + }; 186 + 187 + const tic_script_config* get_fennel_script_config() 188 + { 189 + return &FennelSyntaxConfig; 190 + } 191 + 192 + #endif /* defined(TIC_BUILD_WITH_FENNEL) */ 193 + 194 + #endif
-185
src/api/lua.c
··· 1694 1694 } 1695 1695 1696 1696 1697 - #if defined(TIC_BUILD_WITH_FENNEL) 1698 - 1699 - #include "fennel.h" 1700 - 1701 - #define FENNEL_CODE(...) #__VA_ARGS__ 1702 - 1703 - static const char* execute_fennel_src = FENNEL_CODE( 1704 - local fennel = require("fennel") 1705 - debug.traceback = fennel.traceback 1706 - local opts = {filename="game", allowedGlobals = false} 1707 - local src = ... 1708 - if(src:find("\n;; strict: true")) then opts.allowedGlobals = nil end 1709 - local ok, msg = pcall(fennel.eval, src, opts) 1710 - if(not ok) then return msg end 1711 - ); 1712 - 1713 - static bool initFennel(tic_mem* tic, const char* code) 1714 - { 1715 - tic_core* core = (tic_core*)tic; 1716 - closeLua(tic); 1717 - 1718 - lua_State* lua = core->currentVM = luaL_newstate(); 1719 - lua_open_builtins(lua); 1720 - 1721 - initLuaAPI(core); 1722 - 1723 - { 1724 - lua_State* fennel = core->currentVM; 1725 - 1726 - lua_settop(fennel, 0); 1727 - 1728 - if (luaL_loadbuffer(fennel, (const char *)loadfennel_lua, 1729 - loadfennel_lua_len, "fennel.lua") != LUA_OK) 1730 - { 1731 - core->data->error(core->data->data, "failed to load fennel compiler"); 1732 - return false; 1733 - } 1734 - 1735 - lua_call(fennel, 0, 0); 1736 - 1737 - if (luaL_loadbuffer(fennel, execute_fennel_src, strlen(execute_fennel_src), "execute_fennel") != LUA_OK) 1738 - { 1739 - core->data->error(core->data->data, "failed to load fennel compiler"); 1740 - return false; 1741 - } 1742 - 1743 - lua_pushstring(fennel, code); 1744 - lua_call(fennel, 1, 1); 1745 - const char* err = lua_tostring(fennel, -1); 1746 - 1747 - if (err) 1748 - { 1749 - core->data->error(core->data->data, err); 1750 - return false; 1751 - } 1752 - } 1753 - 1754 - return true; 1755 - } 1756 - 1757 - static const char* const FennelKeywords [] = 1758 - { 1759 - "lua", "hashfn","macro", "macros", "macroexpand", "macrodebug", 1760 - "do", "values", "if", "when", "each", "for", "fn", "lambda", "partial", 1761 - "while", "set", "global", "var", "local", "let", "tset", "doto", "match", 1762 - "or", "and", "true", "false", "nil", "not", "not=", "length", "set-forcibly!", 1763 - "rshift", "lshift", "bor", "band", "bnot", "bxor", "pick-values", "pick-args", 1764 - ".", "..", "#", "...", ":", "->", "->>", "-?>", "-?>>", "$", "with-open" 1765 - }; 1766 - 1767 - static const tic_outline_item* getFennelOutline(const char* code, s32* size) 1768 - { 1769 - enum{Size = sizeof(tic_outline_item)}; 1770 - 1771 - *size = 0; 1772 - 1773 - static tic_outline_item* items = NULL; 1774 - 1775 - if(items) 1776 - { 1777 - free(items); 1778 - items = NULL; 1779 - } 1780 - 1781 - const char* ptr = code; 1782 - 1783 - while(true) 1784 - { 1785 - static const char FuncString[] = "(fn "; 1786 - 1787 - ptr = strstr(ptr, FuncString); 1788 - 1789 - if(ptr) 1790 - { 1791 - ptr += sizeof FuncString - 1; 1792 - 1793 - const char* start = ptr; 1794 - const char* end = start; 1795 - 1796 - while(*ptr) 1797 - { 1798 - char c = *ptr; 1799 - 1800 - if(c == ' ' || c == '\t' || c == '\n' || c == '[') 1801 - { 1802 - end = ptr; 1803 - break; 1804 - } 1805 - 1806 - ptr++; 1807 - } 1808 - 1809 - if(end > start) 1810 - { 1811 - items = realloc(items, (*size + 1) * Size); 1812 - 1813 - items[*size].pos = start; 1814 - items[*size].size = (s32)(end - start); 1815 - 1816 - (*size)++; 1817 - } 1818 - } 1819 - else break; 1820 - } 1821 - 1822 - return items; 1823 - } 1824 - 1825 - static void evalFennel(tic_mem* tic, const char* code) { 1826 - tic_core* core = (tic_core*)tic; 1827 - lua_State* fennel = core->currentVM; 1828 - 1829 - lua_settop(fennel, 0); 1830 - 1831 - if (luaL_loadbuffer(fennel, execute_fennel_src, strlen(execute_fennel_src), "execute_fennel") != LUA_OK) 1832 - { 1833 - core->data->error(core->data->data, "failed to load fennel compiler"); 1834 - } 1835 - 1836 - lua_pushstring(fennel, code); 1837 - lua_call(fennel, 1, 1); 1838 - const char* err = lua_tostring(fennel, -1); 1839 - 1840 - if (err) 1841 - { 1842 - core->data->error(core->data->data, err); 1843 - } 1844 - } 1845 - 1846 - tic_script_config FennelSyntaxConfig = 1847 - { 1848 - .name = "fennel", 1849 - .fileExtension = ".fnl", 1850 - .projectComment = ";;", 1851 - .init = initFennel, 1852 - .close = closeLua, 1853 - .tick = callLuaTick, 1854 - .callback = 1855 - { 1856 - .scanline = callLuaScanline, 1857 - .border = callLuaBorder, 1858 - .overline = callLuaOverline, 1859 - }, 1860 - 1861 - .getOutline = getFennelOutline, 1862 - .eval = evalFennel, 1863 - 1864 - .blockCommentStart = NULL, 1865 - .blockCommentEnd = NULL, 1866 - .blockCommentStart2 = NULL, 1867 - .blockCommentEnd2 = NULL, 1868 - .blockStringStart = NULL, 1869 - .blockStringEnd = NULL, 1870 - .singleComment = ";", 1871 - 1872 - .keywords = FennelKeywords, 1873 - .keywordsCount = COUNT_OF(FennelKeywords), 1874 - }; 1875 - 1876 - const tic_script_config* get_fennel_script_config() 1877 - { 1878 - return &FennelSyntaxConfig; 1879 - } 1880 - 1881 - #endif /* defined(TIC_BUILD_WITH_FENNEL) */ 1882 1697 1883 1698 #endif /* defined(TIC_BUILD_WITH_LUA) */
+3 -1
src/api/lua_api.h
··· 7 7 #include <lualib.h> 8 8 #include <ctype.h> 9 9 10 + s32 luaopen_lpeg(lua_State *lua); 11 + 12 + extern void initLuaAPI(tic_core* core); 10 13 extern void callLuaTick(tic_mem* tic); 11 14 extern void callLuaScanlineName(tic_mem* tic, s32 row, void* data, const char* name); 12 15 extern void callLuaScanline(tic_mem* tic, s32 row, void* data); ··· 15 18 extern void closeLua(tic_mem* tic); 16 19 extern void callLuaTick(tic_mem* tic); 17 20 extern void lua_open_builtins(lua_State *lua); 18 - extern void initLuaAPI(core); 19 21 20 22 #define lua_api_h 21 23 #endif