Files
crafting-interpreters/rlox/src/main.rs

59 lines
1.1 KiB
Rust
Raw Normal View History

2023-04-14 15:27:24 +02:00
mod bc;
2023-04-12 12:46:24 +02:00
mod lc;
2023-04-14 15:27:24 +02:00
mod vm;
2023-03-29 20:03:16 +02:00
2023-04-12 12:46:24 +02:00
use std::env;
2023-10-08 12:25:21 +02:00
use std::io;
2023-04-12 12:46:24 +02:00
2023-10-08 12:25:21 +02:00
use bc::Chunk;
use vm::VM;
use crate::vm::VMError;
2023-04-14 15:27:24 +02:00
2023-10-08 12:25:21 +02:00
fn repl() {
let mut buffer = String::new();
loop {
match io::stdin().read_line(&mut buffer) {
Ok(n) => {
let mut chunk = Chunk::new();
lc::compile(buffer.as_str(), &mut chunk);
let mut vm = VM::new();
let result = vm.run(&chunk);
println!("{:?}", result);
buffer.clear();
},
Err(error) => todo!()
}
}
2023-04-12 12:46:24 +02:00
}
fn run_file() {
2023-10-08 12:25:21 +02:00
todo!()
2023-04-12 12:46:24 +02:00
}
2023-04-04 19:03:57 +02:00
2023-04-04 20:34:36 +02:00
fn main() {
2023-04-12 12:46:24 +02:00
let num_args = env::args().len();
if num_args == 1 {
2023-10-08 12:25:21 +02:00
repl()
2023-04-12 12:46:24 +02:00
} else if num_args == 2 {
2023-10-08 12:25:21 +02:00
run_file()
2023-04-12 12:46:24 +02:00
} else {
2023-10-08 12:25:21 +02:00
println!("Usage: rlox [path]")
2023-04-12 12:46:24 +02:00
}
2023-03-29 20:03:16 +02:00
}
2023-10-08 11:20:00 +02:00
#[cfg(test)]
mod tests {
use crate::{bc::Chunk, lc::compile, vm::VM};
#[test]
fn test_compile_and_run_pi_math() {
let source = "-(3 * 7 * 11 * 17) / -(500 + 1000 - 250)";
let mut chunk = Chunk::new();
compile(source, &mut chunk);
let mut vm = VM::new();
vm.run(&chunk).unwrap();
}
}