$ show FuelLabs/sway
This repository is the primary implementation of the Sway programming language and its toolchain, targeting the Fuel blockchain. It contains a full compiler pipeline (lexer, parser, AST, semantic analysis, IR with optimization passes, and code generation for the FuelVM), a package manager CLI (`forc`), a language server (`sway-lsp`), a code formatter (`swayfmt`), a debugger (`forc-debug`), a documentation generator (`forc-doc`), and a migration tool (`forc-migrate`). The standard library (`sway-lib-std`) provides essential blockchain primitives (assets, storage, crypto, etc.). The architecture is modular, with separate crates for the AST (`sway-ast`), IR (`sway-ir`), parsing (`sway-parse`), core compiler logic (`sway-core`), and error handling (`sway-error`). In production, developers use `forc` to build, test, and deploy Sway smart contracts, scripts, and predicates to the Fuel network. The LSP provides IDE features like completions, go-to-definition, and inlay hints. The formatter ensures consistent code style. The debugger enables step-through debugging of on-chain logic. The compiler supports multiple backends (FuelVM primary, EVM experimental) and includes advanced optimizations such as constant folding, dead code elimination, common subexpression elimination, and register allocation. Key entry points include `forc/src/main.rs` (CLI dispatcher), `sway-core/src/lib.rs` (compiler core), `sway-lsp/src/server.rs` (LSP server), and `swayfmt/src/lib.rs` (formatter). External dependencies include the Fuel protocol libraries (`fuel-core`, `fuel-vm`, `fuels-rs`), IPFS for package resolution, and standard Rust ecosystem crates. The repository is production-grade with extensive end-to-end tests, benchmarking infrastructure, and CI workflows.
The LLM identified 4 files worth highlighting for reuse:
Provides `ConcurrentSlab<T>` — a thread-safe slab allocator with a free list for O(1) insertion and removal. Uses `Arc<RwLock<...>>` for interior mutability and supports iteration, indexing, and clearing. This is a reusable pattern for any system that needs concurrent, stable-indexed storage of large numbers of items (e.g., declaration engines, symbol tables) without external dependencies.
Implements a rich diagnostic system with `Reason`, `Code`, `Issue`, `Hint`, and `Label` structs, along with formatting helpers like `sequence_to_str`, `did_you_mean`, and `short_name`. The `Diagnostic` struct aggregates errors with spans and levels. This is a self-contained, high-craftsmanship component that could be extracted as a generic diagnostic library for any compiler or linter.
Defines IR `Constant` values (ints, bools, u256, etc.) with a `unique` method that uses `FxHasher` to deduplicate constants within a `Context`. Includes serialization to `fuel-vm` interpreter constants. This pattern of hash-consing constants is a reusable optimization technique for intermediate representations.
Defines engine-aware traits (`DisplayWithEngines`, `DebugWithEngines`, `HashWithEngines`, `EqWithEngines`, `OrdWithEngines`) and the `Engines` struct that bundles type, declaration, and source engines. These traits allow compiler types to be compared/hashed/displayed with context (e.g., engine-specific resolution). The pattern is reusable for any application requiring context-dependent operations on AST or IR nodes.