-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move REPL and filerunner in to piper crate, and implement --use-vm flag
- Loading branch information
1 parent
1a2b80c
commit 3c74331
Showing
9 changed files
with
127 additions
and
77 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use std::fs; | ||
|
||
use interpreter::{ | ||
eval::{self, objects::Environment, EvaledProgram}, | ||
parser::{ParsedProgram, Parser}, | ||
}; | ||
|
||
pub fn execute_file(filename: &str) -> Result<(), std::io::Error> { | ||
let file_content = fs::read_to_string(filename)?; | ||
let execution_scope = &mut Environment::new_env_reference(); | ||
let evaluated_output = eval::eval(&file_content, execution_scope); | ||
handle_output(evaluated_output); | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn parse_file(filename: &str) -> Result<(), std::io::Error> { | ||
let file_content = fs::read_to_string(filename)?; | ||
let mut parser = Parser::new(&file_content); | ||
|
||
let parsed_program = parser.parse_program(); | ||
match parsed_program { | ||
ParsedProgram::ValidProgram(_) => println!("Program contained no errors"), | ||
ParsedProgram::InvalidProgram(parse_errors) => { | ||
eprintln!("Found parse errors:"); | ||
parse_errors.into_iter().for_each(|error| { | ||
eprintln!("{error}"); | ||
}); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn handle_output(evaluated_output: EvaledProgram) { | ||
match evaluated_output { | ||
EvaledProgram::Valid(object) => println!("{object}"), | ||
EvaledProgram::ParseError(parse_errors) => { | ||
eprintln!("Found parse errors:"); | ||
parse_errors.into_iter().for_each(|error| { | ||
eprintln!("{error}"); | ||
}); | ||
} | ||
EvaledProgram::EvalError(runtime_error) => { | ||
eprintln!("Runtime error: {runtime_error}") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use std::io::{stdin, stdout, Write}; | ||
|
||
use interpreter::{ | ||
compiler::Compiler, | ||
eval::{self, objects::Environment, EvaledProgram}, | ||
parser::{ParsedProgram, Parser}, | ||
vm::VirtualMachine, | ||
}; | ||
|
||
use crate::args::PiperArgs; | ||
|
||
pub fn execute_repl(args: &PiperArgs) -> Result<(), std::io::Error> { | ||
println!("Welcome to piperscript, try and write some code:"); | ||
let repl_scope = &mut Environment::new_env_reference(); | ||
|
||
loop { | ||
let mut buffer = String::new(); | ||
|
||
print!("> "); | ||
stdout().flush()?; | ||
|
||
match stdin().read_line(&mut buffer) { | ||
Ok(_) => { | ||
let input = buffer.trim_end(); | ||
|
||
if args.use_vm { | ||
let mut parser = Parser::new(input); | ||
match parser.parse_program() { | ||
ParsedProgram::ValidProgram(ast) => { | ||
let mut compiler: Compiler = Compiler::default(); | ||
compiler.compile(ast); | ||
let bytecode = compiler.bytecode(); | ||
|
||
let mut vm = VirtualMachine::new(bytecode); | ||
vm.run().unwrap(); | ||
|
||
let stack_elem = vm.stack_top(); | ||
println!("{stack_elem}"); | ||
} | ||
ParsedProgram::InvalidProgram(errors) => { | ||
eprintln!("Found parse errors:"); | ||
errors.into_iter().for_each(|error| { | ||
eprintln!("{error}"); | ||
}); | ||
} | ||
} | ||
} else { | ||
let evaluated_output = eval::eval(input, repl_scope); | ||
handle_output(evaluated_output); | ||
} | ||
} | ||
Err(_) => panic!(), | ||
} | ||
} | ||
} | ||
|
||
fn handle_output(evaluated_output: EvaledProgram) { | ||
match evaluated_output { | ||
EvaledProgram::Valid(object) => println!("{object}"), | ||
EvaledProgram::ParseError(parse_errors) => { | ||
eprintln!("Found parse errors:"); | ||
parse_errors.into_iter().for_each(|error| { | ||
eprintln!("{error}"); | ||
}); | ||
} | ||
EvaledProgram::EvalError(runtime_error) => { | ||
eprintln!("Runtime error: {runtime_error}") | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.