-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMethod.cs
158 lines (157 loc) · 6.1 KB
/
Method.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace MSC.Script
{
public class Method
{
public List<Instruction> Instructions = new List<Instruction>();
public MethodType Type { set; get; }
public int IndexPous { set; get; }
public int IndexPlay { set; get; }
public static MethodType GetPous(string Type)
{
var List = Enum.GetValues(typeof(MethodType)).Cast<MethodType>().ToList();
MethodType ReturnDef = MethodType.Unkown;
foreach (MethodType item in List)
if (Type.ToLower() == item.ToString().ToLower())
{
ReturnDef = item;
break;
}
if (ReturnDef != MethodType.Unkown)
return ReturnDef;
else
throw new Exception("Unkown Pous! ==> " + Type);
}
}
public class Body
{
public List<Method> Methods = new List<Method>();
public List<Method> Initialize(string[] Lines)
{
bool InAdding = false;
int indexplay = 0;
Method method = new Method();
for (int i = 0; i < Lines.Length; i++)
{
if(!IsValidLine(Lines[i]))
throw new Exception("Error > The instruction not valid => line:" + (i + 1));
if (!InAdding)
{
if (IsEndMethodLine(Lines[i])) throw new Exception("Error > The method hasn't end instruction => line:" + (i + 1));
if (IsStartMethodLine(Lines[i]))
{
if(InAdding)
throw new Exception("method have not end instruction! ==>" + method.Type.ToString());
indexplay++;
method = new Method();
method.IndexPlay = indexplay;
Match mh = Regex.Match(Lines[i], @"==>(.*?)<==");
string valuenow = mh.Groups[1].Value.ToString();
string[] st = valuenow.Split('-');
method.Type = Method.GetPous(st[0]);
if (st.Length == 2)
{
try
{
method.IndexPous = int.Parse(st[1]);
}
catch { throw new Exception("Couldn't get index pous value! ==>" + method.Type.ToString()); }
}
InAdding = true;
}
}
else
{
if (IsStartMethodLine(Lines[i])) throw new Exception("Error > The method need end instruction => line:" + (i + 1));
if (!IsEndMethodLine(Lines[i]))
{
if (!IsCommendLine(Lines[i]))
{
if (!string.IsNullOrWhiteSpace(Lines[i]))
{
try
{
Instruction line = Instruction.ReadLine(Lines[i]);
line.LineIndex = i;
method.Instructions.Add(line);
}catch
{
throw new Exception("Error > Can't read line ==> line:" + (i + 1));
}
}
}
}
else { InAdding = false; Methods.Add(method); }
}
}
if(InAdding == true)
throw new Exception("A method haven't end line!");
return Methods;
}
bool IsValidLine(string Line)
{
if (IsCommendLine(Line)) return true;
if (string.IsNullOrWhiteSpace(Line)) return true;
if (Line.Contains("=>")) return true;
if (Line.Contains("<==>")) return true;
if (Line.Contains("<=")) return true;
return false;
}
bool IsCommendLine(string Line)
{
if (Line.StartsWith(@"\*/"))
return true;
else return false;
}
bool IsEndMethodLine(string Line)
{
if (Line == "<==>")
return true;
else return false;
}
bool IsStartMethodLine(string Line)
{
if (string.IsNullOrWhiteSpace(Line))
return false;
if (Line.Substring(0, 3) == "==>" && Line.Substring(Line.Length - 3, 3) == "<==")
return true;
else return false;
}
}
public class Instruction
{
public override string ToString()
{
return Type.ToString() + "=>" + Value;
}
public OpCode Type { set; get; }
public string Value { set; get; }
public int LineIndex { set; get; }
public static Instruction ReadLine(string Line)
{
Instruction line = new Instruction();
int start = Line.IndexOf("=>");
line.Type = GetModule(Line.Substring(0, start));
line.Value = Line.Substring(start + 2, Line.Length - start - 2);
return line;
}
public static OpCode GetModule(string Type)
{
var List = Enum.GetValues(typeof(OpCode)).Cast<OpCode>().ToList();
OpCode ReturnDef = OpCode.Unkown;
foreach (OpCode item in List)
if (Type.ToLower() == item.ToString().ToLower())
{
ReturnDef = item;
break;
}
if (ReturnDef != OpCode.Unkown)
return ReturnDef;
else
throw new Exception("Unkown Module! ==> " + Type);
}
}
}