Nimbin[12]?SDL & Graphics / sdl_runtime_compiler / commits / 6ee6a64

sdl_runtime_compiler git · main

SDL3 game for running and compiling code at runtime

sdl3 c++ compiler dlopen cmake · first commit 2026-04-19 · last commit 2026-07-03 (3 months ago) · synced 3 days ago · upstream: git.ide3.de/hsnr/sdl-runtime-compiler

C++ 72.3% C 26.2%
git clone https://git.christianimmanuel.de/sdl-graphics/sdl_runtime_compiler.gitwget https://git.christianimmanuel.de/sdl-graphics/sdl_runtime_compiler/archive/sdl_runtime_compiler.tar.gz

Fix token coloring

Christian Immanuel · 2026-05-31 21:17 · 6ee6a64d63de7b8e5ea56c133928663cf986f88f

 src/c_token/token.hpp          |  1 +
 src/c_token/token_color.hpp    | 13 +++++++------
 src/c_token/token_db.cpp       | 42 ++++++++++++++++++++++++++++++------------
 src/systems/physics_system.cpp |  2 +-
 src/systems/word_ai_system.cpp |  9 +++++----
 src/text_3d/text_3d.hpp        |  3 +--
 6 files changed, 45 insertions(+), 25 deletions(-)

diff --git a/src/c_token/token.hpp b/src/c_token/token.hpp
index 6c5deac..924621f 100644
--- a/src/c_token/token.hpp
+++ b/src/c_token/token.hpp
@@ -10,6 +10,7 @@ enum class TokenType : uint8_t {
     Literal,
     Operator,
     Delimiter,
+    Preprocessor,
 };
 
 struct Token {
diff --git a/src/c_token/token_color.hpp b/src/c_token/token_color.hpp
index ddec0bc..b99795f 100644
--- a/src/c_token/token_color.hpp
+++ b/src/c_token/token_color.hpp
@@ -10,12 +10,13 @@ inline SDL_FColor
 get(TokenType t)
 {
     switch (t) {
-        case TokenType::Keyword:    return Text3d::color_orange;   // if, else, while, for, return
-        case TokenType::Type:       return Text3d::color_green;    // int, float, char, void
-        case TokenType::Identifier: return Text3d::color_light_gray;    // main, printf, i, x …
-        case TokenType::Literal:    return Text3d::color_red;      // 0, 1, "\n", %s
-        case TokenType::Operator:   return Text3d::color_light_gray;    // = == + - * / ++ 
-        case TokenType::Delimiter:  return Text3d::color_light_gray;    // ( ) { } ; , [ ]
+        case TokenType::Keyword:     return Text3d::color_orange;   // if, for, return
+        case TokenType::Type:        return Text3d::color_green;    // int, void
+        case TokenType::Identifier:  return Text3d::color_light_gray;    // main, printf, i, x …
+        case TokenType::Literal:     return Text3d::color_red;      // 0, 1, "\n", %s
+        case TokenType::Operator:    return Text3d::color_light_gray;    // = == + - * / ++ 
+        case TokenType::Delimiter:   return Text3d::color_light_gray;    // ( ) { } ; , [ ]
+        case TokenType::Preprocessor:return Text3d::color_pink;    // #define #if
     }
     return Text3d::color_white;
 }
diff --git a/src/c_token/token_db.cpp b/src/c_token/token_db.cpp
index e69081e..bb85ab0 100644
--- a/src/c_token/token_db.cpp
+++ b/src/c_token/token_db.cpp
@@ -4,10 +4,10 @@
 
 static const DynArray<Token>
 all_tokens = {
-    { "int",    TokenType::Type,       "Integer type",        "Declares a 32-bit integer variable"  },
-    { "float",  TokenType::Type,       "Float type",          "Declares a floating point variable"  },
-    { "char",   TokenType::Type,       "Character type",      "Declares a single character"         },
-    { "void",   TokenType::Type,       "No value type",       "Function returns nothing"            },
+    { "int",       TokenType::Type, "Integer type",           "Declares a 32-bit integer variable"  },
+    { "float",     TokenType::Type, "Float type",             "Declares a floating point variable"  },
+    { "char",      TokenType::Type, "Character type",         "Declares a single character"         },
+    { "void",      TokenType::Type, "No value type",          "Function returns nothing"            },
     { "bool",      TokenType::Type, "Boolean type",           "Stores true or false values"                  },
     { "short",     TokenType::Type, "Short integer type",     "Declares a short integer variable"            },
     { "long",      TokenType::Type, "Long integer type",      "Declares a long integer variable"             },
@@ -56,7 +56,7 @@ all_tokens = {
     { "j",      TokenType::Identifier, "Loop counter",        "Common loop index variable"          },
     { "k",      TokenType::Identifier, "Loop counter",        "Common loop index variable"          },
 
-    { "NULL",   TokenType::Literal,    "Integer zero",        "The integer constant 0"              },
+    { "NULL",   TokenType::Literal, "Null pointer constant",  "Macro representing a null pointer" },
     { "0",      TokenType::Literal,    "Integer zero",        "The integer constant 0"              },
     { "1",      TokenType::Literal,    "Integer one",         "The integer constant 1"              },
     { "\"\\n\"",TokenType::Literal,    "Newline string",      "String containing a newline character"},
@@ -72,6 +72,13 @@ all_tokens = {
     { "%p",     TokenType::Literal,    "Pointer format",      "Printf format specifier for pointers"    },
     { "%%",     TokenType::Literal,    "Percent format",      "Printf escape sequence for percent sign" },
 
+    { "+",      TokenType::Operator, "Addition",        "Adds two operands" },
+    { "-",      TokenType::Operator, "Subtraction",     "Subtracts right operand from left" },
+    //{ "*",      TokenType::Operator, "Multiplication",  "Multiplies two operands" },
+    { "/",      TokenType::Operator, "Division",        "Divides left operand by right" },
+
+    { "++",     TokenType::Operator, "Increment",       "Increases value by 1" },
+    { "--",     TokenType::Operator, "Decrement",       "Decreases value by 1" },
     { "!=",     TokenType::Operator, "Not equal",             "Checks whether two values differ"           },
     { "<",      TokenType::Operator, "Less than",             "Checks whether left is less than right"  },
     { ">",      TokenType::Operator, "Greater than",          "Checks whether left is greater than right"  },
@@ -115,6 +122,16 @@ all_tokens = {
     { "[",      TokenType::Delimiter,  "Open bracket",        "Start array subscript"               },
     { "]",      TokenType::Delimiter,  "Close bracket",       "End array subscript"                 },
 
+    { "#include", TokenType::Preprocessor, "Include directive", "Includes a header file" },
+    { "#define",  TokenType::Preprocessor, "Macro definition",  "Defines a macro" },
+    { "#undef",   TokenType::Preprocessor, "Macro undefinition","Removes a macro definition" },
+
+    { "#ifdef",   TokenType::Preprocessor, "If defined",        "Compiles code if macro is defined" },
+    { "#ifndef",  TokenType::Preprocessor, "If not defined",    "Compiles code if macro is not defined" },
+    { "#if",      TokenType::Preprocessor, "If condition",      "Conditional compilation block" },
+    { "#elif",    TokenType::Preprocessor, "Else if",           "Alternative conditional branch" },
+    { "#else",    TokenType::Preprocessor, "Else branch",       "Fallback conditional branch" },
+    { "#endif",   TokenType::Preprocessor, "End conditional",   "Ends conditional compilation block" },
 };
 
 
@@ -135,7 +152,7 @@ token_index = build_index();
 DynArray<std::string> 
 TokenDB::get_TestArrayAllKeywords()
 {
-    return { "float", "return", "main", "0", "++", "(", ",", ";", "\"", "\"" };
+    return { "float", "return", "main", "0", "++", "(", ",", ";", "\"", "\"", "#define" };
 }
 
 
@@ -162,12 +179,13 @@ const char*
 TokenDB::type_name(TokenType t)
 {
     switch (t) {
-        case TokenType::Keyword:    return "Keyword";
-        case TokenType::Type:       return "Type";
-        case TokenType::Identifier: return "Identifier";
-        case TokenType::Literal:    return "Literal";
-        case TokenType::Operator:   return "Operator";
-        case TokenType::Delimiter:  return "Delimiter";
+        case TokenType::Keyword:     return "Keyword";
+        case TokenType::Type:        return "Type";
+        case TokenType::Identifier:  return "Identifier";
+        case TokenType::Literal:     return "Literal";
+        case TokenType::Operator:    return "Operator";
+        case TokenType::Delimiter:   return "Delimiter";
+        case TokenType::Preprocessor:return "Preprocessor";
     }
     return "Unknown";
 }
diff --git a/src/systems/physics_system.cpp b/src/systems/physics_system.cpp
index 3e4de5b..6862242 100644
--- a/src/systems/physics_system.cpp
+++ b/src/systems/physics_system.cpp
@@ -385,7 +385,7 @@ PhysicsSystem::resolve_Projectiles(DynArray<Projectile>& projectiles,
             if (w) { word_he = w->shape.half_extents; break; }
         }
 
-        // 1. Resolve against Static GameObjects
+        // 1. Resolve against GameObjects
         if (statics) {
             constexpr double RESTITUTION_STATIC = 0.45;
 
diff --git a/src/systems/word_ai_system.cpp b/src/systems/word_ai_system.cpp
index 1ae44c2..63c4680 100644
--- a/src/systems/word_ai_system.cpp
+++ b/src/systems/word_ai_system.cpp
@@ -3,6 +3,7 @@
 #include <SDL3/SDL.h>
 #include <cstdlib>
 
+#include "c_token/token_color.hpp"
 #include "entities/player.hpp"
 #include "systems/carry_system.hpp"
 #include "systems/collision.hpp"
@@ -56,12 +57,12 @@ WordAiSystem::update(DynArray<Text3dPtr>& words,
                 if (!s) continue;
                 s->body.mode = PhysicsMode::Dynamic;
                 s->body.vel  = w->body.vel * 0.5;
-                s->set_Color(s->token_info ? Text3d::color_token : Text3d::color_floor);
+                s->set_Color(s->token_info ? TokenColor::get(s->token_info->type) : Text3d::color_floor);
                 released.push_back(std::move(s));   // deferred — not words.push_back
             }
             w->ai_stolen.clear();
             w->ai_state = Text3d::Ai::Idle;
-            w->set_Color(w->token_info ? Text3d::color_token : Text3d::color_floor);
+            w->set_Color(w->token_info ? TokenColor::get(w->token_info->type) : Text3d::color_floor);
             continue;
         }
 
@@ -289,7 +290,7 @@ WordAiSystem::release_StolenWords(Text3d& w, DynArray<Text3dPtr>& out)
         if (!s) continue;
         s->body.mode = PhysicsMode::Dynamic;
         s->body.vel  = { w.body.vel.x * 0.5, 2.0, w.body.vel.z * 0.5 };
-        s->set_Color(s->token_info ? Text3d::color_token : Text3d::color_floor);
+        s->set_Color(s->token_info ? TokenColor::get(s->token_info->type) : Text3d::color_floor);
         out.push_back(std::move(s));
     }
     w.ai_stolen.clear();
@@ -344,7 +345,7 @@ WordAiSystem::enter_Idle(Text3d& w)
     w.ai_state_timer = 0.0;
     w.body.mode      = PhysicsMode::Hover;
     w.body.hover_height = w.ai_hover_height_saved;
-    w.set_Color(w.token_info ? Text3d::color_token : Text3d::color_floor);
+    w.set_Color(w.token_info ? TokenColor::get(w.token_info->type) : Text3d::color_floor);
 }
 
 
diff --git a/src/text_3d/text_3d.hpp b/src/text_3d/text_3d.hpp
index 5ceb9fc..671075c 100644
--- a/src/text_3d/text_3d.hpp
+++ b/src/text_3d/text_3d.hpp
@@ -46,7 +46,6 @@ struct Text3d final : public GameObject {
     float  half_extent_y { 0.2f };
 
     static inline constexpr SDL_FColor color_keep    = {0.0f, -1.0f, 0.0f,  1.0f};
-    static inline constexpr SDL_FColor color_token   = {-1.0f, 0.0f, 0.0f,  1.0f};
     static inline constexpr SDL_FColor color_attack  = {1.0f, 0.35f, 0.2f,  1.0f};
     static inline constexpr SDL_FColor color_flee    = {1.0f, 0.95f, 0.4f,  1.0f};
     static inline constexpr SDL_FColor color_stunned = {0.45f, 0.45f, 0.5f, 1.0f};
@@ -67,7 +66,7 @@ struct Text3d final : public GameObject {
     static inline constexpr SDL_FColor color_sky        = {0.40f, 0.70f, 1.0f,  1.0f};
     static inline constexpr SDL_FColor color_indigo     = {0.30f, 0.30f, 0.80f, 1.0f};
     static inline constexpr SDL_FColor color_purple     = {0.60f, 0.30f, 0.85f, 1.0f};
-    static inline constexpr SDL_FColor color_pink       = {1.0f,  0.45f, 0.70f, 1.0f};
+    static inline constexpr SDL_FColor color_pink       = {1.0f,  0.34f, 0.87f, 1.0f};
     static inline constexpr SDL_FColor color_brown      = {0.55f, 0.35f, 0.20f, 1.0f};
     static inline constexpr SDL_FColor color_dark_gray  = {0.25f, 0.25f, 0.25f, 1.0f};
     static inline constexpr SDL_FColor color_light_gray = {0.82f, 0.82f, 0.82f, 1.0f};