1
0
Fork 0
mirror of https://github.com/suchmememanyskill/TegraExplorer.git synced 2024-11-22 20:06:43 +00:00

[script] vector slicing, basic file functions

new operators:
vector(int, byte) - int -> works like py's list[0:-int]
vector(int, byte) : int -> works like py's list[int:]

new functions:
fileRead, fileWrite, fileExists, bytesToStr
This commit is contained in:
suchmememanyskill 2021-01-01 18:30:51 +01:00
parent 3d4182ec57
commit e1f292fe0d
8 changed files with 84 additions and 12 deletions

View file

@ -46,7 +46,7 @@ CUSTOMDEFINES += -DGFX_INC=$(GFX_INC) -DFFCFG_INC=$(FFCFG_INC)
#CUSTOMDEFINES += -DDEBUG #CUSTOMDEFINES += -DDEBUG
ARCH := -march=armv4t -mtune=arm7tdmi -mthumb -mthumb-interwork ARCH := -march=armv4t -mtune=arm7tdmi -mthumb -mthumb-interwork
CFLAGS = $(ARCH) -Os -nostdlib -ffunction-sections -fdata-sections -fomit-frame-pointer -fno-inline -std=gnu11 -Wall $(CUSTOMDEFINES) CFLAGS = $(ARCH) -Os -nostdlib -ffunction-sections -fdata-sections -fomit-frame-pointer -fno-inline -std=gnu11 -Wall -Wno-missing-braces $(CUSTOMDEFINES)
LDFLAGS = $(ARCH) -nostartfiles -lgcc -Wl,--nmagic,--gc-sections -Xlinker --defsym=IPL_LOAD_ADDR=$(IPL_LOAD_ADDR) LDFLAGS = $(ARCH) -nostartfiles -lgcc -Wl,--nmagic,--gc-sections -Xlinker --defsym=IPL_LOAD_ADDR=$(IPL_LOAD_ADDR)
################################################################################ ################################################################################

View file

@ -82,6 +82,8 @@ void RunScript(char *path, FSEntry_t entry){
ctx.script = runLexar(script, size); ctx.script = runLexar(script, size);
free(script); free(script);
dictVectorAdd(&ctx.varDict, newDict(CpyStr("_CWD"), (newVar(StringType, 0, .stringType = path))));
printError(mainLoop(&ctx)); printError(mainLoop(&ctx));
freeVariableVector(&ctx.varDict); freeVariableVector(&ctx.varDict);

View file

@ -343,6 +343,25 @@ Variable_t solveEquation(scriptCtx_t* ctx, lexarToken_t* tokens, u32 len, u8 sho
vecAddElement(&res.vectorType, in); vecAddElement(&res.vectorType, in);
} }
} }
ELIFT(Minus){
if (val.integerType >= res.vectorType.count)
return ErrValue(ERRSYNTAX);
res.vectorType.count -= val.integerType;
Vector_t newV = vecCopy(&res.vectorType);
freeVariable(res);
res.vectorType = newV;
res.free = 1;
}
ELIFT(Selector){
if (val.integerType >= res.vectorType.count)
return ErrValue(ERRSYNTAX);
Vector_t newV = vecCopyOffset(&res.vectorType, val.integerType);
freeVariable(res);
res.vectorType = newV;
res.free = 1;
}
} }
else if (res.varType == StringType && val.varType == IntType){ else if (res.varType == StringType && val.varType == IntType){
if (localOpToken == Minus){ if (localOpToken == Minus){

View file

@ -6,6 +6,9 @@
#include <mem/heap.h> #include <mem/heap.h>
#include "lexer.h" #include "lexer.h"
#include <storage/nx_sd.h>
#include "../fs/fsutils.h"
#define scriptFunction(name) Variable_t name(scriptCtx_t *ctx, Variable_t *vars, u32 varLen) #define scriptFunction(name) Variable_t name(scriptCtx_t *ctx, Variable_t *vars, u32 varLen)
scriptFunction(funcIf) { scriptFunction(funcIf) {
@ -76,10 +79,39 @@ scriptFunction(funcSetPixel){
return NullVar; return NullVar;
} }
scriptFunction(funcFileExists){
return newVar(IntType, 0, FileExists(vars[0].stringType));
}
// Args: Str (Path)
scriptFunction(funcReadFile){
u32 fSize = 0;
u8 *buff = sd_file_read(vars[0].stringType, &fSize);
if (buff == NULL)
return ErrVar(ERRFATALFUNCFAIL);
Vector_t vec = vecFromArray(buff, fSize, sizeof(u8));
return newVar(ByteArrayType, 1, .vectorType = vec);
}
// Args: Str (Path), vector(Byte) (toWrite)
scriptFunction(funcWriteFile){
return newVar(IntType, 0, sd_save_to_file(vars[1].vectorType.data, vars[1].vectorType.count, vars[0].stringType));
}
scriptFunction(funcByteToStr){
char *str = calloc(vars[0].vectorType.count + 1, 1);
memcpy(str, vars[0].vectorType.data, vars[0].vectorType.count);
return newVar(StringType, 1, .stringType = str);
}
u8 fiveInts[] = {IntType, IntType, IntType, IntType, IntType}; u8 fiveInts[] = {IntType, IntType, IntType, IntType, IntType};
u8 singleIntArray[] = { IntArrayType }; u8 singleIntArray[] = { IntArrayType };
u8 singleInt[] = { IntType }; u8 singleInt[] = { IntType };
u8 singleAny[] = { varArgs }; u8 singleAny[] = { varArgs };
u8 singleStr[] = { StringType };
u8 singleByteArr[] = { ByteArrayType };
u8 StrByteVec[] = { StringType, ByteArrayType};
functionStruct_t scriptFunctions[] = { functionStruct_t scriptFunctions[] = {
{"if", funcIf, 1, singleInt}, {"if", funcIf, 1, singleInt},
@ -90,6 +122,10 @@ functionStruct_t scriptFunctions[] = {
{"len", funcLen, 1, singleAny}, {"len", funcLen, 1, singleAny},
{"byte", funcMakeByteArray, 1, singleIntArray}, {"byte", funcMakeByteArray, 1, singleIntArray},
{"setPixel", funcSetPixel, 5, fiveInts}, {"setPixel", funcSetPixel, 5, fiveInts},
{"fileRead", funcReadFile, 1, singleStr},
{"fileWrite", funcWriteFile, 2, StrByteVec},
{"fileExists", funcFileExists, 1, singleStr},
{"bytesToStr", funcByteToStr, 1, singleByteArr},
}; };
Variable_t executeFunction(scriptCtx_t* ctx, char* func_name, lexarToken_t *start, u32 len) { Variable_t executeFunction(scriptCtx_t* ctx, char* func_name, lexarToken_t *start, u32 len) {

View file

@ -102,15 +102,19 @@ Vector_t runLexar(const char* in, u32 len) {
// maybe measure len between ( ) and [ ], so this doesn't have to be done during runtime? // maybe measure len between ( ) and [ ], so this doesn't have to be done during runtime?
// We also have to support (()). maybe if '(' set indent level, then if ')' minus indent level, set len. indent level contains {u8 level, u16 token, u16 startoffset} // We also have to support (()). maybe if '(' set indent level, then if ')' minus indent level, set len. indent level contains {u8 level, u16 token, u16 startoffset}
u32 lastAssignment = 0;
while ((in - start) < len) { while ((in - start) < len) {
lexarToken_t* lx = vecGetArray(lexarToken_t*, vec); lexarToken_t* lx = vecGetArray(lexarToken_t*, vec);
if ((lx[vec.count - 2].token == StrLit || lx[vec.count - 2].token == IntLit || lx[vec.count - 2].token == Variable || lx[vec.count - 2].token == RSBracket || lx[vec.count - 2].token == RBracket) if ((lx[vec.count - 2].token == StrLit || lx[vec.count - 2].token == IntLit || lx[vec.count - 2].token == Variable || lx[vec.count - 2].token == RSBracket || lx[vec.count - 2].token == RBracket)
&& (lx[vec.count - 1].token == Variable || lx[vec.count - 1].token == LCBracket || lx[vec.count - 1].token == RCBracket)) { && (lx[vec.count - 1].token == Variable || lx[vec.count - 1].token == LCBracket || lx[vec.count - 1].token == RCBracket)) {
lexarToken_t holder = lx[vec.count - 1]; if (!(lx[lastAssignment].token == ArrayVariableAssignment && lx[vec.count - 1].token == Variable && lx[vec.count - 2].token == RSBracket)) {
lx[vec.count - 1] = makeLexarToken(EquationSeperator, 0); lexarToken_t holder = lx[vec.count - 1];
vecAddElement(&vec, holder); lx[vec.count - 1] = makeLexarToken(EquationSeperator, 0);
lx = vecGetArray(lexarToken_t*, vec); vecAddElement(&vec, holder);
lx = vecGetArray(lexarToken_t*, vec);
}
} }
if (isValidWord(*in)) { if (isValidWord(*in)) {
@ -170,8 +174,12 @@ Vector_t runLexar(const char* in, u32 len) {
} }
if (lx[vec.count - back].token == ArrayVariable) { if (lx[vec.count - back].token == ArrayVariable) {
lx[vec.count - back].token = ArrayVariableAssignment; lx[vec.count - back].token = ArrayVariableAssignment;
lastAssignment = vec.count - back;
in++;
continue;
} }
} }
lastAssignment = 0;
} }
ELIFC('{') { ELIFC('{') {
if (lx[vec.count - 1].token == VariableAssignment) { if (lx[vec.count - 1].token == VariableAssignment) {

View file

@ -57,7 +57,8 @@ enum Errors {
ERRNOVAR, ERRNOVAR,
ERRNOFUNC, ERRNOFUNC,
ERRINACTIVEINDENT, ERRINACTIVEINDENT,
ERRDIVBYZERO ERRDIVBYZERO,
ERRFATALFUNCFAIL,
}; };
enum Variables { enum Variables {

View file

@ -47,9 +47,14 @@ bool vecAdd(Vector_t* v, void* elem, u32 sz)
return true; return true;
} }
Vector_t vecCopy(Vector_t* orig) { Vector_t vecCopyOffset(Vector_t* orig, u32 offset) {
Vector_t dst = newVec(orig->elemSz, orig->count); Vector_t dst = newVec(orig->elemSz, orig->count - offset);
memcpy(dst.data, orig->data, orig->count * orig->elemSz); memcpy(dst.data, ((u8*)orig->data + orig->elemSz * offset), (orig->count - offset) * orig->elemSz);
dst.count = orig->count; dst.count = orig->count - offset;
return dst; return dst;
} }
Vector_t vecCopy(Vector_t* orig) {
return vecCopyOffset(orig, 0);
}

View file

@ -26,4 +26,5 @@ typedef struct {
Vector_t newVec(u32 typesz, u32 preallocate); Vector_t newVec(u32 typesz, u32 preallocate);
Vector_t vecFromArray(void* array, u32 count, u32 typesz); Vector_t vecFromArray(void* array, u32 count, u32 typesz);
bool vecAdd(Vector_t* v, void* elem, u32 sz); bool vecAdd(Vector_t* v, void* elem, u32 sz);
Vector_t vecCopy(Vector_t* orig); Vector_t vecCopy(Vector_t* orig);
Vector_t vecCopyOffset(Vector_t* orig, u32 offset);