dust/dust-shell/src/main.rs

91 lines
2.4 KiB
Rust
Raw Normal View History

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-23 21:07:41 +00:00
mod error;
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-03-23 21:07:41 +00:00
use error::Error;
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-23 21:07:41 +00:00
rc::Rc,
2024-03-20 08:42:13 +00:00
};
2024-03-06 20:36:58 +00:00
2024-03-23 12:15:48 +00:00
use dust_lang::{context::Context, interpret};
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>,
/// 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();
let log_level = record.level().to_string().bold();
let timestamp = buffer.timestamp_seconds().to_string().dimmed();
writeln!(buffer, "[{log_level} {timestamp}] {args}")
})
.init();
let args = Args::parse();
let context = Context::new();
2024-03-23 21:07:41 +00:00
let (source, source_id) = if let Some(path) = args.path {
(read_to_string(&path).unwrap(), Rc::new(path))
2024-03-06 20:36:58 +00:00
} else if let Some(command) = args.command {
2024-03-23 21:07:41 +00:00
(command, Rc::new("input".to_string()))
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-03-23 12:15:48 +00:00
let eval_result = interpret(&source);
2024-03-06 20:36:58 +00:00
match eval_result {
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-07 03:15:35 +00:00
Err(errors) => {
2024-03-23 21:07:41 +00:00
let reports = Error::Dust { errors }
2024-03-23 23:12:18 +00:00
.build_reports(source_id.clone())
2024-03-23 21:07:41 +00:00
.unwrap();
2024-03-20 08:42:13 +00:00
2024-03-23 21:07:41 +00:00
for report in reports {
report
2024-03-23 23:12:18 +00:00
.write_for_stdout(
sources([
(source_id.clone(), source.as_str()),
(
Rc::new("std/io.ds".to_string()),
include_str!("../../std/io.ds"),
),
(
Rc::new("std/thread.ds".to_string()),
include_str!("../../std/thread.ds"),
),
]),
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
}
}