How peeperlanguage/compiler works
Purpose and Entry Points
Peeper is a Go-implemented compiler toolchain for the Peeper programming language, with package/project management and Language Server Protocol support. The primary executable starts at cmd/main.go; command dispatch and compiler-facing commands are in cmd/dispatch.go, cmd/build.go, and cmd/command.go. The compiler façade is internal/driver/compiler.go.
CLI, Projects, and Dependencies
The CLI handles build plus project lifecycle and dependency operations (init, get, update, remove, scanning/listing orphaned dependencies). Project manifests and lockfiles are modeled in pkg/manifest/manifest.go and pkg/manifest/lockfile.go. Dependency resolution/download boundaries are represented by pkg/remotes/remotes.go, pkg/registry/download.go, and pkg/registry/cache.go.
Project loading establishes module/import context before compilation: internal/project/context.go, internal/project/modules.go, and internal/project/imports.go. This layer separates filesystem/project topology from language analysis.
Frontend: Source to AST
Compilation begins by loading source files through the pipeline, lexing Peeper syntax, and parsing surface declarations/expressions into the AST. Core stages are internal/pipeline/loader.go, internal/frontend/lexer/lexer.go, and internal/frontend/parser/parser.go. The shared syntax model—including declarations, expressions, statements, metadata, and source locations—is in internal/frontend/ast/decl.go, internal/frontend/ast/expr.go, and internal/frontend/ast/stmt.go.
Semantic Analysis and Safety
Semantic passes build symbol/scope information, resolve names/types, validate control flow, and enforce Peeper’s ownership/reference rules. Key boundaries are collection/binding (internal/semantics/collector/collector.go, internal/semantics/binder/binder.go), resolution and typing (internal/semantics/resolver/resolver.go, internal/semantics/typechecker/typechecker.go), and ownership/place analysis (internal/semantics/ownership/ownership.go, internal/semantics/place/origin.go). CFG analysis supports flow-sensitive validation in internal/semantics/cfg/build.go and internal/semantics/cfg/analyze.go.
Diagnostics are accumulated and rendered independently of the compiler stages via internal/diagnostics/bag.go, internal/diagnostics/diagnostic.go, and internal/diagnostics/emitter.go.
IR Lowering and Code Generation
Validated AST is lowered through explicit intermediate representations: high-level IR in internal/ir/hir/model.go, HIR lowering in internal/ir/hir_lower/lower.go, then machine-oriented IR in internal/ir/mir/model.go and internal/ir/mir/lower.go. Constant folding spans this boundary in internal/ir/constfold.go.
The backend abstraction is internal/backend/backend.go; its concrete LLVM implementation lowers IR, types, calls, drops, and modules through internal/backend/llvm/lower_llvm.go, internal/backend/llvm/type_lowering.go, and internal/backend/llvm/module_emit.go. LLVM and target triples are material external/runtime dependencies, represented by internal/target/llvm_triple.go.
Runtime and Built-ins
Language-provided core functionality is compiled from Peeper source under _builtin_library/core/src, notably allocation and globals in _builtin_library/core/src/allocator.peep and _builtin_library/core/src/global.peep. Prelude integration is centralized in internal/prelude/prelude.go. This forms the boundary between user projects and compiler-supplied runtime/library code.
Language Server
The LSP server shares compiler/project concepts while maintaining workspace state for editor requests. Its JSON-RPC server boundary is internal/lsp/server.go and internal/lsp/jsonrpc.go; incremental document/workspace state lives in internal/lsp/state.go and internal/lsp/workspace.go. Editor features such as completion, hover, and navigation are implemented in internal/lsp/completion.go, internal/lsp/hover.go, and internal/lsp/navigation.go.
Main entry points: context.go Project context · dispatch.go Command dispatch · compiler.go Compiler driver · binder.go Symbols & binding
Components 22
CLI & Projects
- Peeper executable
cmd/main.go - Command dispatch · Build and project lifecycle commands.
cmd/dispatch.go - Compiler driver
internal/driver/compiler.go - Manifest & lockfile
pkg/manifest/manifest.go - Dependency services · Resolves, downloads, and caches packages.
pkg/remotes/remotes.go - Project context · Establishes module topology and imports.
internal/project/context.go
Frontend & Semantics
- Source loader
internal/pipeline/loader.go - Lexer
internal/frontend/lexer/lexer.go - Parser
internal/frontend/parser/parser.go - Syntax AST · Declarations, expressions, statements, and locations.
internal/frontend/ast/decl.go - Symbols & binding
internal/semantics/binder/binder.go - Resolution & typing
internal/semantics/typechecker/typechecker.go - Flow & ownership · CFG, ownership, and place analysis.
internal/semantics/ownership/ownership.go - Diagnostics · Collects and renders compiler diagnostics.
internal/diagnostics/bag.go
IR & Backend
- High-level IR
internal/ir/hir/model.go - Machine IR · Includes MIR lowering and constant folding boundary.
internal/ir/mir/lower.go - LLVM backend · Lowers IR and emits LLVM modules.
internal/backend/llvm/lower_llvm.go - LLVM target · LLVM and target triple integration.
internal/target/llvm_triple.go - Prelude & core runtime · Compiler-provided Peeper core code.
internal/prelude/prelude.go
Language Server
- LSP JSON-RPC server
internal/lsp/server.go - Workspace state
internal/lsp/state.go - Editor features
internal/lsp/completion.go
Connections 27
- Peeper executable → Command dispatch
- Command dispatch → Compiler driver: build
- Command dispatch → Manifest & lockfile: project commands
- Command dispatch → Dependency services: dependency commands
- Manifest & lockfile → Project context: module metadata
- Dependency services → Project context: resolved packages
- Compiler driver → Project context: loads project
- Compiler driver → Source loader: compiles sources
- Project context → Source loader: source/module context
- Source loader → Lexer
- Lexer → Parser: tokens
- Parser → Syntax AST: builds
- Syntax AST → Symbols & binding
- Symbols & binding → Resolution & typing: symbols
- Resolution & typing → Flow & ownership: typed AST
- Symbols & binding → Diagnostics: reports
- Resolution & typing → Diagnostics: reports
- Flow & ownership → Diagnostics: reports
- Flow & ownership → High-level IR: validated AST
- High-level IR → Machine IR: lowering
- Machine IR → LLVM backend: code generation
- LLVM backend → LLVM target: uses LLVM
- Prelude & core runtime → Project context: built-in imports
- LSP JSON-RPC server → Workspace state: document updates
- Workspace state → Editor features: workspace model
- Editor features → Compiler driver: shared analysis
- Workspace state → Project context: workspace projects