Skip to content

Commit

Permalink
implement vm inside filerunner
Browse files Browse the repository at this point in the history
  • Loading branch information
oysandvik94 committed Aug 31, 2024
1 parent 3c74331 commit d393545
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 31 deletions.
13 changes: 10 additions & 3 deletions crates/piper/src/filerunner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ use interpreter::{
parser::{ParsedProgram, Parser},
};

pub fn execute_file(filename: &str) -> Result<(), std::io::Error> {
use crate::{args::PiperArgs, execute_code};

pub fn execute_file(filename: &str, args: &PiperArgs) -> 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);

if args.use_vm {
execute_code(&file_content);
} else {
let evaluated_output = eval::eval(&file_content, execution_scope);
handle_output(evaluated_output);
}

Ok(())
}
Expand Down
26 changes: 25 additions & 1 deletion crates/piper/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use args::{PiperArgs, PiperCommands};
use clap::Parser;
use interpreter::{compiler::Compiler, parser::ParsedProgram, vm::VirtualMachine};
use tracing_subscriber::FmtSubscriber;

pub mod args;
Expand All @@ -17,9 +18,32 @@ pub fn execute_piper() -> Result<(), std::io::Error> {
let cli = PiperArgs::parse();
match &cli.command {
PiperCommands::Repl => repl::execute_repl(&cli)?,
PiperCommands::Run { filename } => filerunner::execute_file(filename)?,
PiperCommands::Run { filename } => filerunner::execute_file(filename, &cli)?,
PiperCommands::Check { filename } => filerunner::parse_file(filename)?,
}

Ok(())
}

pub fn execute_code(input: &str) {
let mut parser = interpreter::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}");
});
}
}
}
30 changes: 3 additions & 27 deletions crates/piper/src/repl.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
use std::io::{stdin, stdout, Write};

use interpreter::{
compiler::Compiler,
eval::{self, objects::Environment, EvaledProgram},
parser::{ParsedProgram, Parser},
vm::VirtualMachine,
};
use interpreter::eval::{self, objects::Environment, EvaledProgram};

use crate::args::PiperArgs;
use crate::{args::PiperArgs, execute_code};

pub fn execute_repl(args: &PiperArgs) -> Result<(), std::io::Error> {
println!("Welcome to piperscript, try and write some code:");
Expand All @@ -24,26 +19,7 @@ pub fn execute_repl(args: &PiperArgs) -> Result<(), std::io::Error> {
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}");
});
}
}
execute_code(input);
} else {
let evaluated_output = eval::eval(input, repl_scope);
handle_output(evaluated_output);
Expand Down

0 comments on commit d393545

Please sign in to comment.