Skip to content

Commit

Permalink
Added min/max operators a <? b which lower to a < b ? a : b, whic…
Browse files Browse the repository at this point in the history
…h matches the C++ language extension.
  • Loading branch information
TurkeyMan committed Dec 30, 2024
1 parent da1b69a commit d8d5837
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions compiler/src/dmd/lexer.d
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,11 @@ class Lexer
else
t.value = TOK.leftShift; // <<
}
else if (*p == '?')
{
++p;
t.value = TOK.ltQuestion; // <?

Check warning on line 1154 in compiler/src/dmd/lexer.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/lexer.d#L1153-L1154

Added lines #L1153 - L1154 were not covered by tests
}
else if (*p == ':' && Ccompile)
{
++p;
Expand Down Expand Up @@ -1187,6 +1192,11 @@ class Lexer
else
t.value = TOK.unsignedRightShift; // >>>
}
else if (*p == '?')
{
p++;
t.value = TOK.gtQuestion; // >?

Check warning on line 1198 in compiler/src/dmd/lexer.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/lexer.d#L1197-L1198

Added lines #L1197 - L1198 were not covered by tests
}
else
t.value = TOK.rightShift; // >>
}
Expand Down
8 changes: 8 additions & 0 deletions compiler/src/dmd/parse.d
Original file line number Diff line number Diff line change
Expand Up @@ -9156,6 +9156,14 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
e = new AST.CatExp(loc, e, e2);
continue;

case TOK.ltQuestion:
case TOK.gtQuestion:
EXP op = token.value == TOK.ltQuestion ? EXP.lessThan : EXP.greaterThan;
nextToken();
auto e2 = parseMulExp();
e = new AST.CondExp(loc, new AST.CmpExp(op, loc, e, e2), e, e2);
continue;

Check warning on line 9165 in compiler/src/dmd/parse.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/parse.d#L9159-L9165

Added lines #L9159 - L9165 were not covered by tests

default:
break;
}
Expand Down
4 changes: 4 additions & 0 deletions compiler/src/dmd/tokens.d
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ enum TOK : ubyte
identity,
notIdentity,
is_,
ltQuestion,
gtQuestion,

leftShift,
rightShift,
Expand Down Expand Up @@ -846,6 +848,8 @@ extern (C++) struct Token
TOK.pound: "#",
TOK.arrow: "->",
TOK.colonColon: "::",
TOK.ltQuestion: "<?",
TOK.gtQuestion: ">?",

// For debugging
TOK.error: "error",
Expand Down

0 comments on commit d8d5837

Please sign in to comment.