Nimbin[12]?SDL & Graphics / sdl_runtime_compiler / src/text/token_db.cpp

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
src/text/token_db.cpp 9.9 KB · 200 lines raw
#include <unordered_map>

#include "token.hpp"

namespace
Nimbin
{
namespace
{

struct Entry { const char* short_desc; const char* long_desc; };

// Curated, hand-written help text. Keyed by exact token text.
const std::unordered_map<std::string, Entry>&
table()
{
    static const std::unordered_map<std::string, Entry> m = {
        { "int",      { "Integer type",          "Declares a 32-bit integer variable" } },
        { "float",    { "Float type",            "Declares a floating point variable" } },
        { "char",     { "Character type",        "Declares a single character" } },
        { "void",     { "No value type",         "Function returns nothing" } },
        { "bool",     { "Boolean type",          "Stores true or false values" } },
        { "short",    { "Short integer type",    "Declares a short integer variable" } },
        { "long",     { "Long integer type",     "Declares a long integer variable" } },
        { "unsigned", { "Unsigned modifier",     "Declares an unsigned integer type" } },
        { "signed",   { "Signed modifier",       "Declares a signed integer type" } },
        { "double",   { "Double type",           "Declares a double-precision floating point variable" } },
        { "size_t",   { "Size type",             "Stores object sizes and array indices" } },
        { "struct",   { "Structure type",        "Defines a structure type" } },
        { "union",    { "Union type",            "Defines a union type" } },
        { "enum",     { "Enumeration type",      "Defines an enumeration type" } },
        { "typedef",  { "Type alias",            "Creates an alias for a type" } },
        { "const",    { "Constant qualifier",    "Prevents modification of an object" } },
        { "static",   { "Static storage",        "Declares static storage duration" } },
        { "extern",   { "External linkage",      "Declares a symbol defined elsewhere" } },
        { "volatile", { "Volatile qualifier",    "Prevents certain compiler optimizations" } },

        { "return",   { "Return statement",      "Ends function and returns a value" } },
        { "if",       { "Conditional branch",    "Executes code if condition is true" } },
        { "else",     { "Alternative branch",    "Executes if 'if' condition is false" } },
        { "while",    { "While loop",            "Repeats while condition is true" } },
        { "for",      { "For loop",              "Loop with init, condition, increment" } },
        { "switch",   { "Switch statement",      "Selects execution path based on a value" } },
        { "case",     { "Case label",            "Marks a branch in a switch statement" } },
        { "default",  { "Default label",         "Fallback branch in a switch statement" } },
        { "break",    { "Break statement",       "Exits a loop or switch statement" } },
        { "continue", { "Continue statement",    "Skips to the next loop iteration" } },
        { "goto",     { "Goto statement",        "Jumps to a labeled statement" } },
        { "do",       { "Do loop",               "Begins a do-while loop" } },
        { "sizeof",   { "Size operator",         "Returns the size of a type or object" } },

        { "main",     { "Entry point",           "First function called by the OS" } },
        { "printf",   { "Print formatted",       "Outputs formatted text to stdout" } },
        { "strlen",   { "String length",         "Returns length of a string" } },
        { "i",        { "Loop counter",          "Common loop index variable" } },
        { "j",        { "Loop counter",          "Common loop index variable" } },
        { "k",        { "Loop counter",          "Common loop index variable" } },

        { "NULL",     { "Null pointer constant", "Macro representing a null pointer" } },
        { "true",     { "Boolean true",          "Boolean constant true" } },
        { "false",    { "Boolean false",         "Boolean constant false" } },
        { "%d",       { "Integer format",        "Printf format specifier for integers" } },
        { "%s",       { "String format",         "Printf format specifier for strings" } },
        { "%f",       { "Float format",          "Printf format specifier for floating-point values" } },
        { "%c",       { "Character format",      "Printf format specifier for characters" } },
        { "%p",       { "Pointer format",        "Printf format specifier for pointers" } },
        { "%x",       { "Hex format",            "Printf format specifier for hexadecimal" } },
        { "\\n",      { "Newline",               "Newline escape sequence" } },

        { "#include", { "Include directive",     "Includes a header file" } },
        { "#define",  { "Macro definition",      "Defines a macro" } },
        { "#ifndef",  { "If not defined",        "Compiles code if macro is not defined" } },
        { "#endif",   { "End conditional",       "Ends conditional compilation block" } },

        { "+",        { "Addition",              "Adds two operands" } },
        { "-",        { "Subtraction",           "Subtracts right operand from left" } },
        { "*",        { "Dereference / multiply","Dereferences a pointer or multiplies operands" } },
        { "/",        { "Division",              "Divides left operand by right" } },
        { "=",        { "Assignment",            "Assigns the right value to the left operand" } },
        { "==",       { "Equality",              "Checks whether two values are equal" } },
        { "!=",       { "Not equal",             "Checks whether two values differ" } },
        { "++",       { "Increment",             "Increases value by 1" } },
        { "--",       { "Decrement",             "Decreases value by 1" } },
        { "&&",       { "Logical AND",           "True if both operands are true" } },
        { "||",       { "Logical OR",            "True if either operand is true" } },
        { "->",       { "Member access",         "Accesses a member through a pointer" } },

        { "(", { "Open parenthesis",  "Starts a function call or condition" } },
        { ")", { "Close parenthesis", "Ends a function call or condition" } },
        { "{", { "Open brace",        "Starts a code block" } },
        { "}", { "Close brace",        "Ends a code block" } },
        { ";", { "Statement terminator","Ends a statement" } },
        { ",", { "Separator",          "Separates function arguments" } },
        { "[", { "Open bracket",       "Start array subscript" } },
        { "]", { "Close bracket",      "End array subscript" } },
    };
    return m;
}


} // namespace


namespace
Text
{
namespace
Token
{

SDL_FColor
get_Color(Type t)
{
    switch (t) {
        case Type::Keyword:      return Config::Color::code_keyword;
        case Type::Type:         return Config::Color::code_type;
        case Type::Literal:      return Config::Color::code_literal;
        case Type::StringLit:    return Config::Color::code_literal;
        case Type::NumberLit:    return Config::Color::code_literal;
        case Type::Comment:      return Config::Color::code_comment;
        case Type::Preprocessor: return Config::Color::code_preproc;
        case Type::Operator:     return Config::Color::code_unset;
        case Type::Delimiter:    return Config::Color::code_unset;
        case Type::Identifier:   return Config::Color::code_unset;
        case Type::Function:     return Config::Color::code_unset;
        case Type::Variable:     return Config::Color::code_unset;
        case Type::Namespace:    return Config::Color::code_unset;
        case Type::Whitespace:   return Config::Color::code_unset;
    }
    return { 0.850f, 0.870f, 0.920f, 1.0f };
}


Description
generic_For(Type type)
{
    switch (type) {
        case Type::Keyword:      return { "Keyword",      "A C/C++ language keyword" };
        case Type::Type:         return { "Type",         "A data type" };
        case Type::Identifier:   return { "Identifier",   "A name in the program" };
        case Type::Literal:      return { "Literal",      "A literal value" };
        case Type::Operator:     return { "Operator",     "An operator" };
        case Type::Delimiter:    return { "Delimiter",    "A delimiter" };
        case Type::Preprocessor: return { "Preprocessor", "A preprocessor directive" };
        case Type::Function:     return { "Function",     "A function" };
        case Type::Variable:     return { "Variable",     "A variable" };
        case Type::Comment:      return { "Comment",      "A source comment" };
        case Type::StringLit:    return { "String",       "A string literal" };
        case Type::NumberLit:    return { "Number",       "A numeric literal" };
        case Type::Namespace:    return { "Namespace",    "A namespace" };
        case Type::Whitespace:   return { "WHitespace",   "A whitespace" };
    }
    return { "", "" };
}


Description
get_Description(const std::string& text, Type type)
{
    const auto& t = table();
    auto it = t.find(text);
    if (it != t.end())
        return { it->second.short_desc, it->second.long_desc };
    return generic_For(type);
}


const char*
get_TypeName(Type t)
{
    switch (t) {
        case Type::Keyword:      return "Keyword";
        case Type::Type:         return "Type";
        case Type::Identifier:   return "Identifier";
        case Type::Literal:      return "Literal";
        case Type::Operator:     return "Operator";
        case Type::Delimiter:    return "Delimiter";
        case Type::Preprocessor: return "Preprocessor";
        case Type::Function:     return "Function";
        case Type::Variable:     return "Variable";
        case Type::Comment:      return "Comment";
        case Type::StringLit:    return "String";
        case Type::NumberLit:    return "Number";
        case Type::Namespace:    return "Namespace";
        case Type::Whitespace:   return "whitespace";
    }
    return "Unknown";
}


DynArray<std::string>
get_TestArrayAllKeywords()
{
    return { "float", "return", "main", "0", "++", "(", ",", ";", "\"", "\"", "#define" };
}


} // namespace TokenDB
} // namespace Text
} // namespace Nimbin