From 8bb55d110ac2d3fccf12840ef28cc11de9af5ed1 Mon Sep 17 00:00:00 2001 From: publicmatt Date: Fri, 3 Jan 2025 10:59:21 -0800 Subject: [PATCH] add all --- Justfile | 17 +++++++++++++++++ src/compiler.rs | 50 ++++++++++++++++++++++--------------------------- src/main.rs | 12 +++++++++--- 3 files changed, 48 insertions(+), 31 deletions(-) create mode 100644 Justfile diff --git a/Justfile b/Justfile new file mode 100644 index 0000000..a2d8bf2 --- /dev/null +++ b/Justfile @@ -0,0 +1,17 @@ +test: + cargo test + +build: + cargo build + +scan-file FILENAME: + cargo run -- scan --file {{ FILENAME }} + +scan COMMANDS: + cargo run -- scan "{{ COMMANDS }}" + +compile COMMANDS: + cargo run -- run "{{ COMMANDS }}" + +compile-file FILENAME: + cargo run -- run --file {{ FILENAME }} diff --git a/src/compiler.rs b/src/compiler.rs index 33139cf..71be190 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -14,7 +14,7 @@ pub struct Compiler<'a> { } impl<'a> Compiler<'a> { - pub fn from_source(source: &'a String) -> Self { + pub fn from_source(source: &'a str) -> Self { Compiler { scanner: Scanner::new(source), chunk: None, @@ -31,21 +31,15 @@ impl<'a> Compiler<'a> { self.expression(); //self.consume(TokenType::TokenEof, "Expect end of expression"); self.emit_return(); - return !self.had_error; + !self.had_error } fn advance(&mut self) { - self.previous = self.current.clone(); - while let Some(r) = self.scanner.next() { - match r { - Ok(token) => { - self.current = Some(token); - break; - } - _ => { - self.error_at_current("error as current token"); - } - } + self.previous = self.current; + if let Some(Ok(token)) = self.scanner.next() { + self.current = Some(token); + } else { + self.error_at_current("error as current token"); } } @@ -120,18 +114,18 @@ impl<'a> Compiler<'a> { } } -// use strum_macros::Display; -// #[derive(Display, PartialEq, Eq, PartialOrd, Ord)] -// enum Precedence { -// None, -// Assignment, -// Or, -// And, -// Equality, -// Comparison, -// Term, -// Factor, -// Unary, -// Call, -// Primary, -// } +use strum_macros::Display; +#[derive(Display, PartialEq, Eq, PartialOrd, Ord)] +enum Precedence { + None, + Assignment, + Or, + And, + Equality, + Comparison, + Term, + Factor, + Unary, + Call, + Primary, +} diff --git a/src/main.rs b/src/main.rs index 1b95ddd..dbf2898 100644 --- a/src/main.rs +++ b/src/main.rs @@ -101,9 +101,9 @@ fn scan_content(source: &str) { fn run_content(source: &str) { let mut _vm: VM = VM::new(); let owned = &source.to_owned(); - // let mut compiler: Compiler = Compiler::from_source(owned); - // compiler.compile(); - todo!("run_content is not hooked up yet"); + let mut compiler: Compiler = Compiler::from_source(owned); + compiler.compile(); + //todo!("run_content is not hooked up yet"); // let mut chunk: Chunk = compiler.compile(); // match vm.interpret(&mut chunk) { // InterpretResult::InterpretOk => exit(0), @@ -111,3 +111,9 @@ fn run_content(source: &str) { // InterpretResult::InterpretRuntimeError => exit(70), // } } +fn compile_content(source: &str) { + let mut _vm: VM = VM::new(); + let owned = &source.to_owned(); + let mut compiler: Compiler = Compiler::from_source(owned); + compiler.compile(); +}