c118996746
- SwiftUI macOS app with C++17 code analysis engine (ObjC++ bridge) - Agentic AI loop: LLM plans → tool calls → execution → feedback loop - 15 agent tools: file ops, terminal, git, xcode build, code intel - 7 persistent terminal tools with background session management - Chat sidebar with agent step rendering and auto-apply - NVIDIA NIM API integration (Llama 3.3 70B default) - OpenAI tool_calls format with prompt-based fallback - Code editor with syntax highlighting and multi-tab support - File tree, console view, terminal view - Git integration and workspace management
173 lines
5.2 KiB
C++
173 lines
5.2 KiB
C++
#ifndef CodeEngine_hpp
|
|
#define CodeEngine_hpp
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
#include <map>
|
|
#include <functional>
|
|
|
|
// ─── Configuration ───────────────────────────────────────────────
|
|
|
|
struct EngineConfig {
|
|
int maxLineLength = 120;
|
|
int maxNestingDepth = 5;
|
|
int maxFunctionLength = 50;
|
|
double minCommentRatio = 0.05;
|
|
int minLinesForCommentCheck = 20;
|
|
int maxPrintStatements = 5;
|
|
bool detectForceUnwraps = true;
|
|
bool detectForceTry = true;
|
|
bool detectForceCast = true;
|
|
bool detectLongLines = true;
|
|
bool detectDeepNesting = true;
|
|
bool detectLowComments = true;
|
|
bool detectLargeFunctions = true;
|
|
bool detectDuplicateLines = true;
|
|
bool detectTodoFixme = true;
|
|
bool detectRetainCycles = true;
|
|
};
|
|
|
|
// ─── Token Types ─────────────────────────────────────────────────
|
|
|
|
enum class TokenType {
|
|
Keyword,
|
|
Identifier,
|
|
NumberLiteral,
|
|
StringLiteral,
|
|
Operator,
|
|
Punctuation,
|
|
Comment,
|
|
Whitespace,
|
|
Newline,
|
|
Unknown
|
|
};
|
|
|
|
struct Token {
|
|
TokenType type;
|
|
std::string value;
|
|
int line;
|
|
int column;
|
|
int length;
|
|
};
|
|
|
|
// ─── Symbol Table ────────────────────────────────────────────────
|
|
|
|
enum class SymbolKind {
|
|
Function,
|
|
Class,
|
|
Struct,
|
|
Enum,
|
|
Protocol,
|
|
Variable,
|
|
Constant,
|
|
TypeAlias,
|
|
Extension
|
|
};
|
|
|
|
struct SymbolInfo {
|
|
SymbolKind kind;
|
|
std::string name;
|
|
int line;
|
|
std::string accessLevel; // public, private, internal, etc.
|
|
std::string signature; // full declaration line
|
|
};
|
|
|
|
// ─── Analysis Report ─────────────────────────────────────────────
|
|
|
|
struct AnalysisReport {
|
|
int lineCount;
|
|
int charCount;
|
|
int blankLineCount;
|
|
int codeLineCount;
|
|
int commentLineCount;
|
|
int functionCount;
|
|
int classCount;
|
|
int structCount;
|
|
int enumCount;
|
|
int protocolCount;
|
|
std::string complexity;
|
|
int cyclomaticComplexity;
|
|
double maintainabilityIndex;
|
|
bool bracesBalanced;
|
|
std::vector<std::string> keywords;
|
|
std::vector<std::string> issues;
|
|
std::map<std::string, int> keywordFrequency;
|
|
double commentRatio;
|
|
std::vector<SymbolInfo> symbols;
|
|
std::vector<std::string> imports;
|
|
std::vector<std::pair<int, int>> duplicateLinePairs;
|
|
std::vector<std::pair<int, std::string>> todoComments;
|
|
};
|
|
|
|
// ─── Engine ──────────────────────────────────────────────────────
|
|
|
|
class CodeEngine {
|
|
public:
|
|
CodeEngine();
|
|
explicit CodeEngine(const EngineConfig& config);
|
|
~CodeEngine();
|
|
|
|
// Configuration
|
|
void set_config(const EngineConfig& config);
|
|
EngineConfig get_config() const;
|
|
|
|
// Full syntax analysis report (text)
|
|
std::string analyze_syntax(const std::string& code);
|
|
|
|
// Structured analysis
|
|
AnalysisReport full_analysis(const std::string& code);
|
|
|
|
// DJB2 hash-based checksum
|
|
int calculate_checksum(const std::string& code);
|
|
|
|
// Counting helpers
|
|
int count_lines(const std::string& code);
|
|
int count_blank_lines(const std::string& code);
|
|
int count_code_lines(const std::string& code);
|
|
int count_functions(const std::string& code);
|
|
int count_classes(const std::string& code);
|
|
int count_structs(const std::string& code);
|
|
int count_enums(const std::string& code);
|
|
int count_protocols(const std::string& code);
|
|
|
|
// Analysis helpers
|
|
std::string estimate_complexity(const std::string& code);
|
|
int cyclomatic_complexity(const std::string& code);
|
|
double maintainability_index(const std::string& code);
|
|
bool check_balanced_braces(const std::string& code);
|
|
std::vector<std::string> extract_keywords(const std::string& code);
|
|
std::vector<std::string> find_issues(const std::string& code);
|
|
double comment_ratio(const std::string& code);
|
|
int count_pattern(const std::string& code, const std::string& pattern);
|
|
|
|
// Tokenizer
|
|
std::vector<Token> tokenize(const std::string& code);
|
|
|
|
// Symbol extraction
|
|
std::vector<SymbolInfo> extract_symbols(const std::string& code);
|
|
|
|
// Import dependency tracking
|
|
std::vector<std::string> extract_imports(const std::string& code);
|
|
|
|
// Duplicate detection
|
|
std::vector<std::pair<int, int>> detect_duplicate_lines(const std::string& code);
|
|
|
|
// TODO/FIXME extraction
|
|
std::vector<std::pair<int, std::string>> extract_todo_comments(const std::string& code);
|
|
|
|
// Indentation analysis
|
|
int suggest_indentation(const std::string& codeBefore, int tabWidth = 4);
|
|
|
|
// Formatting
|
|
std::string format_report(const AnalysisReport& report);
|
|
|
|
private:
|
|
EngineConfig m_config;
|
|
|
|
// Internal helpers
|
|
bool is_swift_keyword(const std::string& word) const;
|
|
std::string detect_access_level(const std::string& line) const;
|
|
std::string trim(const std::string& s) const;
|
|
};
|
|
|
|
#endif |