# waf/csharprepl architecture

> Architecture diagram of the GitHub repository waf/csharprepl, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-05). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/waf/csharprepl
- Repository: https://github.com/waf/csharprepl

## Overview

Below are detailed steps and guidelines tailored to C# REPL’s architecture:

##### Main Components
- Command Line Interface (CLI): The entry point (Program.cs in the CSharpRepl project) that receives user input and displays output.
- REPL Engine: The read-evaluate-print loop that processes input and manages the session.
- Service Layer (CSharpRepl.Services): Organized in multiple subcomponents:
- Completion: Provides auto-completion and Intellisense (e.g., AutoCompleteService.cs, OpenAICompleteService.cs).
- Disassembly: Contains IL disassembly services.
- Dotnet: Contains functionality to build or reference .NET projects.
- Extensions: Helper functions and extension methods to support various operations.
- Logging: Handles tracing and error logging.
- Nuget: Manages package installations and references.
- Roslyn Integration: Extensive use of Roslyn to handle code parsing, formatting, compilation, symbol resolution, metadata resolution, and scripting evaluation (multiple folders and files indicate deep integration).
- Testing and Validation Layer: (CSharpRepl.Tests) which confirms behaviors across command evaluation, formatting, and other REPL functionalities.
- Configuration & Utility Components: Supporting files like runtime.json, configuration files, and theming JSON files to drive behavior.

##### Understand Relationships and Data Flow
- User Input flows from the CLI into the core REPL engine, which then delegates tasks:
- For code completion and intellisense, the REPL calls the Completion services, which in one case leverages an OpenAI service.
- The Roslyn-based services compile and interpret code, as well as provide IL disassembly if needed.
- If a NuGet package reference is requested, the REPL routes the request to the Nuget services.
- Logging and tracing throughout the evaluation loop ensures error tracking and debugging.
- The Theming subsystem applies either default (Visual Studio–like) or custom theme JSON definitions to output formatting.

- External Dependencies and Integrations:
- Integration with tools like Windows Terminal, Visual Studio Code, and support for global tool installation.
- Optionally, an external OpenAI API call for enhanced code completion.

##### Architectural Patterns and Design Principles
- Separation of Concerns: The REPL’s design clearly separates the input/output handling from code evaluation and its supportive services.
- Modularization: The services are broken down into components (Completion, Disassembly, NuGet, Roslyn, etc.) that can evolve independently.
- Extensibility: By separating key functionalities and using extension methods (as seen in the Extensions folder), the design allows for future enhancements.
- Dependency Inversion: Although not immediately visible in the file names, interfaces such as ITraceLogger and IConsoleEx hint toward designing around abstractions for easier testing and pluggability.

##### Final Checklist for the Principal Software Engineer
- Clear, labeled components for:
- CLI / User Interface (entry point and command parsing)
- Core REPL Engine (the read-eval-print cycle)
- Service components (each with its specialized functionality: Completion, Roslyn Java-compilers, Nuget, Disassembly, etc.)
- Configuration and Theming (theme files, configuration services)
- Core application logic vs. service helper modules
- External integrations (like OpenAI or NuGet services)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% CLI Layer
    subgraph "CLI Layer"
        CLI_PRG["Entry Point: Program.cs"]:::cli
        CLI_CL["Command Parser: CommandLine.cs"]:::cli
    end

    %% Core Engine Layer
    subgraph "Core Engine"
        REPL_REPL["REPL Engine: ReadEvalPrintLoop.cs"]:::engine
        REPL_PIPE["PipedInputEvaluator.cs"]:::engine
        REPL_PIPE -->|"delegates"| REPL_REPL
    end

    %% Services Layer
    subgraph "Services Layer"
        %% Completion Services
        subgraph "Completion Services"
            AC["AutoCompleteService.cs"]:::service
            OA["OpenAICompleteService.cs"]:::service
        end
        DS["Disassembler.cs"]:::service
        DN["DotnetBuilder.cs"]:::service
        EX["Extensions & Helpers\n(Extensions Directory)"]:::service

        %% Logging Services
        subgraph "Logging Services"
            IL["ITraceLogger.cs"]:::service
            TL["TraceLogger.cs"]:::service
            NL["NullLogger.cs"]:::service
        end

        %% Nuget Services
        subgraph "Nuget Services"
            NU_CNL["ConsoleNugetLogger.cs"]:::service
            NU_HELP["NugetHelper.cs"]:::service
            NU_INSTALL["NugetPackageInstaller.cs"]:::service
        end

        RS["RoslynServices.cs\n(Roslyn Integration)"]:::service
        SH["SyntaxHighlighter.cs"]:::service

        %% Theming & Formatted Output
        subgraph "Theming & Formatted Output"
            THEME["Theme.cs"]:::service
            THEMES_DIR["Themes Directory"]:::service
        end

        %% Configuration & Utilities
        subgraph "Configuration & Utilities"
            CONFIG_FILE["ConfigurationFile.cs"]:::service
            CONFIG_SERVICE["Configuration.cs"]:::service
            RUNTIME["runtime.json"]:::service
        end
    end

    %% Testing & Validation Layer
    subgraph "Testing & Validation"
        TEST["CSharpRepl.Tests Directory"]:::test
    end

    %% Data Flow Connections
    CLI_PRG ---|"user input"| REPL_REPL
    CLI_CL ---|"user input"| REPL_REPL

    REPL_REPL ---|"calls completion"| AC
    REPL_REPL ---|"calls completion"| OA
    REPL_REPL ---|"requests disassembly"| DS
    REPL_REPL ---|"invokes build/ref"| DN
    REPL_REPL ---|"uses helpers"| EX
    REPL_REPL ---|"logs actions"| IL
    REPL_REPL ---|"logs actions"| TL
    REPL_REPL ---|"logs actions"| NL
    REPL_REPL ---|"handles packages"| NU_CNL
    REPL_REPL ---|"handles packages"| NU_HELP
    REPL_REPL ---|"handles packages"| NU_INSTALL
    REPL_REPL ---|"compiles code"| RS
    REPL_REPL ---|"formats syntax"| SH
    REPL_REPL ---|"applies theming"| THEME
    REPL_REPL ---|"applies theming"| THEMES_DIR
    REPL_REPL ---|"reads config"| CONFIG_FILE
    REPL_REPL ---|"reads config"| CONFIG_SERVICE
    REPL_REPL ---|"reads config"| RUNTIME
    REPL_REPL ---|"triggers validation"| TEST
    REPL_REPL ---|"displays output"| CLI_PRG

    %% Click Events for CLI Layer
    click CLI_PRG "https://github.com/waf/csharprepl/blob/main/CSharpRepl/Program.cs"
    click CLI_CL "https://github.com/waf/csharprepl/blob/main/CSharpRepl/CommandLine.cs"

    %% Click Events for Core Engine
    click REPL_REPL "https://github.com/waf/csharprepl/blob/main/CSharpRepl/ReadEvalPrintLoop.cs"
    click REPL_PIPE "https://github.com/waf/csharprepl/blob/main/CSharpRepl/PipedInputEvaluator.cs"

    %% Click Events for Services Layer
    click AC "https://github.com/waf/csharprepl/blob/main/CSharpRepl.Services/Completion/AutoCompleteService.cs"
    click OA "https://github.com/waf/csharprepl/blob/main/CSharpRepl.Services/Completion/OpenAICompleteService.cs"
    click DS "https://github.com/waf/csharprepl/blob/main/CSharpRepl.Services/Disassembly/Disassembler.cs"
    click DN "https://github.com/waf/csharprepl/blob/main/CSharpRepl.Services/Dotnet/DotnetBuilder.cs"
    click EX "https://github.com/waf/csharprepl/tree/main/CSharpRepl.Services/Extensions"

    click IL "https://github.com/waf/csharprepl/blob/main/CSharpRepl.Services/Logging/ITraceLogger.cs"
    click TL "https://github.com/waf/csharprepl/blob/main/CSharpRepl/Logging/TraceLogger.cs"
    click NL "https://github.com/waf/csharprepl/blob/main/CSharpRepl/Logging/NullLogger.cs"

    click NU_CNL "https://github.com/waf/csharprepl/blob/main/CSharpRepl.Services/Nuget/ConsoleNugetLogger.cs"
    click NU_HELP "https://github.com/waf/csharprepl/blob/main/CSharpRepl.Services/Nuget/NugetHelper.cs"
    click NU_INSTALL "https://github.com/waf/csharprepl/blob/main/CSharpRepl.Services/Nuget/NugetPackageInstaller.cs"

    click RS "https://github.com/waf/csharprepl/blob/main/CSharpRepl.Services/Roslyn/RoslynServices.cs"
    click SH "https://github.com/waf/csharprepl/blob/main/CSharpRepl.Services/SyntaxHighlighting/SyntaxHighlighter.cs"

    click THEME "https://github.com/waf/csharprepl/blob/main/CSharpRepl.Services/Theming/Theme.cs"
    click THEMES_DIR "https://github.com/waf/csharprepl/tree/main/CSharpRepl/themes"

    click CONFIG_FILE "https://github.com/waf/csharprepl/blob/main/CSharpRepl/ConfigurationFile.cs"
    click CONFIG_SERVICE "https://github.com/waf/csharprepl/blob/main/CSharpRepl.Services/Configuration.cs"
    click RUNTIME "https://github.com/waf/csharprepl/blob/main/CSharpRepl/Services/runtime.json"

    %% Click Event for Testing
    click TEST "https://github.com/waf/csharprepl/blob/main/CSharpRepl.Tests"

    %% Styles
    classDef cli fill:#bbdefb,stroke:#0d47a1,stroke-width:2px;
    classDef engine fill:#c8e6c9,stroke:#2e7d32,stroke-width:2px;
    classDef service fill:#ffe082,stroke:#ff6f00,stroke-width:2px;
    classDef test fill:#d1c4e9,stroke:#6a1b9a,stroke-width:2px;
```
