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:
@@ -108,11 +108,16 @@ impl Environment {
|
|||||||
let mut parser = Parser::new(source)?;
|
let mut parser = Parser::new(source)?;
|
||||||
let untyped_ast = parser.parse_expression()?;
|
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 mut binder = Binder::new(self.global_names.clone());
|
||||||
let bound_ast = binder.bind(&untyped_ast)?;
|
let bound_ast = binder.bind(&untyped_ast)?;
|
||||||
|
|
||||||
// 3. Execute
|
// 4. Execute
|
||||||
let mut vm = VM::new(self.global_values.clone());
|
let mut vm = VM::new(self.global_values.clone());
|
||||||
vm.run(&bound_ast)
|
vm.run(&bound_ast)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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> {
|
fn parse_atom(&mut self) -> Result<Node<UntypedKind>, String> {
|
||||||
let token = self.advance()?;
|
let token = self.advance()?;
|
||||||
let identity = Rc::new(NodeIdentity { location: token.location });
|
let identity = Rc::new(NodeIdentity { location: token.location });
|
||||||
|
|||||||
Reference in New Issue
Block a user