forked from EdisonTalk/DesignPattern.Samples.CSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.cs
165 lines (150 loc) · 4.65 KB
/
Node.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
159
160
161
162
163
164
165
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EDC.DesignPattern.Interpreter
{
/// <summary>
/// 抽象表达式:抽象节点类
/// </summary>
public abstract class Node
{
// 声明一个方法用于解释语句
public abstract void Interpret(Context context);
// 声明一个方法用于执行标记对应的命令
public abstract void Execute();
}
/// <summary>
/// 非终结符表达式:表达式节点类
/// </summary>
public class ExpressionNode : Node
{
// 用于存储多条命令的集合
private IList<Node> nodeList = new List<Node>();
public override void Interpret(Context context)
{
// 循环处理Context中的标记
while (true)
{
// 如果已经没有任何标记,则退出解释
if (context.GetCurrentToken() == null)
{
break;
}
// 如果标记为END,则不解释END并结束本次解释过程,可以继续之后的解释
else if (context.GetCurrentToken().Equals("END", StringComparison.OrdinalIgnoreCase))
{
context.SkipToken("END");
break;
}
// 如果为其它标记,则解释标记并加入命令集合
else
{
Node node = new CommandNode();
node.Interpret(context);
nodeList.Add(node);
}
}
}
// 循环执行命令集合中的每一条指令
public override void Execute()
{
foreach (var node in nodeList)
{
node.Execute();
}
}
}
/// <summary>
/// 非终结符表达式:语句命令节点类
/// </summary>
public class CommandNode : Node
{
private Node node;
public override void Interpret(Context context)
{
// 处理LOOP指令
if (context.GetCurrentToken().Equals("LOOP", StringComparison.OrdinalIgnoreCase))
{
node = new LoopCommand();
node.Interpret(context);
}
// 处理其他指令
else
{
node = new PrimitiveCommand();
node.Interpret(context);
}
}
public override void Execute()
{
node.Execute();
}
}
/// <summary>
/// 非终结符表达式:循环命令类
/// </summary>
public class LoopCommand : Node
{
// 循环次数
private int number;
// 循环语句中的表达式
private Node commandNode;
public override void Interpret(Context context)
{
context.SkipToken("LOOP");
number = context.GetCurrentNumber();
context.NextToken();
// 循环语句中的表达式
commandNode = new ExpressionNode();
commandNode.Interpret(context);
}
public override void Execute()
{
for (int i = 0; i < number; i++)
{
commandNode.Execute();
}
}
}
/// <summary>
/// 终结符表达式:基本命令类
/// </summary>
public class PrimitiveCommand : Node
{
private string name;
private string text;
public override void Interpret(Context context)
{
name = context.GetCurrentToken();
context.SkipToken(name);
if (!name.Equals("PRINT", StringComparison.OrdinalIgnoreCase)
&& !name.Equals("BREAK", StringComparison.OrdinalIgnoreCase)
&& !name.Equals("SPACE", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("非法命令!");
}
if (name.Equals("PRINT", StringComparison.OrdinalIgnoreCase))
{
text = context.GetCurrentToken();
context.NextToken();
}
}
public override void Execute()
{
if (name.Equals("PRINT", StringComparison.OrdinalIgnoreCase))
{
Console.Write(text);
}
else if (name.Equals("SPACE", StringComparison.OrdinalIgnoreCase))
{
Console.Write(" ");
}
else if (name.Equals("BREAK", StringComparison.OrdinalIgnoreCase))
{
Console.Write("\r\n");
}
}
}
}