-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
527 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using expressionparser.model; | ||
|
||
namespace ParserExample.expressionModel | ||
{ | ||
public class BinaryOperation : Expression | ||
{ | ||
private readonly Expression LeftExpresion; | ||
private readonly FormulaToken Operator; | ||
private readonly Expression RightExpression; | ||
|
||
|
||
public BinaryOperation(Expression left, FormulaToken op, Expression right) | ||
{ | ||
LeftExpresion = left; | ||
Operator = op; | ||
RightExpression = right; | ||
} | ||
|
||
public double? Evaluate(ExpressionContext context) | ||
{ | ||
var left = LeftExpresion.Evaluate(context); | ||
var right = RightExpression.Evaluate(context); | ||
|
||
if (left.HasValue && right.HasValue) | ||
switch (Operator) | ||
{ | ||
case FormulaToken.PLUS: | ||
{ | ||
return left.Value + right.Value; | ||
} | ||
case FormulaToken.MINUS: | ||
{ | ||
return left.Value - right.Value; | ||
} | ||
case FormulaToken.TIMES: | ||
{ | ||
return left.Value * right.Value; | ||
} | ||
case FormulaToken.DIVIDE: | ||
{ | ||
return left.Value / right.Value; | ||
} | ||
case FormulaToken.EXP: | ||
{ | ||
return Math.Pow(left.Value,right.Value); | ||
} | ||
default: | ||
{ | ||
return null; | ||
} | ||
} | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using expressionparser.model; | ||
|
||
namespace ParserExample.expressionModel | ||
{ | ||
public interface Expression | ||
{ | ||
double? Evaluate(ExpressionContext context); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
samples/ParserExample/expressionModel/ExpressionContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace ParserExample.expressionModel | ||
{ | ||
public class ExpressionContext | ||
{ | ||
private readonly Dictionary<string, double> Variables; | ||
|
||
public ExpressionContext() | ||
{ | ||
Variables = new Dictionary<string, double>(); | ||
} | ||
|
||
public ExpressionContext(Dictionary<string, double> variables) | ||
{ | ||
Variables = variables; | ||
} | ||
|
||
public double? GetValue(string variable) | ||
{ | ||
if (Variables.ContainsKey(variable)) return Variables[variable]; | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using sly.lexer; | ||
using sly.parser.generator; | ||
using sly.parser.parser; | ||
|
||
namespace ParserExample.expressionModel | ||
{ | ||
public class FormulaParser | ||
{ | ||
[Operation((int) FormulaToken.PLUS, Affix.InFix, Associativity.Right, 10)] | ||
[Operation((int) FormulaToken.MINUS, Affix.InFix, Associativity.Left, 10)] | ||
public Expression BinaryTermExpression(Expression left, Token<FormulaToken> operation, Expression right) | ||
{ | ||
return new BinaryOperation(left, operation.TokenID, right); | ||
} | ||
|
||
|
||
[Operation((int) FormulaToken.TIMES, Affix.InFix, Associativity.Right, 50)] | ||
[Operation((int) FormulaToken.DIVIDE, Affix.InFix, Associativity.Left, 50)] | ||
public Expression BinaryFactorExpression(Expression left, Token<FormulaToken> operation, Expression right) | ||
{ | ||
return new BinaryOperation(left, operation.TokenID, right); | ||
} | ||
|
||
|
||
[Operation((int) FormulaToken.MINUS, Affix.PreFix, Associativity.Right, 100)] | ||
public Expression PreFixExpression(Token<FormulaToken> operation, Expression value) | ||
{ | ||
return new UnaryOperation(FormulaToken.MINUS,value); | ||
} | ||
|
||
[Operation((int) FormulaToken.EXP, Affix.InFix, Associativity.Left, 90)] | ||
public Expression ExpExpression(Expression left, Token<FormulaToken> operation, Expression right) | ||
{ | ||
return new BinaryOperation(left, operation.TokenID, right); | ||
} | ||
|
||
[Operand] | ||
[Production("operand : primary_value")] | ||
public Expression OperandValue(Expression value) | ||
{ | ||
return value; | ||
} | ||
|
||
|
||
[Production("primary_value : IDENTIFIER")] | ||
public Expression OperandVariable(Token<FormulaToken> identifier) | ||
{ | ||
return new Variable(identifier.Value); | ||
} | ||
|
||
[Production("primary_value : DOUBLE")] | ||
[Production("primary_value : INT")] | ||
public Expression OperandInt(Token<FormulaToken> value) | ||
{ | ||
return new Number(value.DoubleValue); | ||
} | ||
|
||
[Production("primary_value : LPAREN FormulaParser_expressions RPAREN")] | ||
public Expression OperandParens(Token<FormulaToken> lparen, Expression value, Token<FormulaToken> rparen) | ||
{ | ||
return value; | ||
} | ||
|
||
[Production( | ||
"primary_value : IDENTIFIER LPAREN[d] FormulaParser_expressions (COMMA FormulaParser_expressions)* RPAREN[d]")] | ||
public Expression FunctionCall(Token<FormulaToken> funcName, Expression first, | ||
List<Group<FormulaToken, Expression>> others) | ||
{ | ||
List<Expression> parameters = new List<Expression>(); | ||
parameters.Add(first); | ||
parameters.AddRange(others.Select(x => x.Value(0))); | ||
return new FunctionCall(funcName.Value, parameters); | ||
} | ||
} | ||
} |
Oops, something went wrong.