From e6eaf708367591c470e4715af2fa3c398c17c252 Mon Sep 17 00:00:00 2001 From: Michael Schimmel Date: Tue, 17 Feb 2026 22:33:49 +0100 Subject: [PATCH] Add check for trailing tokens Ensure that a script does not contain any tokens after the main expression. This prevents accidental trailing expressions and enforces a clearer script structure. --- src/ast/environment.rs | 9 +++++++-- src/ast/parser.rs | 4 ++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/ast/environment.rs b/src/ast/environment.rs index f3f18af..f2d8ce9 100644 --- a/src/ast/environment.rs +++ b/src/ast/environment.rs @@ -108,11 +108,16 @@ impl Environment { let mut parser = Parser::new(source)?; let untyped_ast = parser.parse_expression()?; - // 2. Bind & Type Check + // 2. Check for trailing tokens + if !parser.at_eof() { + return Err("Unexpected trailing expressions in script. Use (do ...) for sequences.".to_string()); + } + + // 3. Bind & Type Check let mut binder = Binder::new(self.global_names.clone()); let bound_ast = binder.bind(&untyped_ast)?; - // 3. Execute + // 4. Execute let mut vm = VM::new(self.global_values.clone()); vm.run(&bound_ast) } diff --git a/src/ast/parser.rs b/src/ast/parser.rs index 7338a0e..4a6dfe3 100644 --- a/src/ast/parser.rs +++ b/src/ast/parser.rs @@ -46,6 +46,10 @@ impl<'a> Parser<'a> { } } + pub fn at_eof(&self) -> bool { + matches!(self.current_token.kind, TokenKind::EOF) + } + fn parse_atom(&mut self) -> Result, String> { let token = self.advance()?; let identity = Rc::new(NodeIdentity { location: token.location });