Skip to content

Commit

Permalink
#119 - Add support of ternary operator
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed May 28, 2020
1 parent 98cd6bd commit 3736b5f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions xFunc Grammar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ statement = unaryAssign
/ def
/ undef
/ if
/ ternary
/ for
/ while
/ exp
Expand All @@ -19,6 +20,7 @@ def = ('def' / 'define') '(' assignmentKey ',' exp ')'
undef = ('undef' / 'undefine') '(' assignmentKey ')'

if = 'if' '(' conditional ',' exp (',' exp)* ')'
ternary = conditional '?' exp ':' exp
for = 'for' '(' statement ',' exp ',' conditional ',' statement ')'
while = 'while' '(' exp ',' conditional ')'

Expand Down
3 changes: 3 additions & 0 deletions xFunc.Maths/Parser.ExpressionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,5 +290,8 @@ private IExpression CreateFromKeyword(KeywordToken keywordToken, params IExpress

private IExpression CreateMultiplication(params IExpression[] arguments) =>
new Mul(arguments);

private IExpression CreateTernary(IExpression condition, IExpression then, IExpression @else) =>
new If(condition, then, @else);
}
}
21 changes: 21 additions & 0 deletions xFunc.Maths/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ private IExpression Statement(TokenEnumerator tokenEnumerator)
Def(tokenEnumerator) ??
Undef(tokenEnumerator) ??
If(tokenEnumerator) ??
Ternary(tokenEnumerator) ??
For(tokenEnumerator) ??
While(tokenEnumerator) ??
Expression(tokenEnumerator);
Expand Down Expand Up @@ -232,6 +233,26 @@ private IExpression If(TokenEnumerator tokenEnumerator)
return CreateFromKeyword(@if, condition, then);
}

private IExpression Ternary(TokenEnumerator tokenEnumerator)
{
var condition = ConditionalOperator(tokenEnumerator) ??
throw new ParseException(); // TODO:

if (!tokenEnumerator.Symbol(SymbolToken.QuestionMark))
throw new ParseException(); // TODO:

var then = Expression(tokenEnumerator) ??
throw new ParseException(); // TODO:

if (!tokenEnumerator.Symbol(SymbolToken.Colon))
throw new ParseException(); // TODO:

var @else = Expression(tokenEnumerator) ??
throw new ParseException(); // TODO:

return CreateTernary(condition, then, @else);
}

private IExpression For(TokenEnumerator tokenEnumerator)
{
var @for = tokenEnumerator.Keyword(KeywordToken.For);
Expand Down
2 changes: 2 additions & 0 deletions xFunc.Maths/Tokenization/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ private IToken CreateSymbol(ref ReadOnlyMemory<char> function)
',' => Comma,
'∠' => Angle,
'°' => Degree,
':' => Colon,
'?' => QuestionMark,
_ => null,
};

Expand Down
10 changes: 10 additions & 0 deletions xFunc.Maths/Tokenization/Tokens/SymbolToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,15 @@ private SymbolToken(char symbol)
/// Gets the '°' token.
/// </summary>
public static SymbolToken Degree { get; } = new SymbolToken('°');

/// <summary>
/// Gets the ':' token.
/// </summary>
public static SymbolToken Colon { get; } = new SymbolToken(':');

/// <summary>
/// Gets the '?' token.
/// </summary>
public static SymbolToken QuestionMark { get; } = new SymbolToken('?');
}
}

0 comments on commit 3736b5f

Please sign in to comment.