summaryrefslogtreecommitdiffstats
path: root/src/forth
diff options
context:
space:
mode:
Diffstat (limited to 'src/forth')
-rw-r--r--src/forth/compiler.rs26
1 files changed, 26 insertions, 0 deletions
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)]);
+ }
}