From 17e0d24b5294d2ee66ecbbc8733843ad9263713c Mon Sep 17 00:00:00 2001 From: Patric Bucher Date: Sun, 12 Nov 2023 22:14:54 +0100 Subject: [PATCH] Moved compiler into a submodule --- src/main.rs | 18 +----------------- src/vm/block/tests.rs | 13 +++++++++++++ src/vm/{ => compiler}/compiler.rs | 7 +++---- src/vm/compiler/mod.rs | 4 ++++ src/vm/{ => compiler}/scanner.rs | 3 ++- src/vm/mod.rs | 3 +-- 6 files changed, 24 insertions(+), 24 deletions(-) rename src/vm/{ => compiler}/compiler.rs (51%) create mode 100644 src/vm/compiler/mod.rs rename src/vm/{ => compiler}/scanner.rs (99%) diff --git a/src/main.rs b/src/main.rs index 033eac1..4eca367 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,8 +7,6 @@ use std::process::exit; use std::fs::File; use std::{env, io}; -use crate::vm::block::{Block, OpCode}; - use crate::vm::vm::VirtualMachine; fn main() { @@ -18,7 +16,7 @@ fn main() { if args.len() == 1 { run_repl(); - } else if args.len() == 2 { + } else if args.len() >= 2 { run_file(&args[1]); } else { exit(64); @@ -85,17 +83,3 @@ fn read_file(path: &str) -> String { .expect(format!("Failed to read the file {}", path).as_str()); contents } - -fn write_block() -> Block { - let mut block = Block::new("ZeBlock"); - - block.write_constant(1234.56, 2); - block.write_op_code(OpCode::Negate, 3); - block.write_constant(345.67, 4); - block.write_op_code(OpCode::Add, 4); - block.write_constant(1.2, 5); - block.write_op_code(OpCode::Multiply, 6); - block.write_op_code(OpCode::Return, 8); - - block -} diff --git a/src/vm/block/tests.rs b/src/vm/block/tests.rs index 19800e0..b651585 100644 --- a/src/vm/block/tests.rs +++ b/src/vm/block/tests.rs @@ -64,3 +64,16 @@ fn can_write_u32() { block.write_u32(12345678); assert_eq!(12345678, block.read_u32(0)); } + +#[test] +fn can_write_block() { + let mut block = Block::new("ZeBlock"); + + block.write_constant(1234.56, 2); + block.write_op_code(OpCode::Negate, 3); + block.write_constant(345.67, 4); + block.write_op_code(OpCode::Add, 4); + block.write_constant(1.2, 5); + block.write_op_code(OpCode::Multiply, 6); + block.write_op_code(OpCode::Return, 8); +} diff --git a/src/vm/compiler.rs b/src/vm/compiler/compiler.rs similarity index 51% rename from src/vm/compiler.rs rename to src/vm/compiler/compiler.rs index df544b5..faf6f80 100644 --- a/src/vm/compiler.rs +++ b/src/vm/compiler/compiler.rs @@ -1,9 +1,8 @@ -use crate::vm::scanner::Scanner; - -pub(crate) struct Compiler {} +use crate::vm::compiler::scanner::Scanner; +use crate::vm::compiler::Compiler; impl Compiler { - pub(super) fn compile(&self, source: String) { + pub(in crate::vm) fn compile(&self, source: String) { let scanner = Scanner::new(source); for token in scanner { println!("{:?}", token); diff --git a/src/vm/compiler/mod.rs b/src/vm/compiler/mod.rs new file mode 100644 index 0000000..4fbee41 --- /dev/null +++ b/src/vm/compiler/mod.rs @@ -0,0 +1,4 @@ +mod compiler; +mod scanner; + +pub(super) struct Compiler {} diff --git a/src/vm/scanner.rs b/src/vm/compiler/scanner.rs similarity index 99% rename from src/vm/scanner.rs rename to src/vm/compiler/scanner.rs index e4c28a1..af74983 100644 --- a/src/vm/scanner.rs +++ b/src/vm/compiler/scanner.rs @@ -292,7 +292,8 @@ impl Scanner { } fn skip_whitespace(&mut self) { - while let c = self.peek() { + loop { + let c = self.peek(); match c { ' ' | '\r' | '\t' => { self.advance(); diff --git a/src/vm/mod.rs b/src/vm/mod.rs index 0e2298f..9de7d10 100644 --- a/src/vm/mod.rs +++ b/src/vm/mod.rs @@ -1,4 +1,3 @@ -pub mod block; +mod block; mod compiler; -mod scanner; pub mod vm;