Add cst command.
This commit is contained in:
parent
8eb13791ee
commit
8ba79be920
30
src/bin/dmc/cst.rs
Normal file
30
src/bin/dmc/cst.rs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,3 +1,4 @@
|
|||||||
|
mod cst;
|
||||||
mod ir;
|
mod ir;
|
||||||
mod name_analysis;
|
mod name_analysis;
|
||||||
mod p3;
|
mod p3;
|
||||||
@ -5,11 +6,12 @@ mod p3;
|
|||||||
|
|
||||||
use std::path::PathBuf;
|
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::name_analysis::name_analysis;
|
||||||
use crate::p3::pretty_print_parse;
|
use crate::p3::pretty_print_parse;
|
||||||
// use crate::unparse::unparse;
|
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
use crate::ir::compile_to_ir;
|
|
||||||
|
|
||||||
#[derive(Debug, Parser)]
|
#[derive(Debug, Parser)]
|
||||||
#[command(name = "dmc")]
|
#[command(name = "dmc")]
|
||||||
@ -21,46 +23,36 @@ struct Cli {
|
|||||||
|
|
||||||
#[derive(Debug, Subcommand)]
|
#[derive(Debug, Subcommand)]
|
||||||
enum Commands {
|
enum Commands {
|
||||||
#[command(arg_required_else_help = true)]
|
Cst { paths: Vec<PathBuf> },
|
||||||
Unparse {
|
P3 { paths: Vec<PathBuf> },
|
||||||
paths: Vec<PathBuf>,
|
NameAnalysis { paths: Vec<PathBuf> },
|
||||||
},
|
Ir { paths: Vec<PathBuf> },
|
||||||
P3 {
|
|
||||||
paths: Vec<PathBuf>,
|
|
||||||
},
|
|
||||||
NameAnalysis {
|
|
||||||
paths: Vec<PathBuf>,
|
|
||||||
},
|
|
||||||
Ir {
|
|
||||||
paths: Vec<PathBuf>,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args = Cli::parse();
|
let args = Cli::parse();
|
||||||
match args.command {
|
match args.command {
|
||||||
// Commands::Unparse { paths } => {
|
Commands::Cst { paths } => {
|
||||||
// for path in paths {
|
for path in &paths {
|
||||||
// unparse(&path);
|
show_cst(path);
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
Commands::P3 { paths } => {
|
Commands::P3 { paths } => {
|
||||||
for path in paths {
|
for path in paths {
|
||||||
pretty_print_parse(&path)
|
pretty_print_parse(&path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Commands::NameAnalysis { paths } => {
|
Commands::NameAnalysis { paths } => {
|
||||||
let result = name_analysis(&paths);
|
let result = name_analysis(&paths);
|
||||||
if let Err(e) = result {
|
if let Err(e) = result {
|
||||||
eprintln!("{}", e)
|
eprintln!("{}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Commands::Ir { paths } => {
|
Commands::Ir { paths } => {
|
||||||
let result = compile_to_ir(&paths);
|
let result = compile_to_ir(&paths);
|
||||||
if let Err(e) = result {
|
if let Err(e) = result {
|
||||||
eprintln!("{}", e)
|
eprintln!("{}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => todo!(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user