#477 outline func moved to TIC module

This commit is contained in:
BADIM-PC\Vadim 2017-12-22 14:20:15 +03:00
parent f8af6390ab
commit ef502b2496
6 changed files with 237 additions and 146 deletions

View File

@ -865,37 +865,7 @@ static int funcCompare(const void* a, const void* b)
if(item1->pos == NULL) return 1; if(item1->pos == NULL) return 1;
if(item2->pos == NULL) return -1; if(item2->pos == NULL) return -1;
return strcmp(item1->name, item2->name); return SDL_strcasecmp(item1->name, item2->name);
}
static char* getFuncName(const char* start, char* buffer)
{
const char* ptr = start;
while(*ptr)
{
char sym = *ptr;
if(isalpha_(sym) || isdigit(sym) || sym == ':'){}
else if(sym == '(') break;
else return NULL;
ptr++;
}
if(ptr)
{
size_t len = ptr - start;
if(len < STUDIO_TEXT_BUFFER_WIDTH)
{
memcpy(buffer, start, len);
buffer[len] = '\0';
return buffer;
}
}
return NULL;
} }
static void normalizeScroll(Code* code) static void normalizeScroll(Code* code)
@ -938,118 +908,56 @@ static void updateOutlineCode(Code* code)
updateEditor(code); updateEditor(code);
} }
static void setMoonscriptOutlineMode(Code* code) static void initOutlineMode(Code* code)
{ {
OutlineItem* out = code->outline.items; OutlineItem* out = code->outline.items;
OutlineItem* end = out + OUTLINE_SIZE; OutlineItem* end = out + OUTLINE_SIZE;
char* ptr = code->src; tic_mem* tic = code->tic;
static const char FuncString[] = "=->";
char buffer[STUDIO_TEXT_BUFFER_WIDTH]; char buffer[STUDIO_TEXT_BUFFER_WIDTH] = {0};
char filter[STUDIO_TEXT_BUFFER_WIDTH]; char filter[STUDIO_TEXT_BUFFER_WIDTH] = {0};
strcpy(filter, code->popup.text); SDL_strlcpy(filter, code->popup.text, sizeof(filter));
SDL_strlwr(filter); SDL_strlwr(filter);
while(ptr) const tic_script_config* config = tic->api.get_script_config(tic);
if(config->getOutline)
{ {
ptr = strstr(ptr, FuncString); s32 size = 0;
const tic_outline_item* items = config->getOutline(code->src, &size);
if(ptr && ptr > code->src) for(s32 i = 0; i < size; i++)
{ {
char* endPtr = ptr; const tic_outline_item* item = items + i;
ptr += sizeof FuncString - 1;
while(endPtr >= code->src && !isalpha_(*endPtr) && !isdigit(*endPtr)) endPtr--; if(out < end)
char* start = endPtr;
for (const char* val = start-1; val >= code->src && (isalpha_(*val) || isdigit(*val)); val--, start--);
if(start >= code->src)
{ {
memset(buffer, 0, sizeof buffer); out->pos = code->src + item->pos;
memcpy(buffer, start, endPtr - start + 1); memset(out->name, 0, STUDIO_TEXT_BUFFER_WIDTH);
memcpy(out->name, out->pos, SDL_min(item->size, STUDIO_TEXT_BUFFER_WIDTH-1));
strcpy(out->name, buffer);
if(*filter) if(*filter)
{ {
SDL_strlcpy(buffer, out->name, sizeof(buffer));
SDL_strlwr(buffer); SDL_strlwr(buffer);
if(strstr(buffer, filter))
{ if(strstr(buffer, filter)) out++;
out->pos = start; else out->pos = NULL;
out++;
}
}
else
{
out->pos = start;
out++;
} }
else out++;
} }
else break;
} }
else break;
if(out >= end) break;
} }
} }
static void setLuaOutlineMode(Code* code)
{
OutlineItem* out = code->outline.items;
OutlineItem* end = out + OUTLINE_SIZE;
char* ptr = code->src;
static const char FuncString[] = "function ";
char buffer[STUDIO_TEXT_BUFFER_WIDTH];
char filter[STUDIO_TEXT_BUFFER_WIDTH];
strcpy(filter, code->popup.text);
SDL_strlwr(filter);
while(ptr)
{
ptr = strstr(ptr, FuncString);
if(ptr)
{
ptr += sizeof FuncString - 1;
if(getFuncName(ptr, buffer))
{
strcpy(out->name, buffer);
if(*filter)
{
SDL_strlwr(buffer);
if(strstr(buffer, filter))
{
out->pos = ptr;
out++;
}
}
else
{
out->pos = ptr;
out++;
}
}
}
if(out >= end) break;
}
}
static void setOutlineMode(Code* code) static void setOutlineMode(Code* code)
{ {
code->outline.index = 0; code->outline.index = 0;
memset(code->outline.items, 0, OUTLINE_ITEMS_SIZE); memset(code->outline.items, 0, OUTLINE_ITEMS_SIZE);
code->tic->api.get_script_config(code->tic)->lang == tic_script_moon initOutlineMode(code);
? setMoonscriptOutlineMode(code)
: setLuaOutlineMode(code);
qsort(code->outline.items, OUTLINE_SIZE, sizeof(OutlineItem), funcCompare); qsort(code->outline.items, OUTLINE_SIZE, sizeof(OutlineItem), funcCompare);
updateOutlineCode(code); updateOutlineCode(code);

View File

@ -40,6 +40,13 @@
#define CONSOLE_BUFFER_SCREENS 64 #define CONSOLE_BUFFER_SCREENS 64
#define CONSOLE_BUFFER_SIZE (CONSOLE_BUFFER_WIDTH * CONSOLE_BUFFER_HEIGHT * CONSOLE_BUFFER_SCREENS) #define CONSOLE_BUFFER_SIZE (CONSOLE_BUFFER_WIDTH * CONSOLE_BUFFER_HEIGHT * CONSOLE_BUFFER_SCREENS)
typedef enum
{
LuaScript,
MoonScript,
JavaScript,
} ScriptLang;
#if defined(__WINDOWS__) || defined(__LINUX__) || defined(__MACOSX__) #if defined(__WINDOWS__) || defined(__LINUX__) || defined(__MACOSX__)
#define CAN_EXPORT 1 #define CAN_EXPORT 1
#endif #endif
@ -381,20 +388,20 @@ static bool onConsoleLoadSectionCommand(Console* console, const char* param)
return result; return result;
} }
static void* getDemoCart(Console* console, tic_script_lang script, s32* size) static void* getDemoCart(Console* console, ScriptLang script, s32* size)
{ {
char path[FILENAME_MAX] = {0}; char path[FILENAME_MAX] = {0};
{ {
switch(script) switch(script)
{ {
case tic_script_lua: case LuaScript:
strcpy(path, DefaultLuaTicPath); strcpy(path, DefaultLuaTicPath);
break; break;
case tic_script_moon: case MoonScript:
strcpy(path, DefaultMoonTicPath); strcpy(path, DefaultMoonTicPath);
break; break;
case tic_script_js: case JavaScript:
strcpy(path, DefaultJSTicPath); strcpy(path, DefaultJSTicPath);
break; break;
} }
@ -425,15 +432,15 @@ static void* getDemoCart(Console* console, tic_script_lang script, s32* size)
switch(script) switch(script)
{ {
case tic_script_lua: case LuaScript:
demo = LuaDemoRom; demo = LuaDemoRom;
romSize = sizeof LuaDemoRom; romSize = sizeof LuaDemoRom;
break; break;
case tic_script_moon: case MoonScript:
demo = MoonDemoRom; demo = MoonDemoRom;
romSize = sizeof MoonDemoRom; romSize = sizeof MoonDemoRom;
break; break;
case tic_script_js: case JavaScript:
demo = JsDemoRom; demo = JsDemoRom;
romSize = sizeof JsDemoRom; romSize = sizeof JsDemoRom;
break; break;
@ -464,11 +471,11 @@ static void onConsoleLoadDemoCommandConfirmed(Console* console, const char* para
console->showGameMenu = false; console->showGameMenu = false;
if(strcmp(param, DefaultLuaTicPath) == 0) if(strcmp(param, DefaultLuaTicPath) == 0)
data = getDemoCart(console, tic_script_lua, &size); data = getDemoCart(console, LuaScript, &size);
else if(strcmp(param, DefaultMoonTicPath) == 0) else if(strcmp(param, DefaultMoonTicPath) == 0)
data = getDemoCart(console, tic_script_moon, &size); data = getDemoCart(console, MoonScript, &size);
else if(strcmp(param, DefaultJSTicPath) == 0) else if(strcmp(param, DefaultJSTicPath) == 0)
data = getDemoCart(console, tic_script_js, &size); data = getDemoCart(console, JavaScript, &size);
const char* name = getCartName(param); const char* name = getCartName(param);
@ -1012,7 +1019,7 @@ static void onConsoleLoadCommand(Console* console, const char* param)
} }
} }
static void loadDemo(Console* console, tic_script_lang script) static void loadDemo(Console* console, ScriptLang script)
{ {
s32 size = 0; s32 size = 0;
u8* data = getDemoCart(console, script, &size); u8* data = getDemoCart(console, script, &size);
@ -1034,11 +1041,11 @@ static void onConsoleNewCommandConfirmed(Console* console, const char* param)
if(param && strlen(param)) if(param && strlen(param))
{ {
if(strcmp(param, "lua") == 0) if(strcmp(param, "lua") == 0)
loadDemo(console, tic_script_lua); loadDemo(console, LuaScript);
else if(strcmp(param, "moon") == 0 || strcmp(param, "moonscript") == 0) else if(strcmp(param, "moon") == 0 || strcmp(param, "moonscript") == 0)
loadDemo(console, tic_script_moon); loadDemo(console, MoonScript);
else if(strcmp(param, "js") == 0 || strcmp(param, "javascript") == 0) else if(strcmp(param, "js") == 0 || strcmp(param, "javascript") == 0)
loadDemo(console, tic_script_js); loadDemo(console, JavaScript);
else else
{ {
printError(console, "\nunknown parameter: "); printError(console, "\nunknown parameter: ");
@ -1047,7 +1054,7 @@ static void onConsoleNewCommandConfirmed(Console* console, const char* param)
return; return;
} }
} }
else loadDemo(console, tic_script_lua); else loadDemo(console, LuaScript);
printBack(console, "\nnew cart is created"); printBack(console, "\nnew cart is created");
commandDone(console); commandDone(console);
@ -2661,7 +2668,7 @@ static void tick(Console* console)
{ {
if(!console->embed.yes) if(!console->embed.yes)
{ {
loadDemo(console, tic_script_lua); loadDemo(console, LuaScript);
printBack(console, "\n hello! type "); printBack(console, "\n hello! type ");
printFront(console, "help"); printFront(console, "help");

View File

@ -23,6 +23,8 @@
#include "machine.h" #include "machine.h"
#include "tools.h" #include "tools.h"
#include <ctype.h>
#include "ext/duktape/duktape.h" #include "ext/duktape/duktape.h"
static const char TicMachine[] = "_TIC80"; static const char TicMachine[] = "_TIC80";
@ -886,16 +888,78 @@ static const char* const JsKeywords [] =
"default", "if", "throw", "delete", "in", "try", "const" "default", "if", "throw", "delete", "in", "try", "const"
}; };
static inline bool isalnum_(char c) {return isalnum(c) || c == '_';}
static const tic_outline_item* getJsOutline(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[] = "function ";
ptr = strstr(ptr, FuncString);
if(ptr)
{
ptr += sizeof FuncString - 1;
const char* start = ptr;
const char* end = start;
while(*ptr)
{
char c = *ptr;
if(isalnum_(c));
else if(c == '(')
{
end = ptr;
break;
}
else break;
ptr++;
}
if(end > start)
{
items = items ? realloc(items, (*size + 1) * Size) : malloc(Size);
items[*size].pos = start - code;
items[*size].size = end - start;
(*size)++;
}
}
else break;
}
return items;
}
static const tic_script_config JsSyntaxConfig = static const tic_script_config JsSyntaxConfig =
{ {
.lang = tic_script_js,
.init = initJavascript, .init = initJavascript,
.close = closeJavascript, .close = closeJavascript,
.tick = callJavascriptTick, .tick = callJavascriptTick,
.scanline = callJavascriptScanline, .scanline = callJavascriptScanline,
.overlap = callJavascriptOverlap, .overlap = callJavascriptOverlap,
.getOutline = getJsOutline,
.blockCommentStart = "/*", .blockCommentStart = "/*",
.blockCommentEnd = "*/", .blockCommentEnd = "*/",
.blockStringStart = NULL, .blockStringStart = NULL,

View File

@ -25,6 +25,7 @@
#include <lua.h> #include <lua.h>
#include <lauxlib.h> #include <lauxlib.h>
#include <lualib.h> #include <lualib.h>
#include <ctype.h>
#include "machine.h" #include "machine.h"
#include "ext/moonscript.h" #include "ext/moonscript.h"
@ -1314,16 +1315,78 @@ static const char* const LuaKeywords [] =
"return", "then", "true", "until", "while" "return", "then", "true", "until", "while"
}; };
static inline bool isalnum_(char c) {return isalnum(c) || c == '_';}
static const tic_outline_item* getLuaOutline(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[] = "function ";
ptr = strstr(ptr, FuncString);
if(ptr)
{
ptr += sizeof FuncString - 1;
const char* start = ptr;
const char* end = start;
while(*ptr)
{
char c = *ptr;
if(isalnum_(c) || c == ':');
else if(c == '(')
{
end = ptr;
break;
}
else break;
ptr++;
}
if(end > start)
{
items = items ? realloc(items, (*size + 1) * Size) : malloc(Size);
items[*size].pos = start - code;
items[*size].size = end - start;
(*size)++;
}
}
else break;
}
return items;
}
static const tic_script_config LuaSyntaxConfig = static const tic_script_config LuaSyntaxConfig =
{ {
.lang = tic_script_lua,
.init = initLua, .init = initLua,
.close = closeLua, .close = closeLua,
.tick = callLuaTick, .tick = callLuaTick,
.scanline = callLuaScanline, .scanline = callLuaScanline,
.overlap = callLuaOverlap, .overlap = callLuaOverlap,
.getOutline = getLuaOutline,
.blockCommentStart = "--[[", .blockCommentStart = "--[[",
.blockCommentEnd = "]]", .blockCommentEnd = "]]",
.singleComment = "--", .singleComment = "--",
@ -1353,16 +1416,66 @@ static const char* const MoonKeywords [] =
"from", "class", "extends", "new" "from", "class", "extends", "new"
}; };
static const tic_outline_item* getMoonOutline(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[] = "=->";
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 MoonSyntaxConfig = static const tic_script_config MoonSyntaxConfig =
{ {
.lang = tic_script_moon,
.init = initMoonscript, .init = initMoonscript,
.close = closeLua, .close = closeLua,
.tick = callLuaTick, .tick = callLuaTick,
.scanline = callLuaScanline, .scanline = callLuaScanline,
.overlap = callLuaOverlap, .overlap = callLuaOverlap,
.getOutline = getMoonOutline,
.blockCommentStart = NULL, .blockCommentStart = NULL,
.blockCommentEnd = NULL, .blockCommentEnd = NULL,
.blockStringStart = NULL, .blockStringStart = NULL,

View File

@ -430,10 +430,3 @@ typedef enum
tic_gamepad_input, tic_gamepad_input,
tic_mouse_input, tic_mouse_input,
} tic_input_method; } tic_input_method;
typedef enum
{
tic_script_lua,
tic_script_moon,
tic_script_js,
} tic_script_lang;

View File

@ -70,8 +70,12 @@ typedef void(*tic_overlap)(tic_mem* memory, void* data);
typedef struct typedef struct
{ {
tic_script_lang lang; s32 pos;
s32 size;
} tic_outline_item;
typedef struct
{
struct struct
{ {
bool(*init)(tic_mem* memory, const char* code); bool(*init)(tic_mem* memory, const char* code);
@ -82,6 +86,8 @@ typedef struct
tic_overlap overlap; tic_overlap overlap;
}; };
const tic_outline_item* (*getOutline)(const char* code, s32* size);
const char* blockCommentStart; const char* blockCommentStart;
const char* blockCommentEnd; const char* blockCommentEnd;
const char* blockStringStart; const char* blockStringStart;