Skip to content

Commit

Permalink
Moved compiler into a submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
patbuc committed Nov 12, 2023
1 parent 58bc2d9 commit 17e0d24
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 24 deletions.
18 changes: 1 addition & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
Expand Down Expand Up @@ -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
}
13 changes: 13 additions & 0 deletions src/vm/block/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
7 changes: 3 additions & 4 deletions src/vm/compiler.rs → src/vm/compiler/compiler.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/vm/compiler/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod compiler;
mod scanner;

pub(super) struct Compiler {}
3 changes: 2 additions & 1 deletion src/vm/scanner.rs → src/vm/compiler/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 1 addition & 2 deletions src/vm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod block;
mod block;
mod compiler;
mod scanner;
pub mod vm;

0 comments on commit 17e0d24

Please sign in to comment.