refactor(tazjin/rlox): Retain interpreter state in REPL
Change-Id: Id60760e241ad0e45871b48e499f58e9831d57316 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2298 Tested-by: BuildkiteCI Reviewed-by: tazjin <mail@tazj.in>
This commit is contained in:
		
							parent
							
								
									78355d3c0b
								
							
						
					
					
						commit
						8ab66f1689
					
				
					 2 changed files with 30 additions and 30 deletions
				
			
		|  | @ -1,33 +1,8 @@ | |||
| use crate::errors::{report, Error, ErrorKind}; | ||||
| use crate::errors::{Error, ErrorKind}; | ||||
| use crate::parser::{self, Declaration, Expr, Literal, Program, Statement}; | ||||
| use crate::scanner::{self, TokenKind}; | ||||
| use crate::scanner::TokenKind; | ||||
| use std::collections::HashMap; | ||||
| 
 | ||||
| // Run some Lox code and print it to stdout
 | ||||
| pub fn run(code: &str) { | ||||
|     let chars: Vec<char> = code.chars().collect(); | ||||
| 
 | ||||
|     match scanner::scan(&chars) { | ||||
|         Ok(tokens) => match parser::parse(tokens) { | ||||
|             Ok(program) => { | ||||
|                 let mut interpreter = Interpreter::default(); | ||||
|                 println!("Program:\n{:?}", program); | ||||
|                 if let Err(err) = interpreter.interpret(&program) { | ||||
|                     println!("Error in program: {:?}", err); | ||||
|                 } | ||||
|             } | ||||
|             Err(errors) => report_errors(errors), | ||||
|         }, | ||||
|         Err(errors) => report_errors(errors), | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| fn report_errors(errors: Vec<Error>) { | ||||
|     for error in errors { | ||||
|         report(&error); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| // Tree-walk interpreter
 | ||||
| 
 | ||||
| #[derive(Debug, Default)] | ||||
|  | @ -60,7 +35,7 @@ impl Environment { | |||
| } | ||||
| 
 | ||||
| #[derive(Debug, Default)] | ||||
| struct Interpreter { | ||||
| pub struct Interpreter { | ||||
|     globals: Environment, | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -25,12 +25,14 @@ fn main() { | |||
| // Run Lox code from a file and print results to stdout
 | ||||
| fn run_file(file: &str) { | ||||
|     let contents = fs::read_to_string(file).expect("failed to read the input file"); | ||||
|     interpreter::run(&contents); | ||||
|     let mut lox = interpreter::Interpreter::default(); | ||||
|     run(&mut lox, &contents); | ||||
| } | ||||
| 
 | ||||
| // Evaluate Lox code interactively in a shitty REPL.
 | ||||
| fn run_prompt() { | ||||
|     let mut line = String::new(); | ||||
|     let mut lox = interpreter::Interpreter::default(); | ||||
| 
 | ||||
|     loop { | ||||
|         print!("> "); | ||||
|  | @ -38,7 +40,30 @@ fn run_prompt() { | |||
|         io::stdin() | ||||
|             .read_line(&mut line) | ||||
|             .expect("failed to read user input"); | ||||
|         interpreter::run(&line); | ||||
|         run(&mut lox, &line); | ||||
|         line.clear(); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| fn run(lox: &mut interpreter::Interpreter, code: &str) { | ||||
|     let chars: Vec<char> = code.chars().collect(); | ||||
| 
 | ||||
|     match scanner::scan(&chars) { | ||||
|         Ok(tokens) => match parser::parse(tokens) { | ||||
|             Ok(program) => { | ||||
|                 println!("Program:\n{:?}", program); | ||||
|                 if let Err(err) = lox.interpret(&program) { | ||||
|                     println!("Error in program: {:?}", err); | ||||
|                 } | ||||
|             } | ||||
|             Err(errors) => report_errors(errors), | ||||
|         }, | ||||
|         Err(errors) => report_errors(errors), | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| fn report_errors(errors: Vec<errors::Error>) { | ||||
|     for error in errors { | ||||
|         errors::report(&error); | ||||
|     } | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue