examples/interpret: Improve the repl's UX somewhat
This commit is contained in:
parent
8fe89e6297
commit
9a6eb6b0f1
@ -1,5 +1,10 @@
|
|||||||
//! This example grabs input from stdin or a file, lexes it, parses it, and interprets it
|
//! This example grabs input from stdin or a file, lexes it, parses it, and interprets it
|
||||||
use conlang::{interpreter::Interpreter, lexer::Lexer, parser::Parser};
|
use conlang::{
|
||||||
|
ast::{expression::Expr, Start},
|
||||||
|
interpreter::Interpreter,
|
||||||
|
lexer::Lexer,
|
||||||
|
parser::Parser,
|
||||||
|
};
|
||||||
use std::{
|
use std::{
|
||||||
error::Error,
|
error::Error,
|
||||||
io::{stdin, stdout, IsTerminal, Write},
|
io::{stdin, stdout, IsTerminal, Write},
|
||||||
@ -28,27 +33,48 @@ impl Config {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! prompt {
|
||||||
|
($($t:tt)*) => {{
|
||||||
|
let mut out = stdout().lock();
|
||||||
|
out.write_all(b"\x1b[36m")?;
|
||||||
|
write!(out, $($t)*)?;
|
||||||
|
out.write_all(b"\x1b[0m")?;
|
||||||
|
out.flush()
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
fn take_stdin() -> Result<(), Box<dyn Error>> {
|
fn take_stdin() -> Result<(), Box<dyn Error>> {
|
||||||
const PROMPT: &str = "> ";
|
const CONLANG: &str = "cl>";
|
||||||
|
const MOREPLS: &str = " ?>";
|
||||||
if stdin().is_terminal() {
|
if stdin().is_terminal() {
|
||||||
let mut interpreter = Interpreter::new();
|
let mut interpreter = Interpreter::new();
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
print!("{PROMPT}");
|
prompt!("{CONLANG} ")?;
|
||||||
stdout().flush()?;
|
|
||||||
while let Ok(len) = stdin().read_line(&mut buf) {
|
while let Ok(len) = stdin().read_line(&mut buf) {
|
||||||
match len {
|
let code = Program::parse(&buf);
|
||||||
0 => {
|
match (len, code) {
|
||||||
|
// Exit the loop
|
||||||
|
(0, _) => {
|
||||||
println!();
|
println!();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
1 => {
|
// If the code is OK, run it and print any errors
|
||||||
let _ = run(&buf, &mut interpreter).map_err(|e| eprintln!("{e}"));
|
(_, Ok(code)) => {
|
||||||
print!("\n{PROMPT}");
|
let _ = code.run(&mut interpreter).map_err(|e| eprintln!("{e}"));
|
||||||
buf.clear();
|
buf.clear();
|
||||||
}
|
}
|
||||||
_ => print!(". "),
|
// If the user types two newlines, print syntax errors
|
||||||
|
(1, Err(e)) => {
|
||||||
|
eprintln!("{e}");
|
||||||
|
buf.clear();
|
||||||
}
|
}
|
||||||
stdout().flush()?;
|
// Otherwise, ask for more input
|
||||||
|
_ => {
|
||||||
|
prompt!("{MOREPLS} ")?;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
prompt!("{CONLANG} ")?;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
run_file(&std::io::read_to_string(stdin())?, None)?
|
run_file(&std::io::read_to_string(stdin())?, None)?
|
||||||
@ -66,16 +92,28 @@ fn run_file(file: &str, path: Option<&Path>) -> Result<(), Box<dyn Error>> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(input: &str, interpreter: &mut Interpreter) -> Result<(), Box<dyn Error>> {
|
enum Program {
|
||||||
// If it parses successfully as a program, run the program
|
Program(Start),
|
||||||
|
Expr(Expr),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Program {
|
||||||
|
fn parse(input: &str) -> conlang::parser::error::PResult<Self> {
|
||||||
let ast = Parser::from(Lexer::new(input)).parse();
|
let ast = Parser::from(Lexer::new(input)).parse();
|
||||||
if let Ok(ast) = ast {
|
if let Ok(ast) = ast {
|
||||||
interpreter.interpret(&ast)?;
|
return Ok(Self::Program(ast));
|
||||||
return Ok(());
|
|
||||||
}
|
}
|
||||||
let ast = Parser::from(Lexer::new(input)).parse_expr()?;
|
Ok(Self::Expr(Parser::from(Lexer::new(input)).parse_expr()?))
|
||||||
for value in interpreter.eval(&ast)? {
|
}
|
||||||
println!("{value}");
|
fn run(&self, interpreter: &mut Interpreter) -> conlang::interpreter::error::IResult<()> {
|
||||||
|
match self {
|
||||||
|
Program::Program(start) => interpreter.interpret(start),
|
||||||
|
Program::Expr(expr) => {
|
||||||
|
for value in interpreter.eval(expr)? {
|
||||||
|
println!("{value}")
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user