-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParseUtils.cs
51 lines (49 loc) · 2.52 KB
/
ParseUtils.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ToluPL
{
internal static class ParseUtils
{
public static Node TryParseNode(Node node, string StrConv)
{
bool done;
switch (StrConv)
{
case Values.T_INT:
int INTres;
done = Int32.TryParse(node.token.TValue.ToString(), out INTres);
if (done) return new Node(new Token(Values.T_INT, INTres));
else throw new Exception($"Invalid type conversion{node.token.TType}->{Values.T_INT}");
case Values.T_LONG:
long LONGres;
done = long.TryParse(node.token.TValue.ToString(), out LONGres);
if (done) return new Node(new Token(Values.T_LONG, LONGres));
else throw new Exception($"Invalid type conversion{node.token.TType}->{Values.T_LONG}");
case Values.T_FLOAT:
float FLOATres;
done = float.TryParse(node.token.TValue.ToString(), out FLOATres);
if (done) return new Node(new Token(Values.T_FLOAT, FLOATres));
else throw new Exception($"Invalid type conversion{node.token.TType}->{Values.T_FLOAT}");
case Values.T_DOUBLE:
double DOUBLEres;
done = double.TryParse(node.token.TValue.ToString(), out DOUBLEres);
if (done) return new Node(new Token(Values.T_FLOAT, DOUBLEres));
else throw new Exception($"Invalid type conversion{node.token.TType}->{Values.T_DOUBLE}");
case Values.T_STRING:
if (node.token.TType.ToString() == Values.T_STRING) return node;
throw new Exception($"Invalid type conversion{node.token.TType}->{Values.T_DOUBLE}");
case Values.T_LIST:
if (node.token.TType.ToString() == Values.T_LIST) return node;
throw new Exception($"Invalid type conversion{node.token.TType}->{Values.T_DOUBLE}");
case Values.T_BOOL:
if (node.token.TType.ToString() == Values.T_BOOL) return node;
throw new Exception($"Invalid type conversion{node.token.TType}->{Values.T_DOUBLE}");
default:
throw new Exception("Invalid type passed");
}
}
}
}