From 00579203e0cb10b2be793c4f215716554e58690e Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Mon, 25 Aug 2025 11:24:24 -0400 Subject: add comments --- src/forth/compiler.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/forth/compiler.rs') diff --git a/src/forth/compiler.rs b/src/forth/compiler.rs index b3f5f08..f593806 100644 --- a/src/forth/compiler.rs +++ b/src/forth/compiler.rs @@ -10,6 +10,7 @@ pub enum CompileError { DefStackEmpty, MissingIf, MissingQuote, + MissingParen, UnknownWord(String), } impl std::fmt::Display for CompileError { @@ -19,6 +20,7 @@ impl std::fmt::Display for CompileError { Self::DefStackEmpty => write!(f, "def stack empty"), Self::MissingIf => write!(f, "missing if"), Self::MissingQuote => write!(f, "missing ending quote"), + Self::MissingParen => write!(f, "missing ending parenthesis"), Self::UnknownWord(word) => write!(f, "unknown word: {word}"), } } @@ -153,6 +155,16 @@ impl<'a> Compiler<'a> { .ok_or(CompileError::MissingQuote)?; self.bc_push(OpCode::Str(end+1, s_end), anno); }, + "(" => { + self.enumerator + .find(|(_i, c)| *c == ')') + .ok_or(CompileError::MissingParen)?; + }, + "\\" => { + self.enumerator + .find(|(_i, c)| *c == '\n') + .ok_or(CompileError::EOF)?; + }, ":" => { let (name, _, _) = self.next_word().ok_or(CompileError::EOF)?; self.wordalog.0.insert(name, self.wordlist.0.len()); @@ -409,4 +421,18 @@ mod tests { eprintln!("false: {fbranch:?}"); assert_eq!(tbranch.0[0], OpCode::TCall(1)); } + + #[test] + fn comment_paren() { + let comp = compiler_for("1 ( foo ) 2 ( bar) 3\n"); + let main = &comp.wordlist.0[0]; + assert_eq!(main, vec![OpCode::Num(1), OpCode::Num(2), OpCode::Num(3)]); + } + + #[test] + fn comment_backslash() { + let comp = compiler_for("1 \\ foobar\n2\n"); + let main = &comp.wordlist.0[0]; + assert_eq!(main, vec![OpCode::Num(1), OpCode::Num(2)]); + } } -- cgit v1.3