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 66cdde0 commit cec892b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 22 deletions.
25 changes: 3 additions & 22 deletions src/tdme/tools/cli/miniscripttranspiler-main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,27 +395,8 @@ static void createArrayAccessMethods(MiniScript* miniScript, string& generatedDe

static void generateMiniScriptEvaluateMemberAccessArrays(MiniScript* miniScript, vector<string>& declarations, vector<string>& definitions) {
auto scriptMethods = miniScript->getMethods();
//
set<string> allMethods;
map<string, vector<string>> methodByCategory;
for (auto scriptMethod: scriptMethods) {
string category;
if (scriptMethod->getMethodName().rfind('.') != string::npos) category = StringTools::substring(scriptMethod->getMethodName(), 0, scriptMethod->getMethodName().rfind('.'));
string method =
StringTools::substring(
scriptMethod->getMethodName(),
category.empty() == true?0:category.size() + 1,
scriptMethod->getMethodName().size());
// TODO: improve me
if (scriptMethod->getArgumentTypes().empty() == true ||
scriptMethod->getArgumentTypes()[0].name != category) continue;
//
if (category != MiniScript::ScriptVariable::getClassName(scriptMethod->getArgumentTypes()[0].type) ||
category != scriptMethod->getArgumentTypes()[0].name) continue;
//
methodByCategory[category].push_back(method);
allMethods.insert(method);
}
auto allMethods = MiniScriptTranspiler::getAllMethodNames(miniScript);
auto methodsByClasses = MiniScriptTranspiler::getAllClassesMethodNames(miniScript);
declarations.push_back("// evaluate member access constants");
declarations.push_back("static constexpr int EVALUATEMEMBERACCESSARRAYIDX_NONE { -1 };");
auto methodIdx = 0;
Expand All @@ -436,7 +417,7 @@ static void generateMiniScriptEvaluateMemberAccessArrays(MiniScript* miniScript,
definitions.push_back("evaluateMemberAccessArrays = {};");
for (auto typeIdx = static_cast<int>(MiniScript::TYPE_STRING); typeIdx <= static_cast<int>(MiniScript::TYPE_SET); typeIdx++) {
const auto& className = MiniScript::ScriptVariable::getClassName(static_cast<MiniScript::ScriptVariableType>(typeIdx));
const auto& methods = methodByCategory[className];
const auto& methods = methodsByClasses[className];
auto methodIdx = 0;
for (const auto& method: allMethods) {
//
Expand Down
51 changes: 51 additions & 0 deletions src/tdme/utilities/MiniScriptTranspiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using tdme::utilities::MiniScriptTranspiler;

using std::remove;
using std::sort;
using std::string;
using std::to_string;
using std::unordered_map;
Expand All @@ -37,6 +38,56 @@ using tdme::math::Vector4;
using tdme::utilities::Console;
using tdme::utilities::StringTools;

const vector<string> MiniScriptTranspiler::getAllMethodNames(MiniScript* miniScript) {
unordered_set<string> allMethods;
for (auto scriptMethod: miniScript->getMethods()) {
string className;
if (scriptMethod->getMethodName().rfind('.') != string::npos) className = StringTools::substring(scriptMethod->getMethodName(), 0, scriptMethod->getMethodName().rfind('.'));
string method =
StringTools::substring(
scriptMethod->getMethodName(),
className.empty() == true?0:className.size() + 1,
scriptMethod->getMethodName().size());
// first argument name of method must equal the name of the class
if (scriptMethod->getArgumentTypes().empty() == true ||
scriptMethod->getArgumentTypes()[0].name != className) continue;
// first argument of method must be of type of the class
if (className != MiniScript::ScriptVariable::getClassName(scriptMethod->getArgumentTypes()[0].type) ||
className != scriptMethod->getArgumentTypes()[0].name) continue;
//
allMethods.insert(method);
}
//
vector<string> result;
for (auto method: allMethods) result.push_back(method);
sort(result.begin(), result.end());
//
return result;
}

const unordered_map<string, vector<string>> MiniScriptTranspiler::getAllClassesMethodNames(MiniScript* miniScript) {
unordered_map<string, vector<string>> methodByClasses;
for (auto scriptMethod: miniScript->getMethods()) {
string className;
if (scriptMethod->getMethodName().rfind('.') != string::npos) className = StringTools::substring(scriptMethod->getMethodName(), 0, scriptMethod->getMethodName().rfind('.'));
string method =
StringTools::substring(
scriptMethod->getMethodName(),
className.empty() == true?0:className.size() + 1,
scriptMethod->getMethodName().size());
// first argument name of method must equal the name of the class
if (scriptMethod->getArgumentTypes().empty() == true ||
scriptMethod->getArgumentTypes()[0].name != className) continue;
// first argument of method must be of type of the class
if (className != MiniScript::ScriptVariable::getClassName(scriptMethod->getArgumentTypes()[0].type) ||
className != scriptMethod->getArgumentTypes()[0].name) continue;
//
methodByClasses[className].push_back(method);
}
//
return methodByClasses;
}

bool MiniScriptTranspiler::transpileScriptStatement(MiniScript* miniScript, string& generatedCode, const MiniScript::ScriptSyntaxTreeNode& syntaxTree, const MiniScript::ScriptStatement& statement, int scriptConditionIdx, int scriptIdx, int& statementIdx, const unordered_map<string, vector<string>>& methodCodeMap, bool& scriptStateChanged, bool& scriptStopped, vector<string>& enabledNamedConditions, int depth, const vector<int>& argumentIndices, const string& returnValue, const string& injectCode, int additionalIndent) {
//
statementIdx++;
Expand Down
14 changes: 14 additions & 0 deletions src/tdme/utilities/MiniScriptTranspiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ using tdme::utilities::MiniScript;
*/
class tdme::utilities::MiniScriptTranspiler {
public:
/**
* Get all method names
* @param miniScript MiniScript instance
* @return all method names
*/
static const vector<string> getAllMethodNames(MiniScript* miniScript);

/**
* Get all method names
* @param miniScript MiniScript instance
* @return all method names
*/
static const unordered_map<string, vector<string>> getAllClassesMethodNames(MiniScript* miniScript);

/**
* Transpile script statement
* @param miniScript MiniScript instance
Expand Down

0 comments on commit cec892b

Please sign in to comment.