Skip to content

Commit a0599f8

Browse files
added identifier class
1 parent adc2de5 commit a0599f8

22 files changed

+223
-107
lines changed

Demo/Program.cs

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,58 +5,43 @@
55

66
namespace Demo
77
{
8-
public delegate void Funny();
98
class MainClass
109
{
11-
public static void digest(Object del)
12-
{ }
10+
const string exitCommand = "exit";
11+
static string currentLine;
1312

14-
15-
16-
public static void Test()
17-
{
18-
}
1913
public static void Main(string[] args)
2014
{
21-
string line;
2215
MathParser.MathParser mathParser = new MathParser.MathParser();
2316

2417
SymbolManager symbolManager = new SymbolManager();
2518
symbolManager.SetTrigonometrySymbols();
2619

27-
while ((line = Console.ReadLine()) != "exit")
20+
Console.WriteLine("Enter {0} to quit calculator.", exitCommand);
21+
22+
while (ReadNextLine())
2823
{
2924
try
3025
{
31-
ExpressionTree e = mathParser.Parse(line);
32-
try
33-
{
34-
//e.Assign();
35-
}
36-
catch (Exception e1)
37-
{
38-
Console.WriteLine("parse error: " + e1.Message);
39-
}
40-
41-
try
42-
{
43-
string detail = e.ToDebug();
44-
Console.Write(detail);
45-
46-
object d = e.Evaluate(symbolManager);
47-
Console.WriteLine(" = " + d);
48-
}
49-
catch (Exception e2)
50-
{
51-
Console.WriteLine("parse error: " + e2.Message);
52-
}
26+
ExpressionTree expressionTree = mathParser.Parse(currentLine);
5327

28+
string detail = expressionTree.ToDebug();
29+
Value result = expressionTree.Evaluate(symbolManager);
30+
31+
Console.WriteLine("{0} = {1}", detail, result);
5432
}
55-
catch (Exception fe)
33+
catch (Exception exception)
5634
{
57-
Console.WriteLine(fe.Message);
35+
Console.WriteLine("error: {0}", exception.Message);
5836
}
5937
}
6038
}
39+
40+
static bool ReadNextLine()
41+
{
42+
Console.Write("> ");
43+
currentLine = Console.ReadLine();
44+
return currentLine != exitCommand;
45+
}
6146
}
6247
}

ExpressionTest/ExpressionTest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<Compile Include="TestFunctions.cs" />
4242
<Compile Include="TestParser.cs" />
4343
<Compile Include="TestValueClass.cs" />
44+
<Compile Include="TestIdentifier.cs" />
4445
</ItemGroup>
4546
<ItemGroup>
4647
<None Include="packages.config" />

ExpressionTest/TestIdentifier.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using MathParser;
3+
using NUnit.Framework;
4+
5+
namespace ExpressionTest
6+
{
7+
[TestFixture]
8+
public class TestIdentifier
9+
{
10+
[Test]
11+
public void TestComparison()
12+
{
13+
Identifier identifierX = "x";
14+
Identifier identifierY = "y";
15+
16+
Assert.AreNotSame(identifierX, identifierY);
17+
Assert.AreNotEqual(identifierX, identifierY);
18+
Assert.IsTrue(identifierX != identifierY);
19+
// TODO more checks on valid identifiers
20+
}
21+
}
22+
}

ExpressionTest/TestValueClass.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,8 @@ Value someCalculationFunction(params Value[] arguments)
5050
{
5151
return null;
5252
}
53+
54+
// TODO test exception throwing on conversions
55+
5356
}
5457
}

ExpressionTest/TestVariables.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ public void TestTernary()
2323
{
2424
"1<2 ? 5 : 3".ShouldEvaluateTo(5);
2525
"2<1 ? 5 : 1<2 ? 22 : 20".ShouldEvaluateTo(22);
26-
27-
2826
}
2927
}
3028
}

ExpressionTest/TokenizerTest.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ public void TestSomeOperators()
7575
Token(TokenType.Identifier, "y", 4),
7676
Token(TokenType.EndOfFile, "", 5));
7777

78+
AssertTokensMatch("1<=2",
79+
Token(TokenType.Numeric, "1", 0),
80+
Token(TokenType.LessOrEqual, "<=", 1),
81+
Token(TokenType.Numeric, "2", 3),
82+
Token(TokenType.EndOfFile, "", 4));
7883
}
7984

8085
Token Token(TokenType tokenType, string value, int position)

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
[![Build status](https://ci.appveyor.com/api/projects/status/ud2t2nlje0si5grt/branch/master?svg=true)](https://ci.appveyor.com/project/timmi-on-rails/mathparser/branch/master)
22

33
Demo: https://ci.appveyor.com/api/projects/timmi-on-rails/mathparser/artifacts/Demo/bin/Demo.zip
4+
5+
TODO
6+
faculty !
7+
modulo %
8+
vector algebra
9+
evaluate stuff like "3x+2"
10+
11+
not 100% confident with the Identifier solution

shunting_yard/MathParser.csproj

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

shunting_yard/src/expressions/FunctionAssignmentExpression.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ namespace MathParser
55
{
66
class FunctionAssignmentExpression : IExpression
77
{
8-
public IExpression Expression { get; }
9-
108
public string FunctionName { get; }
119

12-
public IEnumerable<string> ArgumentNames { get; }
10+
public IEnumerable<Identifier> ArgumentNames { get; }
11+
12+
public IExpression Expression { get; }
1313

14-
public FunctionAssignmentExpression(string functionName, IEnumerable<string> arguments, IExpression expression)
14+
public FunctionAssignmentExpression(string functionName, IEnumerable<Identifier> arguments, IExpression expression)
1515
{
1616
FunctionName = functionName;
1717
Expression = expression;

shunting_yard/src/expressions/VariableAssignmentExpression.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
{
33
class VariableAssignmentExpression : IExpression
44
{
5-
public IExpression Expression { get; }
5+
public Identifier Identifier { get; }
66

7-
public string VariableName { get; }
7+
public IExpression Expression { get; }
88

9-
public VariableAssignmentExpression(string variableName, IExpression expression)
9+
public VariableAssignmentExpression(Identifier identifier, IExpression expression)
1010
{
11-
VariableName = variableName;
11+
Identifier = identifier;
1212
Expression = expression;
1313
}
1414

0 commit comments

Comments
 (0)