solilang 0.21.1

A statically-typed, class-based OOP language with pipeline operators
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Module resolution for import statements.

use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::{Path, PathBuf};

use crate::ast::{ImportDecl, ImportSpecifier, Program, Stmt, StmtKind};
use crate::lexer::Scanner;
use crate::parser::Parser;

use super::package::Package;

/// A resolved module with its exports.
#[derive(Debug, Clone)]
pub struct ResolvedModule {
    /// Canonical path to the module
    pub path: PathBuf,
    /// Original parsed program (before resolution, contains Export statements)
    pub original_program: Program,
    /// Resolved program (imports resolved, exports unwrapped)
    pub program: Program,
    /// Names exported by this module
    pub exports: HashSet<String>,
}

/// Errors that can occur during module resolution.
#[derive(Debug)]
pub enum ResolveError {
    /// File not found
    NotFound(String),
    /// Circular dependency detected
    CircularDependency(Vec<String>),
    /// Import error (item not exported)
    ImportError(String),
    /// Parse error in module
    ParseError(String),
    /// IO error
    IoError(std::io::Error),
}

impl std::fmt::Display for ResolveError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ResolveError::NotFound(path) => write!(f, "Module not found: {}", path),
            ResolveError::CircularDependency(cycle) => {
                write!(f, "Circular dependency: {}", cycle.join(" -> "))
            }
            ResolveError::ImportError(msg) => write!(f, "Import error: {}", msg),
            ResolveError::ParseError(msg) => write!(f, "Parse error: {}", msg),
            ResolveError::IoError(e) => write!(f, "IO error: {}", e),
        }
    }
}

impl std::error::Error for ResolveError {}

impl From<std::io::Error> for ResolveError {
    fn from(e: std::io::Error) -> Self {
        ResolveError::IoError(e)
    }
}

/// Module resolver that handles import resolution.
pub struct ModuleResolver {
    /// Base directory for resolving relative paths
    base_dir: PathBuf,
    /// Optional package configuration
    package: Option<Package>,
    /// Cache of resolved modules
    cache: HashMap<PathBuf, ResolvedModule>,
    /// Currently resolving stack (for cycle detection)
    resolving: Vec<PathBuf>,
}

impl ModuleResolver {
    /// Create a new module resolver.
    pub fn new(base_dir: &Path) -> Self {
        // Try to find and load package file
        let package = Package::find(base_dir).and_then(|p| Package::load(&p).ok());

        ModuleResolver {
            base_dir: base_dir.to_path_buf(),
            package,
            cache: HashMap::new(),
            resolving: Vec::new(),
        }
    }

    /// Create a module resolver with an explicit package.
    pub fn with_package(base_dir: &Path, package: Package) -> Self {
        ModuleResolver {
            base_dir: base_dir.to_path_buf(),
            package: Some(package),
            cache: HashMap::new(),
            resolving: Vec::new(),
        }
    }

    /// Resolve all imports in a program and return a combined program.
    ///
    /// The returned program contains:
    /// 1. All imported definitions (exported from other modules)
    /// 2. All statements from the main program (with imports removed)
    pub fn resolve(
        &mut self,
        program: Program,
        source_path: &Path,
    ) -> Result<Program, ResolveError> {
        let canonical = self.canonicalize(source_path)?;
        let mut combined_statements = Vec::new();

        // First pass: collect all imports and their resolved modules
        for stmt in &program.statements {
            if let StmtKind::Import(import) = &stmt.kind {
                let module = self.resolve_import(import, &canonical)?;

                // Add the imported definitions to the combined program
                let imported_stmts = get_imported_statements(&module, import)?;
                combined_statements.extend(imported_stmts);
            }
        }

        // Second pass: add non-import statements from the main program
        for stmt in program.statements {
            match &stmt.kind {
                StmtKind::Import(_) => {
                    // Skip imports, they've been resolved
                }
                StmtKind::Export(inner) => {
                    // Unwrap exports for the main module (they're still executed)
                    combined_statements.push((**inner).clone());
                }
                _ => {
                    combined_statements.push(stmt);
                }
            }
        }

        Ok(Program::new(combined_statements))
    }

    /// Resolve an import declaration.
    fn resolve_import(
        &mut self,
        import: &ImportDecl,
        from_path: &Path,
    ) -> Result<ResolvedModule, ResolveError> {
        let module_path = self.resolve_path(&import.path, from_path)?;

        // Check for circular dependency
        if self.resolving.contains(&module_path) {
            let cycle: Vec<String> = self
                .resolving
                .iter()
                .map(|p| p.display().to_string())
                .chain(std::iter::once(module_path.display().to_string()))
                .collect();
            return Err(ResolveError::CircularDependency(cycle));
        }

        // Check cache
        if let Some(cached) = self.cache.get(&module_path) {
            return Ok(cached.clone());
        }

        // Read and parse the module
        let content = fs::read_to_string(&module_path)?;
        let tokens = Scanner::new(&content).scan_tokens().map_err(|e| {
            ResolveError::ParseError(format!("in {}: {}", module_path.display(), e))
        })?;
        let program = match Parser::new(tokens).parse() {
            Ok(p) => p,
            Err(e) => {
                return Err(ResolveError::ParseError(format!(
                    "in {}: {}",
                    module_path.display(),
                    e
                )))
            }
        };

        // Track that we're resolving this module
        self.resolving.push(module_path.clone());

        // Recursively resolve imports in the module
        let resolved_program = self.resolve(program.clone(), &module_path)?;

        // Done resolving this module
        self.resolving.pop();

        // Collect exports from the original program
        let exports = collect_exports(&program);

        let module = ResolvedModule {
            path: module_path.clone(),
            original_program: program,
            program: resolved_program,
            exports,
        };

        // Cache the result
        self.cache.insert(module_path, module.clone());

        Ok(module)
    }

    /// Resolve an import path to an absolute file path.
    fn resolve_path(&self, import_path: &str, from_path: &Path) -> Result<PathBuf, ResolveError> {
        // Relative path (starts with . or ..)
        if import_path.starts_with('.') {
            let from_dir = from_path.parent().unwrap_or(Path::new("."));
            let resolved = from_dir.join(import_path);
            return self.find_module_file(&resolved);
        }

        // Check package dependencies
        if let Some(ref pkg) = self.package {
            // Check if import_path matches a dependency name
            let parts: Vec<&str> = import_path.split('/').collect();
            if let Some(dep) = pkg.dependencies.get(parts[0]) {
                match dep {
                    super::package::Dependency::Path(dep_path) => {
                        let mut resolved = self.base_dir.join(dep_path);
                        // If there's a sub-path, append it
                        for part in &parts[1..] {
                            resolved = resolved.join(part);
                        }
                        return self.find_module_file(&resolved);
                    }
                    super::package::Dependency::Version(_) => {
                        return Err(ResolveError::NotFound(format!(
                            "Version-based dependencies not yet supported: {}",
                            import_path
                        )));
                    }
                }
            }
        }

        // Absolute path from base directory
        let resolved = self.base_dir.join(import_path);
        self.find_module_file(&resolved)
    }

    /// Find the actual module file (handles .sl extension).
    fn find_module_file(&self, path: &Path) -> Result<PathBuf, ResolveError> {
        // Normalize the path to resolve . and .. components
        // This is needed because paths like "../stdlib/file.sl" don't exist as-is
        let normalized = normalize_path(path);

        // Try exact path
        if normalized.exists() && normalized.is_file() {
            return self.canonicalize(&normalized);
        }

        // Try with .sl extension
        let with_ext = normalized.with_extension("sl");
        if with_ext.exists() && with_ext.is_file() {
            return self.canonicalize(&with_ext);
        }

        // Try as directory with index.sl
        let index = normalized.join("index.sl");
        if index.exists() && index.is_file() {
            return self.canonicalize(&index);
        }

        // Try as directory with mod.sl
        let mod_file = normalized.join("mod.sl");
        if mod_file.exists() && mod_file.is_file() {
            return self.canonicalize(&mod_file);
        }

        Err(ResolveError::NotFound(path.display().to_string()))
    }

    /// Canonicalize a path (resolve symlinks, etc.).
    fn canonicalize(&self, path: &Path) -> Result<PathBuf, ResolveError> {
        path.canonicalize()
            .map_err(|_| ResolveError::NotFound(path.display().to_string()))
    }
}

/// Normalize a path string by resolving . and .. components.
fn normalize_path(path: &Path) -> PathBuf {
    if let Some(s) = path.to_str() {
        let mut result = Vec::new();
        let starts_with_slash = s.starts_with('/');
        for component in s.split('/') {
            match component {
                "" => {
                    // Keep leading slash marker
                    if starts_with_slash && result.is_empty() {
                        // Don't add empty component for leading slash
                    }
                }
                "." => {}
                ".." => {
                    if !result.is_empty() && result.last() != Some(&"..") {
                        result.pop();
                    } else {
                        result.push("..");
                    }
                }
                _ => result.push(component),
            }
        }
        let normalized_str = if starts_with_slash {
            format!("/{}", result.join("/"))
        } else {
            result.join("/")
        };
        PathBuf::from(normalized_str)
    } else {
        path.to_path_buf()
    }
}

/// Collect exported names from a program.
fn collect_exports(program: &Program) -> HashSet<String> {
    let mut exports = HashSet::new();

    for stmt in &program.statements {
        if let StmtKind::Export(inner) = &stmt.kind {
            if let Some(name) = get_declaration_name(inner) {
                exports.insert(name);
            }
        }
    }

    exports
}

/// Get the statements to import from a module based on the import specifier.
fn get_imported_statements(
    module: &ResolvedModule,
    import: &ImportDecl,
) -> Result<Vec<Stmt>, ResolveError> {
    match &import.specifier {
        ImportSpecifier::All => {
            // Import all exported definitions
            let mut stmts = Vec::new();
            for stmt in &module.original_program.statements {
                if let StmtKind::Export(inner) = &stmt.kind {
                    stmts.push((**inner).clone());
                }
            }
            Ok(stmts)
        }

        ImportSpecifier::Named(items) => {
            // Import specific named items
            let mut stmts = Vec::new();
            for item in items {
                if !module.exports.contains(&item.name) {
                    return Err(ResolveError::ImportError(format!(
                        "'{}' is not exported from '{}'",
                        item.name, import.path
                    )));
                }

                // Find the exported statement in the original program
                for stmt in &module.original_program.statements {
                    if let StmtKind::Export(inner) = &stmt.kind {
                        if let Some(name) = get_declaration_name(inner) {
                            if name == item.name {
                                if let Some(ref alias) = item.alias {
                                    // Rename the declaration
                                    let renamed = rename_declaration(inner, alias);
                                    stmts.push(renamed);
                                } else {
                                    stmts.push((**inner).clone());
                                }
                                break;
                            }
                        }
                    }
                }
            }
            Ok(stmts)
        }

        ImportSpecifier::Namespace(_name) => {
            // Namespace imports create a module object
            // For now, we just import all exports
            // TODO: Implement proper namespace object
            let mut stmts = Vec::new();
            for stmt in &module.original_program.statements {
                if let StmtKind::Export(inner) = &stmt.kind {
                    stmts.push((**inner).clone());
                }
            }
            Ok(stmts)
        }
    }
}

/// Get the name declared by a statement.
fn get_declaration_name(stmt: &Stmt) -> Option<String> {
    match &stmt.kind {
        StmtKind::Function(decl) => Some(decl.name.clone()),
        StmtKind::Class(decl) => Some(decl.name.clone()),
        StmtKind::Interface(decl) => Some(decl.name.clone()),
        StmtKind::Let { name, .. } => Some(name.clone()),
        _ => None,
    }
}

/// Rename a declaration.
fn rename_declaration(stmt: &Stmt, new_name: &str) -> Stmt {
    let mut new_stmt = stmt.clone();

    match &mut new_stmt.kind {
        StmtKind::Function(decl) => {
            decl.name = new_name.to_string();
        }
        StmtKind::Class(decl) => {
            decl.name = new_name.to_string();
        }
        StmtKind::Interface(decl) => {
            decl.name = new_name.to_string();
        }
        StmtKind::Let { name, .. } => {
            *name = new_name.to_string();
        }
        _ => {}
    }

    new_stmt
}

// Module resolution tests would require tempfile crate which is not in dev-dependencies.
// These tests should be moved to integration tests or tempfile should be added.