# atlazean/interpreter-in-cpp architecture

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

- Interactive diagram: https://gitdiagram.com/atlazean/interpreter-in-cpp
- Repository: https://github.com/atlazean/interpreter-in-cpp

## Overview

##### Main Components
- Input Source – This represents the user input (expression text) which may come from either file-based examples (as seen in example1.txt, example2.txt, example3.txt) or interactive REPL.
- Scanner (Lexical Analysis) – Implemented in both C++ (e.g., Scanner.cpp in the interpreter directory) and C (in the multiline-repl and parser modules). Its responsibility is to tokenize the source input.
- Parser (Syntactic Analysis) – Converts tokens produced by the scanner into an Abstract Syntax Tree (AST). In the interpreter directory, Parser.cpp handles this, and there are C-based implementations in the “parser” and “multiline-repl” directories.
- Abstract Syntax Tree (AST) – Represented by the AbstractSyntaxTree.cpp/h files in the interpreter folder. The AST is the central structure used by the interpreter to apply evaluation logic.
- Interpreter (Evaluator) – Reads through the AST and executes/evaluates the expression(s). This logic is defined in Interpreter.cpp/h.
- Error & Exception Handling – There are dedicated files in both the C++ and C implementations (e.g., error.c/error.h, exception.c/exception.h, and even references to visitor patterns in VisitorPattern.h).
- REPL (Read-Eval-Print Loop) – Specifically provided in the “multiline-repl” directory to allow interactive expression evaluation.
- Auxiliary / Documentation Components – The “doc” directory includes example code for setjmp_longjmp (for error recovery) and visitor pattern examples, indicating that error handling and flexible design pattern usage are emphasized.

##### Relationships and Interactions
- The Input Source feeds into the Scanner. The Scanner produces tokens.
- Tokens are passed into the Parser, which constructs the AST.
- The AST is then used by the Interpreter to evaluate the expression.
- Error and Exception handling modules interact closely with both the Scanner and Parser to capture and process errors.
- The REPL component, available in “multiline-repl,” integrates with the parser and interpreter for interactive use.
- Various Makefiles and project files (e.g., interpreter.pro, parser.pro) orchestrate the build process.

##### Architectural Patterns & Design Principles
- Modular Design – Each subsystem (scanning, parsing, interpreting, error handling) is clearly isolated into its own set of files and directories.
- Use of Visitor Pattern – Indicated in VisitorPattern.h and also demonstrated by accompanying sample code in the “doc” directory.
- Use of setjmp/longjmp – The “doc/setjmp_longjmp.c” hints at advanced error recovery techniques.
- Possibly a variation of the classic compile/evaluate cycle, tailored for an interpreter and a REPL environment.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Input Sources"
        subgraph "File-based Examples"
            EX1["Example1.txt"]:::input
            EX2["Example2.txt"]:::input
            EX3["Example3.txt"]:::input
        end
        REPL["REPL"]:::c
    end

    %% C++ Pipeline (File-based)
    SCANNER["Scanner (C++)"]:::cpp
    PARSER_CPP["Parser (C++)"]:::cpp
    AST["AST (Abstract Syntax Tree)"]:::cpp
    INTERPRETER["Interpreter (Evaluator)"]:::cpp
    OUTPUT["Evaluation Result"]:::cpp

    %% C Pipeline (REPL)
    PARSER_C["Parser (C)"]:::c
    ERROR_C["Error Handling (C)"]:::error

    %% Auxiliary Components
    VISITOR["Visitor Pattern"]:::cpp
    DOC["Documentation"]:::aux
    BUILD["Build & Project Files"]:::build

    %% Connections for File-based Examples (C++ Pipeline)
    EX1 --> SCANNER
    SCANNER --> PARSER_CPP
    PARSER_CPP --> AST
    AST --> INTERPRETER
    INTERPRETER --> OUTPUT

    %% Connections for REPL (C Pipeline)
    REPL --> PARSER_C
    PARSER_C --> ERROR_C
    ERROR_C --> INTERPRETER
    INTERPRETER -.-> REPL

    %% Auxiliary connections
    AST -.-> VISITOR
    INTERPRETER --- DOC
    INTERPRETER --- BUILD

    %% Click Events for Input Source Files
    click EX1 "https://github.com/atlazean/interpreter-in-cpp/blob/master/multiline-repl/example1.txt"
    click EX2 "https://github.com/atlazean/interpreter-in-cpp/blob/master/multiline-repl/example2.txt"
    click EX3 "https://github.com/atlazean/interpreter-in-cpp/blob/master/multiline-repl/example3.txt"

    %% Click Events for C++ Components
    click SCANNER "https://github.com/atlazean/interpreter-in-cpp/blob/master/interpreter/Scanner.cpp"
    click SCANNER "https://github.com/atlazean/interpreter-in-cpp/blob/master/interpreter/Scanner.h"
    click PARSER_CPP "https://github.com/atlazean/interpreter-in-cpp/blob/master/interpreter/Parser.cpp"
    click PARSER_CPP "https://github.com/atlazean/interpreter-in-cpp/blob/master/interpreter/Parser.h"
    click AST "https://github.com/atlazean/interpreter-in-cpp/blob/master/interpreter/AbstractSyntaxTree.cpp"
    click AST "https://github.com/atlazean/interpreter-in-cpp/blob/master/interpreter/AbstractSyntaxTree.h"
    click INTERPRETER "https://github.com/atlazean/interpreter-in-cpp/blob/master/interpreter/Interpreter.cpp"
    click INTERPRETER "https://github.com/atlazean/interpreter-in-cpp/blob/master/interpreter/Interpreter.h"
    click VISITOR "https://github.com/atlazean/interpreter-in-cpp/blob/master/interpreter/VisitorPattern.h"

    %% Click Events for C Components
    click PARSER_C "https://github.com/atlazean/interpreter-in-cpp/blob/master/multiline-repl/parser.c"
    click PARSER_C "https://github.com/atlazean/interpreter-in-cpp/blob/master/parser/parser.c"
    click ERROR_C "https://github.com/atlazean/interpreter-in-cpp/blob/master/multiline-repl/error.c"
    click ERROR_C "https://github.com/atlazean/interpreter-in-cpp/blob/master/multiline-repl/error.h"
    click ERROR_C "https://github.com/atlazean/interpreter-in-cpp/blob/master/multiline-repl/exception.c"
    click ERROR_C "https://github.com/atlazean/interpreter-in-cpp/blob/master/multiline-repl/exception.h"
    click ERROR_C "https://github.com/atlazean/interpreter-in-cpp/blob/master/parser/error.c"
    click ERROR_C "https://github.com/atlazean/interpreter-in-cpp/blob/master/parser/error.h"
    click ERROR_C "https://github.com/atlazean/interpreter-in-cpp/blob/master/parser/exception.c"
    click ERROR_C "https://github.com/atlazean/interpreter-in-cpp/blob/master/parser/exception.h"

    %% Click Event for REPL Directory
    click REPL "https://github.com/atlazean/interpreter-in-cpp/tree/master/multiline-repl"

    %% Click Events for Documentation
    click DOC "https://github.com/atlazean/interpreter-in-cpp/blob/master/doc/setjmp_longjmp.c"
    click DOC "https://github.com/atlazean/interpreter-in-cpp/blob/master/doc/visitor_pattern.cpp"

    %% Styling
    classDef cpp fill:#a2d2ff,stroke:#000,stroke-width:2px;
    classDef c fill:#bde0fe,stroke:#000,stroke-width:2px;
    classDef aux fill:#ffcfdf,stroke:#000,stroke-width:2px;
    classDef input fill:#d0f4de,stroke:#000,stroke-width:2px;
    classDef error fill:#ffadad,stroke:#000,stroke-width:2px;
    classDef build fill:#ffd6a5,stroke:#000,stroke-width:2px;
```
