From 65a28d8ef61e9c05d960f0143ed969b998c5a17f Mon Sep 17 00:00:00 2001 From: suchmememanyskill Date: Fri, 9 Jul 2021 23:46:21 +0200 Subject: [PATCH] implement if.else() --- source/script/ABadIdeaVersion3.vcxproj | 2 ++ .../script/ABadIdeaVersion3.vcxproj.filters | 9 ++++++++ source/script/else.c | 22 +++++++++++++++++++ source/script/else.h | 7 ++++++ source/script/genericClass.c | 2 ++ source/script/model.h | 1 + source/script/parser.c | 14 +++++++----- source/script/standardLibrary.c | 8 +++---- 8 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 source/script/else.c create mode 100644 source/script/else.h diff --git a/source/script/ABadIdeaVersion3.vcxproj b/source/script/ABadIdeaVersion3.vcxproj index c34bbfa..2deb8e2 100644 --- a/source/script/ABadIdeaVersion3.vcxproj +++ b/source/script/ABadIdeaVersion3.vcxproj @@ -145,6 +145,7 @@ + @@ -163,6 +164,7 @@ + diff --git a/source/script/ABadIdeaVersion3.vcxproj.filters b/source/script/ABadIdeaVersion3.vcxproj.filters index 490e559..b6dfa81 100644 --- a/source/script/ABadIdeaVersion3.vcxproj.filters +++ b/source/script/ABadIdeaVersion3.vcxproj.filters @@ -34,6 +34,9 @@ {64738969-63b9-4d0e-85b9-d9a63a7e36b3} + + {ba83a808-23f1-44c6-85e4-d0c6c77200fa} + @@ -81,6 +84,9 @@ Source Files + + Source Files\Classes\Elseable + @@ -131,5 +137,8 @@ Header Files + + Source Files\Classes\Elseable + \ No newline at end of file diff --git a/source/script/else.c b/source/script/else.c new file mode 100644 index 0000000..dca9498 --- /dev/null +++ b/source/script/else.c @@ -0,0 +1,22 @@ +#include "else.h" + +ClassFunction(scriptElse) { + if (!caller->integer.value) { + Variable_t* res = genericCallDirect(args[0], NULL, 0); + if (res == NULL) + return NULL; + + removePendingReference(res); + } + return &emptyClass; +} + +u8 elseOneFunction[] = { FunctionClass }; + +ClassFunctionTableEntry_t elseFunctions[] = { + {"else", scriptElse, 1, elseOneFunction}, +}; + +Variable_t getElseMember(Variable_t* var, char* memberName) { + return getGenericFunctionMember(var, memberName, elseFunctions, ARRAY_SIZE(elseFunctions)); +} \ No newline at end of file diff --git a/source/script/else.h b/source/script/else.h new file mode 100644 index 0000000..170e7be --- /dev/null +++ b/source/script/else.h @@ -0,0 +1,7 @@ +#pragma once +#include "model.h" +#include "genericClass.h" +#include "compat.h" +#include "garbageCollector.h" + +Variable_t getElseMember(Variable_t* var, char* memberName); \ No newline at end of file diff --git a/source/script/genericClass.c b/source/script/genericClass.c index 0f344e2..02334f0 100644 --- a/source/script/genericClass.c +++ b/source/script/genericClass.c @@ -12,6 +12,7 @@ #include "scriptError.h" #include "saveClass.h" #include "unsolvedArrayClass.h" +#include "else.h" Variable_t* copyVariableToPtr(Variable_t var) { Variable_t* a = malloc(sizeof(Variable_t)); @@ -28,6 +29,7 @@ MemberGetters_t memberGetters[] = { {ByteArrayClass, getArrayMember}, {SolvedArrayReferenceClass, getArrayReferenceMember}, {UnresolvedArrayClass, getUnsolvedArrayMember}, + {ElseClass, getElseMember}, #ifndef WIN32 {SaveClass, getSaveMember}, #endif diff --git a/source/script/model.h b/source/script/model.h index c6fe409..2faaaef 100644 --- a/source/script/model.h +++ b/source/script/model.h @@ -35,6 +35,7 @@ typedef enum { EmptyClass, SolvedArrayReferenceClass, SaveClass, + ElseClass, } VariableType_t; typedef enum { diff --git a/source/script/parser.c b/source/script/parser.c index fe24daf..f49f931 100644 --- a/source/script/parser.c +++ b/source/script/parser.c @@ -389,15 +389,17 @@ ParserRet_t parseScript(char* in) { SCRIPT_PARSER_ERR("GET variable before {}"); continue; } - + + // Set last arg to {} CallArgs_t* lastCall = getLastRef(&lastOp->callArgs); if (lastCall->extraAction == ActionExtraCallArgs) { Function_t* funcArgs = lastCall->extra; - - op.token = EquationSeperator; - op.lineNumber = lineNumber; - vecAdd(&funcArgs->operations, op); - op.token = Variable; + if (funcArgs->operations.count != 0) { + op.token = EquationSeperator; + op.lineNumber = lineNumber; + vecAdd(&funcArgs->operations, op); + op.token = Variable; + } Variable_t a = newFunctionVariable(createFunctionClass(*popFunc, NULL)); vecAdd(&staticVariableHolder, a); diff --git a/source/script/standardLibrary.c b/source/script/standardLibrary.c index 6d601e0..0077caf 100644 --- a/source/script/standardLibrary.c +++ b/source/script/standardLibrary.c @@ -15,7 +15,6 @@ ClassFunction(stdIf) { s64 value = getIntValue(args[0]); - if (value) { Variable_t* res = genericCallDirect(args[1], NULL, 0); if (res == NULL) @@ -24,10 +23,11 @@ ClassFunction(stdIf) { removePendingReference(res); } - return &emptyClass; -} + Variable_t* ret = newIntVariablePtr(value); + ret->variableType = ElseClass; -// TODO: implement else by making if return a class that is else-able + return ret; +} ClassFunction(stdWhile) { Variable_t* result = eval(args[0]->function.function.operations.data, args[0]->function.function.operations.count, 1);