nushell/src/main.rs

232 lines
8.1 KiB
Rust
Raw Normal View History

2021-07-30 08:06:48 +00:00
use std::{cell::RefCell, rc::Rc};
2021-07-17 06:31:34 +00:00
2021-07-23 05:14:49 +00:00
use engine_q::{
2021-07-30 08:06:48 +00:00
eval_block, NuHighlighter, ParserState, ParserWorkingSet, Signature, Stack, State, SyntaxShape,
2021-07-23 05:14:49 +00:00
};
2021-06-30 01:42:56 +00:00
fn main() -> std::io::Result<()> {
let parser_state = Rc::new(RefCell::new(ParserState::new()));
let delta = {
let parser_state = parser_state.borrow();
let mut working_set = ParserWorkingSet::new(&*parser_state);
let sig =
Signature::build("where").required("cond", SyntaxShape::RowCondition, "condition");
working_set.add_decl(sig.into());
let sig = Signature::build("if")
2021-07-24 05:57:17 +00:00
.required("cond", SyntaxShape::Expression, "condition")
.required("then_block", SyntaxShape::Block, "then block")
2021-07-24 05:57:17 +00:00
.optional(
"else",
2021-07-24 06:44:38 +00:00
SyntaxShape::Keyword(b"else".to_vec(), Box::new(SyntaxShape::Expression)),
2021-07-24 05:57:17 +00:00
"optional else followed by else block",
);
working_set.add_decl(sig.into());
let sig = Signature::build("let")
2021-07-30 07:30:11 +00:00
.required("var_name", SyntaxShape::VarWithOptType, "variable name")
.required(
2021-07-24 05:57:17 +00:00
"initial_value",
SyntaxShape::Keyword(b"=".to_vec(), Box::new(SyntaxShape::Expression)),
"equals sign followed by value",
);
working_set.add_decl(sig.into());
2021-08-08 21:02:47 +00:00
let sig = Signature::build("let-env")
.required("var_name", SyntaxShape::String, "variable name")
.required(
"initial_value",
SyntaxShape::Keyword(b"=".to_vec(), Box::new(SyntaxShape::String)),
"equals sign followed by value",
);
working_set.add_decl(sig.into());
let sig = Signature::build("alias")
2021-08-09 00:19:07 +00:00
.required("name", SyntaxShape::String, "name of the alias")
.required(
2021-07-24 05:57:17 +00:00
"initial_value",
SyntaxShape::Keyword(b"=".to_vec(), Box::new(SyntaxShape::Expression)),
"equals sign followed by value",
);
working_set.add_decl(sig.into());
let sig = Signature::build("sum").required(
"arg",
SyntaxShape::List(Box::new(SyntaxShape::Number)),
"list of numbers",
2021-07-08 07:49:17 +00:00
);
working_set.add_decl(sig.into());
2021-07-30 03:26:06 +00:00
let sig = Signature::build("build-string").rest(SyntaxShape::String, "list of string");
working_set.add_decl(sig.into());
let sig = Signature::build("def")
.required("def_name", SyntaxShape::String, "definition name")
.required("params", SyntaxShape::Signature, "parameters")
.required("block", SyntaxShape::Block, "body of the definition");
2021-07-30 06:10:40 +00:00
working_set.add_decl(sig.into());
let sig = Signature::build("for")
.required(
"var_name",
SyntaxShape::Variable,
"name of the looping variable",
)
.required(
"range",
SyntaxShape::Keyword(b"in".to_vec(), Box::new(SyntaxShape::Int)),
"range of the loop",
)
.required("block", SyntaxShape::Block, "the block to run");
working_set.add_decl(sig.into());
let sig =
Signature::build("benchmark").required("block", SyntaxShape::Block, "the block to run");
working_set.add_decl(sig.into());
// let sig = Signature::build("foo").named("--jazz", SyntaxShape::Int, "jazz!!", Some('j'));
// working_set.add_decl(sig.into());
// let sig = Signature::build("bar")
// .named("--jazz", SyntaxShape::Int, "jazz!!", Some('j'))
// .switch("--rock", "rock!!", Some('r'));
// working_set.add_decl(sig.into());
2021-07-23 21:19:30 +00:00
let sig = Signature::build("exit");
working_set.add_decl(sig.into());
let sig = Signature::build("vars");
working_set.add_decl(sig.into());
let sig = Signature::build("decls");
working_set.add_decl(sig.into());
let sig = Signature::build("blocks");
working_set.add_decl(sig.into());
2021-07-30 05:42:33 +00:00
let sig = Signature::build("stack");
working_set.add_decl(sig.into());
let sig = Signature::build("add");
working_set.add_decl(sig.into());
let sig = Signature::build("add it");
working_set.add_decl(sig.into());
let sig = Signature::build("add it together")
.required("x", SyntaxShape::Int, "x value")
.required("y", SyntaxShape::Int, "y value");
working_set.add_decl(sig.into());
2021-07-22 07:48:45 +00:00
working_set.render()
};
{
ParserState::merge_delta(&mut *parser_state.borrow_mut(), delta);
}
2021-07-17 06:31:34 +00:00
if let Some(path) = std::env::args().nth(1) {
2021-07-30 21:26:05 +00:00
let parser_state = parser_state;
2021-07-23 05:14:49 +00:00
let file = std::fs::read(&path)?;
2021-07-30 21:26:05 +00:00
let (block, delta) = {
let parser_state = parser_state.borrow();
let mut working_set = ParserWorkingSet::new(&*parser_state);
let (output, err) = working_set.parse_file(&path, &file, false);
if let Some(err) = err {
2021-07-30 21:57:22 +00:00
eprintln!("Parse Error: {:?}", err);
2021-07-30 21:26:05 +00:00
std::process::exit(1);
}
(output, working_set.render())
};
2021-07-16 06:24:46 +00:00
2021-07-30 21:26:05 +00:00
ParserState::merge_delta(&mut *parser_state.borrow_mut(), delta);
2021-07-30 20:02:16 +00:00
let state = State {
2021-07-30 21:26:05 +00:00
parser_state: &*parser_state.borrow(),
2021-07-30 20:02:16 +00:00
};
2021-07-30 20:02:16 +00:00
let stack = Stack::new();
2021-07-30 20:02:16 +00:00
match eval_block(&state, stack, &block) {
Ok(value) => {
println!("{}", value);
}
Err(err) => {
2021-07-30 21:57:22 +00:00
eprintln!("Eval Error: {:?}", err);
2021-07-30 20:02:16 +00:00
std::process::exit(1);
}
}
2021-06-30 01:42:56 +00:00
Ok(())
} else {
use reedline::{DefaultPrompt, FileBackedHistory, Reedline, Signal};
2021-07-22 19:50:59 +00:00
let mut line_editor = Reedline::new()
2021-07-22 20:45:23 +00:00
.with_history(Box::new(FileBackedHistory::with_file(
1000,
"history.txt".into(),
)?))?
2021-07-22 19:50:59 +00:00
.with_highlighter(Box::new(NuHighlighter {
parser_state: parser_state.clone(),
}));
let prompt = DefaultPrompt::new(1);
let mut current_line = 1;
2021-07-30 08:06:48 +00:00
let stack = Stack::new();
loop {
2021-08-09 05:29:25 +00:00
let input = line_editor.read_line(&prompt);
match input {
2021-08-09 05:29:25 +00:00
Ok(Signal::Success(s)) => {
if s.trim() == "exit" {
break;
}
2021-07-23 05:14:49 +00:00
// println!("input: '{}'", s);
2021-07-23 05:14:49 +00:00
let (block, delta) = {
let parser_state = parser_state.borrow();
let mut working_set = ParserWorkingSet::new(&*parser_state);
let (output, err) = working_set.parse_file(
&format!("line_{}", current_line),
s.as_bytes(),
false,
);
2021-07-23 05:14:49 +00:00
if let Some(err) = err {
2021-07-30 21:57:22 +00:00
eprintln!("Parse Error: {:?}", err);
2021-07-31 05:20:40 +00:00
continue;
2021-07-23 05:14:49 +00:00
}
(output, working_set.render())
};
ParserState::merge_delta(&mut *parser_state.borrow_mut(), delta);
2021-07-23 05:14:49 +00:00
let state = State {
parser_state: &*parser_state.borrow(),
};
2021-07-30 20:02:16 +00:00
match eval_block(&state, stack.clone(), &block) {
Ok(value) => {
println!("{}", value);
}
Err(err) => {
2021-07-30 21:57:22 +00:00
eprintln!("Eval Error: {:?}", err);
2021-07-30 20:02:16 +00:00
}
}
}
2021-08-09 05:29:25 +00:00
Ok(Signal::CtrlC) => {
println!("Ctrl-c");
}
2021-08-09 05:29:25 +00:00
Ok(Signal::CtrlD) => {
break;
}
2021-08-09 05:29:25 +00:00
Ok(Signal::CtrlL) => {
line_editor.clear_screen()?;
}
2021-08-09 05:29:25 +00:00
Err(err) => {
println!("Error: {:?}", err);
}
}
current_line += 1;
}
2021-06-30 01:42:56 +00:00
Ok(())
}
}