From a36f425491aaf019243a31179e80cb10ea62db59 Mon Sep 17 00:00:00 2001 From: Matthew Kosarek Date: Mon, 21 Jun 2021 20:05:08 -0400 Subject: (mkosarek) Transpiling code snippets in a shotty way, but it is good enough --- 2d/rigidbody/rigidbody_1.html | 36 +++++++++-- 2d/rigidbody/rigidbody_1.html.content | 4 +- index.css | 20 +++--- transpiler/MyString.cpp | 6 +- transpiler/MyString.h | 4 +- transpiler/build.sh | 2 +- transpiler/replacer.cpp | 117 ++++++++++++++++++++++++++++++++++ transpiler/replacer.h | 3 + transpiler/transpiler | Bin 38744 -> 49640 bytes transpiler/transpiler.cpp | 7 +- 10 files changed, 175 insertions(+), 24 deletions(-) create mode 100644 transpiler/replacer.cpp create mode 100644 transpiler/replacer.h diff --git a/2d/rigidbody/rigidbody_1.html b/2d/rigidbody/rigidbody_1.html index e7e848d..0751499 100644 --- a/2d/rigidbody/rigidbody_1.html +++ b/2d/rigidbody/rigidbody_1.html @@ -93,8 +93,14 @@

Now that we have that understanding, we can begin setting up our rigidbody data structure. - {{{rigidbody_1/snippet1.cpp}}} - +

struct Rigidbody {
+    Vector2 force = { 0, 0 };
+    Vector2 acceleration = { 0, 0 };
+    Vector2 velocity = { 0, 0 };
+    Vector2 position = { 0, 0 };
+    float32 mass = 1.f;
+};
+
As you can see, the base data structure exactly mirrors what we already know from 2D newtonian physics.

@@ -103,8 +109,30 @@

Now, let's put that Rigidbody data structure to work! As I mentioned earlier, you can think of dynamics as the input to the system. What we're going to do now is add a way to - {{{rigidbody_1/snippet2.cpp}}} -

+
struct Rigidbody {
+    Vector2 force = { 0, 0 };
+    Vector2 velocity = { 0, 0 };
+    Vector2 position = { 0, 0 };
+    float32 mass = 1.f;
+
+    void applyForce(Vector2 f) {
+        force += f;
+    }
+
+    void applyGravity(float32 deltaTimeSeconds) {
+        velocity += (Vector2 { 0.f, -50.f } * deltaTimeSeconds);
+    }
+
+    void update(float32 deltaTimeSeconds) {
+        applyGravity(deltaTimeSeconds);
+        
+        Vector2 acceleration = force / mass;
+        velocity += (acceleration * deltaTimeSeconds);
+        position += (velocity * deltaTimeSeconds);
+        force = Vector2 { 0.f, 0.f };
+    }
+};
+

diff --git a/2d/rigidbody/rigidbody_1.html.content b/2d/rigidbody/rigidbody_1.html.content index 0e75bdc..de3898a 100644 --- a/2d/rigidbody/rigidbody_1.html.content +++ b/2d/rigidbody/rigidbody_1.html.content @@ -43,7 +43,7 @@

Now that we have that understanding, we can begin setting up our rigidbody data structure. - {{{rigidbody_1/snippet1.cpp}}} + #SNIPPET rigidbody_1/snippet1.cpp As you can see, the base data structure exactly mirrors what we already know from 2D newtonian physics.

@@ -53,7 +53,7 @@

Now, let's put that Rigidbody data structure to work! As I mentioned earlier, you can think of dynamics as the input to the system. What we're going to do now is add a way to - {{{rigidbody_1/snippet2.cpp}}} + #SNIPPET rigidbody_1/snippet2.cpp

diff --git a/index.css b/index.css index 758afa3..8f0670b 100644 --- a/index.css +++ b/index.css @@ -152,16 +152,6 @@ section > h2 { font-weight: 500; } -section pre { - background-color: white; - border: 1px solid black; - border-radius: 3px; - overflow-x: auto; - padding: 0.5rem; - font-size: 0.9rem; - line-height: 1.1rem; -} - #references a { color: inherit; text-decoration: none; @@ -175,6 +165,16 @@ section pre { /*********************************** Code stylings ************************************/ +pre { + background-color: white; + border: 1px solid #D0D0D0; + border-radius: 3px; + overflow-x: auto; + padding: 1rem; + font-size: 0.8rem; + line-height: 1.0rem; +} + .code_keyword { color: blue; font-weight: bold; diff --git a/transpiler/MyString.cpp b/transpiler/MyString.cpp index 1cb3d43..1a63960 100644 --- a/transpiler/MyString.cpp +++ b/transpiler/MyString.cpp @@ -105,8 +105,8 @@ void StringBuilder::addStr(String* str) { addStr(str->getValue()); } -void StringBuilder::addStr(const char* str) { - int amountLeft = strlen(str); +void StringBuilder::addStr(const char* str, int inLength) { + int amountLeft = inLength < 0 ? strlen(str) : inLength; length += amountLeft; int ptr = 0; @@ -229,7 +229,7 @@ void StringBuilder::format(const char* str, ...) { addFloat(static_cast(va_arg(args, double))); break; case 's': - addStr(va_arg(args, char*)); + addStr(va_arg(args, char*), -1); default: break; } diff --git a/transpiler/MyString.h b/transpiler/MyString.h index d05d84a..805f5de 100644 --- a/transpiler/MyString.h +++ b/transpiler/MyString.h @@ -59,7 +59,7 @@ struct StringBuilder { StringBuffer* getBufferAtIdx(int index); const StringBuffer* getBufferAtIdxConst(int index) const; void addStr(String* str); - void addStr(const char* str); + void addStr(const char* str, int length = -1); void addChar(char c); void format(const char* str, ...); void addInt(int value); @@ -73,4 +73,4 @@ struct StringBuilder { char getCharAtIdx(int index) const; void insert(char c, int index); void free(); -}; \ No newline at end of file +}; diff --git a/transpiler/build.sh b/transpiler/build.sh index 6ca478e..ea1cb93 100755 --- a/transpiler/build.sh +++ b/transpiler/build.sh @@ -1 +1 @@ -g++ -o transpiler transpiler.cpp Logger.cpp MyString.cpp \ No newline at end of file +g++ -o transpiler transpiler.cpp Logger.cpp MyString.cpp replacer.cpp diff --git a/transpiler/replacer.cpp b/transpiler/replacer.cpp new file mode 100644 index 0000000..28c2e8e --- /dev/null +++ b/transpiler/replacer.cpp @@ -0,0 +1,117 @@ +#include "replacer.h" +#include "Logger.h" +#include + +const char* keywords[] = { + "cosnt", "struct", "static" +}; + +const char* types[] = { + "void", + "int", "int8", "int16", "int32", "int64", + "float", "float8", "float16", "float32", "float64", + "long", + "double", + "bool", + "Vector2", "Vector3", "Vector4", + "struct", "class" +}; + +bool isType(char* str, int size) { + if (size == 0) { + return false; + } + + for (int i = 0; i < sizeof(types) / sizeof(char*); i++) { + if (size == strlen(types[i]) && strncmp(str, types[i], size) == 0) { + return true; + } + } + + return false; +} + +bool isTokenDelim(char c) { + return isspace(c) || c == '\0' || c == ';' || c == ','; +} + +// @TODO: This has brought me to two realizations: +// 1) C++ string manipulaton is difficult for no apparent reason +// 2) My current implementation of String is lacking so many features that I would want +// 3) I have no plans to update my current implementation any time soon, so I will +// begrudginly give my heart and soul over to the standard library. +std::string insertCodeSnippets(char* workingDirectory, char* bodyContent) { + std::string strWorkingDirectory (workingDirectory); + strWorkingDirectory = strWorkingDirectory.substr(0, strWorkingDirectory.find_last_of("/") + 1); + std::string strContent(bodyContent); + + size_t found = -1; + do { + found = strContent.find("#SNIPPET", found + 1); + if (found == std::string::npos) { + break; + } + int startFound = found; + found += strlen("#SNIPPET "); + + std::string fileName(strWorkingDirectory); + while (!isspace(strContent[found])) { + fileName += strContent[found]; + found++; + } + + size_t endFound = found + 1; + + FILE* snippetFile = fopen(fileName.c_str(), "r+"); + if (snippetFile == NULL) { + logger_warning("Could not find snippet: %s", fileName.c_str()); + continue; + } + + fseek(snippetFile, 0, SEEK_END); + long fsize = ftell(snippetFile); + fseek(snippetFile, 0, SEEK_SET); + + char* snippetContent = new char[fsize + 1]; + fread(snippetContent, 1, fsize, snippetFile); + snippetContent[fsize] = '\0'; + + std::string s; + int tokenStart = 0, tokenEnd = 0; + while (snippetContent[tokenEnd] != '\0') { + while (!isTokenDelim(snippetContent[tokenEnd])) { + tokenEnd++; + } + + int tokenLength = (tokenEnd - tokenStart); + if (isType(&snippetContent[tokenStart], tokenLength)) { + s.append(""); + s.append(&snippetContent[tokenStart], tokenLength); + s.append(""); + } else { + s.append(&snippetContent[tokenStart], tokenLength); + } + + s += snippetContent[tokenEnd]; + + tokenStart = tokenEnd + 1; + tokenEnd = tokenStart; + } + + while (s[0] == '\n') { + s.erase(0, 1); + } + + s.insert(0, ""); + s.insert(0, "
");
+        s.append("");
+        s.append("
"); + + delete [] snippetContent; + + strContent.replace(startFound, endFound - startFound, s); + + } while (true); + + return strContent; +} diff --git a/transpiler/replacer.h b/transpiler/replacer.h new file mode 100644 index 0000000..f795bca --- /dev/null +++ b/transpiler/replacer.h @@ -0,0 +1,3 @@ +#include + +std::string insertCodeSnippets(char* workingDirectory, char* bodyContent); diff --git a/transpiler/transpiler b/transpiler/transpiler index 5672fb4..509db3e 100755 Binary files a/transpiler/transpiler and b/transpiler/transpiler differ diff --git a/transpiler/transpiler.cpp b/transpiler/transpiler.cpp index 5c1de66..a171fc6 100644 --- a/transpiler/transpiler.cpp +++ b/transpiler/transpiler.cpp @@ -1,6 +1,7 @@ #include "MyString.h" #include "List.h" #include "Logger.h" +#include "replacer.h" #include #include #include @@ -149,7 +150,6 @@ int main() { servedFiles.add(pathCopy); otherSb.clear(); - while (line[0] != '\n') { if (line[0] != '\"') { otherSb.addChar(line[0]); @@ -215,7 +215,10 @@ int main() { char* bodyContent = new char[fsize + 1]; fread(bodyContent, 1, fsize, contentFile); bodyContent[fsize] = '\0'; - sb.addStr(bodyContent); + + auto processedBody = insertCodeSnippets(contentFileName.getValue(), bodyContent); + + sb.addStr(processedBody.c_str()); delete [] bodyContent; -- cgit v1.2.1