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.
This commit is contained in:
Michael Schimmel
2026-02-17 22:33:49 +01:00
parent b0f139f389
commit e6eaf70836
2 changed files with 11 additions and 2 deletions
+7 -2
View File
@@ -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)
}
+4
View File
@@ -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<Node<UntypedKind>, String> {
let token = self.advance()?;
let identity = Rc::new(NodeIdentity { location: token.location });