Fixed documentation rendering and added ternary expressions
This commit is contained in:
@@ -293,12 +293,12 @@ impl<'a> Parser<'a> {
|
||||
// Handle Infix operators (Binary, Logical, Assignment)
|
||||
if self_matches_peek!(
|
||||
self,
|
||||
TokenType::Symbol(s) if s.is_operator() || s.is_comparison() || s.is_logical() || matches!(s, Symbol::Assign)
|
||||
TokenType::Symbol(s) if s.is_operator() || s.is_comparison() || s.is_logical() || matches!(s, Symbol::Assign | Symbol::Question)
|
||||
) {
|
||||
return Ok(Some(self.infix(lhs)?));
|
||||
} else if self_matches_current!(
|
||||
self,
|
||||
TokenType::Symbol(s) if s.is_operator() || s.is_comparison() || s.is_logical() || matches!(s, Symbol::Assign)
|
||||
TokenType::Symbol(s) if s.is_operator() || s.is_comparison() || s.is_logical() || matches!(s, Symbol::Assign | Symbol::Question)
|
||||
) {
|
||||
self.tokenizer.seek(SeekFrom::Current(-1))?;
|
||||
return Ok(Some(self.infix(lhs)?));
|
||||
@@ -769,6 +769,7 @@ impl<'a> Parser<'a> {
|
||||
| Expression::Priority(_)
|
||||
| Expression::Literal(_)
|
||||
| Expression::Variable(_)
|
||||
| Expression::Ternary(_)
|
||||
| Expression::Negation(_)
|
||||
| Expression::MemberAccess(_)
|
||||
| Expression::MethodCall(_) => {}
|
||||
@@ -788,7 +789,7 @@ impl<'a> Parser<'a> {
|
||||
// Include Assign in the operator loop
|
||||
while token_matches!(
|
||||
temp_token,
|
||||
TokenType::Symbol(s) if s.is_operator() || s.is_comparison() || s.is_logical() || matches!(s, Symbol::Assign)
|
||||
TokenType::Symbol(s) if s.is_operator() || s.is_comparison() || s.is_logical() || matches!(s, Symbol::Assign | Symbol::Question | Symbol::Colon)
|
||||
) {
|
||||
let operator = match temp_token.token_type {
|
||||
TokenType::Symbol(s) => s,
|
||||
@@ -1019,7 +1020,52 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
operators.retain(|symbol| !matches!(symbol, Symbol::LogicalOr));
|
||||
|
||||
// --- PRECEDENCE LEVEL 8: Assignment (=) ---
|
||||
// -- PRECEDENCE LEVEL 8: Ternary (x ? 1 : 2)
|
||||
for i in (0..operators.len()).rev() {
|
||||
if matches!(operators[i], Symbol::Question) {
|
||||
// Ensure next operator is a colon
|
||||
if i + 1 >= operators.len() || !matches!(operators[i + 1], Symbol::Colon) {
|
||||
return Err(Error::InvalidSyntax(
|
||||
self.current_span(),
|
||||
"Ternary operator '?' missing matching ':'".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
let false_branch = expressions.remove(i + 2);
|
||||
let true_branch = expressions.remove(i + 1);
|
||||
let condition = expressions.remove(i);
|
||||
|
||||
let span = Span {
|
||||
start_line: condition.span.start_line,
|
||||
end_line: false_branch.span.end_line,
|
||||
start_col: condition.span.start_col,
|
||||
end_col: false_branch.span.end_col,
|
||||
};
|
||||
|
||||
let ternary_node = Spanned {
|
||||
span,
|
||||
node: TernaryExpression {
|
||||
condition: Box::new(condition),
|
||||
true_value: Box::new(true_branch),
|
||||
false_value: Box::new(false_branch),
|
||||
},
|
||||
};
|
||||
|
||||
expressions.insert(
|
||||
i,
|
||||
Spanned {
|
||||
node: Expression::Ternary(ternary_node),
|
||||
span,
|
||||
},
|
||||
);
|
||||
|
||||
// Remove the `?` and the `:` from the operators list
|
||||
operators.remove(i);
|
||||
operators.remove(i);
|
||||
}
|
||||
}
|
||||
|
||||
// --- PRECEDENCE LEVEL 9: Assignment (=) ---
|
||||
// Assignment is Right Associative: a = b = c => a = (b = c)
|
||||
// We iterate Right to Left
|
||||
for (i, operator) in operators.iter().enumerate().rev() {
|
||||
|
||||
@@ -160,3 +160,37 @@ fn test_negative_literal_const() -> Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ternary_expression() -> Result<()> {
|
||||
let expr = parser!(r#"let i = x ? 1 : 2;"#).parse()?.unwrap();
|
||||
|
||||
assert_eq!("(let i = (x ? 1 : 2))", expr.to_string());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_complex_binary_with_ternary() -> Result<()> {
|
||||
let expr = parser!("let i = (x ? 1 : 3) * 2;").parse()?.unwrap();
|
||||
|
||||
assert_eq!("(let i = ((x ? 1 : 3) * 2))", expr.to_string());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_operator_prescedence_with_ternary() -> Result<()> {
|
||||
let expr = parser!("let x = x ? 1 : 3 * 2;").parse()?.unwrap();
|
||||
|
||||
assert_eq!("(let x = (x ? 1 : (3 * 2)))", expr.to_string());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_nested_ternary_right_associativity() -> Result<()> {
|
||||
let expr = parser!("let i = a ? b : c ? d : e;").parse()?.unwrap();
|
||||
|
||||
assert_eq!("(let i = (a ? b : (c ? d : e)))", expr.to_string());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -277,6 +277,23 @@ pub struct WhileExpression<'a> {
|
||||
pub body: BlockExpression<'a>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct TernaryExpression<'a> {
|
||||
pub condition: Box<Spanned<Expression<'a>>>,
|
||||
pub true_value: Box<Spanned<Expression<'a>>>,
|
||||
pub false_value: Box<Spanned<Expression<'a>>>,
|
||||
}
|
||||
|
||||
impl<'a> std::fmt::Display for TernaryExpression<'a> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"({} ? {} : {})",
|
||||
self.condition, self.true_value, self.false_value
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> std::fmt::Display for WhileExpression<'a> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "(while {} {})", self.condition, self.body)
|
||||
@@ -366,6 +383,7 @@ pub enum Expression<'a> {
|
||||
Priority(Box<Spanned<Expression<'a>>>),
|
||||
Return(Box<Spanned<Expression<'a>>>),
|
||||
Syscall(Spanned<SysCall<'a>>),
|
||||
Ternary(Spanned<TernaryExpression<'a>>),
|
||||
Variable(Spanned<Cow<'a, str>>),
|
||||
While(Spanned<WhileExpression<'a>>),
|
||||
}
|
||||
@@ -393,6 +411,7 @@ impl<'a> std::fmt::Display for Expression<'a> {
|
||||
Expression::Priority(e) => write!(f, "({})", e),
|
||||
Expression::Return(e) => write!(f, "(return {})", e),
|
||||
Expression::Syscall(e) => write!(f, "{}", e),
|
||||
Expression::Ternary(e) => write!(f, "{}", e),
|
||||
Expression::Variable(id) => write!(f, "{}", id),
|
||||
Expression::While(e) => write!(f, "{}", e),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user