2021-07-23 07:14:49 +02:00
|
|
|
use std::{cell::RefCell, collections::HashMap, rc::Rc};
|
2021-07-17 08:31:34 +02:00
|
|
|
|
2021-07-23 07:14:49 +02:00
|
|
|
use engine_q::{
|
|
|
|
eval_block, NuHighlighter, ParserState, ParserWorkingSet, Signature, Stack, State, SyntaxShape,
|
|
|
|
};
|
2021-06-30 03:42:56 +02:00
|
|
|
|
|
|
|
fn main() -> std::io::Result<()> {
|
2021-07-22 09:33:38 +02:00
|
|
|
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")
|
|
|
|
.required("cond", SyntaxShape::RowCondition, "condition")
|
|
|
|
.required("then_block", SyntaxShape::Block, "then block")
|
|
|
|
.required(
|
|
|
|
"else",
|
|
|
|
SyntaxShape::Literal(b"else".to_vec()),
|
|
|
|
"else keyword",
|
|
|
|
)
|
|
|
|
.required("else_block", SyntaxShape::Block, "else block");
|
|
|
|
working_set.add_decl(sig.into());
|
|
|
|
|
|
|
|
let sig = Signature::build("let")
|
|
|
|
.required("var_name", SyntaxShape::Variable, "variable name")
|
|
|
|
.required("=", SyntaxShape::Literal(b"=".to_vec()), "equals sign")
|
|
|
|
.required(
|
|
|
|
"value",
|
|
|
|
SyntaxShape::Expression,
|
|
|
|
"the value to set the variable to",
|
|
|
|
);
|
|
|
|
working_set.add_decl(sig.into());
|
|
|
|
|
|
|
|
let sig = Signature::build("alias")
|
|
|
|
.required("var_name", SyntaxShape::Variable, "variable name")
|
|
|
|
.required("=", SyntaxShape::Literal(b"=".to_vec()), "equals sign")
|
|
|
|
.required(
|
|
|
|
"value",
|
|
|
|
SyntaxShape::Expression,
|
|
|
|
"the value to set the variable to",
|
|
|
|
);
|
|
|
|
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 09:49:17 +02:00
|
|
|
);
|
2021-07-22 09:33:38 +02:00
|
|
|
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");
|
|
|
|
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());
|
|
|
|
|
|
|
|
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 09:48:45 +02:00
|
|
|
working_set.render()
|
2021-07-22 09:33:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
ParserState::merge_delta(&mut *parser_state.borrow_mut(), delta);
|
|
|
|
}
|
2021-07-17 08:31:34 +02:00
|
|
|
|
2021-07-22 08:04:50 +02:00
|
|
|
if let Some(path) = std::env::args().nth(1) {
|
2021-07-22 09:33:38 +02:00
|
|
|
let parser_state = parser_state.borrow();
|
|
|
|
let mut working_set = ParserWorkingSet::new(&*parser_state);
|
2021-07-23 07:14:49 +02:00
|
|
|
|
|
|
|
let file = std::fs::read(&path)?;
|
|
|
|
|
|
|
|
let (block, err) = working_set.parse_file(&path, &file, false);
|
|
|
|
println!("{}", block.len());
|
|
|
|
// println!("{:#?}", output);
|
|
|
|
// println!("error: {:?}", err);
|
2021-07-16 08:24:46 +02:00
|
|
|
|
2021-07-16 22:26:40 +02:00
|
|
|
//println!("working set: {:#?}", working_set);
|
2021-07-16 08:24:46 +02:00
|
|
|
|
2021-07-03 05:35:15 +02:00
|
|
|
// println!("{}", size_of::<Statement>());
|
|
|
|
|
2021-07-16 08:24:46 +02:00
|
|
|
// let engine = Engine::new();
|
|
|
|
// let result = engine.eval_block(&output);
|
|
|
|
// println!("{:?}", result);
|
2021-07-16 03:10:22 +02:00
|
|
|
|
2021-07-03 05:35:15 +02:00
|
|
|
// let mut buffer = String::new();
|
|
|
|
// let stdin = std::io::stdin();
|
|
|
|
// let mut handle = stdin.lock();
|
|
|
|
|
|
|
|
// handle.read_to_string(&mut buffer)?;
|
2021-06-30 03:42:56 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2021-07-22 08:04:50 +02:00
|
|
|
use reedline::{DefaultPrompt, FileBackedHistory, Reedline, Signal};
|
|
|
|
|
2021-07-22 21:50:59 +02:00
|
|
|
let mut line_editor = Reedline::new()
|
2021-07-22 22:45:23 +02:00
|
|
|
.with_history(Box::new(FileBackedHistory::with_file(
|
|
|
|
1000,
|
|
|
|
"history.txt".into(),
|
|
|
|
)?))?
|
2021-07-22 21:50:59 +02:00
|
|
|
.with_highlighter(Box::new(NuHighlighter {
|
|
|
|
parser_state: parser_state.clone(),
|
|
|
|
}));
|
2021-07-22 08:04:50 +02:00
|
|
|
|
|
|
|
let prompt = DefaultPrompt::new(1);
|
|
|
|
let mut current_line = 1;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let input = line_editor.read_line(&prompt)?;
|
|
|
|
match input {
|
|
|
|
Signal::Success(s) => {
|
|
|
|
if s.trim() == "exit" {
|
|
|
|
break;
|
|
|
|
}
|
2021-07-23 07:14:49 +02:00
|
|
|
// println!("input: '{}'", s);
|
2021-07-22 09:33:38 +02:00
|
|
|
|
2021-07-23 07:14:49 +02:00
|
|
|
let (block, delta) = {
|
2021-07-22 09:33:38 +02:00
|
|
|
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-22 21:50:59 +02:00
|
|
|
println!("{:?}", output);
|
2021-07-23 07:14:49 +02:00
|
|
|
if let Some(err) = err {
|
|
|
|
println!("Error: {:?}", err);
|
|
|
|
}
|
|
|
|
// println!("Error: {:?}", err);
|
|
|
|
(output, working_set.render())
|
2021-07-22 09:33:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ParserState::merge_delta(&mut *parser_state.borrow_mut(), delta);
|
2021-07-23 07:14:49 +02:00
|
|
|
|
|
|
|
let mut stack = Stack {
|
|
|
|
vars: HashMap::new(),
|
|
|
|
};
|
|
|
|
let state = State {
|
|
|
|
parser_state: &*parser_state.borrow(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let output = eval_block(&state, &mut stack, &block);
|
|
|
|
println!("{:#?}", output);
|
2021-07-22 08:04:50 +02:00
|
|
|
}
|
|
|
|
Signal::CtrlC => {
|
|
|
|
println!("Ctrl-c");
|
|
|
|
}
|
|
|
|
Signal::CtrlD => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Signal::CtrlL => {
|
|
|
|
line_editor.clear_screen()?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
current_line += 1;
|
|
|
|
}
|
|
|
|
|
2021-06-30 03:42:56 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|