|
| 1 | +//! The `driver` module is responsible for module resolution and dependency management. |
| 2 | +//! |
| 3 | +//! Our compiler operates in a strict pipeline: `Lexer -> Parser -> Driver -> AST`. |
| 4 | +//! While the Parser only understands a single file at a time, the Driver processes |
| 5 | +//! multiple files, resolves their dependencies, and converts them into a unified |
| 6 | +//! structure ready for final AST construction. |
| 7 | +//! |
| 8 | +//! # Package Management & Distribution |
| 9 | +//! |
| 10 | +//! SimplicityHL is currently **transport-agnostic**, meaning it does not provide its own |
| 11 | +//! package registry. Developers can use existing ecosystems to distribute `.simf` modules. |
| 12 | +//! |
| 13 | +//! ## Distributing via crates.io |
| 14 | +//! |
| 15 | +//! 1. **Publishing:** Create a Rust library (`cargo new --lib`), and place |
| 16 | +//! your `.simf` files inside it. Then publish the crate to `crates.io`. |
| 17 | +//! |
| 18 | +//! 2. **Downloading:** In the consuming Rust project, add the published crate to |
| 19 | +//! your `Cargo.toml`. Running `cargo build` forces Cargo to download the files |
| 20 | +//! to your global registry cache. |
| 21 | +//! |
| 22 | +//! 3. **Locating:** Find the exact path to the downloaded crate package on your system. |
| 23 | +//! *(e.g., `~/.cargo/registry/src/index.crates.io-<hash>/simplicity-calculator-0.1.0/`)* |
| 24 | +//! |
| 25 | +//! 4. **Linking:** Pass this path to the compiler using |
| 26 | +//! the `--lib` flag, assigning it a custom alias: |
| 27 | +//! ``` text |
| 28 | +//! simc path/to/main.simf --lib calc=~/.cargo/registry/src/.../simplicity-calculator-0.1.0/ |
| 29 | +//! ``` |
| 30 | +//! |
| 31 | +//! 5. **Importing:** Inside your `main.simf`, start with the assigned alias (`calc`). |
| 32 | +//! If the `.simf` file is nested inside subdirectories, the path must reflect |
| 33 | +//! that folder structure. Follow the alias with the directory names, the target |
| 34 | +//! filename, and finally the items to import: |
| 35 | +//! ``` text |
| 36 | +//! // If `calculator.simf` is directly in the library's root: |
| 37 | +//! use calc::calculator::{add_32, subtract_32}; |
| 38 | +//! |
| 39 | +//! // If `calculator.simf` is nested inside a `utils` directory: |
| 40 | +//! use calc::utils::calculator::{add_32, subtract_32}; |
| 41 | +//! ``` |
| 42 | +//! |
| 43 | +//! ## Distributing via npm |
| 44 | +//! |
| 45 | +//! NPM can also be used as a simple file delivery system for SimplicityHL, |
| 46 | +//! which is especially useful for developers working in web environments. |
| 47 | +//! |
| 48 | +//! 1. **Publishing:** Create a directory, generate a `package.json` file |
| 49 | +//! (`npm init -y`), place your `.simf` files inside, and run `npm publish`. |
| 50 | +//! |
| 51 | +//! 2. **Downloading:** In the consuming project's directory, run |
| 52 | +//! `npm install <package-name>`. NPM will download the files locally. |
| 53 | +//! |
| 54 | +//! 3. **Locating:** The `.simf` files will be placed inside the standard |
| 55 | +//! `node_modules` directory at `./node_modules/<package-name>/`. |
| 56 | +//! |
| 57 | +//! 4. **Linking:** Pass this local path to the Simplicity compiler using |
| 58 | +//! the `--lib` flag, assigning it a custom alias: |
| 59 | +//! ``` text |
| 60 | +//! simc main.simf --lib calc=./node_modules/simplicity-calculator/ |
| 61 | +//! ``` |
| 62 | +//! |
| 63 | +//! 5. **Importing:** Inside your `main.simf`, use the alias exactly as |
| 64 | +//! you would with any other dependency: |
| 65 | +//! ``` text |
| 66 | +//! use calc::calculator::{add_32, subtract_32}; |
| 67 | +//! ``` |
| 68 | +//! |
| 69 | +//! # Architecture |
| 70 | +//! |
| 71 | +//! ## Dependency Graph & Linearization |
| 72 | +//! |
| 73 | +//! The driver parses the root file and recursively discovers all imported modules |
| 74 | +//! to build a Directed Acyclic Graph (DAG) of the project's dependencies. Because |
| 75 | +//! the final AST requires a flat array of items, the driver applies a deterministic |
| 76 | +//! linearization strategy to this DAG. This safely flattens the multi-file project |
| 77 | +//! into a single, logically ordered sequence, strictly enforcing visibility rules |
| 78 | +//! and preventing duplicate imports. |
| 79 | +//! |
| 80 | +//! ## Project Structure & Entry Point |
| 81 | +//! |
| 82 | +//! SimplicityHL does not define a "project root" directory. Instead, the compiler |
| 83 | +//! relies on a single entry point: the file passed as the first positional argument. |
| 84 | +//! This file must contain the `main` function, which serves as the program's |
| 85 | +//! starting point. |
| 86 | +//! |
| 87 | +//! External libraries are explicitly linked using the `--lib` flag. The driver |
| 88 | +//! resolves and parses these external files relative to the entry point during |
| 89 | +//! the dependency graph construction. |
| 90 | +
|
1 | 91 | mod linearization; |
| 92 | +mod resolve_order; |
2 | 93 |
|
3 | 94 | use std::collections::{HashMap, HashSet, VecDeque}; |
4 | 95 | use std::path::PathBuf; |
|
0 commit comments