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
// Copyright 2022 Oxide Computer Company

use anyhow::{anyhow, Result};
use clap::Parser;
use p4::check::Diagnostics;
use p4::{
    ast::AST, check, error, error::SemanticError, lexer, parser, preprocessor,
};
use std::fs;
use std::path::Path;
use std::sync::Arc;

#[derive(Parser)]
#[clap(version = "0.1")]
pub struct Opts {
    /// Show parsed lexical tokens.
    #[clap(long)]
    pub show_tokens: bool,

    /// Show parsed abstract syntax tree.
    #[clap(long)]
    pub show_ast: bool,

    /// Show parsed preprocessor info.
    #[clap(long)]
    pub show_pre: bool,

    /// Show high-level intermediate representation info.
    #[clap(long)]
    pub show_hlir: bool,

    /// File to compile.
    pub filename: String,

    /// What target to generate code for.
    #[clap(arg_enum, default_value_t = Target::Rust)]
    pub target: Target,

    /// Just check code, do not compile.
    #[clap(long)]
    pub check: bool,

    /// Filename to write generated code to.
    #[clap(short, long, default_value = "out.rs")]
    pub out: String,
}

#[derive(clap::ArgEnum, Clone)]
pub enum Target {
    Rust,
    RedHawk,
    Docs,
}

pub fn process_file(
    filename: Arc<String>,
    ast: &mut AST,
    opts: &Opts,
) -> Result<()> {
    let contents = fs::read_to_string(&*filename)
        .map_err(|e| anyhow!("read input: {}: {}", &*filename, e))?;

    let ppr = preprocessor::run(&contents, filename.clone())?;
    if opts.show_pre {
        println!("{:#?}", ppr.elements);
    }

    for included in &ppr.elements.includes {
        let path = Path::new(included);
        if !path.is_absolute() {
            let parent = Path::new(&*filename).parent().unwrap();
            let joined = parent.join(included);
            process_file(
                Arc::new(joined.to_str().unwrap().to_string()),
                ast,
                opts,
            )?
        } else {
            process_file(Arc::new(included.clone()), ast, opts)?
        }
    }

    let lines: Vec<&str> = ppr.lines.iter().map(|x| x.as_str()).collect();

    let mut lxr = lexer::Lexer::new(lines.clone(), filename);
    lxr.show_tokens = opts.show_tokens;

    let mut psr = parser::Parser::new(lxr);
    psr.run(ast)?;
    if opts.show_ast {
        println!("{:#?}", ast);
    }

    let (hlir, diags) = check::all(ast);
    check(&lines, &diags)?;

    if opts.show_hlir {
        println!("{:#?}", hlir);
    }

    Ok(())
}

fn check(lines: &[&str], diagnostics: &Diagnostics) -> Result<()> {
    let errors = diagnostics.errors();
    if !errors.is_empty() {
        let mut err = Vec::new();
        for e in errors {
            err.push(SemanticError {
                at: e.token.clone(),
                message: e.message.clone(),
                source: lines[e.token.line].into(),
            });
        }
        Err(error::Error::Semantic(err))?;
    }
    Ok(())
}