mirror of
https://github.com/MorizzG/rlox.git
synced 2025-12-06 04:12:42 +00:00
chapter 8 done
This commit is contained in:
parent
f56fcc4a8b
commit
956c4d0f28
13 changed files with 570 additions and 177 deletions
|
|
@ -1,9 +1,12 @@
|
|||
use std::io::Write;
|
||||
|
||||
use crate::error::LoxError;
|
||||
use crate::interpreter::eval::evaluate;
|
||||
use crate::interpreter::eval::execute;
|
||||
use crate::interpreter::Value;
|
||||
use crate::lexer::{scan_tokens, Token};
|
||||
use crate::parser::parser::parse_tokens;
|
||||
use crate::parser::parse_tokens;
|
||||
|
||||
use super::environment::Environment;
|
||||
|
||||
pub fn interpreter_main() {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
|
|
@ -29,7 +32,9 @@ fn run_file(script_path: &str) {
|
|||
std::process::exit(65);
|
||||
} */
|
||||
|
||||
if let Err(err) = run(&source_code) {
|
||||
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),
|
||||
|
|
@ -41,6 +46,8 @@ fn run_file(script_path: &str) {
|
|||
fn run_repl() {
|
||||
let stdin = std::io::stdin();
|
||||
|
||||
let mut env = Environment::new();
|
||||
|
||||
'outer: loop {
|
||||
let mut input_buf = String::new();
|
||||
|
||||
|
|
@ -53,7 +60,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);
|
||||
|
||||
|
|
@ -65,7 +72,9 @@ 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();
|
||||
|
|
@ -77,14 +86,14 @@ fn run_repl() {
|
|||
break 'outer;
|
||||
}
|
||||
|
||||
match run(&input_buf) {
|
||||
match run(&input_buf, &mut env) {
|
||||
Ok(()) => {}
|
||||
Err(err) => eprintln!("{}", err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn run(code_string: &str) -> Result<(), LoxError> {
|
||||
fn run(code_string: &str, env: &mut Environment) -> Result<(), LoxError> {
|
||||
let tokens: Vec<Token> = scan_tokens(code_string)?;
|
||||
|
||||
/* let token_str = tokens
|
||||
|
|
@ -95,13 +104,20 @@ fn run(code_string: &str) -> Result<(), LoxError> {
|
|||
|
||||
println!("{token_str}"); */
|
||||
|
||||
let expr = parse_tokens(tokens)?;
|
||||
let statements = parse_tokens(tokens)?;
|
||||
|
||||
println!("{expr}");
|
||||
// println!("{expr}");
|
||||
|
||||
let result = evaluate(expr)?;
|
||||
// let mut result = Value::Nil;
|
||||
|
||||
println!("{result}");
|
||||
for statement in statements {
|
||||
execute(statement, env)?;
|
||||
}
|
||||
|
||||
/* match result {
|
||||
Value::Nil => {}
|
||||
result => println!("{result}"),
|
||||
} */
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue