Add cst command.

This commit is contained in:
Jesse Brault 2025-12-03 10:38:22 -06:00
parent 8eb13791ee
commit 8ba79be920
2 changed files with 46 additions and 24 deletions

30
src/bin/dmc/cst.rs Normal file
View File

@ -0,0 +1,30 @@
use deimos::parser::{DeimosParser, Rule};
use std::path::PathBuf;
use pest::iterators::{Pair, Pairs};
use pest::Parser;
fn print_pair(pair: Pair<Rule>) {
println!("{:?}", pair.as_rule());
println!("{:?}", pair.as_span());
println!("----");
print_pairs(pair.into_inner());
}
fn print_pairs(pairs: Pairs<Rule>) {
for pair in pairs {
print_pair(pair);
}
}
pub fn show_cst(path: &PathBuf) {
let src = std::fs::read_to_string(path).unwrap();
let parse_result = DeimosParser::parse(Rule::CompilationUnit, &src);
match parse_result {
Ok(pairs) => {
print_pairs(pairs);
}
Err(error) => {
eprintln!("{:?}", error);
}
}
}

View File

@ -1,3 +1,4 @@
mod cst;
mod ir;
mod name_analysis;
mod p3;
@ -5,11 +6,12 @@ mod p3;
use std::path::PathBuf;
// use crate::unparse::unparse;
use crate::cst::show_cst;
use crate::ir::compile_to_ir;
use crate::name_analysis::name_analysis;
use crate::p3::pretty_print_parse;
// use crate::unparse::unparse;
use clap::{Parser, Subcommand};
use crate::ir::compile_to_ir;
#[derive(Debug, Parser)]
#[command(name = "dmc")]
@ -21,46 +23,36 @@ struct Cli {
#[derive(Debug, Subcommand)]
enum Commands {
#[command(arg_required_else_help = true)]
Unparse {
paths: Vec<PathBuf>,
},
P3 {
paths: Vec<PathBuf>,
},
NameAnalysis {
paths: Vec<PathBuf>,
},
Ir {
paths: Vec<PathBuf>,
}
Cst { paths: Vec<PathBuf> },
P3 { paths: Vec<PathBuf> },
NameAnalysis { paths: Vec<PathBuf> },
Ir { paths: Vec<PathBuf> },
}
fn main() {
let args = Cli::parse();
match args.command {
// Commands::Unparse { paths } => {
// for path in paths {
// unparse(&path);
// }
// }
Commands::Cst { paths } => {
for path in &paths {
show_cst(path);
}
}
Commands::P3 { paths } => {
for path in paths {
pretty_print_parse(&path)
pretty_print_parse(&path);
}
}
Commands::NameAnalysis { paths } => {
let result = name_analysis(&paths);
if let Err(e) = result {
eprintln!("{}", e)
eprintln!("{}", e);
}
}
Commands::Ir { paths } => {
let result = compile_to_ir(&paths);
if let Err(e) = result {
eprintln!("{}", e)
eprintln!("{}", e);
}
}
_ => todo!(),
}
}