Skip to content

Commit

Permalink
move REPL and filerunner in to piper crate, and implement --use-vm flag
Browse files Browse the repository at this point in the history
  • Loading branch information
oysandvik94 committed Aug 31, 2024
1 parent 1a2b80c commit 3c74331
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 77 deletions.
21 changes: 1 addition & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ resolver = "2"

members = [
"crates/interpreter",
"crates/repl",
"crates/piper",
"crates/filerunner",
"crates/piperfmt",
"crates/son_of_anton",
]
Expand Down
3 changes: 1 addition & 2 deletions crates/piper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
repl = { path = "../repl/" }
filerunner = { path = "../filerunner/" }
interpreter = { path = "../interpreter" }
clap = { version = "4.5.11", features = ["derive"] }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
3 changes: 3 additions & 0 deletions crates/piper/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use clap::{Parser, Subcommand};
pub struct PiperArgs {
#[command(subcommand)]
pub command: PiperCommands,

#[arg(long, global = true)]
pub use_vm: bool,
}

#[derive(Subcommand)]
Expand Down
48 changes: 48 additions & 0 deletions crates/piper/src/filerunner.rs
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}")
}
}
}
6 changes: 4 additions & 2 deletions crates/piper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use args::{PiperArgs, PiperCommands};
use clap::Parser;
use tracing_subscriber::FmtSubscriber;

mod args;
pub mod args;
mod filerunner;
mod repl;

pub fn setup_logging() {
let subscriber = FmtSubscriber::builder()
Expand All @@ -14,7 +16,7 @@ pub fn setup_logging() {
pub fn execute_piper() -> Result<(), std::io::Error> {
let cli = PiperArgs::parse();
match &cli.command {
PiperCommands::Repl => repl::execute_repl()?,
PiperCommands::Repl => repl::execute_repl(&cli)?,
PiperCommands::Run { filename } => filerunner::execute_file(filename)?,
PiperCommands::Check { filename } => filerunner::parse_file(filename)?,
}
Expand Down
70 changes: 70 additions & 0 deletions crates/piper/src/repl.rs
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}")
}
}
}
11 changes: 0 additions & 11 deletions crates/repl/Cargo.toml

This file was deleted.

40 changes: 0 additions & 40 deletions crates/repl/src/lib.rs

This file was deleted.

0 comments on commit 3c74331

Please sign in to comment.