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

add dicts

This commit is contained in:
suchmememanyskill 2021-07-12 18:04:52 +02:00
parent 0bef41c033
commit 8c6e70b63d
8 changed files with 97 additions and 3 deletions

View file

@ -145,6 +145,7 @@
<ClCompile Include="ABadIdeaVersion3.c" />
<ClCompile Include="arrayClass.c" />
<ClCompile Include="arrayReferenceClass.c" />
<ClCompile Include="dictionaryClass.c" />
<ClCompile Include="else.c" />
<ClCompile Include="eval.c" />
<ClCompile Include="functionClass.c" />

View file

@ -87,6 +87,9 @@
<ClCompile Include="else.c">
<Filter>Source Files\Classes\Elseable</Filter>
</ClCompile>
<ClCompile Include="dictionaryClass.c">
<Filter>Source Files\Classes\Dictionary</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="compat.h">

View file

@ -0,0 +1,50 @@
#include "dictionaryClass.h"
#include <string.h>
#include "garbageCollector.h"
u8 dictOneStrOneAll[] = { StringClass, VARARGCOUNT };
Dict_t* getEntry(Vector_t *v, char* name) {
vecForEach(Dict_t*, dict, v) {
if (!strcmp(name, dict->name)) {
return dict;
}
}
return NULL;
}
ClassFunction(dictSet) {
addPendingReference(args[1]);
char* arg = CpyStr(args[0]->string.value);
Dict_t* possibleEntry = getEntry(&caller->dictionary.vector, arg);
if (possibleEntry == NULL) {
Dict_t a = { .name = arg, .var = args[1] };
vecAdd(&caller->dictionary.vector, a);
}
else {
possibleEntry->var = args[1];
free(arg);
}
return &emptyClass;
}
ClassFunctionTableEntry_t dictFunctions[] = {
{"set", dictSet, 2, dictOneStrOneAll},
};
Variable_t getDictMember(Variable_t* var, char* memberName) {
if (!strcmp(memberName, "set"))
return getGenericFunctionMember(var, memberName, dictFunctions, ARRAY_SIZE(dictFunctions));
vecForEach(Dict_t*, dict, (&var->dictionary.vector)) {
if (!strcmp(dict->name, memberName)) {
Variable_t a = { 0 };
a.variableType = ReferenceType;
a.referenceType = dict->var;
addPendingReference(dict->var);
return a;
}
}
return (Variable_t) { 0 };
}

View file

@ -1,3 +1,6 @@
#pragma once
#include "model.h"
#include "genericClass.h"
#include "compat.h"
Variable_t getDictMember(Variable_t* var, char* memberName);

View file

@ -187,6 +187,9 @@ void runtimeVariableEdit(Callback_SetVar_t* set, Variable_t* curRes) {
callMemberFunctionDirect(set->setVar, "set", args, 2);
}
removePendingReference(curRes);
removePendingReference(set->setVar);
// TODO: add non-top level sets
}

View file

@ -13,6 +13,7 @@
#include "saveClass.h"
#include "unsolvedArrayClass.h"
#include "else.h"
#include "dictionaryClass.h"
Variable_t* copyVariableToPtr(Variable_t var) {
Variable_t* a = malloc(sizeof(Variable_t));
@ -31,12 +32,12 @@ MemberGetters_t memberGetters[] = {
{SolvedArrayReferenceClass, getArrayReferenceMember},
{UnresolvedArrayClass, getUnsolvedArrayMember},
{ElseClass, getElseMember},
{DictionaryClass, getDictMember},
#ifndef WIN32
{SaveClass, getSaveMember},
#endif
};
Variable_t* genericGet(Variable_t* var, CallArgs_t* ref) {
if (ref->extraAction == ActionExtraMemberName) {
for (u32 i = 0; i < ARRAY_SIZE(memberGetters); i++) {
@ -45,6 +46,10 @@ Variable_t* genericGet(Variable_t* var, CallArgs_t* ref) {
if (member.variableType == None)
return NULL;
if (member.variableType == ReferenceType) {
return member.referenceType;
}
addPendingReference(var); // So caller doesn't fall out of scope. Don't forget to free!
return copyVariableToPtr(member);
}
@ -186,6 +191,7 @@ Variable_t* genericCall(Variable_t* var, CallArgs_t* ref) {
}
}
// TODO: add staticStorage
Variable_t getGenericFunctionMember(Variable_t* var, char* memberName, ClassFunctionTableEntry_t* entries, u8 len) {
Variable_t newVar = {.readOnly = 1, .variableType = FunctionClass};
newVar.function.origin = var;
@ -213,7 +219,12 @@ Variable_t* callMemberFunction(Variable_t* var, char* memberName, CallArgs_t* ar
if (funcRef.variableType == None)
return NULL;
return genericCall(&funcRef, args);
Variable_t* ptr = &funcRef;
if (funcRef.variableType == ReferenceType) {
ptr = funcRef.referenceType;
}
return genericCall(ptr, args);
}
}
@ -228,7 +239,12 @@ Variable_t* callMemberFunctionDirect(Variable_t* var, char* memberName, Variable
SCRIPT_FATAL_ERR("Did not find member '%s'", memberName);
}
return genericCallDirect(&funcRef, args, argsLen);
Variable_t* ptr = &funcRef;
if (funcRef.variableType == ReferenceType) {
ptr = funcRef.referenceType;
}
return genericCallDirect(ptr, args, argsLen);
}
}
@ -251,6 +267,13 @@ void freeVariableInternal(Variable_t* referencedTarget) {
case IntArrayClass:
vecFree(referencedTarget->solvedArray.vector);
break;
case DictionaryClass:;
vecForEach(Dict_t*, dict, (&referencedTarget->dictionary.vector)) {
modReference(dict->var, 0);
free(dict->name);
}
FREE(referencedTarget->dictionary.vector.data);
break;
}
}

View file

@ -36,6 +36,7 @@ typedef enum {
SolvedArrayReferenceClass,
SaveClass,
ElseClass,
ReferenceType,
} VariableType_t;
typedef enum {
@ -176,6 +177,7 @@ typedef struct _Variable_t {
#ifndef WIN32
SaveClass_t *save;
#endif
struct _Variable_t* referenceType;
};
union {
struct {

View file

@ -79,6 +79,13 @@ ClassFunction(stdBreak) {
return NULL;
}
ClassFunction(stdDict) {
Variable_t a = { 0 };
a.variableType = DictionaryClass;
a.dictionary.vector = newVec(sizeof(Dict_t), 0);
return copyVariableToPtr(a);
}
#ifndef WIN32
ClassFunction(stdMountSysmmc){
if (connectMMC(MMC_CONN_EMMC))
@ -121,6 +128,7 @@ enum standardFunctionIndexes {
STD_MOUNTSAVE,
STD_EXIT,
STD_BREAK,
STD_DICT,
};
u8 oneIntoneFunction[] = { IntClass, FunctionClass };
@ -135,6 +143,7 @@ ClassFunctionTableEntry_t standardFunctionDefenitions[] = {
[STD_MOUNTSAVE] = {"readsave", stdMountSave, 1, oneStringArgStd},
[STD_EXIT] = {"exit", stdExit, 0, 0},
[STD_BREAK] = {"break", stdBreak, 0, 0},
[STD_DICT] = {"dict", stdDict, 0, 0},
};
ClassFunctionTableEntry_t* searchStdLib(char* funcName) {