-
Notifications
You must be signed in to change notification settings - Fork 0
/
Token.cs
70 lines (62 loc) · 1.86 KB
/
Token.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Collections.Generic;
namespace callculator
{
public enum TokenType { None, Number, Function, FunctionArgSeparator, Operator, LeftParen, RightParen };
public class Token
{
private static readonly Dictionary<string, TokenType> validTokens = new Dictionary<string, TokenType>() {
{"^", TokenType.Operator},
{"*", TokenType.Operator},
{"/", TokenType.Operator},
{"+", TokenType.Operator},
{"-", TokenType.Operator},
{"#", TokenType.Operator},
{"@", TokenType.Operator},
{"(", TokenType.LeftParen},
{")", TokenType.RightParen},
{",", TokenType.FunctionArgSeparator},
{"sin", TokenType.Function},
{"cos", TokenType.Function},
{"tan", TokenType.Function},
{"abs", TokenType.Function},
{"max", TokenType.Function},
{"min", TokenType.Function},
{"neg", TokenType.Function},
{"avg", TokenType.Function},
{"sqrt", TokenType.Function},
{"vavg", TokenType.Function}
};
TokenType type;
string value;
public TokenType Type
{
get
{
return type;
}
}
public String Value
{
get
{
return value;
}
}
public Token(string value)
{
this.value = value;
this.type = GetTokenType();
}
private TokenType GetTokenType()
{
TokenType tokenType;
if (!validTokens.TryGetValue(this.value, out tokenType))
{
// TODO may not actually be a number.
tokenType = TokenType.Number;
}
return tokenType;
}
}
}