2023-10-26 19:48:44 +00:00
|
|
|
//! 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 std::{
|
|
|
|
error::Error,
|
|
|
|
io::{stdin, stdout, IsTerminal, Write},
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
|
|
|
|
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
|
|
let conf = Config::new();
|
|
|
|
if conf.paths.is_empty() {
|
|
|
|
take_stdin()?;
|
|
|
|
} else {
|
|
|
|
for path in conf.paths.iter().map(PathBuf::as_path) {
|
2023-10-27 03:13:48 +00:00
|
|
|
run_file(&std::fs::read_to_string(path)?, Some(path))?;
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Config {
|
|
|
|
paths: Vec<PathBuf>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
|
|
|
fn new() -> Self {
|
|
|
|
Config { paths: std::env::args().skip(1).map(PathBuf::from).collect() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn take_stdin() -> Result<(), Box<dyn Error>> {
|
|
|
|
const PROMPT: &str = "> ";
|
|
|
|
if stdin().is_terminal() {
|
2023-10-27 02:51:18 +00:00
|
|
|
let mut interpreter = Interpreter::new();
|
2023-10-27 03:13:48 +00:00
|
|
|
let mut buf = String::new();
|
2023-10-26 19:48:44 +00:00
|
|
|
print!("{PROMPT}");
|
|
|
|
stdout().flush()?;
|
2023-10-27 03:13:48 +00:00
|
|
|
while let Ok(len) = stdin().read_line(&mut buf) {
|
|
|
|
match len {
|
|
|
|
0 => {
|
|
|
|
println!();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
1 => {
|
|
|
|
let _ = run(&buf, &mut interpreter).map_err(|e| eprintln!("{e}"));
|
2023-10-27 03:33:31 +00:00
|
|
|
print!("\n{PROMPT}");
|
2023-10-27 03:13:48 +00:00
|
|
|
buf.clear();
|
|
|
|
}
|
2023-10-27 03:33:31 +00:00
|
|
|
_ => print!(". "),
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
|
|
|
stdout().flush()?;
|
|
|
|
}
|
|
|
|
} else {
|
2023-10-27 03:13:48 +00:00
|
|
|
run_file(&std::io::read_to_string(stdin())?, None)?
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-10-27 03:13:48 +00:00
|
|
|
fn run_file(file: &str, path: Option<&Path>) -> Result<(), Box<dyn Error>> {
|
2023-10-26 19:48:44 +00:00
|
|
|
match Parser::from(Lexer::new(file)).parse() {
|
|
|
|
Ok(ast) => Interpreter::new().interpret(&ast)?,
|
|
|
|
Err(e) if e.start().is_some() => print!("{:?}:{}", path.unwrap_or(Path::new("-")), e),
|
|
|
|
Err(e) => print!("{e}"),
|
|
|
|
}
|
|
|
|
println!();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-10-27 03:13:48 +00:00
|
|
|
fn run(input: &str, interpreter: &mut Interpreter) -> Result<(), Box<dyn Error>> {
|
2023-10-26 19:48:44 +00:00
|
|
|
// If it parses successfully as a program, run the program
|
2023-10-27 03:13:48 +00:00
|
|
|
let ast = Parser::from(Lexer::new(input)).parse();
|
|
|
|
if let Ok(ast) = ast {
|
|
|
|
interpreter.interpret(&ast)?;
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
let ast = Parser::from(Lexer::new(input)).parse_expr()?;
|
|
|
|
for value in interpreter.eval(&ast)? {
|
|
|
|
println!("{value}");
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|