Skip to content

Commit

Permalink
MiniScript: Transpilation: some improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasdr committed Oct 22, 2023
1 parent ccef36e commit 66cdde0
Show file tree
Hide file tree
Showing 16 changed files with 1,119 additions and 966 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ SRCS = \
src/tdme/tests/MathOperatorTest.cpp \
src/tdme/tests/MiniScriptAdvancedTest.cpp \
src/tdme/tests/MiniScriptBaseTest.cpp \
src/tdme/tests/MiniScriptClassTest.cpp \
src/tdme/tests/MiniScriptEmitTest.cpp \
src/tdme/tests/MiniScriptFunctionsTest.cpp \
src/tdme/tests/PathFindingTest.cpp \
Expand Down Expand Up @@ -639,6 +640,7 @@ SRCS = \
src/tdme/utilities/Integer.cpp \
src/tdme/utilities/MiniScript.cpp \
src/tdme/utilities/MiniScriptMath.cpp \
src/tdme/utilities/MiniScriptTranspiler.cpp \
src/tdme/utilities/ModelTools.cpp \
src/tdme/utilities/PathFinding.cpp \
src/tdme/utilities/Primitives.cpp \
Expand Down
2 changes: 2 additions & 0 deletions Makefile.nmake
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ SRCS = \
src/tdme/tests/MathOperatorTest.cpp \
src/tdme/tests/MiniScriptAdvancedTest.cpp \
src/tdme/tests/MiniScriptBaseTest.cpp \
src/tdme/tests/MiniScriptClassTest.cpp \
src/tdme/tests/MiniScriptEmitTest.cpp \
src/tdme/tests/MiniScriptFunctionsTest.cpp \
src/tdme/tests/PathFindingTest.cpp \
Expand Down Expand Up @@ -500,6 +501,7 @@ SRCS = \
src/tdme/utilities/Integer.cpp \
src/tdme/utilities/MiniScript.cpp \
src/tdme/utilities/MiniScriptMath.cpp \
src/tdme/utilities/MiniScriptTranspiler.cpp \
src/tdme/utilities/ModelTools.cpp \
src/tdme/utilities/PathFinding.cpp \
src/tdme/utilities/Primitives.cpp \
Expand Down
3 changes: 2 additions & 1 deletion scripts/transpile-scripts.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.\bin\tdme\tools\cli\miniscripttranspiler resources\tests\scripts\advanced-test.tscript src\tdme\tests\MiniScriptAdvancedTest.cpp
.\bin\tdme\tools\cli\miniscripttranspiler resources\tests\scripts\base-test.tscript src\tdme\tests\MiniScriptBaseTest.cpp
.\bin\tdme\tools\cli\miniscripttranspiler resources\tests\scripts\class-test.tscript src\tdme\tests\MiniScriptClassTest.cpp
.\bin\tdme\tools\cli\miniscripttranspiler resources\tests\scripts\emit-test.tscript src\tdme\tests\MiniScriptEmitTest.cpp
.\bin\tdme\tools\cli\miniscripttranspiler resources\tests\scripts\functions-test.tscript src\tdme\tests\MiniScriptFunctionsTest.cpp
.\bin\tdme\tools\cli\miniscripttranspiler resources\tests\scripts\functions-test.tscript src\tdme\tests\MiniScriptFunctionsTest.cpp
3 changes: 2 additions & 1 deletion scripts/transpile-scripts.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash
./bin/tdme/tools/cli/miniscripttranspiler resources/tests/scripts/advanced-test.tscript src/tdme/tests/MiniScriptAdvancedTest.cpp
./bin/tdme/tools/cli/miniscripttranspiler resources/tests/scripts/base-test.tscript src/tdme/tests/MiniScriptBaseTest.cpp
./bin/tdme/tools/cli/miniscripttranspiler resources/tests/scripts/class-test.tscript src/tdme/tests/MiniScriptClassTest.cpp
./bin/tdme/tools/cli/miniscripttranspiler resources/tests/scripts/emit-test.tscript src/tdme/tests/MiniScriptEmitTest.cpp
./bin/tdme/tools/cli/miniscripttranspiler resources/tests/scripts/functions-test.tscript src/tdme/tests/MiniScriptFunctionsTest.cpp
./bin/tdme/tools/cli/miniscripttranspiler resources/tests/scripts/functions-test.tscript src/tdme/tests/MiniScriptFunctionsTest.cpp
1 change: 1 addition & 0 deletions scripts/untranspile-scripts.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.\bin\tdme\tools\cli\miniscriptuntranspiler resources\tests\scripts\advanced-test.tscript src\tdme\tests\MiniScriptAdvancedTest.cpp
.\bin\tdme\tools\cli\miniscriptuntranspiler resources\tests\scripts\base-test.tscript src\tdme\tests\MiniScriptBaseTest.cpp
.\bin\tdme\tools\cli\miniscriptuntranspiler resources\tests\scripts\class-test.tscript src\tdme\tests\MiniScriptClassTest.cpp
.\bin\tdme\tools\cli\miniscriptuntranspiler resources\tests\scripts\emit-test.tscript src\tdme\tests\MiniScriptEmitTest.cpp
.\bin\tdme\tools\cli\miniscriptuntranspiler resources\tests\scripts\functions-test.tscript src\tdme\tests\MiniScriptFunctionsTest.cpp
1 change: 1 addition & 0 deletions scripts/untranspile-scripts.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash
./bin/tdme/tools/cli/miniscriptuntranspiler resources/tests/scripts/advanced-test.tscript src/tdme/tests/MiniScriptAdvancedTest.cpp
./bin/tdme/tools/cli/miniscriptuntranspiler resources/tests/scripts/base-test.tscript src/tdme/tests/MiniScriptBaseTest.cpp
./bin/tdme/tools/cli/miniscriptuntranspiler resources/tests/scripts/class-test.tscript src/tdme/tests/MiniScriptClassTest.cpp
./bin/tdme/tools/cli/miniscriptuntranspiler resources/tests/scripts/emit-test.tscript src/tdme/tests/MiniScriptEmitTest.cpp
./bin/tdme/tools/cli/miniscriptuntranspiler resources/tests/scripts/functions-test.tscript src/tdme/tests/MiniScriptFunctionsTest.cpp
17 changes: 17 additions & 0 deletions src/tdme/tests/MiniScriptClassTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "MiniScriptClassTest.h"

#include <tdme/tdme.h>
#include <tdme/utilities/Console.h>
#include <tdme/utilities/MiniScript.h>
#include <tdme/utilities/MiniScriptMath.h>

using tdme::utilities::Console;
using tdme::utilities::MiniScript;
using tdme::utilities::MiniScriptMath;

MiniScriptClassTest::MiniScriptClassTest(): MiniScript() {
initializeNative();
}

/*__MINISCRIPT_TRANSPILEDMINISCRIPTCODE_DEFINITIONS_START__*/
/*__MINISCRIPT_TRANSPILEDMINISCRIPTCODE_DEFINITIONS_END__*/
24 changes: 24 additions & 0 deletions src/tdme/tests/MiniScriptClassTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <tdme/tdme.h>
#include <tdme/utilities/Console.h>
#include <tdme/utilities/MiniScript.h>

using tdme::utilities::Console;
using tdme::utilities::MiniScript;

/**
* Transpile with './bin/tdme/tools/cli/miniscripttranspiler resources/tests/scripts/class-test.tscript src/tdme/tests/MiniScriptClassTest.cpp'
*/
class MiniScriptClassTest: public MiniScript {
public:
// forbid class copy
FORBID_CLASS_COPY(MiniScriptClassTest)

/**
* Public constructor
*/
MiniScriptClassTest();

/*__MINISCRIPT_TRANSPILEDMINISCRIPTCODE_DECLARATIONS_START__*/
/*__MINISCRIPT_TRANSPILEDMINISCRIPTCODE_DECLARATIONS_END__*/

};
21 changes: 21 additions & 0 deletions src/tdme/tests/MiniScriptTest-main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "MiniScriptAdvancedTest.h"
#include "MiniScriptBaseTest.h"
#include "MiniScriptClassTest.h"
#include "MiniScriptEmitTest.h"
#include "MiniScriptFunctionsTest.h"

Expand Down Expand Up @@ -34,6 +35,26 @@ int main(int argc, char *argv[]) {
Console::println();
Console::println();
}
// class test
{
auto script = make_unique<MiniScriptClassTest>();
script->parseScript("resources/tests/scripts", "class-test.tscript");
Console::println("---------------------------------------------------------");
Console::println("Loaded test script: " + script->getScriptFileName() + ": runs " + (script->isNative() == true?"natively":"interpreted"));
Console::println("---------------------------------------------------------");
// Console::println(script->getInformation());
if (script->isValid() == false) {
Console::println("Script not valid. Exiting");
return 1;
}
while (script->isRunning() == true) {
script->execute();
}
//
Console::println();
Console::println();
Console::println();
}
// advanced test
{
auto script = make_unique<MiniScriptAdvancedTest>();
Expand Down
1 change: 1 addition & 0 deletions src/tdme/tests/fwd-tdme.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace tests {
class MathOperatorTest;
class MiniScriptBaseTest;
class MiniScriptAdvancedTest;
class MiniScriptClassTest;
class MiniScriptEmitTest;
class MiniScriptFunctionsTest;
class PathFindingTest;
Expand Down
25 changes: 21 additions & 4 deletions src/tdme/tools/cli/miniscripttranspiler-main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <tdme/utilities/Exception.h>
#include <tdme/utilities/Integer.h>
#include <tdme/utilities/MiniScript.h>
#include <tdme/utilities/MiniScriptTranspiler.h>
#include <tdme/utilities/StringTools.h>

using std::find;
Expand All @@ -39,6 +40,7 @@ using tdme::utilities::Console;
using tdme::utilities::Exception;
using tdme::utilities::Integer;
using tdme::utilities::MiniScript;
using tdme::utilities::MiniScriptTranspiler;
using tdme::utilities::StringTools;

namespace tdme {
Expand Down Expand Up @@ -338,7 +340,7 @@ static void createArrayAccessMethods(MiniScript* miniScript, string& generatedDe
auto scriptStateChanged = false;
auto scriptStopped = false;
vector<string >enabledNamedConditions;
scriptInstance->transpileScriptStatement(transpiledCode, arrayAccessSyntaxTree, arrayAccessStatement, MiniScript::SCRIPTIDX_NONE, MiniScript::SCRIPTIDX_NONE, statementIdx, methodCodeMap, scriptStateChanged, scriptStopped, enabledNamedConditions, 0, {}, "ScriptVariable()", "return returnValue;");
MiniScriptTranspiler::transpileScriptStatement(scriptInstance, transpiledCode, arrayAccessSyntaxTree, arrayAccessStatement, MiniScript::SCRIPTIDX_NONE, MiniScript::SCRIPTIDX_NONE, statementIdx, methodCodeMap, scriptStateChanged, scriptStopped, enabledNamedConditions, 0, {}, "ScriptVariable()", "return returnValue;");
generatedDeclarations+= headerIndent + "/**\n";
generatedDeclarations+= headerIndent + " * Miniscript transpilation for a " + (condition == true?"condition":"statement") + " array access statement for method '" + methodName + "', statement index " + to_string(statement.statementIdx) + ", argument indices " + MiniScript::getArgumentIndicesAsString(nextArgumentIndices, ", ") + ", array access statement index " + to_string(arrayAccessStatementIdx) + "\n";
generatedDeclarations+= headerIndent + " * @param statement statement" + "\n";
Expand Down Expand Up @@ -415,6 +417,7 @@ static void generateMiniScriptEvaluateMemberAccessArrays(MiniScript* miniScript,
allMethods.insert(method);
}
declarations.push_back("// evaluate member access constants");
declarations.push_back("static constexpr int EVALUATEMEMBERACCESSARRAYIDX_NONE { -1 };");
auto methodIdx = 0;
for (const auto& method: allMethods) {
declarations.push_back("static constexpr int EVALUATEMEMBERACCESSARRAYIDX_" + StringTools::toUpperCase(method) + " { " + to_string(methodIdx) + " };");
Expand Down Expand Up @@ -551,10 +554,23 @@ static void createArrayMapSetVariable(MiniScript* miniScript, const MiniScript::
auto scriptStateChanged = false;
auto scriptStopped = false;
vector<string>enabledNamedConditions;
miniScript->transpileScriptStatement(transpiledCode, *variable.getInitializer()->getSyntaxTree(), statement, MiniScript::SCRIPTIDX_NONE, MiniScript::SCRIPTIDX_NONE, statementIdx, methodCodeMap, scriptStateChanged, scriptStopped, enabledNamedConditions, 0, {}, "ScriptVariable()", "const auto& variableD" + to_string(initializerDepth) + " = returnValue; " + postStatement + "\n", 1);
MiniScriptTranspiler::transpileScriptStatement(miniScript, transpiledCode, *variable.getInitializer()->getSyntaxTree(), statement, MiniScript::SCRIPTIDX_NONE, MiniScript::SCRIPTIDX_NONE, statementIdx, methodCodeMap, scriptStateChanged, scriptStopped, enabledNamedConditions, 0, {}, "ScriptVariable()", "const auto& variableD" + to_string(initializerDepth) + " = returnValue; " + postStatement + "\n", 1);
generatedDefinitions+= transpiledCode;
}
break;
case MiniScript::TYPE_FUNCTION_ASSIGNMENT:
{
string value;
variable.getStringValue(value);
value = StringTools::replace(StringTools::replace(value, "\\", "\\\\"), "\"", "\\\"");
//
generatedDefinitions+= indent + "{" + "\n";
generatedDefinitions+= indent + "\t" + "ScriptVariable variableD" + to_string(initializerDepth) + ";" + "\n";
generatedDefinitions+= indent + "\t" + "variableD" + to_string(initializerDepth) + ".setFunctionAssignment(\"" + value + "\");" + "\n";
generatedDefinitions+= indent + "\t" + postStatement + "\n";
generatedDefinitions+= indent + "}" + "\n";
}
break;
default: break;
}
}
Expand Down Expand Up @@ -914,7 +930,7 @@ static void processFile(const string& scriptFileName, const string& miniscriptTr
// transpile definition
generatedDefinitions+= "void " + miniScriptClassName + "::" + methodName + "(int miniScriptGotoStatementIdx) {" + "\n";
string generatedSubCode;
scriptInstance->transpile(generatedSubCode, scriptIdx, methodCodeMap);
MiniScriptTranspiler::transpile(scriptInstance.get(), generatedSubCode, scriptIdx, methodCodeMap);
generatedDefinitions+= generatedSubCode;
generatedDefinitions+= string() + "}" + "\n\n";

Expand All @@ -925,7 +941,8 @@ static void processFile(const string& scriptFileName, const string& miniscriptTr
generatedDetermineNamedScriptIdxToStartDefinition+= string() + "\t" + "\t" + "// next statements belong to tested enabled named condition with name \"" + script.name + "\"" + "\n";
generatedDetermineNamedScriptIdxToStartDefinition+= string() + "\t" + "\t" + "if (enabledNamedCondition == \"" + script.name + "\")" + "\n";
}
scriptInstance->transpileScriptCondition(
MiniScriptTranspiler::transpileScriptCondition(
scriptInstance.get(),
script.scriptType == MiniScript::Script::SCRIPTTYPE_ON?generatedDetermineScriptIdxToStartDefinition:generatedDetermineNamedScriptIdxToStartDefinition,
scriptIdx,
methodCodeMap,
Expand Down
Loading

0 comments on commit 66cdde0

Please sign in to comment.