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 });