Chapter 10 done

This commit is contained in:
Moritz Gmeiner 2023-01-25 19:01:13 +01:00
commit 1b216c0e4e
16 changed files with 1196 additions and 201 deletions

View file

@ -1,8 +1,7 @@
use std::io::Write;
use crate::error::LoxError;
use crate::interpreter::eval::execute;
use crate::interpreter::Value;
use crate::interpreter::interpret::execute;
use crate::lexer::{scan_tokens, Token};
use crate::parser::parse_tokens;
@ -34,11 +33,15 @@ fn run_file(script_path: &str) {
let mut env = Environment::new();
if let Err(err) = run(&source_code, &mut env) {
eprintln!("{err}");
match err {
LoxError::LexerError { .. } | LoxError::ParserError { .. } => std::process::exit(65),
LoxError::EvalError { .. } => std::process::exit(70),
match run(&source_code, &mut env) {
Ok(()) => std::process::exit(0),
Err(err) => {
eprintln!("{err}");
match err {
LoxError::LexerError { .. } | LoxError::ParserError { .. } => std::process::exit(65),
LoxError::RuntimeError { .. } => std::process::exit(70),
LoxError::Exit { exit_code } => std::process::exit(exit_code),
}
}
}
}
@ -48,7 +51,7 @@ fn run_repl() {
let mut env = Environment::new();
'outer: loop {
loop {
let mut input_buf = String::new();
print!("> ");
@ -60,7 +63,7 @@ fn run_repl() {
std::process::exit(66);
});
/* let num_open_braces = (input_buf.matches('{').count() as i64) - (input_buf.matches('}').count() as i64);
let num_open_braces = (input_buf.matches('{').count() as i64) - (input_buf.matches('}').count() as i64);
let num_open_parens = (input_buf.matches('(').count() as i64) - (input_buf.matches(')').count() as i64);
let num_open_brackets = (input_buf.matches('[').count() as i64) - (input_buf.matches(']').count() as i64);
@ -72,22 +75,21 @@ fn run_repl() {
// any braces/parens/brackets more closing than opening => break (will be parse error)
if num_open_braces < 0 || num_open_parens < 0 || num_open_brackets < 0 {
break 'inner;
} */
break 'inner;
}
print!("< ");
std::io::stdout().flush().unwrap();
}
input_buf = input_buf.trim().to_owned();
let input_buf = input_buf.trim();
if input_buf.is_empty() || input_buf == "exit" || input_buf == "quit" {
break 'outer;
std::process::exit(0);
}
match run(&input_buf, &mut env) {
match run(input_buf, &mut env) {
Ok(()) => {}
Err(LoxError::Exit { exit_code }) => std::process::exit(exit_code),
Err(err) => eprintln!("{}", err),
}
}
@ -106,7 +108,9 @@ fn run(code_string: &str, env: &mut Environment) -> Result<(), LoxError> {
let statements = parse_tokens(tokens)?;
// println!("{expr}");
/* for statement in statements.iter() {
println!("{statement}");
} */
// let mut result = Value::Nil;