From 38f349618fe8272f79992ded90e92cb7d5b03532 Mon Sep 17 00:00:00 2001 From: "Such Meme, Many Skill" Date: Tue, 31 Mar 2020 14:24:34 +0200 Subject: [PATCH] Add functions and int vars --- source/tegraexplorer/fs/filemenu.c | 2 +- source/tegraexplorer/script/functions.c | 66 ++++++++++++++ source/tegraexplorer/script/functions.h | 13 +++ .../script/{script.c => parser.c} | 57 ++++++++++-- source/tegraexplorer/script/parser.h | 5 ++ source/tegraexplorer/script/script.h | 3 - source/tegraexplorer/script/variables.c | 89 +++++++++++++++++++ source/tegraexplorer/script/variables.h | 25 ++++++ 8 files changed, 250 insertions(+), 10 deletions(-) create mode 100644 source/tegraexplorer/script/functions.c create mode 100644 source/tegraexplorer/script/functions.h rename source/tegraexplorer/script/{script.c => parser.c} (82%) create mode 100644 source/tegraexplorer/script/parser.h delete mode 100644 source/tegraexplorer/script/script.h create mode 100644 source/tegraexplorer/script/variables.c create mode 100644 source/tegraexplorer/script/variables.h diff --git a/source/tegraexplorer/fs/filemenu.c b/source/tegraexplorer/fs/filemenu.c index 68e9b3c..428004c 100644 --- a/source/tegraexplorer/fs/filemenu.c +++ b/source/tegraexplorer/fs/filemenu.c @@ -13,7 +13,7 @@ #include "../gfx/menu.h" #include "../common/types.h" #include "../../utils/sprintf.h" -#include "../script/script.h" +#include "../script/parser.h" #include "../emmc/emmcoperations.h" extern char *currentpath; diff --git a/source/tegraexplorer/script/functions.c b/source/tegraexplorer/script/functions.c new file mode 100644 index 0000000..fb8515b --- /dev/null +++ b/source/tegraexplorer/script/functions.c @@ -0,0 +1,66 @@ +#include +#include +#include "../../mem/heap.h" +#include "../gfx/gfxutils.h" +#include "../emmc/emmc.h" +#include "../../utils/types.h" +#include "../../libs/fatfs/ff.h" +#include "../../utils/sprintf.h" +#include "../../utils/btn.h" +#include "../../gfx/gfx.h" +#include "../../utils/util.h" +#include "../../storage/emummc.h" +#include "parser.h" +#include "../common/common.h" +#include "../fs/fsactions.h" +#include "variables.h" +#include "../utils/utils.h" +#include "functions.h" + +extern FIL scriptin; +extern char **argv; +extern u32 argc; + +int part_printf(){ + gfx_printf(argv[0]); + gfx_printf("\n"); + return 0; +} + +int part_if(){ + int condition; + if (argv[0][0] == '@'){ + if (str_int_find(argv[0], &condition)) + return -10; + } + else + condition = atoi(argv[0]); + + getfollowingchar('{'); + + if (condition) + return 0; + else { + skipbrackets(); + return 0; + } +} + +str_fnc_struct functions[] = { + {"printf", part_printf, 1}, + {"if", part_if, 1}, + {NULL, NULL, 0} +}; + +int run_function(char *func_name, int *out){ + for (u32 i = 0; functions[i].key != NULL; i++){ + if (!strcmp(functions[i].key, func_name)){ + if (argc != functions[i].arg_count) + return -2; + + *out = functions[i].value(); + return 0; + } + } + return -1; +} \ No newline at end of file diff --git a/source/tegraexplorer/script/functions.h b/source/tegraexplorer/script/functions.h new file mode 100644 index 0000000..ac9e03d --- /dev/null +++ b/source/tegraexplorer/script/functions.h @@ -0,0 +1,13 @@ +#pragma once +#include "../../utils/types.h" + +typedef void (*func_void_ptr)(); +typedef int (*func_int_ptr)(); + +typedef struct { + char *key; + func_int_ptr value; + u8 arg_count; +} str_fnc_struct; + +int run_function(char *func_name, int *out); \ No newline at end of file diff --git a/source/tegraexplorer/script/script.c b/source/tegraexplorer/script/parser.c similarity index 82% rename from source/tegraexplorer/script/script.c rename to source/tegraexplorer/script/parser.c index d0a505f..863d740 100644 --- a/source/tegraexplorer/script/script.c +++ b/source/tegraexplorer/script/parser.c @@ -9,9 +9,11 @@ #include "../../gfx/gfx.h" #include "../../utils/util.h" #include "../../storage/emummc.h" -#include "script.h" +#include "parser.h" #include "../common/common.h" #include "../fs/fsactions.h" +#include "functions.h" +#include "variables.h" u32 countchars(char* in, char target) { @@ -90,7 +92,7 @@ void getfollowingchar(char end){ } void getnextvalidchar(){ - while ((!((currentchar >= '0' && currentchar <= 'Z') || (currentchar >= 'a' && currentchar <= 'z') || currentchar == '#' || currentchar == '@') && !f_eof(&scriptin)) || currentchar == ';') + while ((!((currentchar >= 'A' && currentchar <= 'Z') || (currentchar >= 'a' && currentchar <= 'z') || currentchar == '#' || currentchar == '@') && !f_eof(&scriptin)) /*|| currentchar == ';' */) getnextchar(); } @@ -149,13 +151,18 @@ char *readtilchar(char end, char ignore){ // this will strip spaces unless it's return makestr((u32)size, ignore); } + +char *funcbuff = NULL; void functionparser(){ char *unsplitargs; FSIZE_t fileoffset; u32 argsize = 0; + if (funcbuff != NULL) + free(funcbuff); + //gfx_printf("getting func %c\n", currentchar); - char *funcbuff = readtilchar('(', ' '); + funcbuff = readtilchar('(', ' '); /*calloc(20, sizeof(char)); for (int i = 0; i < 19 && currentchar != '(' && currentchar != ' '; i++){ funcbuff[i] = currentchar; @@ -176,7 +183,7 @@ void functionparser(){ getnextchar(); - //gfx_printf("getting args %c\n", currentchar); + gfx_printf("getting args %c\n", currentchar); unsplitargs = calloc(argsize + 1, sizeof(char)); for (int i = 0; i < argsize; i++){ @@ -191,15 +198,16 @@ void functionparser(){ getnextchar(); getnextchar(); + /* gfx_printf("\n\nFunc: %s\n", funcbuff, currentchar); gfx_printf("Split: %s\n", unsplitargs); for (int i = 0; i < argc; i++) gfx_printf("%d | %s\n", i, argv[i]); + */ //gfx_printf("\ncurrent char: %c", currentchar); free(unsplitargs); - free(funcbuff); } char *gettargetvar(){ @@ -235,6 +243,7 @@ char *gettargetvar(){ void mainparser(){ char *variable = NULL; + int res, out = 0; FSIZE_t fileoffset; u32 varsize = 0; @@ -254,12 +263,38 @@ void mainparser(){ } functionparser(); + /* if (variable != NULL) - gfx_printf("target: %s", variable); + gfx_printf("target: %s\n", variable); + */ + //gfx_printf("Func: %s\n", funcbuff); + res = run_function(funcbuff, &out); + str_int_add("@RESULT", res); + + if (variable != NULL) + str_int_add(variable, res); + + //gfx_printf("\nGoing to next func %c\n", currentchar); free(variable); } +void skipbrackets(){ + u32 bracketcounter = 0; + + getfollowingchar('{'); + getnextchar(); + + while (currentchar != '}' || bracketcounter != 0){ + if (currentchar == '{') + bracketcounter++; + else if (currentchar == '}') + bracketcounter--; + + getnextchar(); + } +} + void tester(char *path){ int res; @@ -271,6 +306,12 @@ void tester(char *path){ return; } + //add builtin vars + str_int_add("@EMUMMC", emu_cfg.enabled); + str_int_add("@RESULT", 0); + str_int_add("@BTN_POWER", 0); + str_int_add("@BTN_VOL+", 0); + str_int_add("@BTN_VOL-", 0); printerrors = false; @@ -278,6 +319,10 @@ void tester(char *path){ mainparser(); } + printerrors = true; + //str_int_printall(); + f_close(&scriptin); + str_int_clear(); btn_wait(); } \ No newline at end of file diff --git a/source/tegraexplorer/script/parser.h b/source/tegraexplorer/script/parser.h new file mode 100644 index 0000000..2c42b86 --- /dev/null +++ b/source/tegraexplorer/script/parser.h @@ -0,0 +1,5 @@ +#pragma once + +void tester(char *path); +void skipbrackets(); +void getfollowingchar(char end); \ No newline at end of file diff --git a/source/tegraexplorer/script/script.h b/source/tegraexplorer/script/script.h deleted file mode 100644 index 5b35982..0000000 --- a/source/tegraexplorer/script/script.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -void tester(char *path); \ No newline at end of file diff --git a/source/tegraexplorer/script/variables.c b/source/tegraexplorer/script/variables.c new file mode 100644 index 0000000..70ec073 --- /dev/null +++ b/source/tegraexplorer/script/variables.c @@ -0,0 +1,89 @@ +#include +#include "../../mem/heap.h" +#include "../gfx/gfxutils.h" +#include "../emmc/emmc.h" +#include "../../utils/types.h" +#include "../../libs/fatfs/ff.h" +#include "../../utils/sprintf.h" +#include "../../utils/btn.h" +#include "../../gfx/gfx.h" +#include "../../utils/util.h" +#include "../../storage/emummc.h" +#include "parser.h" +#include "../common/common.h" +#include "../fs/fsactions.h" +#include "variables.h" +#include "../utils/utils.h" + +static dict_str_int *str_int_table = NULL; +static dict_str_str *str_str_table = NULL; +static dict_str_loc *str_jmp_table = NULL; + +int str_int_add(char *key, int value){ + char *key_local; + dict_str_int *keyvaluepair; + + utils_copystring(key, &key_local); + + keyvaluepair = calloc(1, sizeof(keyvaluepair)); + keyvaluepair->key = key_local; + keyvaluepair->value = value; + keyvaluepair->next = NULL; + if (str_int_table == NULL){ + str_int_table = keyvaluepair; + } + else { + dict_str_int *temp; + temp = str_int_table; + while (temp != NULL){ + if (!strcmp(temp->key, key_local)){ + temp->value = value; + return 0; + } + + if (temp->next == NULL){ + temp->next = keyvaluepair; + return 0; + } + + temp = temp->next; + } + } + + return 0; +} + +int str_int_find(char *key, int *out){ + dict_str_int *temp; + temp = str_int_table; + while (temp != NULL){ + if (!strcmp(temp->key, key)){ + *out = temp->value; + return 0; + } + temp = temp->next; + } + + return -1; +} + +void str_int_clear(){ + dict_str_int *cur, *next; + cur = str_int_table; + + while (cur != NULL){ + next = cur->next; + free(cur->key); + free(cur); + cur = next; + } +} + +void str_int_printall(){ + dict_str_int *temp; + temp = str_int_table; + while (temp != NULL){ + gfx_printf("%s -> %d\n", temp->key, temp->value); + temp = temp->next; + } +} \ No newline at end of file diff --git a/source/tegraexplorer/script/variables.h b/source/tegraexplorer/script/variables.h new file mode 100644 index 0000000..6f34133 --- /dev/null +++ b/source/tegraexplorer/script/variables.h @@ -0,0 +1,25 @@ +#pragma once +#include "../../utils/types.h" + +typedef struct _dict_str_int { + char *key; + int value; + struct _dict_str_int *next; +} dict_str_int; + +typedef struct _dict_str_str { + char *key; + char *value; + struct _dict_str_str *next; +} dict_str_str; + +typedef struct _dict_str_loc { + char *key; + u64 value; + struct _dict_str_loc *next; +} dict_str_loc; + +int str_int_add(char *key, int value); +int str_int_find(char *key, int *out); +void str_int_clear(); +void str_int_printall(); \ No newline at end of file