nushell/src/main.rs

218 lines
7.8 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());
let sig = Signature::build("alias")
.required("var_name", SyntaxShape::Variable, "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());
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) {
let parser_state = parser_state.borrow();
let mut working_set = ParserWorkingSet::new(&*parser_state);
2021-07-23 05:14:49 +00:00
let file = std::fs::read(&path)?;
2021-07-23 21:19:30 +00:00
let (block, _err) = working_set.parse_file(&path, &file, false);
2021-07-23 05:14:49 +00:00
println!("{}", block.len());
// println!("{:#?}", output);
// println!("error: {:?}", err);
2021-07-16 06:24:46 +00:00
2021-07-16 20:26:40 +00:00
//println!("working set: {:#?}", working_set);
2021-07-16 06:24:46 +00:00
// println!("{}", size_of::<Statement>());
2021-07-16 06:24:46 +00:00
// let engine = Engine::new();
// let result = engine.eval_block(&output);
// println!("{:?}", result);
// let mut buffer = String::new();
// let stdin = std::io::stdin();
// let mut handle = stdin.lock();
// handle.read_to_string(&mut buffer)?;
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 {
let input = line_editor.read_line(&prompt)?;
match input {
Signal::Success(s) => {
if s.trim() == "exit" {
break;
2021-07-23 21:19:30 +00:00
} else if s.trim() == "vars" {
parser_state.borrow().print_vars();
continue;
} else if s.trim() == "decls" {
parser_state.borrow().print_decls();
continue;
} else if s.trim() == "blocks" {
parser_state.borrow().print_blocks();
continue;
2021-07-30 05:42:33 +00:00
} else if s.trim() == "stack" {
2021-07-30 08:06:48 +00:00
stack.print_stack();
}
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-24 05:57:17 +00:00
println!("{:?}", output);
2021-07-23 05:14:49 +00:00
if let Some(err) = err {
println!("Error: {:?}", err);
2021-07-23 21:46:55 +00:00
continue;
2021-07-23 05:14:49 +00:00
}
2021-07-29 22:56:51 +00:00
println!("Error: {:?}", err);
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 05:42:33 +00:00
let output = eval_block(&state, stack.clone(), &block);
2021-07-23 05:14:49 +00:00
println!("{:#?}", output);
}
Signal::CtrlC => {
println!("Ctrl-c");
}
Signal::CtrlD => {
break;
}
Signal::CtrlL => {
line_editor.clear_screen()?;
}
}
current_line += 1;
}
2021-06-30 01:42:56 +00:00
Ok(())
}
}