Skip to content

Commit

Permalink
[PP] Conditional expression
Browse files Browse the repository at this point in the history
  • Loading branch information
LuisHsu committed Mar 21, 2024
1 parent 884b133 commit d430b39
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/include/PreProcessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ struct PreProcessor {
Result bitwise_inclusive_OR();
Result logical_AND();
Result logical_OR();
Result conditional(); // TODO:
Result conditional();

template<typename T = void>
struct lshift {
Expand Down
32 changes: 31 additions & 1 deletion src/lib/PreProcessor/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ PreProcessor::Expression::Result PreProcessor::Expression::relation_op(PreProces
}

PreProcessor::Expression::Result PreProcessor::Expression::eval(){
return logical_OR(); // TODO:
return conditional(); // TODO:
}

PreProcessor::Expression::Result PreProcessor::Expression::primary(){
Expand Down Expand Up @@ -421,4 +421,34 @@ PreProcessor::Expression::Result PreProcessor::Expression::logical_OR(){
}
}
return res;
}

PreProcessor::Expression::Result PreProcessor::Expression::conditional(){
PreProcessor::Expression::Result cond = logical_OR();
Line::iterator head = skip_whitespace(cur, end);
cur = head;
if(cur != end && cur->hold<TokenType::Punctuator>()){
TokenType::Punctuator punct = cur->value();
if(punct.type == TokenType::Punctuator::Query){
cur = std::next(cur);
PreProcessor::Expression::Result operand_true = eval();
if(cur != end && cur->hold<TokenType::Punctuator>() && ((TokenType::Punctuator)cur->value()).type == TokenType::Punctuator::Colon){
cur = std::next(cur);
PreProcessor::Expression::Result operand_false = conditional();
implicit_cast(operand_true, operand_false);
if(std::visit(overloaded {
[](auto val){
return val != 0;
}
}, cond)){
return operand_true;
}else{
return operand_false;
}
}else{
throw Exception::Error(head->value().pos, "missing ':' in conditional expression");
}
}
}
return cond;
}

0 comments on commit d430b39

Please sign in to comment.