From 3a1f8d4243dcc8cecdd4747b2c1b082d3cae9493 Mon Sep 17 00:00:00 2001 From: suchmememanyskill Date: Mon, 1 Feb 2021 17:58:23 +0100 Subject: [PATCH] untested, added dict_t as a var type in tscript --- source/script/args.c | 18 ++++++++++++++++++ source/script/functions.c | 19 +++++++++++++++++++ source/script/lexer.c | 4 ++++ source/script/types.h | 3 +++ source/script/variables.c | 23 +++++++++++++++++++++++ source/script/variables.h | 1 + source/utils/vector.c | 3 +-- 7 files changed, 69 insertions(+), 2 deletions(-) diff --git a/source/script/args.c b/source/script/args.c index ff53131..8d0652f 100644 --- a/source/script/args.c +++ b/source/script/args.c @@ -123,6 +123,24 @@ Variable_t getVarFromToken(scriptCtx_t* ctx, lexarToken_t* tokens, int* index, u return ErrValue(ERRINVALIDTYPE); } } + ELIFTX(MemberVariable){ + Variable_t* var = dictVectorFind(&ctx->varDict, tokens[i].text); + i++; + + if (var == NULL) + return ErrValue(ERRNOVAR); + + if (tokens[i].token == MemberVariable){ + return ErrValue(ERRINVALIDTYPE); // Recursive members currently not supported! + } + + Variable_t *find = dictVectorFind(&var->vectorType, tokens[i].text); + if (find == NULL) + return ErrValue(ERRNOVAR); // No member with that variable was found + + val = *find; + val.free = 0; + } ELIFTX(Function) { i += 2; diff --git a/source/script/functions.c b/source/script/functions.c index c2b7d75..b919f2b 100644 --- a/source/script/functions.c +++ b/source/script/functions.c @@ -422,6 +422,22 @@ scriptFunction(funcLaunchPayload){ return varInt(launch_payload(vars[0].stringType)); } +scriptFunction(funcNewDict){ + Variable_t dict = {0}; + dict.varType = DictionaryType; + dict.free = 1; + dict.vectorType = newVec(sizeof(dict_t), 4); + dict.vectorPtr = &dict.vectorType; + return dict; +} + +// Args: Dict, Str, Var +scriptFunction(funcAddToDict){ + Variable_t copy = copyVariable(vars[2]); + dictVectorAdd(vars[0].vectorPtr, newDict(CpyStr(vars[1].stringType), copy)); + return NullVar; +} + u8 fiveInts[] = {IntType, IntType, IntType, IntType, IntType}; u8 singleIntArray[] = { IntArrayType }; u8 singleInt[] = { IntType }; @@ -432,6 +448,7 @@ u8 StrByteVec[] = { StringType, ByteArrayType}; u8 MenuArgs[] = { StringArrayType, IntType, StringArrayType, IntArrayType}; u8 twoStrings[] = { StringType, StringType }; u8 mmcReadWrite[] = { StringType, StringType, IntType}; +u8 dictAdd[] = {DictionaryType, StringType, varArgs}; functionStruct_t scriptFunctions[] = { {"if", funcIf, 1, singleInt}, @@ -476,6 +493,8 @@ functionStruct_t scriptFunctions[] = { {"saveSign", funcSignSave, 1, singleStr}, {"timerMs", funcGetMs, 0, NULL}, {"launchPayload", funcLaunchPayload, 1, singleStr}, + {"dict", funcNewDict, 0, NULL}, + {"dictAdd", funcAddToDict, 3, dictAdd}, // Left from old: keyboard(?) }; diff --git a/source/script/lexer.c b/source/script/lexer.c index 3bc203a..1bc1ded 100644 --- a/source/script/lexer.c +++ b/source/script/lexer.c @@ -161,6 +161,10 @@ Vector_t runLexer(const char* in, u32 len) { vecAddElement(&vec, makeLexarToken(LSBracket, 0)); } + ELIFC('.'){ + if (lx[vec.count - 1].token == Variable) + lx[vec.count - 1].token = MemberVariable; + } ELIFC('=') { // Do we need to keep = if the vars are assignments anyway? if (in[1] == '='){ vecAddElement(&vec, makeLexarToken(EqualEqual, 0)); diff --git a/source/script/types.h b/source/script/types.h index e224c94..cc5edf4 100644 --- a/source/script/types.h +++ b/source/script/types.h @@ -7,6 +7,7 @@ enum Tokens { Invalid = 0, Variable = 1, ArrayVariable, + MemberVariable, Function, LBracket, StrLit, @@ -70,6 +71,7 @@ enum Variables { IntArrayType, StringArrayType, ByteArrayType, + DictionaryType, JumpType, DictType, NullType, @@ -103,6 +105,7 @@ typedef struct { char* stringType; Vector_t vectorType; }; + Vector_t *vectorPtr; } Variable_t; typedef struct { diff --git a/source/script/variables.c b/source/script/variables.c index ed3c2a5..9013424 100644 --- a/source/script/variables.c +++ b/source/script/variables.c @@ -2,6 +2,7 @@ #include "types.h" #include #include +#include "../utils/utils.h" void freeVariable(Variable_t dv) { if (!dv.free) @@ -22,9 +23,27 @@ void freeVariable(Variable_t dv) { case ByteArrayType: vecFree(dv.vectorType); break; + + case DictionaryType: + freeDictVector(&dv.vectorType); + break; } } +Variable_t copyVariable(Variable_t copy){ + switch (copy.varType){ + case IntType: + return copy; + case StringType: + return newVar(StringType, 1, .stringType = CpyStr(copy.stringType)); + case IntArrayType: + case StringArrayType: + return newVar(copy.varType, 1, .vectorType = vecCopy(©.vectorType)); + } + + return NullVar; // Other types are not supported or whatever +} + void freeVariableVector(Vector_t *v) { Variable_t* vars = vecGetArrayPtr(v, Variable_t*); for (int i = 0; i < v->count; i++) { @@ -69,6 +88,10 @@ void dictVectorAdd(Vector_t* v, dict_t add) { } else { vecAddElement(v, add); + dict_t *dict = vecGetArrayPtr(v, dict_t*); + for (int i = 0; i < v->count; i++){ + dict[i].value.vectorPtr = &dict[i].value.vectorType; + } } } diff --git a/source/script/variables.h b/source/script/variables.h index dac6115..16750ca 100644 --- a/source/script/variables.h +++ b/source/script/variables.h @@ -7,6 +7,7 @@ void freeDictVector(Vector_t* v); void freeVariableVector(Vector_t* v); void freeVariable(Variable_t dv); scriptCtx_t createScriptCtx(); +Variable_t copyVariable(Variable_t copy); u8 setIndentInstruction(scriptCtx_t* ctx, u8 level, u8 skip, u8 func, int jumpLoc); indentInstructor_t* getCurIndentInstruction(scriptCtx_t* ctx); diff --git a/source/utils/vector.c b/source/utils/vector.c index 19f9c31..ee2e08c 100644 --- a/source/utils/vector.c +++ b/source/utils/vector.c @@ -56,5 +56,4 @@ Vector_t vecCopyOffset(Vector_t* orig, u32 offset) { Vector_t vecCopy(Vector_t* orig) { return vecCopyOffset(orig, 0); -} - +} \ No newline at end of file