# mohamtahab/interpreter-go architecture

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

- Interactive diagram: https://gitdiagram.com/mohamtahab/interpreter-go
- Repository: https://github.com/mohamtahab/interpreter-go

## Overview

##### Project Overview and Main Components
- Interpreter: The project implements a language interpreter.
- Key components include:
- REPL (Read-Evaluate-Print Loop) – provided by the “repl” package and main.go.
- Lexer – located in the “lexer” directory. It converts raw source code into a stream of tokens.
- Token – the “token” package which defines the token types and properties.
- Parser – in the “parser” directory, which uses a Pratt parsing (top-down operator precedence) approach to build an Abstract Syntax Tree (AST) from the token stream.
- AST – in the “ast” directory. This component defines the structure of the parsed expressions.
- Evaluator – in the “eval” directory, which traverses the AST to evaluate the expressions.
- Object System – in the “object” package, used during evaluation to represent various value types, functions, or other runtime entities.

##### Relationships and Data Flow
- Input flows from the user (or file input) into the REPL.
- The REPL hands off the raw source code to the Lexer.
- The Lexer processes the input and produces a stream of tokens (with definitions provided by the token package).
- These tokens feed into the Parser, which uses Pratt parsing techniques to build the AST.
- The AST represents the syntactic structure of the program or expression.
- The Evaluator takes the AST and computes results, interacting with the Object system to manage values and operations.
- Finally, the computed result is returned to the REPL for display or further interaction.

##### Architectural Patterns and Design Principles
- Modular design: Each stage of the interpreter (lexing, parsing, evaluation) is isolated in its own package, making the system easier to maintain and extend.
- Extensibility: The interpreter is designed to be easily extended with custom operators, as highlighted in the README.
- Clear separation of concerns: The parsing (syntax processing) and evaluation (runtime processing) facets are clearly separated into “parser”/“ast” and “eval”/“object” respectively.
- Use of automated tests is evident (files ending in _test.go) to ensure each component functions as expected.

##### Final Remarks for the Principal Software Engineer
- Annotations for each component to capture how they interact (e.g., “Lexer produces token stream,” “Parser constructs the AST based on operator precedence”).
- Dependencies (like Go modules indicated in go.mod) and include notes on integration and test coverage if needed.
- This detailed visual representation will help team members quickly understand the interpreter’s structure and support further development or troubleshooting.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Input Layer
    subgraph "Input Layer"
        Main(("Main")):::input
        REPL(("REPL")):::input
    end

    %% Processing Layer
    subgraph "Processing Layer"
        Lexer["Lexer"]:::processing
        Token(("Token Definitions")):::processing
        Parser["Parser (Pratt Parsing)"]:::processing
        AST(("AST")):::processing
    end

    %% Evaluation Layer
    subgraph "Evaluation Layer"
        Evaluator["Evaluator"]:::evaluation
        ObjectSystem(("Object System")):::evaluation
    end

    %% Data Flow Connections
    Main -->|"initiates"| REPL
    REPL -->|"source_code_input"| Lexer
    Lexer -->|"tokens_produced"| Token
    Lexer -->|"token_stream"| Parser
    Parser -->|"AST_generation_using_PrattParsing"| AST
    AST -->|"AST_evaluation"| Evaluator
    Evaluator -->|"runtime_objects/values"| ObjectSystem
    Evaluator -->|"evaluation_result"| REPL

    %% Click Events
    click Main "https://github.com/mohamtahab/interpreter-go/blob/main/main.go"
    click REPL "https://github.com/mohamtahab/interpreter-go/blob/main/repl/repl.go"
    click Lexer "https://github.com/mohamtahab/interpreter-go/blob/main/lexer/lexer.go"
    click Token "https://github.com/mohamtahab/interpreter-go/blob/main/token/token.go"
    click Parser "https://github.com/mohamtahab/interpreter-go/blob/main/parser/parser.go"
    click AST "https://github.com/mohamtahab/interpreter-go/blob/main/ast/ast.go"
    click Evaluator "https://github.com/mohamtahab/interpreter-go/blob/main/eval/eval.go"
    click ObjectSystem "https://github.com/mohamtahab/interpreter-go/blob/main/object/object.go"

    %% Styles
    classDef input fill:#bbdefb,stroke:#0d47a1,stroke-width:2px;
    classDef processing fill:#c8e6c9,stroke:#1b5e20,stroke-width:2px;
    classDef evaluation fill:#ffe0b2,stroke:#e65100,stroke-width:2px;
```
