2024-03-06 20:36:58 +00:00
|
|
|
//! Command line interface for the dust programming language.
|
2024-03-20 08:42:13 +00:00
|
|
|
mod cli;
|
2024-03-06 20:36:58 +00:00
|
|
|
|
2024-03-23 21:07:41 +00:00
|
|
|
use ariadne::sources;
|
2024-03-06 20:36:58 +00:00
|
|
|
use clap::Parser;
|
2024-03-20 08:42:13 +00:00
|
|
|
use cli::run_shell;
|
2024-03-06 20:36:58 +00:00
|
|
|
use colored::Colorize;
|
2024-04-27 07:40:05 +00:00
|
|
|
use log::Level;
|
2024-03-06 20:36:58 +00:00
|
|
|
|
2024-03-20 08:42:13 +00:00
|
|
|
use std::{
|
|
|
|
fs::read_to_string,
|
|
|
|
io::{stderr, Write},
|
2024-03-24 21:34:36 +00:00
|
|
|
sync::Arc,
|
2024-03-20 08:42:13 +00:00
|
|
|
};
|
2024-03-06 20:36:58 +00:00
|
|
|
|
2024-03-24 19:35:19 +00:00
|
|
|
use dust_lang::{context::Context, Interpreter};
|
2024-03-06 20:36:58 +00:00
|
|
|
|
|
|
|
/// Command-line arguments to be parsed.
|
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
#[command(author, version, about, long_about = None)]
|
|
|
|
struct Args {
|
|
|
|
/// Dust source code to evaluate.
|
|
|
|
#[arg(short, long)]
|
|
|
|
command: Option<String>,
|
|
|
|
|
2024-06-04 18:47:15 +00:00
|
|
|
// Display lexer tokens of the input source.
|
|
|
|
#[arg(short, long)]
|
|
|
|
lex: bool,
|
|
|
|
|
|
|
|
// Display abstract tree of the input source.
|
|
|
|
#[arg(short, long)]
|
|
|
|
parse: bool,
|
|
|
|
|
2024-03-24 13:10:49 +00:00
|
|
|
#[arg(long)]
|
|
|
|
no_std: bool,
|
|
|
|
|
2024-03-06 20:36:58 +00:00
|
|
|
/// Location of the file to run.
|
|
|
|
path: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
env_logger::Builder::from_env("DUST_LOG")
|
|
|
|
.format(|buffer, record| {
|
|
|
|
let args = record.args();
|
2024-04-27 07:40:05 +00:00
|
|
|
let log_level = match record.level() {
|
|
|
|
Level::Trace => "TRACE".cyan().bold(),
|
|
|
|
Level::Warn => "WARN".yellow().bold(),
|
|
|
|
Level::Debug => "DEBUG".green().bold(),
|
|
|
|
Level::Error => "ERROR".red().bold(),
|
|
|
|
Level::Info => "INFO".white().bold(),
|
|
|
|
};
|
2024-03-06 20:36:58 +00:00
|
|
|
let timestamp = buffer.timestamp_seconds().to_string().dimmed();
|
|
|
|
|
2024-04-27 07:40:05 +00:00
|
|
|
writeln!(buffer, "[{} {}] {}", log_level, timestamp, args)
|
2024-03-06 20:36:58 +00:00
|
|
|
})
|
|
|
|
.init();
|
|
|
|
|
|
|
|
let args = Args::parse();
|
2024-05-21 20:52:29 +00:00
|
|
|
let context = Context::new(None);
|
2024-03-24 19:35:19 +00:00
|
|
|
let mut interpreter = Interpreter::new(context.clone());
|
|
|
|
|
2024-06-17 19:47:07 +00:00
|
|
|
if !args.no_std {
|
|
|
|
interpreter.load_std().unwrap();
|
|
|
|
}
|
2024-03-24 19:35:19 +00:00
|
|
|
|
2024-03-24 21:34:36 +00:00
|
|
|
let (source_id, source): (Arc<str>, Arc<str>) = if let Some(path) = args.path {
|
2024-03-24 19:35:19 +00:00
|
|
|
let source = read_to_string(&path).unwrap();
|
|
|
|
|
2024-03-24 21:34:36 +00:00
|
|
|
(Arc::from(path), Arc::from(source.as_str()))
|
2024-03-06 20:36:58 +00:00
|
|
|
} else if let Some(command) = args.command {
|
2024-03-24 21:34:36 +00:00
|
|
|
(Arc::from("command"), Arc::from(command.as_str()))
|
2024-03-06 20:36:58 +00:00
|
|
|
} else {
|
2024-03-23 23:12:18 +00:00
|
|
|
match run_shell(context) {
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(error) => eprintln!("{error}"),
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2024-03-06 20:36:58 +00:00
|
|
|
};
|
|
|
|
|
2024-06-04 18:47:15 +00:00
|
|
|
if args.lex {
|
|
|
|
match interpreter.lex(source_id, source.as_ref()) {
|
|
|
|
Ok(tokens) => println!("{tokens:?}"),
|
|
|
|
Err(error) => {
|
|
|
|
for report in error.build_reports() {
|
|
|
|
report
|
|
|
|
.write_for_stdout(sources(interpreter.sources()), stderr())
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.parse {
|
|
|
|
match interpreter.parse(source_id, source.as_ref()) {
|
|
|
|
Ok(abstract_tree) => println!("{abstract_tree:?}"),
|
|
|
|
Err(error) => {
|
|
|
|
for report in error.build_reports() {
|
|
|
|
report
|
|
|
|
.write_for_stdout(sources(interpreter.sources()), stderr())
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-24 21:34:36 +00:00
|
|
|
let run_result = interpreter.run(source_id.clone(), source.clone());
|
2024-03-06 20:36:58 +00:00
|
|
|
|
2024-03-24 19:35:19 +00:00
|
|
|
match run_result {
|
2024-03-06 20:36:58 +00:00
|
|
|
Ok(value) => {
|
2024-03-08 17:24:11 +00:00
|
|
|
if let Some(value) = value {
|
2024-03-06 20:36:58 +00:00
|
|
|
println!("{value}")
|
|
|
|
}
|
|
|
|
}
|
2024-03-24 13:10:49 +00:00
|
|
|
Err(error) => {
|
|
|
|
for report in error.build_reports() {
|
2024-03-23 21:07:41 +00:00
|
|
|
report
|
2024-03-24 19:35:19 +00:00
|
|
|
.write_for_stdout(sources(interpreter.sources()), stderr())
|
2024-03-23 21:07:41 +00:00
|
|
|
.unwrap();
|
2024-03-18 07:24:41 +00:00
|
|
|
}
|
2024-03-07 03:15:35 +00:00
|
|
|
}
|
2024-03-06 20:36:58 +00:00
|
|
|
}
|
|
|
|
}
|