Skip to content

Commit 8697ce7

Browse files
added bool as value type
1 parent a0599f8 commit 8697ce7

File tree

14 files changed

+61
-31
lines changed

14 files changed

+61
-31
lines changed

Demo/Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Diagnostics;
3-
using System.IO;
42
using MathParser;
53

64
namespace Demo

shunting_yard/MathParser.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
<Compile Include="src\symbol_manager\TrigonometrySymbols.cs" />
9494
<Compile Include="src\expressions\Value.cs" />
9595
<Compile Include="src\tokenizer\Identifier.cs" />
96+
<Compile Include="src\parser\parselets\FixValueParselet.cs" />
9697
</ItemGroup>
9798
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
9899
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

shunting_yard/src/expressions/BinaryExpressionType.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
{
33
enum BinaryExpressionType
44
{
5-
Add,
6-
Sub,
7-
Mul,
8-
Div,
9-
Pow
5+
Addition,
6+
Substraction,
7+
Multiplication,
8+
Division,
9+
Power,
10+
Modulo
1011
}
1112
}

shunting_yard/src/parser/Associativity.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static class AssociativityExtensions
1515
/// is done by parsing the right hand side expression with a lower precedence.
1616
/// </summary>
1717
/// <returns>The precedence increment.</returns>
18-
public static int ToPrecedence(this Associativity associativity)
18+
public static int ToPrecedenceIncrement(this Associativity associativity)
1919
{
2020
switch (associativity)
2121
{

shunting_yard/src/parser/ExpressionParser.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ static ExpressionParser()
1212
{
1313
registerPrefixParselet(TokenType.Identifier, new VariableParselet());
1414
registerPrefixParselet(TokenType.Numeric, new NumberParselet());
15+
registerPrefixParselet(TokenType.False, new FixValueParselet(Value.Boolean(false)));
16+
registerPrefixParselet(TokenType.True, new FixValueParselet(Value.Boolean(true)));
1517
registerPrefixParselet(TokenType.LeftParenthesis, new GroupParselet());
1618
registerPrefixParselet(TokenType.Minus, new PrefixOperatorParselet(PrefixExpressionType.Negation, Precedences.PREFIX));
1719
registerPrefixParselet(TokenType.Plus, new PrefixOperatorParselet(PrefixExpressionType.Positive, Precedences.PREFIX));
@@ -21,11 +23,13 @@ static ExpressionParser()
2123
registerInfixParselet(TokenType.Greater, new ComparisonParselet(ComparisonExpressionType.Bigger, Precedences.COMPARISON, Associativity.Left));
2224
registerInfixParselet(TokenType.QuestionMark, new TernaryParselet());
2325
registerInfixParselet(TokenType.LeftParenthesis, new CallParselet());
24-
registerInfixParselet(TokenType.Plus, new BinaryOperatorParselet(BinaryExpressionType.Add, Precedences.SUM, Associativity.Left));
25-
registerInfixParselet(TokenType.Minus, new BinaryOperatorParselet(BinaryExpressionType.Sub, Precedences.SUM, Associativity.Left));
26-
registerInfixParselet(TokenType.Star, new BinaryOperatorParselet(BinaryExpressionType.Mul, Precedences.PRODUCT, Associativity.Left));
27-
registerInfixParselet(TokenType.Slash, new BinaryOperatorParselet(BinaryExpressionType.Div, Precedences.PRODUCT, Associativity.Left));
28-
registerInfixParselet(TokenType.Pow, new BinaryOperatorParselet(BinaryExpressionType.Pow, Precedences.EXPONENT, Associativity.Right));
26+
registerInfixParselet(TokenType.Plus, new BinaryOperatorParselet(BinaryExpressionType.Addition, Precedences.SUM, Associativity.Left));
27+
registerInfixParselet(TokenType.Minus, new BinaryOperatorParselet(BinaryExpressionType.Substraction, Precedences.SUM, Associativity.Left));
28+
registerInfixParselet(TokenType.Star, new BinaryOperatorParselet(BinaryExpressionType.Multiplication, Precedences.PRODUCT, Associativity.Left));
29+
registerInfixParselet(TokenType.Identifier, new BinaryOperatorParselet(BinaryExpressionType.Multiplication, Precedences.PRODUCT, Associativity.Left));
30+
registerInfixParselet(TokenType.Slash, new BinaryOperatorParselet(BinaryExpressionType.Division, Precedences.PRODUCT, Associativity.Left));
31+
registerInfixParselet(TokenType.Pow, new BinaryOperatorParselet(BinaryExpressionType.Power, Precedences.EXPONENT, Associativity.Right));
32+
registerInfixParselet(TokenType.Percent, new BinaryOperatorParselet(BinaryExpressionType.Modulo, Precedences.PRODUCT, Associativity.Left));
2933
}
3034

3135
static void registerPrefixParselet(TokenType tokenType, IPrefixParselet prefixParselet)

shunting_yard/src/parser/parselets/AssignParselet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class AssignParselet : IInfixParselet
77
{
88
public IExpression Parse(ParseExpressionDelegate parseExpression, TokenStream tokenStream, IExpression leftExpression)
99
{
10-
IExpression rightExpression = parseExpression(tokenStream, Precedences.ASSIGNMENT + Associativity.Right.ToPrecedence());
10+
IExpression rightExpression = parseExpression(tokenStream, Precedences.ASSIGNMENT + Associativity.Right.ToPrecedenceIncrement());
1111

1212
if (leftExpression is VariableExpression)
1313
{

shunting_yard/src/parser/parselets/BinaryOperatorParselet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public BinaryOperatorParselet(BinaryExpressionType binaryExpressionType, int pre
1414

1515
public IExpression Parse(ParseExpressionDelegate parseExpression, TokenStream tokenStream, IExpression leftExpression)
1616
{
17-
IExpression rightExpression = parseExpression(tokenStream, Precedence + _associativity.ToPrecedence());
17+
IExpression rightExpression = parseExpression(tokenStream, Precedence + _associativity.ToPrecedenceIncrement());
1818
return new BinaryExpression(_binaryExpressionType, leftExpression, rightExpression);
1919
}
2020

shunting_yard/src/parser/parselets/ComparisonParselet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public ComparisonParselet(ComparisonExpressionType comparisonExpressionType, int
1414

1515
public IExpression Parse(ParseExpressionDelegate parseExpression, TokenStream tokenStream, IExpression leftExpression)
1616
{
17-
IExpression rightExpression = parseExpression(tokenStream, Precedence + _associativity.ToPrecedence());
17+
IExpression rightExpression = parseExpression(tokenStream, Precedence + _associativity.ToPrecedenceIncrement());
1818
return new ComparisonExpression(_comparisonExpressionType, leftExpression, rightExpression);
1919
}
2020

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace MathParser
2+
{
3+
class FixValueParselet : IPrefixParselet
4+
{
5+
readonly Value _value;
6+
7+
public FixValueParselet(Value value)
8+
{
9+
_value = value;
10+
}
11+
12+
public IExpression Parse(ParseExpressionDelegate parseExpression, TokenStream tokenStream, Token token)
13+
{
14+
return new ValueExpression(_value);
15+
}
16+
}
17+
}

shunting_yard/src/parser/parselets/TernaryParselet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public IExpression Parse(ParseExpressionDelegate parseExpression, TokenStream to
66
{
77
IExpression trueExpression = parseExpression(tokenStream);
88
tokenStream.Consume(TokenType.Colon);
9-
IExpression falseExpression = parseExpression(tokenStream, Precedences.CONDITIONAL + Associativity.Right.ToPrecedence());
9+
IExpression falseExpression = parseExpression(tokenStream, Precedences.CONDITIONAL + Associativity.Right.ToPrecedenceIncrement());
1010
return new TernaryExpression(leftExpression, trueExpression, falseExpression);
1111
}
1212

0 commit comments

Comments
 (0)