# binxly/monkey-interpreter architecture

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

- Interactive diagram: https://gitdiagram.com/binxly/monkey-interpreter
- Repository: https://github.com/binxly/monkey-interpreter

## Overview

##### Main Components
- Entry Point (main.go) – This is where the interpreter is launched.
- REPL (repl/repl.go) – Acts as the interactive command-line interface where users input Monkey code.
- Lexer (lexer/lexer.go & lexer/lexer_test.go) – Responsible for tokenizing the input string into a sequence of tokens.
- Token Definitions (token/token.go) – Contains the Token structure and constants used by the lexer.
- Parser (parser/parser.go & parser/parser_test.go) – Converts tokens into an Abstract Syntax Tree (AST) structure.
- AST (ast/ast.go & ast/ast_test.go) – Represents the syntactic structure of Monkey code, forming the basis for evaluation.

##### Relationships and Data Flow
- User inputs code into the REPL.
- The REPL directs this input to the Lexer.
- The Lexer processes the input and emits tokens, using the definitions in token/token.go.
- These tokens are passed to the Parser to construct an AST.
- The AST (as defined in ast/ast.go) is then used for interpretation and evaluation.
- The evaluated result is presented back to the user via the REPL.
- The main.go file ties these components together, initializing the REPL and bootstrapping the interpreter's workflow.

##### Additional Guidance
- Since this is an interpreter project based on concepts from “Writing an Interpreter in Go”, you might note that iterative development (e.g., adding language features like if statements, grouped expressions, function literals) is a key design consideration, though not all may be fully implemented yet.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Global Entities
    U("User Input"):::input
    EP("Entry Point (main.go)"):::entry
    REPL("REPL (repl/repl.go)"):::io

    %% Subgraph: Lexer Package
    subgraph "Lexer Package"
        L("Lexer (lexer/lexer.go)"):::proc
        LT("Lexer Tests (lexer/lexer_test.go)"):::test
    end

    %% Token Definitions
    TD("Token Definitions (token/token.go)"):::module

    %% Subgraph: Parser Package
    subgraph "Parser Package"
        P("Parser (parser/parser.go)"):::proc
        PT("Parser Tests (parser/parser_test.go)"):::test
    end

    %% Subgraph: AST Package
    subgraph "AST Package"
        A("AST (ast/ast.go)"):::proc
        AT("AST Tests (ast/ast_test.go)"):::test
    end

    Eval("Evaluation"):::proc

    %% Data Flow Connections
    U -->|"inputs"| REPL
    EP -->|"initializes"| REPL
    REPL -->|"sends code to"| L
    L -->|"tokenizes using"| TD
    L -->|"emits tokens to"| P
    P -->|"builds"| A
    A -->|"processed by"| Eval
    Eval -->|"returns result to"| REPL
    REPL -->|"displays result to"| U

    %% Click Events
    click EP "https://github.com/binxly/monkey-interpreter/blob/main/main.go"
    click REPL "https://github.com/binxly/monkey-interpreter/blob/main/repl/repl.go"
    click L "https://github.com/binxly/monkey-interpreter/blob/main/lexer/lexer.go"
    click LT "https://github.com/binxly/monkey-interpreter/blob/main/lexer/lexer_test.go"
    click TD "https://github.com/binxly/monkey-interpreter/blob/main/token/token.go"
    click P "https://github.com/binxly/monkey-interpreter/blob/main/parser/parser.go"
    click PT "https://github.com/binxly/monkey-interpreter/blob/main/parser/parser_test.go"
    click A "https://github.com/binxly/monkey-interpreter/blob/main/ast/ast.go"
    click AT "https://github.com/binxly/monkey-interpreter/blob/main/ast/ast_test.go"

    %% Styles
    classDef entry fill:#FEEFB3,stroke:#333,stroke-width:2px
    classDef io fill:#C2E7DA,stroke:#333,stroke-width:2px
    classDef input fill:#D6EAF8,stroke:#333,stroke-width:2px
    classDef proc fill:#D5F5E3,stroke:#333,stroke-width:2px
    classDef module fill:#FADBD8,stroke:#333,stroke-width:2px
    classDef test fill:#E8DAEF,stroke:#333,stroke-width:2px
```
