# tadd/schaf architecture

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

- Interactive diagram: https://gitdiagram.com/tadd/schaf
- Repository: https://github.com/tadd/schaf

## Overview

##### Main Components
- Interpreter Core: Represented mainly by the schaf.[c/h] files. This forms the central engine and likely hosts the evaluation loop, integration of primitives, and the overall management of evaluation.
- Parser: The parse.c file is responsible for reading Scheme source code and turning it into an abstract representation (e.g., an Abstract Syntax Tree or direct S-expression data structures). This module feeds data to the evaluator.
▪ Input: Takes in parsed S-expressions (or AST nodes).
▪ Environment Handling: Interacts with the symbol table and environment (provided by table.c/table.h and intern.h) to perform variable lookups and bindings.
▪ Recursion/Control Flow: Supports Scheme constructs such as function calls, lambda expressions, and control structures.
▪ Interaction with Primitives: Integrates built-in functions implemented either directly or via supporting modules.
- Symbol Table/Interning: table.c/h and intern.h facilitate the interning of symbols and managing environments. This is crucial for efficient variable/referential integrity during evaluation.
- Utilities: The utils.c/h files offer general support functions (such as memory management, error handling, or other helpers) to various components.
- Testing & Benchmarks: Directories “test” and “microbench” incorporate tests (including basic and more comprehensive Scheme tests) and performance benchmarks (with scripts and additional Scheme files) that ensure correctness and performance of the evaluator and overall engine.

##### Relationships and Interactions
- Input Flow: Source code is supplied to the interpreter’s entry point (main.c), where initial setup occurs.
- Parsing: main.c calls functions in parse.c to convert the source text into an internal representation.
▪ It receives an S-expression and recursively evaluates it.
▪ It consults the environment, which is maintained by the symbol table modules, to resolve variable bindings.
▪ It applies built-in functions and user-defined lambdas accordingly.
- Symbol & Environment Management: The evaluator queries/updates the symbol table to ensure that symbols are interned and correctly resolved during evaluation.
- Testing/Benchmarking: Both system test cases and microbench scripts simulate various workloads, ensuring the evaluator (and thus the overall engine) meets R⁵RS standards.

##### Architectural Patterns
- Interpreter Loop: Emphasize the read–eval–print loop (REPL) that might be embedded in schaf.c.
- Separation of Concerns: Notice how parsing, evaluation, and symbol management are modularized into distinct components.

##### Incorporate Technologies and Tools
- Language: C (with C17 standard).
- Testing: Embed references to test files under the “test” directory and benchmark scripts under “microbench”.
- Build/Deployment: Mention the Makefile and GitHub Actions workflow (in .github/workflows) that orchestrates tests/builds.

##### Focused Detail About the Evaluator
- In the evaluator component’s detailing, include key stages such as:
▪ Parsing a raw Scheme expression.
▪ Checking if the expression is atomic (e.g., number, symbol) or compound.
▪ If compound, recursively evaluating the operator and operands.
▪ Error and edge-case handling, likely done through utility functions in utils.c/h.
- This granular detail is important, as the evaluator is the heart of the interpreter. It might also include hooks for extending the language (as hinted by exported symbols beginning with “sch_”).

- The entry point (main function),
- Breaking down the Scheme processing pipeline (from parsing to evaluation),
- The core evaluator workflow, including environment lookups and recursive processing,
- Auxiliary modules (utilities, testing, microbenchmarks).

This detailed and component-wise layout will give you a clear architectural view that addresses both the overall system design and, with extra focus, the evaluator’s inner workings, ensuring that all essential elements and their relationships are visually communicated.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Interpreter Pipeline"
        MAIN["Main/Entry (main.c)"]:::core
        PARSER["Parser (parse.c)"]:::core
        EVALUATOR["Evaluator (schaf.c/h)"]:::core

        MAIN -->|"setup&init"| PARSER
        PARSER -->|"parsed_AST"| EVALUATOR

        subgraph "Evaluator Workflow"
            EVAL_INPUT["Input: S-expression"]:::evaluator
            ENV_LOOKUP["Environment Lookup"]:::evaluator
            EVAL_RECURSION["Recursive Evaluation"]:::evaluator
            EVAL_BUILTINS["Apply_Built-in&User_Functions"]:::evaluator
            ERROR_HANDLING["Error_Handling_(via_utilities)"]:::evaluator
        end

        EVALUATOR -->|"evaluates"| EVAL_INPUT
        EVAL_INPUT -->|"lookup"| ENV_LOOKUP
        ENV_LOOKUP -->|"recursive_processing"| EVAL_RECURSION
        EVAL_RECURSION -->|"apply_operations"| EVAL_BUILTINS
        EVAL_BUILTINS -->|"utilizes"| ERROR_HANDLING

        SYMBOL_TABLE["Symbol_Table&Interning (table.c, table.h, intern.h)"]:::data
        UTILITIES["Utilities (utils.c/h)"]:::utility

        EVALUATOR -->|"symbol_binding"| SYMBOL_TABLE
        EVALUATOR -->|"error&mem_mgmt"| UTILITIES

        EVALUATOR -->|"REPL_loop"| MAIN
    end

    subgraph "Quality Checks"
        TEST["Testing (test directory)"]:::test
        BENCH["Benchmarks (microbench directory)"]:::test
    end

    MAIN -->|"trigger_tests"| TEST
    MAIN -->|"benchmark_run"| BENCH

    MAKEFILE["Makefile&GitHub_Actions (.github/workflows)"]:::build
    MAIN -->|"build_pipeline"| MAKEFILE

    click MAIN "https://github.com/tadd/schaf/blob/main/main.c"
    click PARSER "https://github.com/tadd/schaf/blob/main/parse.c"
    click EVALUATOR "https://github.com/tadd/schaf/blob/main/schaf.c"
    click SYMBOL_TABLE "https://github.com/tadd/schaf/blob/main/table.c"
    click UTILITIES "https://github.com/tadd/schaf/blob/main/utils.c"
    click TEST "https://github.com/tadd/schaf/tree/main/test"
    click BENCH "https://github.com/tadd/schaf/tree/main/microbench"

    classDef core fill:#f9f,stroke:#333,stroke-width:2px;
    classDef evaluator fill:#ff9,stroke:#333,stroke-width:2px;
    classDef data fill:#9ff,stroke:#333,stroke-width:2px;
    classDef utility fill:#9f9,stroke:#333,stroke-width:2px;
    classDef test fill:#f99,stroke:#333,stroke-width:2px;
    classDef build fill:#99f,stroke:#333,stroke-width:2px;
```
