dol 0.8.1

DOL (Design Ontology Language) - A declarative specification language for ontology-first development
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
//! Crate-level code generation for DOL.
//!
//! Generates a complete Rust crate from multiple DOL files,
//! with one .rs file per DOL module.

use std::collections::HashSet;
use std::fs;
use std::path::Path;

use crate::ast::{DolFile, UseDecl, UseItems};
use crate::codegen::rust::RustCodegen;

/// Configuration for crate generation.
#[derive(Debug, Clone)]
pub struct CrateConfig {
    /// Name of the generated crate
    pub crate_name: String,
    /// Version of the generated crate
    pub crate_version: String,
    /// Output directory for the crate
    pub output_dir: String,
}

impl Default for CrateConfig {
    fn default() -> Self {
        Self {
            crate_name: "dol_generated".to_string(),
            crate_version: "0.1.0".to_string(),
            output_dir: "stage2".to_string(),
        }
    }
}

/// Crate code generator.
///
/// Generates a complete Rust crate from multiple DOL files.
pub struct CrateCodegen {
    config: CrateConfig,
}

/// Information about a DOL module for code generation.
#[derive(Debug, Clone)]
pub struct ModuleInfo {
    /// Module name (last component of path, e.g., "lexer" from "dol.lexer")
    pub name: String,
    /// Full module path (e.g., "dol.lexer")
    pub full_path: String,
    /// Source file path
    pub source_path: String,
    /// Dependencies (other modules this one imports)
    pub dependencies: Vec<String>,
}

impl CrateCodegen {
    /// Create a new crate generator with default configuration.
    pub fn new(output_dir: &str) -> Self {
        Self {
            config: CrateConfig {
                output_dir: output_dir.to_string(),
                ..Default::default()
            },
        }
    }

    /// Create a new crate generator with custom configuration.
    pub fn with_config(config: CrateConfig) -> Self {
        Self { config }
    }

    /// Generate a complete Rust crate from DOL files.
    ///
    /// # Arguments
    ///
    /// * `files` - Parsed DOL files to generate from
    ///
    /// # Returns
    ///
    /// Ok(()) on success, or an error message on failure.
    pub fn generate(&self, files: &[(String, DolFile)]) -> Result<(), String> {
        // Create output directory structure
        let src_dir = format!("{}/src", self.config.output_dir);
        fs::create_dir_all(&src_dir).map_err(|e| format!("Failed to create src dir: {}", e))?;

        // Analyze modules to get dependency order
        let modules: Vec<ModuleInfo> = files
            .iter()
            .map(|(path, file)| self.analyze_module(path, file))
            .collect();

        // Generate each module file
        for ((_source_path, file), module) in files.iter().zip(modules.iter()) {
            self.gen_module_file(file, module, &modules)?;
        }

        // Generate lib.rs
        self.gen_lib_rs(&modules)?;

        // Generate prelude.rs
        self.gen_prelude_rs(&modules)?;

        // Generate Cargo.toml
        self.gen_cargo_toml()?;

        Ok(())
    }

    /// Analyze a DOL file to extract module information.
    fn analyze_module(&self, source_path: &str, file: &DolFile) -> ModuleInfo {
        let name = file
            .module
            .as_ref()
            .and_then(|m| m.path.last().cloned())
            .unwrap_or_else(|| {
                // Extract from filename
                Path::new(source_path)
                    .file_stem()
                    .and_then(|s| s.to_str())
                    .unwrap_or("unknown")
                    .to_string()
            });

        let full_path = file
            .module
            .as_ref()
            .map(|m| m.path.join("."))
            .unwrap_or_else(|| name.clone());

        // Extract dependencies from use declarations
        let dependencies: Vec<String> = file
            .uses
            .iter()
            .filter_map(|u| {
                // Get the first part of the path if it starts with "dol"
                if u.path.first().map(|s| s.as_str()) == Some("dol") {
                    u.path.get(1).cloned()
                } else {
                    None
                }
            })
            .collect::<HashSet<_>>()
            .into_iter()
            .collect();

        ModuleInfo {
            name,
            full_path,
            source_path: source_path.to_string(),
            dependencies,
        }
    }

    /// Generate a single module file.
    fn gen_module_file(
        &self,
        file: &DolFile,
        module: &ModuleInfo,
        all_modules: &[ModuleInfo],
    ) -> Result<(), String> {
        let mut content = String::new();

        // Module doc comment
        content.push_str(&format!("//! Module: {}\n", module.full_path));
        content.push_str("//! Generated from DOL source - do not edit\n\n");

        // Generate imports based on use declarations
        content.push_str(&self.gen_imports(file));

        // Add imports from other DOL modules to resolve cross-module references
        // This handles cases where types are used but not explicitly imported in DOL
        content.push_str(&self.gen_sibling_imports(module, all_modules));
        content.push('\n');

        // Generate declarations using existing RustCodegen
        let rust_gen = RustCodegen::new();
        content.push_str(&rust_gen.gen_file(&file.declarations));

        // Write file
        let file_path = format!("{}/src/{}.rs", self.config.output_dir, module.name);
        fs::write(&file_path, content)
            .map_err(|e| format!("Failed to write {}: {}", file_path, e))?;

        Ok(())
    }

    /// Generate import statements from DOL use declarations.
    fn gen_imports(&self, file: &DolFile) -> String {
        let mut output = String::new();
        let mut seen = HashSet::new();

        for use_decl in &file.uses {
            let rust_import = self.resolve_import(use_decl);
            if !rust_import.is_empty() && !seen.contains(&rust_import) {
                output.push_str(&rust_import);
                output.push('\n');
                seen.insert(rust_import);
            }
        }

        output
    }

    /// Convert a DOL use declaration to a Rust use statement.
    fn resolve_import(&self, use_decl: &UseDecl) -> String {
        let path = use_decl.path.join("::");

        // dol.X -> crate::X
        if path.starts_with("dol::") {
            let internal = path.replacen("dol::", "crate::", 1);
            match &use_decl.items {
                UseItems::All => format!("use {}::*;", internal),
                UseItems::Single => format!("use {};", internal),
                UseItems::Named(items) => {
                    let names: Vec<&str> = items.iter().map(|i| i.name.as_str()).collect();
                    format!("use {}::{{ {} }};", internal, names.join(", "))
                }
            }
        }
        // External crates (std::, etc.)
        else {
            format!("use {};", path)
        }
    }

    /// Generate imports from sibling modules to handle cross-module type references.
    /// This adds `use crate::sibling::*;` for each other module.
    fn gen_sibling_imports(&self, current: &ModuleInfo, all_modules: &[ModuleInfo]) -> String {
        let mut output = String::new();
        output.push_str("#[allow(unused_imports)]\n");

        // Always import compatibility types first
        output.push_str("use crate::compat::*;\n");

        for module in all_modules {
            // Skip self, main, and mod (root)
            if module.name == current.name
                || module.name == "main"
                || module.name == "mod"
                || module.name == "dol"
            {
                continue;
            }
            output.push_str(&format!("use crate::{}::*;\n", module.name));
        }

        output
    }

    /// Generate lib.rs with mod declarations.
    fn gen_lib_rs(&self, modules: &[ModuleInfo]) -> Result<(), String> {
        let mut content = String::from("//! DOL Generated Crate\n");
        content.push_str("//! Do not edit manually - regenerate from DOL source\n\n");

        // Add standard library imports
        content.push_str("use std::collections::HashMap;\n\n");

        // Compatibility module must come first
        content.push_str("pub mod compat;\n\n");

        // Module declarations
        for module in modules {
            if module.name == "main" {
                continue; // main.rs is separate, not a module
            }
            if module.name == "mod" {
                continue; // skip mod.dol as it's the root
            }
            content.push_str(&format!("pub mod {};\n", module.name));
        }

        content.push_str("\npub mod prelude;\n");

        let path = format!("{}/src/lib.rs", self.config.output_dir);
        fs::write(&path, content).map_err(|e| format!("Failed to write lib.rs: {}", e))?;

        // Generate compat.rs
        self.gen_compat_rs()?;

        Ok(())
    }

    /// Generate compat.rs with compatibility types for DOL sources.
    fn gen_compat_rs(&self) -> Result<(), String> {
        let content = r#"//! Compatibility layer for types expected by DOL sources
//! These are stub/alias types that bridge the gap between DOL source expectations
//! and the generated AST structure.

// Type aliases for renamed types
pub type BinaryOp = super::ast::BinOp;
pub type Statement = super::ast::Stmt;
pub type TypeParams = Vec<super::ast::TypeParam>;

// Placeholder - Literal is used as a parameter type but doesn't exist as separate enum
pub type Literal = super::ast::Expr;

// Stub types for missing definitions
#[derive(Debug, Clone, PartialEq)]
pub struct UseDecl {
    pub path: Vec<String>,
    pub items: UseItems,
    pub alias: Option<String>,
    pub span: super::token::Span,
}

#[derive(Debug, Clone, PartialEq)]
pub enum UseItems {
    All,
    Single,
    Named(Vec<UseItem>),
}

#[derive(Debug, Clone, PartialEq)]
pub struct UseItem {
    pub name: String,
    pub alias: Option<String>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct HasField {
    pub name: String,
    pub ty: super::ast::TypeExpr,
    pub default: Option<super::ast::Expr>,
    pub span: super::token::Span,
}

#[derive(Debug, Clone, PartialEq)]
pub struct EvolutionDecl {
    pub name: String,
    pub span: super::token::Span,
}

#[derive(Debug, Clone, PartialEq, Copy)]
pub enum Purity {
    Pure,
    Impure,
    Sex,
}

#[derive(Debug, Clone, PartialEq)]
pub enum Predicate {
    Has(String, String, super::ast::TypeExpr, super::token::Span),
    Is(String, String, super::token::Span),
    Requires(super::ast::Expr, super::token::Span),
    Ensures(super::ast::Expr, super::token::Span),
    Invariant(super::ast::Expr, super::token::Span),
    Law(String, super::ast::Expr, super::token::Span),
}
"#;
        let path = format!("{}/src/compat.rs", self.config.output_dir);
        fs::write(&path, content).map_err(|e| format!("Failed to write compat.rs: {}", e))?;

        Ok(())
    }

    /// Generate prelude.rs with re-exports.
    fn gen_prelude_rs(&self, modules: &[ModuleInfo]) -> Result<(), String> {
        let mut content = String::from("//! Prelude - common re-exports\n\n");

        // Re-export compat types
        content.push_str("pub use crate::compat::*;\n\n");

        for module in modules {
            if module.name == "main" || module.name == "mod" {
                continue;
            }
            content.push_str(&format!("pub use crate::{}::*;\n", module.name));
        }

        let path = format!("{}/src/prelude.rs", self.config.output_dir);
        fs::write(&path, content).map_err(|e| format!("Failed to write prelude.rs: {}", e))?;

        Ok(())
    }

    /// Generate Cargo.toml.
    fn gen_cargo_toml(&self) -> Result<(), String> {
        let content = format!(
            r#"[package]
name = "{}"
version = "{}"
edition = "2021"

[dependencies]
"#,
            self.config.crate_name, self.config.crate_version
        );

        let path = format!("{}/Cargo.toml", self.config.output_dir);
        fs::write(&path, content).map_err(|e| format!("Failed to write Cargo.toml: {}", e))?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_config() {
        let config = CrateConfig::default();
        assert_eq!(config.crate_name, "dol_generated");
        assert_eq!(config.crate_version, "0.1.0");
    }

    #[test]
    fn test_dol_file_default() {
        let file = DolFile {
            module: None,
            uses: Vec::new(),
            declarations: Vec::new(),
        };
        assert!(file.module.is_none());
        assert!(file.uses.is_empty());
        assert!(file.declarations.is_empty());
    }

    #[test]
    fn test_crate_codegen_new() {
        let codegen = CrateCodegen::new("test_output");
        assert_eq!(codegen.config.output_dir, "test_output");
        assert_eq!(codegen.config.crate_name, "dol_generated");
    }
}