From bd3d9cfbd7a75b18af607569857133dfab0fee4f Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Tue, 15 May 2018 20:33:57 -0700 Subject: [PATCH] Add Fennel as an option for creating games. --- 3rd-party | 2 +- Makefile | 5 ++ bin/assets/fenneldemo.tic.dat | 1 + demos/fenneldemo.tic | Bin 0 -> 1257 bytes include/tic80_config.h | 1 + src/console.c | 46 ++++++++++ src/luaapi.c | 163 ++++++++++++++++++++++++++++++++++ src/machine.h | 6 +- src/tic.c | 9 ++ 9 files changed, 231 insertions(+), 2 deletions(-) create mode 100644 bin/assets/fenneldemo.tic.dat create mode 100644 demos/fenneldemo.tic diff --git a/3rd-party b/3rd-party index d0688b0..1829881 160000 --- a/3rd-party +++ b/3rd-party @@ -1 +1 @@ -Subproject commit d0688b04c6b5ecd62dbf2e8adfe25682ea473dff +Subproject commit 18298814404110bad0aba611bcc901fe6c59a085 diff --git a/Makefile b/Makefile index e1524ae..760e527 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,7 @@ INCLUDES= \ -I$(3RD_PARTY)/sdl-gpu/include \ -I$(3RD_PARTY)/wren-0.1.0/src/include \ -I$(3RD_PARTY)/moonscript \ + -I$(3RD_PARTY)/fennel \ -I$(BLIPBUF_LIB) \ -I$(DUKTAPE_LIB) \ -I$(SDL_NET_LIB) \ @@ -173,6 +174,7 @@ DEMO_ASSETS= \ bin/assets/jsdemo.tic.dat \ bin/assets/luademo.tic.dat \ bin/assets/moondemo.tic.dat \ + bin/assets/fenneldemo.tic.dat \ bin/assets/wrendemo.tic.dat \ bin/assets/benchmark.tic.dat \ bin/assets/config.tic.dat @@ -444,6 +446,9 @@ bin/assets/wrendemo.tic.dat: demos/wrendemo.tic bin/assets/moondemo.tic.dat: demos/moondemo.tic $(BIN2TXT) $< $@ -z +bin/assets/fenneldemo.tic.dat: demos/fenneldemo.tic + $(BIN2TXT) $< $@ -z + bin/assets/benchmark.tic.dat: demos/benchmark.tic $(BIN2TXT) $< $@ -z diff --git a/bin/assets/fenneldemo.tic.dat b/bin/assets/fenneldemo.tic.dat new file mode 100644 index 0000000..007105c --- /dev/null +++ b/bin/assets/fenneldemo.tic.dat @@ -0,0 +1 @@ +0x78, 0xda, 0x63, 0x5c, 0xc0, 0xc4, 0x40, 0x08, 0xfc, 0xfb, 0x0f, 0x04, 0x4a, 0x4a, 0x4a, 0xfd, 0x1d, 0x1d, 0x1d, 0xfd, 0x20, 0x1a, 0xc8, 0xed, 0xff, 0xff, 0x01, 0x8e, 0xff, 0xff, 0x7f, 0xff, 0x4e, 0x49, 0xe9, 0xff, 0xbb, 0x8e, 0x8e, 0x0f, 0xef, 0x94, 0x9a, 0x3e, 0xbc, 0xfb, 0xdf, 0xff, 0xe1, 0xff, 0x87, 0xfe, 0x0f, 0x1f, 0x60, 0x18, 0xbb, 0xfe, 0xff, 0x78, 0xf5, 0xff, 0x47, 0xd2, 0xcf, 0x30, 0x0a, 0x06, 0x14, 0x80, 0xe3, 0x0a, 0x14, 0x77, 0x60, 0xf8, 0x03, 0xcc, 0xd6, 0x07, 0xc6, 0xe3, 0x7f, 0x86, 0x0f, 0xff, 0x81, 0x71, 0xfb, 0xef, 0x7f, 0x3f, 0xc3, 0x7f, 0x60, 0xdc, 0xbd, 0xef, 0x07, 0xc6, 0x5f, 0x07, 0x14, 0x83, 0xe2, 0x93, 0xe1, 0xc3, 0xfb, 0x77, 0xff, 0xff, 0xbf, 0x7b, 0x47, 0xa9, 0x7e, 0xd6, 0x59, 0x8c, 0x0c, 0xd6, 0xd6, 0x0a, 0x25, 0x99, 0x25, 0x39, 0xa9, 0x56, 0x0a, 0x0a, 0xe9, 0x89, 0xb9, 0xa9, 0x10, 0x0e, 0x17, 0x50, 0x34, 0xb1, 0xb4, 0x24, 0x23, 0xbf, 0xc8, 0x0a, 0x22, 0x9a, 0x92, 0x5a, 0x96, 0x9a, 0x93, 0x5f, 0x90, 0x5a, 0x04, 0x92, 0x49, 0x49, 0x2d, 0x4e, 0x06, 0x2a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c7, 0x5f, 0x21, 0xdc, 0x3f, 0xc8, 0xc7, 0x45, 0x51, 0x49, 0xc1, 0xc2, 0x04, 0x88, 0x20, 0x66, 0x03, 0x6d, 0x2e, 0x01, 0x99, 0x5e, 0x02, 0xb6, 0x59, 0x93, 0x8b, 0xb2, 0x94, 0xc5, 0xcc, 0xc2, 0x65, 0x00, 0x61, 0x81, 0x8a, 0x08, 0x18, 0x2d, 0x60, 0x14, 0x52, 0x36, 0x63, 0xd7, 0x9d, 0x7f, 0xef, 0xcf, 0xae, 0xee, 0x4c, 0x77, 0x55, 0x66, 0x84, 0xf1, 0x61, 0x34, 0x0f, 0x50, 0x8f, 0x08, 0x8f, 0x8c, 0x8b, 0x8a, 0x89, 0x81, 0x49, 0xae, 0x9f, 0x97, 0x5f, 0xab, 0x8f, 0x81, 0x49, 0xaa, 0xca, 0x05, 0x37, 0x8f, 0xd2, 0xc2, 0xc4, 0xc8, 0xda, 0x73, 0x97, 0x6a, 0x75, 0x5a, 0xa7, 0x2e, 0xcc, 0x5d, 0xa5, 0x73, 0x69, 0xd5, 0xcc, 0xdc, 0x43, 0xa7, 0x6e, 0x5d, 0x89, 0xbb, 0xf7, 0xee, 0x1a, 0x17, 0x00, 0x6e, 0x06, 0x4f, 0xb2, \ No newline at end of file diff --git a/demos/fenneldemo.tic b/demos/fenneldemo.tic new file mode 100644 index 0000000000000000000000000000000000000000..254678c0655e118b91d0a580825445ef5fc8ddde GIT binary patch literal 1257 zcmeHH&r9P#6i)qd2_8J`(ZkzVVPajHra^Y=(I2Q3g+*9oaUs^R7SgnvjI|!T2tqvN zB7T7dR z3xgmC<&0)1_|p)g6=|A?f`CWbK95Am1rIszjNb8Gj$ZM|@f81(zW@GzdjoO4W$xqt zTgW=dVjT#D3(Rd>go?o2w?fRntbw==upSiNiiC)w*TxTiODR)Rz$(n41%R?$rHP^9 zuD#8+YF;7P-JlKX)auleo(<}k5Dq^2S!{WgI;+%N>GVs|D8MFlUFxVRY1kexF!W^A zgb$OC%Uv)n9RX#hwr)GHvM{Xz*>q7s2|ll>0Ay#2xjE-80p=s1DH)nQ81h{}<{6;m_>jxTU-Ro8kPPI2j04GLfsEn7CQ(`*hZ!rvTLBD4 zg%;$&z;2>PO09bpmw`4vTP!Zar{#~unYS9ex9~~0@o-GCvsjw0zOM562UH_rkiQtY zlJl^+(l|c<)o%SfJ1ozRraE?K9>SP6LnAY3%do0Ti%SPZ!=mZyx%us{_Uhi%&ECYp W(U0os#LekR_2TmW_RB+br~UzM22Zj8 literal 0 HcmV?d00001 diff --git a/include/tic80_config.h b/include/tic80_config.h index 6dbe52a..932eebd 100644 --- a/include/tic80_config.h +++ b/include/tic80_config.h @@ -24,6 +24,7 @@ #define TIC_BUILD_WITH_LUA 1 #define TIC_BUILD_WITH_MOON 1 +#define TIC_BUILD_WITH_FENNEL 1 #define TIC_BUILD_WITH_JS 1 #define TIC_BUILD_WITH_WREN 1 diff --git a/src/console.c b/src/console.c index aefdf82..950508a 100644 --- a/src/console.c +++ b/src/console.c @@ -56,6 +56,10 @@ typedef enum MoonScript, # endif +# if defined(TIC_BUILD_WITH_FENNEL) + Fennel, +# endif + #endif /* defined(TIC_BUILD_WITH_LUA) */ #if defined(TIC_BUILD_WITH_JS) @@ -97,6 +101,10 @@ static const char DefaultLuaTicPath[] = TIC_LOCAL "default.tic"; static const char DefaultMoonTicPath[] = TIC_LOCAL "default_moon.tic"; # endif +# if defined(TIC_BUILD_WITH_FENNEL) +static const char DefaultFennelTicPath[] = TIC_LOCAL "default_fennel.tic"; +# endif + #endif /* defined(TIC_BUILD_WITH_LUA) */ #if defined(TIC_BUILD_WITH_JS) @@ -470,6 +478,10 @@ static void* getDemoCart(Console* console, ScriptLang script, s32* size) case MoonScript: strcpy(path, DefaultMoonTicPath); break; # endif +# if defined(TIC_BUILD_WITH_MOON) + case Fennel: strcpy(path, DefaultFennelTicPath); break; +# endif + #endif /* defined(TIC_BUILD_WITH_LUA) */ #if defined(TIC_BUILD_WITH_JS) @@ -519,6 +531,20 @@ static void* getDemoCart(Console* console, ScriptLang script, s32* size) break; # endif +# if defined(TIC_BUILD_WITH_FENNEL) + case Fennel: + { + static const u8 FennelDemoRom[] = + { + #include "../bin/assets/fenneldemo.tic.dat" + }; + + demo = FennelDemoRom; + romSize = sizeof FennelDemoRom; + } + break; +# endif + #endif /* defined(TIC_BUILD_WITH_LUA) */ @@ -584,6 +610,11 @@ static void onConsoleLoadDemoCommandConfirmed(Console* console, const char* para data = getDemoCart(console, MoonScript, &size); # endif +# if defined(TIC_BUILD_WITH_FENNEL) + if(strcmp(param, DefaultFennelTicPath) == 0) + data = getDemoCart(console, Fennel, &size); +# endif + #endif /* defined(TIC_BUILD_WITH_LUA) */ #if defined(TIC_BUILD_WITH_JS) @@ -1166,6 +1197,14 @@ static void onConsoleNewCommandConfirmed(Console* console, const char* param) } # endif +# if defined(TIC_BUILD_WITH_FENNEL) + if(strcmp(param, "fennel") == 0) + { + loadDemo(console, Fennel); + done = true; + } +# endif + #endif /* defined(TIC_BUILD_WITH_LUA) */ #if defined(TIC_BUILD_WITH_JS) @@ -1460,6 +1499,13 @@ static void onConsoleConfigCommand(Console* console, const char* param) } # endif +# if defined(TIC_BUILD_WITH_FENNEL) + else if(strcmp(param, "default fennel") == 0) + { + onConsoleLoadDemoCommand(console, DefaultFennelTicPath); + } +# endif + #endif /* defined(TIC_BUILD_WITH_LUA) */ #if defined(TIC_BUILD_WITH_JS) diff --git a/src/luaapi.c b/src/luaapi.c index 15d33a7..b729ed7 100644 --- a/src/luaapi.c +++ b/src/luaapi.c @@ -1613,4 +1613,167 @@ const tic_script_config* getMoonScriptConfig() #endif /* defined(TIC_BUILD_WITH_MOON) */ +#if defined(TIC_BUILD_WITH_FENNEL) + +#include "fennel.h" + +#define FENNEL_CODE(...) #__VA_ARGS__ + +static const char* execute_fennel_src = FENNEL_CODE( + local ok, val = pcall(require('fennel').eval, ...) + -- allow you to return a function instead of setting TIC global + if(not TIC and type(val) == "function") then TIC = val end + return val +); + +static bool initFennel(tic_mem* tic, const char* code) +{ + tic_machine* machine = (tic_machine*)tic; + closeLua(tic); + + lua_State* lua = machine->lua = luaL_newstate(); + + static const luaL_Reg loadedlibs[] = + { + { "_G", luaopen_base }, + { LUA_LOADLIBNAME, luaopen_package }, + { LUA_COLIBNAME, luaopen_coroutine }, + { LUA_TABLIBNAME, luaopen_table }, + { LUA_STRLIBNAME, luaopen_string }, + { LUA_MATHLIBNAME, luaopen_math }, + { LUA_DBLIBNAME, luaopen_debug }, + { NULL, NULL } + }; + + for (const luaL_Reg *lib = loadedlibs; lib->func; lib++) + { + luaL_requiref(lua, lib->name, lib->func, 1); + lua_pop(lua, 1); + } + + initAPI(machine); + + { + lua_State* fennel = machine->lua; + + lua_settop(fennel, 0); + + if (luaL_loadbuffer(fennel, (const char *)fennel_lua, fennel_lua_len, "fennel.lua") != LUA_OK) + { + machine->data->error(machine->data->data, "failed to load fennel.lua"); + return false; + } + + lua_call(fennel, 0, 0); + + if (luaL_loadbuffer(fennel, execute_fennel_src, strlen(execute_fennel_src), "execute_fennel") != LUA_OK) + { + machine->data->error(machine->data->data, "failed to load fennel compiler"); + return false; + } + + lua_pushstring(fennel, code); + if (lua_pcall(fennel, 1, 1, 0) != LUA_OK) + { + const char* msg = lua_tostring(fennel, -1); + + if (msg) + { + machine->data->error(machine->data->data, msg); + return false; + } + } + } + + return true; +} + +static const char* const FennelKeywords [] = +{ + "do", "values", "if", "when", "each", "for", "fn", "lambda", "partial", + "while", "set", "global", "var", "local", "let", "tset", + "or", "and", "true", "false", "nil", "#", ":", "->", "->>" +}; + +static const tic_outline_item* getFennelOutline(const char* code, s32* size) +{ + enum{Size = sizeof(tic_outline_item)}; + + *size = 0; + + static tic_outline_item* items = NULL; + + if(items) + { + free(items); + items = NULL; + } + + const char* ptr = code; + + while(true) + { + static const char FuncString[] = "fn"; /* this probably doesn't work */ + + ptr = strstr(ptr, FuncString); + + if(ptr) + { + const char* end = ptr; + + ptr += sizeof FuncString - 1; + + while(end >= code && !isalnum_(*end)) end--; + + const char* start = end; + + for (const char* val = start-1; val >= code && (isalnum_(*val)); val--, start--); + + if(end > start) + { + items = items ? realloc(items, (*size + 1) * Size) : malloc(Size); + + items[*size].pos = start - code; + items[*size].size = end - start + 1; + + (*size)++; + } + } + else break; + } + + return items; +} + +static const tic_script_config FennelSyntaxConfig = +{ + .init = initFennel, + .close = closeLua, + .tick = callLuaTick, + .scanline = callLuaScanline, + .overline = callLuaOverline, + + .getOutline = getFennelOutline, + .parse = parseCode, + + .blockCommentStart = NULL, + .blockCommentEnd = NULL, + .blockStringStart = NULL, + .blockStringEnd = NULL, + .singleComment = ";", + + .keywords = FennelKeywords, + .keywordsCount = COUNT_OF(FennelKeywords), + + .api = ApiKeywords, + .apiCount = COUNT_OF(ApiKeywords), +}; + +const tic_script_config* getFennelConfig() +{ + return &FennelSyntaxConfig; +} + +#endif /* defined(TIC_BUILD_WITH_FENNEL) */ + #endif /* defined(TIC_BUILD_WITH_LUA) */ diff --git a/src/machine.h b/src/machine.h index ec05c66..56297d8 100644 --- a/src/machine.h +++ b/src/machine.h @@ -123,7 +123,7 @@ typedef struct struct { -#if defined(TIC_BUILD_WITH_LUA) || defined(TIC_BUILD_WITH_MOON) +#if defined(TIC_BUILD_WITH_LUA) || defined(TIC_BUILD_WITH_MOON) || defined(TIC_BUILD_WITH_FENNEL) struct lua_State* lua; #endif @@ -176,6 +176,10 @@ const tic_script_config* getLuaScriptConfig(); const tic_script_config* getMoonScriptConfig(); # endif +# if defined(TIC_BUILD_WITH_FENNEL) +const tic_script_config* getFennelConfig(); +# endif + #endif /* defined(TIC_BUILD_WITH_LUA) */ diff --git a/src/tic.c b/src/tic.c index 0da3dad..4a1d852 100644 --- a/src/tic.c +++ b/src/tic.c @@ -566,6 +566,10 @@ void tic_close(tic_mem* tic) getMoonScriptConfig()->close(tic); # endif +# if defined(TIC_BUILD_WITH_FENNEL) + getFennelConfig()->close(tic); +# endif + #endif /* defined(TIC_BUILD_WITH_LUA) */ @@ -1566,6 +1570,11 @@ static const tic_script_config* getScriptConfig(const char* code) return getMoonScriptConfig(); #endif +#if defined(TIC_BUILD_WITH_FENNEL) + if(compareMetatag(code, "script", "fennel", getFennelConfig()->singleComment)) + return getFennelConfig(); +#endif + #if defined(TIC_BUILD_WITH_JS) if(compareMetatag(code, "script", "js", getJsScriptConfig()->singleComment) || compareMetatag(code, "script", "javascript", getJsScriptConfig()->singleComment))