Skip to main content

doge_compiler/
lib.rs

1#![allow(clippy::result_large_err)]
2
3mod ast;
4mod builtins;
5mod check;
6mod codegen;
7mod complete;
8mod diagnostics;
9mod fmt;
10mod keywords;
11mod lexer;
12mod manifest;
13mod modules;
14mod parser;
15mod project;
16mod stdlib;
17mod token;
18
19pub use ast::{
20    celled_locals, child_funcdefs, dump, free_names, hoisted_names, BinOp, Expr, InterpPart, Param,
21    Params, Script, Stmt, UnOp,
22};
23pub use builtins::{builtin, is_builtin, BuiltinFn, BuiltinShape, BUILTINS};
24pub use check::{check_snippet, ClassInfo, SessionScope};
25pub use complete::{complete, Completion, CompletionKind};
26pub use diagnostics::Diagnostic;
27pub use manifest::{Dependency, DependencySource, GitRev, Manifest, MANIFEST_NAME};
28pub use modules::{
29    load_program, load_program_with_deps, single_file_program, Program, ProgramFile,
30};
31pub use parser::{parse_repl, ReplParse};
32pub use project::{discover_root, read_manifest, resolve_project, DependencyMap};
33pub use stdlib::{module as stdlib_module, Module, ModuleFn, MODULES, PACK_ZOOM_RUNTIME_FN};
34pub use token::Span;
35
36/// Lex and parse `source` (named `path` for diagnostics) into a [`Script`]
37pub fn parse(path: &str, source: &str) -> Result<Script, Diagnostic> {
38    parser::parse(path, source)
39}
40
41/// Run the semantic checks over an already-parsed [`Script`].
42pub fn check(path: &str, source: &str, script: &Script) -> Result<(), Diagnostic> {
43    check::check(path, source, script)
44}
45
46/// Format `source` (named `path` for diagnostics) to canonical Doge style, or a
47/// diagnostic if it does not parse. Comments are preserved; the token stream is
48/// never changed.
49pub fn format(path: &str, source: &str) -> Result<String, Diagnostic> {
50    fmt::format(path, source)
51}
52
53/// Generate a complete Rust source file from a checked [`Script`], or a
54/// diagnostic for a construct it cannot compile (a class used as a value).
55pub fn generate(path: &str, source: &str, script: &Script) -> Result<String, Diagnostic> {
56    let program = modules::single_file_program(path, source, script.clone())?;
57    codegen::generate_program(&program)
58}
59
60/// Load the entry script and every `.doge` module it transitively imports.
61pub fn load(entry_path: &str, entry_source: &str) -> Result<Program, Diagnostic> {
62    modules::load_program(entry_path, entry_source)
63}
64
65/// Run the semantic checks over every file in a loaded [`Program`].
66pub fn check_program(program: &Program) -> Result<(), Diagnostic> {
67    check::check_program(program)
68}
69
70/// Generate one Rust source file wiring together every file in a [`Program`].
71pub fn generate_program(program: &Program) -> Result<String, Diagnostic> {
72    codegen::generate_program(program)
73}