2023-10-26 20:00:06 +00:00
|
|
|
//! Command line interface for the dust programming language.
|
2023-08-22 15:40:50 +00:00
|
|
|
use clap::Parser;
|
2023-08-28 20:14:55 +00:00
|
|
|
use rustyline::{
|
|
|
|
completion::FilenameCompleter,
|
|
|
|
error::ReadlineError,
|
|
|
|
highlight::Highlighter,
|
|
|
|
hint::{Hint, Hinter, HistoryHinter},
|
|
|
|
history::DefaultHistory,
|
2023-08-28 21:24:15 +00:00
|
|
|
Completer, Context, Editor, Helper, Validator,
|
2023-08-22 15:40:50 +00:00
|
|
|
};
|
2023-11-15 00:37:19 +00:00
|
|
|
use tree_sitter::Parser as TSParser;
|
2023-08-22 15:40:50 +00:00
|
|
|
|
2023-11-18 01:10:07 +00:00
|
|
|
use std::{borrow::Cow, fs::read_to_string};
|
2023-08-22 15:40:50 +00:00
|
|
|
|
2023-12-29 19:01:54 +00:00
|
|
|
use dust_lang::{interpret_with_context, language, Interpreter, Map, Value};
|
2023-08-22 15:40:50 +00:00
|
|
|
|
|
|
|
/// Command-line arguments to be parsed.
|
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
#[command(author, version, about, long_about = None)]
|
|
|
|
struct Args {
|
2023-10-26 20:00:06 +00:00
|
|
|
/// Dust source code to evaluate.
|
2023-08-22 15:40:50 +00:00
|
|
|
#[arg(short, long)]
|
|
|
|
command: Option<String>,
|
|
|
|
|
2023-10-26 20:00:06 +00:00
|
|
|
/// Data to assign to the "input" variable.
|
|
|
|
#[arg(short, long)]
|
|
|
|
input: Option<String>,
|
|
|
|
|
|
|
|
/// A path to file whose contents will be assigned to the "input" variable.
|
|
|
|
#[arg(short = 'p', long)]
|
|
|
|
input_path: Option<String>,
|
|
|
|
|
2023-11-30 00:23:42 +00:00
|
|
|
/// Show the syntax tree.
|
2023-11-15 00:37:19 +00:00
|
|
|
#[arg(short = 't', long = "tree")]
|
|
|
|
show_syntax_tree: bool,
|
|
|
|
|
2023-11-30 00:23:42 +00:00
|
|
|
/// Launch in interactive mode.
|
2023-11-30 01:59:58 +00:00
|
|
|
#[arg(short = 'n', long)]
|
2023-11-30 00:23:42 +00:00
|
|
|
interactive: bool,
|
|
|
|
|
2023-08-23 04:39:46 +00:00
|
|
|
/// Location of the file to run.
|
|
|
|
path: Option<String>,
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
|
2023-11-18 01:10:07 +00:00
|
|
|
fn main() {
|
2023-08-22 15:40:50 +00:00
|
|
|
let args = Args::parse();
|
|
|
|
|
2023-08-28 18:28:59 +00:00
|
|
|
if args.path.is_none() && args.command.is_none() {
|
2023-08-22 15:40:50 +00:00
|
|
|
return run_cli_shell();
|
|
|
|
}
|
|
|
|
|
2023-10-30 22:30:41 +00:00
|
|
|
let source = if let Some(path) = &args.path {
|
2023-11-18 01:10:07 +00:00
|
|
|
read_to_string(path).unwrap()
|
2023-10-30 22:30:41 +00:00
|
|
|
} else if let Some(command) = &args.command {
|
|
|
|
command.clone()
|
|
|
|
} else {
|
|
|
|
"".to_string()
|
|
|
|
};
|
|
|
|
|
2023-12-29 19:52:51 +00:00
|
|
|
let context = Map::new();
|
2023-10-26 20:00:06 +00:00
|
|
|
|
2023-10-28 14:28:43 +00:00
|
|
|
if let Some(input) = args.input {
|
|
|
|
context
|
2023-12-13 20:47:41 +00:00
|
|
|
.set("input".to_string(), Value::String(input), None)
|
|
|
|
.unwrap();
|
2023-10-28 14:28:43 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 20:00:06 +00:00
|
|
|
if let Some(path) = args.input_path {
|
2023-11-18 01:10:07 +00:00
|
|
|
let file_contents = read_to_string(path).unwrap();
|
2023-10-26 20:00:06 +00:00
|
|
|
|
2023-12-13 20:47:41 +00:00
|
|
|
context
|
|
|
|
.set("input".to_string(), Value::String(file_contents), None)
|
|
|
|
.unwrap();
|
2023-10-26 20:00:06 +00:00
|
|
|
}
|
|
|
|
|
2023-11-15 00:37:19 +00:00
|
|
|
let mut parser = TSParser::new();
|
|
|
|
parser.set_language(language()).unwrap();
|
|
|
|
|
2023-12-29 19:52:51 +00:00
|
|
|
let mut interpreter = Interpreter::new(context, &source).unwrap();
|
2023-11-30 00:23:42 +00:00
|
|
|
|
|
|
|
if args.interactive {
|
|
|
|
loop {
|
|
|
|
let result = interpreter.run();
|
|
|
|
|
|
|
|
println!("{result:?}")
|
|
|
|
}
|
|
|
|
}
|
2023-11-15 00:37:19 +00:00
|
|
|
|
|
|
|
if args.show_syntax_tree {
|
2023-12-29 19:35:52 +00:00
|
|
|
interpreter.parse_only();
|
|
|
|
|
|
|
|
println!("{}", interpreter.syntax_tree().unwrap());
|
2023-11-15 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 00:23:42 +00:00
|
|
|
let eval_result = interpreter.run();
|
2023-08-22 15:40:50 +00:00
|
|
|
|
2023-10-09 19:54:47 +00:00
|
|
|
match eval_result {
|
2023-10-24 00:45:47 +00:00
|
|
|
Ok(value) => {
|
2023-12-22 20:02:22 +00:00
|
|
|
if !value.is_none() {
|
2023-10-24 00:45:47 +00:00
|
|
|
println!("{value}")
|
|
|
|
}
|
|
|
|
}
|
2023-10-09 19:54:47 +00:00
|
|
|
Err(error) => eprintln!("{error}"),
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-28 20:14:55 +00:00
|
|
|
#[derive(Helper, Completer, Validator)]
|
|
|
|
struct DustReadline {
|
|
|
|
#[rustyline(Completer)]
|
|
|
|
completer: FilenameCompleter,
|
2023-08-22 15:40:50 +00:00
|
|
|
|
2023-08-28 20:14:55 +00:00
|
|
|
tool_hints: Vec<ToolHint>,
|
2023-08-22 15:40:50 +00:00
|
|
|
|
2023-08-28 20:14:55 +00:00
|
|
|
#[rustyline(Hinter)]
|
2023-08-28 21:24:15 +00:00
|
|
|
_hinter: HistoryHinter,
|
2023-08-28 20:14:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DustReadline {
|
|
|
|
fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
completer: FilenameCompleter::new(),
|
2023-08-28 21:24:15 +00:00
|
|
|
_hinter: HistoryHinter {},
|
2023-10-02 21:15:05 +00:00
|
|
|
tool_hints: Vec::new(),
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-28 20:14:55 +00:00
|
|
|
struct ToolHint {
|
|
|
|
display: String,
|
|
|
|
complete_to: usize,
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 20:14:55 +00:00
|
|
|
impl Hint for ToolHint {
|
|
|
|
fn display(&self) -> &str {
|
|
|
|
&self.display
|
|
|
|
}
|
|
|
|
|
|
|
|
fn completion(&self) -> Option<&str> {
|
|
|
|
if self.complete_to > 0 {
|
|
|
|
Some(&self.display[..self.complete_to])
|
|
|
|
} else {
|
|
|
|
None
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-28 20:14:55 +00:00
|
|
|
}
|
2023-08-22 15:40:50 +00:00
|
|
|
|
2023-08-28 20:14:55 +00:00
|
|
|
impl ToolHint {
|
|
|
|
fn suffix(&self, strip_chars: usize) -> ToolHint {
|
|
|
|
ToolHint {
|
|
|
|
display: self.display[strip_chars..].to_string(),
|
|
|
|
complete_to: self.complete_to.saturating_sub(strip_chars),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-22 15:40:50 +00:00
|
|
|
|
2023-08-28 20:14:55 +00:00
|
|
|
impl Hinter for DustReadline {
|
|
|
|
type Hint = ToolHint;
|
2023-08-22 15:40:50 +00:00
|
|
|
|
2023-08-28 20:14:55 +00:00
|
|
|
fn hint(&self, line: &str, pos: usize, _ctx: &Context<'_>) -> Option<Self::Hint> {
|
|
|
|
if line.is_empty() || pos < line.len() {
|
|
|
|
return None;
|
|
|
|
}
|
2023-08-22 15:40:50 +00:00
|
|
|
|
2023-08-28 20:14:55 +00:00
|
|
|
self.tool_hints.iter().find_map(|tool_hint| {
|
|
|
|
if tool_hint.display.starts_with(line) {
|
|
|
|
Some(tool_hint.suffix(pos))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
2023-08-28 20:14:55 +00:00
|
|
|
}
|
2023-08-22 15:40:50 +00:00
|
|
|
|
2023-08-28 20:14:55 +00:00
|
|
|
impl Highlighter for DustReadline {
|
|
|
|
fn highlight_hint<'h>(&self, hint: &'h str) -> std::borrow::Cow<'h, str> {
|
2023-08-28 21:15:05 +00:00
|
|
|
let highlighted = ansi_term::Colour::Red.paint(hint).to_string();
|
2023-08-22 15:40:50 +00:00
|
|
|
|
2023-08-28 21:15:05 +00:00
|
|
|
Cow::Owned(highlighted)
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-28 20:14:55 +00:00
|
|
|
fn run_cli_shell() {
|
2023-12-29 19:52:51 +00:00
|
|
|
let context = Map::new();
|
2023-08-28 20:14:55 +00:00
|
|
|
let mut rl: Editor<DustReadline, DefaultHistory> = Editor::new().unwrap();
|
|
|
|
|
|
|
|
rl.set_helper(Some(DustReadline::new()));
|
2023-08-22 15:40:50 +00:00
|
|
|
|
2023-08-28 20:14:55 +00:00
|
|
|
if rl.load_history("target/history.txt").is_err() {
|
|
|
|
println!("No previous history.");
|
|
|
|
}
|
|
|
|
|
|
|
|
loop {
|
2023-08-28 21:24:15 +00:00
|
|
|
let readline = rl.readline("* ");
|
2023-08-28 20:14:55 +00:00
|
|
|
match readline {
|
|
|
|
Ok(line) => {
|
|
|
|
let line = line.as_str();
|
|
|
|
|
|
|
|
rl.add_history_entry(line).unwrap();
|
|
|
|
|
2023-12-29 19:52:51 +00:00
|
|
|
let eval_result = interpret_with_context(line, context.clone());
|
2023-08-28 20:14:55 +00:00
|
|
|
|
2023-10-09 19:54:47 +00:00
|
|
|
match eval_result {
|
|
|
|
Ok(value) => println!("{value}"),
|
|
|
|
Err(error) => eprintln!("{error}"),
|
2023-08-28 20:14:55 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-09 19:54:47 +00:00
|
|
|
Err(ReadlineError::Interrupted) => break,
|
|
|
|
Err(ReadlineError::Eof) => break,
|
2023-08-28 21:24:15 +00:00
|
|
|
Err(error) => eprintln!("{error}"),
|
2023-08-28 20:14:55 +00:00
|
|
|
}
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 20:14:55 +00:00
|
|
|
rl.save_history("target/history.txt").unwrap();
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|