Fix function invocation stack underflow

This commit is contained in:
2025-12-11 01:03:43 -07:00
parent 0732f68bcf
commit 342b1ab107
13 changed files with 295 additions and 167 deletions

View File

@@ -1225,18 +1225,34 @@ impl<'a> Parser<'a> {
// Need to capture return span
let ret_start_span = Self::token_to_span(&current_token);
self.assign_next()?;
let expression = self.expression()?.ok_or(Error::UnexpectedEOF)?;
let expr = if token_matches!(
self.current_token.as_ref().ok_or(Error::UnexpectedEOF)?,
TokenType::Symbol(Symbol::Semicolon)
) {
// rewind 1 token so we can check for the semicolon at the bottom of this function.
self.tokenizer.seek(SeekFrom::Current(-1))?;
None
} else {
Some(self.expression()?.ok_or(Error::UnexpectedEOF)?)
};
let ret_span = Span {
start_line: ret_start_span.start_line,
start_col: ret_start_span.start_col,
end_line: expression.span.end_line,
end_col: expression.span.end_col,
end_line: expr
.as_ref()
.map(|e| e.span.end_line)
.unwrap_or(ret_start_span.end_line),
end_col: expr
.as_ref()
.map(|e| e.span.end_col)
.unwrap_or(ret_start_span.end_col),
};
let return_expr = Spanned {
span: ret_span,
node: Expression::Return(boxed!(expression)),
node: Expression::Return(expr.map(Box::new)),
};
expressions.push(return_expr);

View File

@@ -381,7 +381,7 @@ pub enum Expression<'a> {
MethodCall(Spanned<MethodCallExpression<'a>>),
Negation(Box<Spanned<Expression<'a>>>),
Priority(Box<Spanned<Expression<'a>>>),
Return(Box<Spanned<Expression<'a>>>),
Return(Option<Box<Spanned<Expression<'a>>>>),
Syscall(Spanned<SysCall<'a>>),
Ternary(Spanned<TernaryExpression<'a>>),
Variable(Spanned<Cow<'a, str>>),
@@ -409,7 +409,15 @@ impl<'a> std::fmt::Display for Expression<'a> {
Expression::MethodCall(e) => write!(f, "{}", e),
Expression::Negation(e) => write!(f, "(-{})", e),
Expression::Priority(e) => write!(f, "({})", e),
Expression::Return(e) => write!(f, "(return {})", e),
Expression::Return(e) => write!(
f,
"(return {})",
if let Some(e) = e {
e.to_string()
} else {
"".to_string()
}
),
Expression::Syscall(e) => write!(f, "{}", e),
Expression::Ternary(e) => write!(f, "{}", e),
Expression::Variable(id) => write!(f, "{}", id),