From fa870f46d5bf60d0175538d951ce6f39d1dbb443 Mon Sep 17 00:00:00 2001 From: sam Date: Sun, 16 Jan 2022 14:07:27 -0500 Subject: [PATCH] Porting code to linux --- Slang/Main.cpp | 71 ++++++++++++++++++++++++++++++++++-------------- Slang/anyops.h | 2 ++ Slang/builtin.h | 34 +++++++++++++---------- Slang/graphics.h | 7 +++-- 4 files changed, 78 insertions(+), 36 deletions(-) diff --git a/Slang/Main.cpp b/Slang/Main.cpp index a8dfef2..95c96a9 100644 --- a/Slang/Main.cpp +++ b/Slang/Main.cpp @@ -2,6 +2,17 @@ #include #include #include +#define DEVELOPER_MESSAGES true +#define EXAMPLE_PROJECT false + +#if defined(__unix__) +#define UNIX true +#define WINDOWS false +#elif defined(_MSC_VER) +#define UNIX false +#define WINDOWS true +#endif + #include #include #include @@ -12,9 +23,9 @@ #include #include -#if defined(__unix__) +#if UNIX #include -#elif defined(_MSC_VER) +#elif WINDOWS #include #endif @@ -26,9 +37,6 @@ #include "SLB.h" -#define DEVELOPER_MESSAGES true -#define EXAMPLE_PROJECT false - using namespace std; using namespace boost; @@ -536,15 +544,23 @@ boost::any ExecuteFunction(const string& functionName, const vector& vector> words = functionValues[functionName]; unordered_map variableValues = {}; - vector args = words[0]; - for (int i = 0; i < (int)inputVarVals.size(); i++) { - - variableValues[args[i]] = inputVarVals[i]; + + vector args = words[0]; // This causes problem in linux + + for (int i = 0; i < (int)inputVarVals.size(); i++) + { + if(i < args.size()) + { + variableValues[args[i]] = inputVarVals[i]; #if DEVELOPER_MESSAGES == true - cout << functionName + " \x1B[33m" << args[i] << " == " << AnyAsString(inputVarVals[i]) << "\033[0m\t\t" << endl; + cout << functionName + " \x1B[33m" << args[i] << " == " << AnyAsString(inputVarVals[i]) << "\033[0m\t\t" << endl; #endif + } } + #if DEVELOPER_MESSAGES + InterpreterLog("Iterate"); + #endif //Iterate through all lines in function for (int lineNum = 1; lineNum < (int)words.size(); lineNum++) { @@ -571,6 +587,9 @@ int parseSlang(string script) for (int i = 0; i < (int)lines.size(); i++) words.push_back(split(lines[i], ' ')); + #if DEVELOPER_MESSAGES + InterpreterLog("Gather variables & functions..."); + #endif // First go through entire script and iterate through all types to see if line is a variable // or function declaration, then store it with it's value for (int lineNum = 0; lineNum < (int)words.size(); lineNum++) @@ -618,6 +637,9 @@ int parseSlang(string script) } } + #if DEVELOPER_MESSAGES + InterpreterLog("Start Main()"); + #endif // Executes main, which is the starting function ExecuteFunction("Main", vector {}); @@ -637,16 +659,19 @@ int main(int argc, char* argv[]) if (argc > 1) { std::string scriptPath = argv[1]; -#if DEVELOPER_MESSAGES == true +#if DEVELOPER_MESSAGES cout << scriptPath << endl; #endif + std::string projectDirectory = scriptPath.substr(0, scriptPath.find_last_of("/\\")); +#if UNIX + chdir(projectDirectory.c_str()); + #if DEVELOPER_MESSAGES + InterpreterLog("Change directory to " + projectDirectory + "..."); + #endif +#elif WINDOWS std::wstring_convert> converter; std::wstring wide = converter.from_bytes(projectDirectory); - -#if defined(__unix__) - chdir(projectDirectory.c_str()); -#elif defined(_MSC_VER) LPCWSTR s = wide.c_str(); SetCurrentDirectory(s); #endif @@ -654,6 +679,9 @@ int main(int argc, char* argv[]) // Get script contents ifstream script(scriptPath); scriptString << script.rdbuf(); + #if DEVELOPER_MESSAGES + InterpreterLog("Gather script contents..."); + #endif } else { @@ -667,14 +695,14 @@ int main(int argc, char* argv[]) #if DEVELOPER_MESSAGES == true cout << scriptPath << endl; #endif + std::string projectDirectory = scriptPath.substr(0, scriptPath.find_last_of("/\\")); +#if UNIX + //chdir(projectDirectory.c_str()); +#elif WINDOWS std::wstring_convert> converter; std::wstring wide = converter.from_bytes(projectDirectory); - -#if defined(__unix__) - chdir(projectDirectory.c_str()); -#elif defined(_MSC_VER) - LPCSTR s = projectDirectory.c_str(); + LPCWSTR s = wide.c_str(); SetCurrentDirectory(s); #endif // Get script contents @@ -684,6 +712,9 @@ int main(int argc, char* argv[]) //system("pause"); + #if DEVELOPER_MESSAGES + InterpreterLog("Parsing..."); + #endif parseSlang(scriptString.str()); return 0; diff --git a/Slang/anyops.h b/Slang/anyops.h index c338e6b..34d0080 100644 --- a/Slang/anyops.h +++ b/Slang/anyops.h @@ -286,6 +286,8 @@ bool any_compare(const boost::any& a, const boost::any& b) // If it is a Vec2, then compare separately after converted else if (aType == 5) return any_cast(a) == any_cast(b); + + return false; } #endif diff --git a/Slang/builtin.h b/Slang/builtin.h index fde2d0b..11e9c13 100644 --- a/Slang/builtin.h +++ b/Slang/builtin.h @@ -16,7 +16,7 @@ #include #include -#define DEVELOPER_MESSAGES false +//#define DEVELOPER_MESSAGES false using namespace std; using namespace boost; @@ -57,7 +57,7 @@ int InterpreterLog(const string& logText) tm bt{}; #if defined(__unix__) - localtime_r(&timer, &bt); + //localtime_r(&timer, &bt); #elif defined(_MSC_VER) localtime_s(&bt, &timer); #else @@ -66,9 +66,12 @@ int InterpreterLog(const string& logText) bt = *localtime(&timer); #endif - int Hour = bt.tm_hour; - int Min = bt.tm_min; - int Sec = bt.tm_sec; + //int Hour = bt.tm_hour; + //int Min = bt.tm_min; + //int Sec = bt.tm_sec; + int Hour = 0; + int Min = 0; + int Sec = 0; cout << "\x1B[34m[" + to_string(Hour) + ":" + to_string(Min) + ":" + to_string(Sec) + "] \x1B[33mSlang: \x1B[32m" << logText << "\033[0m\t\t" << endl; return 1; @@ -80,7 +83,7 @@ int LogCriticalError(const string& errorText) tm bt{}; #if defined(__unix__) - localtime_r(&timer, &bt); + //localtime_r(&timer, &bt); #elif defined(_MSC_VER) localtime_s(&bt, &timer); #else @@ -89,9 +92,12 @@ int LogCriticalError(const string& errorText) bt = *localtime(&timer); #endif - int Hour = bt.tm_hour; - int Min = bt.tm_min; - int Sec = bt.tm_sec; + //int Hour = bt.tm_hour; + //int Min = bt.tm_min; + //int Sec = bt.tm_sec; + int Hour = 0; + int Min = 0; + int Sec = 0; cerr << "\x1B[34m[" + to_string(Hour) + ":" + to_string(Min) + ":" + to_string(Sec) + "] \x1B[33mSlang: \x1B[31mERROR: " << errorText << "\033[0m\t\t" << endl; exit(EXIT_FAILURE); @@ -188,7 +194,7 @@ int GetBuiltins(std::string s) string functName = split(words[lineNum][1], '(')[0]; #if DEVELOPER_MESSAGES == true - InterpreterLog("Load builtin function " + functName); + InterpreterLog("Load builtin function " + functName + "..."); #endif string args = ""; @@ -217,28 +223,28 @@ int GetBuiltins(std::string s) { builtinVarVals[words[lineNum][1]] = StringRaw(words[lineNum][3]); #if DEVELOPER_MESSAGES == true - InterpreterLog("Load builtin variable " + words[lineNum][1]); + InterpreterLog("Load builtin variable " + words[lineNum][1] + "..."); #endif } else if (words[lineNum][0] == "int") { builtinVarVals[words[lineNum][1]] = stoi(words[lineNum][3]); #if DEVELOPER_MESSAGES == true - InterpreterLog("Load builtin variable " + words[lineNum][1]); + InterpreterLog("Load builtin variable " + words[lineNum][1] + "..."); #endif } else if (words[lineNum][0] == "float") { builtinVarVals[words[lineNum][1]] = stof(words[lineNum][3]); #if DEVELOPER_MESSAGES == true - InterpreterLog("Load builtin variable " + words[lineNum][1]); + InterpreterLog("Load builtin variable " + words[lineNum][1] + "..."); #endif } else if (words[lineNum][0] == "bool") { builtinVarVals[words[lineNum][1]] = stob(words[lineNum][3]); #if DEVELOPER_MESSAGES == true - InterpreterLog("Load builtin variable " + words[lineNum][1]); + InterpreterLog("Load builtin variable " + words[lineNum][1] + "..."); #endif } //else diff --git a/Slang/graphics.h b/Slang/graphics.h index dbfd7c9..a23e65c 100644 --- a/Slang/graphics.h +++ b/Slang/graphics.h @@ -217,6 +217,7 @@ public: return x; if (componentName == "y") return y; + return 0; } Vec2 EditSubComponent(std::string componentName, std::string oper, boost::any otherVal) @@ -412,6 +413,7 @@ public: return scale.x; if (componentName == "scale.y") return scale.y; + return 0; } Sprite EditSubComponent(std::string componentName, std::string oper, boost::any otherVal) @@ -527,7 +529,7 @@ public: int Load() { - SDL_Color color = { r, g, b }; + SDL_Color color = { (Uint8)r, (Uint8)g, (Uint8)b }; SDL_Surface* surface = TTF_RenderText_Solid(font, content.c_str(), color); @@ -549,7 +551,7 @@ public: int Update() { - SDL_Color color = { r, g, b }; + SDL_Color color = { (Uint8)r, (Uint8)g, (Uint8)b }; SDL_Surface* surface = TTF_RenderText_Solid(font, content.c_str(), color); @@ -596,6 +598,7 @@ public: return content; if (componentName == "pathToFont") return pathToFont; + return 0; } Text EditSubComponent(const std::string componentName, const std::string oper, const boost::any otherVal)