# kaushalrijal/lipi architecture

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

- Interactive diagram: https://gitdiagram.com/kaushalrijal/lipi
- Repository: https://github.com/kaushalrijal/lipi

## Overview

##### Project Type and Purpose
- This is a compiler for a high‑level, Nepali‐inspired programming language (“Lipi”).
- Core goal: read a `.lipi` source file, lex and parse it into an AST, perform semantic analysis, generate code, then execute it.
- Language elements: variables, expressions, control flow, functions (all with Nepali keywords).

##### File Structure
- Top‑level:
- `main.cpp` – orchestrates the compiler pipeline.
- `CMakeLists.txt` – build configuration.
- `grammar/` – the BNF grammar file (`byakaran.bnf`).
- `include/` – public headers for AST, lexer, parser, semantic analyzer, symbol table, code generator, executor.
- `src/` – implementation of those components.
- `examples/` – sample `.lipi` programs (input for the compiler).
- `test/` – unit tests for each stage (lexer, parser, AST).
- `docs/` – user documentation.

- Pattern: A classical single‐executable compiler pipeline. No microservices, but well‐modularized stages.

##### README for Additional Insights
- Dependencies: C++17, CMake 3.10+, compiler (g++, clang++).
- Build steps produce `Lipi.exe` (or Linux binary).
- Running: `lipi path/to/source.lipi` invokes the compiler+executor.

a. Main Components
1. Source Input (examples/*.lipi or user file)
2. Lexer (include/lexer.hpp + src/lexer.cpp)
3. Parser (include/parser.hpp + src/parser.cpp)
4. AST (include/ast.hpp + src/ast.cpp)
5. Symbol Table (include/symbol_table.hpp + src/symbol_table.cpp)
6. Semantic Analyzer (include/semantic_analyzer.hpp + src/semantic_analyzer.cpp)
7. Code Generator (include/code_generator.hpp + src/code_generator.cpp)
8. Executor (include/executer.hpp + src/executer.cpp)
9. Optional: Grammar file (grammar/byakaran.bnf) feeding into Parser design.
10. Test Suite (test/*.cpp) – verifies individual components.

b. Relationships and Interactions
- Data flow is strictly linear: Source → Lexer → Parser → AST → Semantic Analyzer (which uses Symbol Table) → Code Generator → Executor → Program Output.
- The `main.cpp` module ties all these stages together.
- Tests invoke components in isolation for unit testing.

c. Architectural Patterns & Principles
- Pipeline pattern: each compiler stage transforms intermediate representation.
- Modular separation: headers in `include/`, implementation in `src/`.
- Single responsibility: each class handles one stage (lexing, parsing, code gen, etc.).
- Test‑driven design evident from `test/` directory.

d. Key Technologies, Frameworks, Libraries
- C++17 (standard library data structures).
- CMake (build orchestration).
- BNF grammar for parser design.
- Native in‑process execution (no external VM).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Main orchestration
    Main["main.cpp"]:::static
    Main --> Lexer

    %% Source and Grammar
    Source["Source File (.lipi)"]:::doc
    Grammar["Grammar File (byakaran.bnf)"]:::static
    Source --> Lexer
    Grammar --> Parser

    %% Compiler stages
    Lexer["Lexer"]:::compile
    Lexer -->|"token stream"| Parser

    Parser["Parser"]:::compile
    Parser -->|"AST"| AST

    AST["AST"]:::compile
    AST --> Semantic

    Semantic["Semantic Analyzer"]:::compile
    Semantic -->|"validated AST"| CodeGen
    Semantic --> SymTable
    SymTable(["Symbol Table"]):::static
    SymTable --> CodeGen

    CodeGen["Code Generator"]:::compile
    CodeGen -->|"bytecode"| Exec

    Exec["Executor"]:::runtime
    Exec --> Output

    Output["Program Output"]:::doc

    %% Tests
    subgraph "Test Suite"
        direction TB
        LTest["lexer_test.cpp"]:::tests
        PTest["parser_test.cpp"]:::tests
        ASTTest["ast_test.cpp"]:::tests
    end
    LTest -.-> Lexer
    PTest -.-> Parser
    ASTTest -.-> AST

    %% Click events
    click Main "https://github.com/kaushalrijal/lipi/blob/master/main.cpp"
    click Source "https://github.com/kaushalrijal/lipi/tree/master/examples/"
    click Grammar "https://github.com/kaushalrijal/lipi/blob/master/grammar/byakaran.bnf"
    click Lexer "https://github.com/kaushalrijal/lipi/blob/master/include/lexer.hpp"
    click Parser "https://github.com/kaushalrijal/lipi/blob/master/include/parser.hpp"
    click AST "https://github.com/kaushalrijal/lipi/blob/master/include/ast.hpp"
    click SymTable "https://github.com/kaushalrijal/lipi/blob/master/include/symbol_table.hpp"
    click Semantic "https://github.com/kaushalrijal/lipi/blob/master/include/semantic_analyzer.hpp"
    click CodeGen "https://github.com/kaushalrijal/lipi/blob/master/include/code_generator.hpp"
    click Exec "https://github.com/kaushalrijal/lipi/blob/master/include/executer.hpp"
    click LTest "https://github.com/kaushalrijal/lipi/blob/master/test/lexer_test.cpp"
    click PTest "https://github.com/kaushalrijal/lipi/blob/master/test/parser_test.cpp"
    click ASTTest "https://github.com/kaushalrijal/lipi/blob/master/test/ast_test.cpp"

    %% Styles
    classDef compile fill:#ADD8E6,stroke:#000
    classDef runtime fill:#90EE90,stroke:#000
    classDef static fill:#D3D3D3,stroke:#000
    classDef tests fill:#FFA500,stroke:#000
    classDef doc fill:#D3D3D3,stroke:#000
```
