Skip to content

Commit

Permalink
Add ternary operator, no matter what @cetio thinks
Browse files Browse the repository at this point in the history
  • Loading branch information
HeronErin committed May 22, 2024
1 parent 2f94002 commit ae2f61e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
8 changes: 5 additions & 3 deletions source/fnc/treegen/ast_types.d
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ enum OperationVariety {
Period, // foo.bar
Arrow, // foo->bar
Range, // x..y OR 0..99
TernaryOperator, // 1 if true else false
Voidable, // int?


Expand Down Expand Up @@ -297,7 +298,10 @@ class AstNode {
break;

case AstAction.Call:
writeln("Calling function resolved from:");
writeln("Call:");
tabCount++;
printTabs();
writeln("Function resolved from:");
callNodeData.func.tree(tabCount + 1);
printTabs();
write("With Params (");
Expand All @@ -314,8 +318,6 @@ class AstNode {
arg.value.tree(tabCount + 1);

}

// callNodeData.args.tree(tabCount + 1);
break;
case AstAction.ConversionPipe:
writeln("Conversion Pipe: ");
Expand Down
1 change: 1 addition & 0 deletions source/fnc/treegen/expression_parser.d
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import std.container.array;

// Group letters.letters.letters into NamedUnit s
// Group Parenthesis and indexing into AstNode.Expression s to be parsed speratly
// This should not be used by the end user, and is only exposed for unittests
public AstNode[] phaseOne(Token[] tokens) {
AstNode[] ret;
AstNode[] parenthesisStack;
Expand Down
16 changes: 16 additions & 0 deletions source/fnc/treegen/relationships.d
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,14 @@ const OperatorPrecedenceLayer[] operatorPrecedence = [
Token(TokenType.Filler), OPR('~'), Token(TokenType.Filler)
]),
]),
OperatorPrecedenceLayer(OperatorOrder.RightToLeft, [
OperationPrecedenceEntry(OperationVariety.TernaryOperator, [
Token(TokenType.Filler), Token(TokenType.Letter, "if".makeUnicodeString),
Token(TokenType.Filler), Token(TokenType.Letter, "else".makeUnicodeString),
Token(TokenType.Filler)
]),

]),
OperatorPrecedenceLayer(OperatorOrder.RightToLeft, [
OperationPrecedenceEntry(OperationVariety.Assignment, [
Token(TokenType.Filler), OPR('='), Token(TokenType.Filler)
Expand Down Expand Up @@ -909,6 +917,14 @@ bool testAndJoin(const(OperationPrecedenceEntry) entry, ref Array!AstNode nodes,
return false;
potentialArrayStyleAssignmentData = node.arrayEqNodeData;
break;
case TokenType.Letter:
if (node.action != AstAction.NamedUnit)
return false;
if (node.namedUnit.names.length != 1) // We don't support operators with . in it
return false;
if (node.namedUnit.names[0] != entry.tokens[index].value)
return false;
break;
case TokenType.QuestionMark:
case TokenType.ExclamationMark:
case TokenType.Equals:
Expand Down
7 changes: 7 additions & 0 deletions source/tests/expression_parser.d
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,11 @@ unittest {
auto toks = "int 2 = 2 + 5".tokenizeText;
assert(2 == toks.prematureSingleTokenGroupLength(0));
assert(8 == toks.prematureSingleTokenGroupLength(2));
}

unittest {
Array!AstNode nodes = expressionNodeFromTokens("t = 5 + 5 if foo() else 1 + 4".tokenizeText);
assert(nodes.length == 1);
// assert(nodes[0].);
nodes[0].tree;
}
7 changes: 7 additions & 0 deletions source/tests/tokenizer.d
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,17 @@ unittest {
assert(toks[0].base == SpecialNumberBase.Binary);
assert(toks[0].value == "0b10101010".makeUnicodeString);

toks = "0o1010_1010".tokenizeText;
assert(toks.length == 1);
assert(toks[0].tokenVariety == TokenType.Number);
assert(toks[0].base == SpecialNumberBase.Octal);
assert(toks[0].value == "0o10101010".makeUnicodeString);


toks = "0b1010_1010 + 1".tokenizeText;
assert(toks.length == 5);
assert(toks[0].tokenVariety == TokenType.Number);
assert(toks[0].base == SpecialNumberBase.Binary);
assert(toks[0].value == "0b10101010".makeUnicodeString);

}

0 comments on commit ae2f61e

Please sign in to comment.