Skip to content

Commit ac845f0

Browse files
author
Jiashuo Li
committed
Merge remote-tracking branch 'origin/release-alpha-0.0.1'
2 parents dfcfe78 + 2e2e850 commit ac845f0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+1162
-1344
lines changed

HighLevelProgram/HLProgram.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ public HLProgram(String programString)
2525
{
2626
program = new List<Instruction>();
2727

28+
// Split the string by CR or LF
2829
string[] insStrArray = programString.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
2930
for (int i = 0; i < insStrArray.Length; i++)
3031
{
3132
CurrentLine = i;
3233

33-
// Ignore comment line
34-
if (Instruction.IsValidInstructionLine(insStrArray[i]))
35-
program.Add(new Instruction(insStrArray[i]));
34+
// Remove comment and trim
35+
string unifiedStr = Instruction.UnifyInstructionString(insStrArray[i]);
36+
if (unifiedStr.Length > 0)
37+
program.Add(new Instruction(unifiedStr));
3638
}
3739
}
3840

@@ -70,7 +72,7 @@ public Instruction this[int index]
7072
/// </summary>
7173
/// <param name="ifLoopIns">The starting IF or LOOP instruction.</param>
7274
/// <returns>The completed IF or LOOP block.</returns>
73-
public static HLProgram GetIfLoopBlock(Instruction ifLoopIns)
75+
public static HLProgram GetDefaultIfLoopBlock(Instruction ifLoopIns)
7476
{
7577
HLProgram result = new HLProgram();
7678
result.Add(ifLoopIns);
@@ -254,11 +256,30 @@ public HLProgram SubProgram(int startIndex, int endIndex)
254256
return hlp;
255257
}
256258

259+
/// <summary>
260+
/// Get the HLProgram string.
261+
/// </summary>
262+
/// <returns></returns>
257263
public override string ToString()
258264
{
259265
// Use Windows style CRLF
260266
// http://stackoverflow.com/questions/15433188/r-n-r-n-what-is-the-difference-between-them
261267
return string.Join("\r\n", program);
262268
}
269+
270+
/// <summary>
271+
/// Get the HLProgram string, specifying if to attach the description comment.
272+
/// </summary>
273+
/// <param name="withDescription">If each instruction is trailed by description comment. </param>
274+
/// <returns></returns>
275+
public string ToString(bool withDescription)
276+
{
277+
StringBuilder sb = new StringBuilder();
278+
foreach(Instruction ins in program)
279+
{
280+
sb.AppendLine(ins.ToString(withDescription));
281+
}
282+
return sb.ToString();
283+
}
263284
}
264285
}

HighLevelProgram/HighLevelProgram.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
</DocumentationFile>
2525
</PropertyGroup>
2626
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27-
<DebugType>pdbonly</DebugType>
27+
<DebugType>none</DebugType>
2828
<Optimize>true</Optimize>
2929
<OutputPath>bin\Release\</OutputPath>
3030
<DefineConstants>TRACE</DefineConstants>
@@ -48,6 +48,7 @@
4848
<Compile Include="Operator.cs" />
4949
<Compile Include="Properties\AssemblyInfo.cs" />
5050
<Compile Include="Sensor.cs" />
51+
<Compile Include="TextDescription.cs" />
5152
<Compile Include="Validator.cs" />
5253
</ItemGroup>
5354
<ItemGroup>

HighLevelProgram/Instruction.cs

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
5+
using System.Text.RegularExpressions;
56
using System.Threading.Tasks;
67

78
namespace iRobotGUI
@@ -82,19 +83,21 @@ public class Instruction
8283

8384
#region Constants
8485

85-
public const int STRAIGHT1 = 0x8000; // -32768
86-
public const int STRAIGHT2 = 0x7FFF; // 32767
86+
public const int STRAIGHT1 = 0x7FFF; // 32767
87+
public const int STRAIGHT2 = 0x8000; // 32768
8788
public const int TURN_IN_PLACE_CLOCKWISE = 0xFFFF;
8889
public const int TURN_IN_PLACE_COUNTER_CLOCKWISE = 0x0001;
8990

9091
#endregion
9192

9293

93-
94+
/// <summary>
95+
/// To construct the Instruction. The instruction string must be unified using UnifyInstructionString first.
96+
/// </summary>
97+
/// <param name="insStr"></param>
9498
public Instruction(string insStr)
9599
{
96-
// Remove leading indent.
97-
insStr = insStr.Trim(new char[] { ' ', '\t' });
100+
// The string is already unified.
98101

99102
string opcode;
100103
string[] paramArray;
@@ -201,23 +204,28 @@ public static Instruction CreatFromOpcode(string opcode)
201204

202205

203206
/// <summary>
204-
/// Decide if an instruction string is a valid instruction.
205-
/// Comment line is prefixed by "//" like C.
207+
/// Unify the string. Remove the comment and trim the leading and trailing empty characters.
208+
///
209+
/// Comment is prefixed by "//" like C, which can be put in a single line or at the end of
210+
/// the line.
211+
///
206212
/// Empty line contains only '\t' and ' '.
207213
/// </summary>
208-
/// <param name="insStr"></param>
209-
/// <returns>True if it is.</returns>
210-
public static bool IsValidInstructionLine(string insStr)
214+
/// <param name="insStr">The string to unify.</param>
215+
/// <returns>Unified instruction string.</returns>
216+
public static string UnifyInstructionString(string insStr)
211217
{
212-
// Trim the \t and space
213-
insStr = insStr.Trim(new char[] { '\t', ' ' });
218+
// Remove comment
219+
// https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx
220+
// This pattern matches all substrings starting with // and all following chars.
221+
string commentPattern = "//.*";
222+
Regex rgx = new Regex(commentPattern);
223+
insStr = rgx.Replace(insStr, string.Empty);
214224

215-
// Empty line
216-
if (insStr.Length == 0) return false;
225+
// Trim the '\t' and ' '
226+
insStr = insStr.Trim(new char[] { '\t', ' ' });
217227

218-
// Command line
219-
if (insStr[0] == '/') return false;
220-
else return true;
228+
return insStr;
221229
}
222230

223231
/// <summary>
@@ -231,5 +239,26 @@ public override string ToString()
231239
sb.Append(" ").Append(string.Join(",", paramList));
232240
return sb.ToString();
233241
}
242+
243+
/// <summary>
244+
/// Get the instruction string with the description.
245+
/// </summary>
246+
/// <param name="withDescription">If to have the description attached as comment.</param>
247+
/// <returns></returns>
248+
public string ToString(bool withDescription)
249+
{
250+
if (withDescription)
251+
return this.ToString() + " // " + this.GetTextDescription();
252+
else return this.ToString();
253+
}
254+
255+
/// <summary>
256+
/// Get the human-readable text description of the Instruction.
257+
/// </summary>
258+
/// <returns></returns>
259+
public string GetTextDescription()
260+
{
261+
return TextDescription.GetTextDescription(this);
262+
}
234263
}
235264
}

HighLevelProgram/TextDescription.cs

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace iRobotGUI
8+
{
9+
/// <summary>
10+
/// This class generates the text description for an <see cref="Instruction"/>.
11+
/// </summary>
12+
/// <remarks>
13+
/// The implementation of this class is similar to that of the Translator in TranslatorLib,
14+
/// since it translates the instruction to human-readable format instead of C code.
15+
/// </remarks>
16+
public static class TextDescription
17+
{
18+
19+
private static string GetMoveDescription(Instruction ins)
20+
{
21+
if (ins.paramList[0] < 0)
22+
return string.Format("Move backward {0}cm in {1}s.", -ins.paramList[0], ins.paramList[1]);
23+
else if (ins.paramList[0] > 0)
24+
return string.Format("Move forward {0}cm in {1}s.", ins.paramList[0], ins.paramList[1]);
25+
else if (ins.paramList[0] == 0)
26+
return "Stop moving.";
27+
28+
// For return check.
29+
return "";
30+
}
31+
32+
private static string GetRotateDescription(Instruction ins)
33+
{
34+
if (ins.paramList[0] < 0)
35+
return string.Format("Turn right by {0} degree(s).", -ins.paramList[0]);
36+
else if (ins.paramList[0] > 0)
37+
return string.Format("Turn left by {0} degree(s).", ins.paramList[0]);
38+
else if (ins.paramList[0] == 0)
39+
return "Stop rotating.";
40+
41+
// For return check.
42+
return "";
43+
}
44+
45+
private static string GetDriveDescription(Instruction ins)
46+
{
47+
if (ins.paramList[0] < 0 && ins.paramList[1] == 32767)
48+
return string.Format("Drive forward straightly at {0}mm/s.", -ins.paramList[0]);
49+
else if (ins.paramList[0] > 0 && ins.paramList[1] == 32767)
50+
return string.Format("Drive backward straightly at {0}mm/s.", ins.paramList[0]);
51+
else if (ins.paramList[0] < 0 && ins.paramList[1] < 0)
52+
return string.Format("Drive backward while turning right at {0}mm/s with radius {1}mm.", -ins.paramList[0], -ins.paramList[1]);
53+
else if (ins.paramList[0] < 0 && ins.paramList[1] > 0)
54+
return string.Format("Drive backward while turning left at {0}mm/s with radius {1}mm.", ins.paramList[0], ins.paramList[1]);
55+
else if (ins.paramList[0] > 0 && ins.paramList[1] < 0)
56+
return string.Format("Drive forward while turning right at {0}mm/s with radius {1}mm.", -ins.paramList[0], -ins.paramList[1]);
57+
else if (ins.paramList[0] > 0 && ins.paramList[1] > 0)
58+
return string.Format("Drive forward while turning left at {0}mm/s with radius {1}mm.", ins.paramList[0], ins.paramList[1]);
59+
else if (ins.paramList[0] == 0)
60+
return "Stop driving.";
61+
62+
// For return check.
63+
return "";
64+
}
65+
66+
private static string GetLedDescription(Instruction ins)
67+
{
68+
if (ins.paramList[0] == 0)
69+
return string.Format("Turn off both Play LED and Advance LED.");
70+
else if (ins.paramList[0] == 2)
71+
return string.Format("Turn on Play LED.");
72+
else if (ins.paramList[0] == 8)
73+
return "Turn on Advance LED.";
74+
else if (ins.paramList[0] == 10)
75+
return "Turn on both Play LED and Advance LED.";
76+
77+
// For return check.
78+
return "";
79+
}
80+
81+
private static string GetSongDescription(Instruction ins)
82+
{
83+
return string.Format("Song");
84+
}
85+
86+
private static string GetIfDescription(Instruction ins)
87+
{
88+
return string.Format("If");
89+
}
90+
91+
private static string GetLoopDescription(Instruction ins)
92+
{
93+
return string.Format("Loop");
94+
}
95+
96+
private static string GetDemoDescription(Instruction ins)
97+
{
98+
switch (ins.paramList[0])
99+
{
100+
case 0:
101+
return string.Format("Play demo Cover");
102+
case 1:
103+
return string.Format("Play demo Cover and Dock");
104+
case 2:
105+
return string.Format("Play demo Spot Cover");
106+
case 3:
107+
return string.Format("Play demo Mouse");
108+
case 4:
109+
return string.Format("Play demo Drive Figure Eight");
110+
case 5:
111+
return string.Format("Play demo Wimp");
112+
case 6:
113+
return string.Format("Play demo Home");
114+
case 7:
115+
return string.Format("Play demo Tag");
116+
case 8:
117+
return string.Format("Play demo Pachelbel");
118+
case 9:
119+
return string.Format("Play demo Banjo");
120+
case -1:
121+
return string.Format("Stop the demo");
122+
}
123+
124+
// For return check.
125+
return "";
126+
}
127+
128+
private static string GetDelayDescription(Instruction ins)
129+
{
130+
return string.Format("Delay for {0}ms.", ins.paramList[0]);
131+
}
132+
133+
/// <summary>
134+
/// Get the text description for the spefified Instruction.
135+
/// </summary>
136+
/// <param name="ins"></param>
137+
/// <returns></returns>
138+
public static string GetTextDescription(Instruction ins)
139+
{
140+
switch (ins.opcode)
141+
{
142+
case Instruction.MOVE:
143+
return GetMoveDescription(ins);
144+
case Instruction.ROTATE:
145+
return GetRotateDescription(ins);
146+
case Instruction.DRIVE:
147+
return GetDriveDescription(ins);
148+
case Instruction.LED:
149+
return GetLedDescription(ins);
150+
case Instruction.SONG:
151+
return GetSongDescription(ins);
152+
case Instruction.IF:
153+
return GetIfDescription(ins);
154+
case Instruction.LOOP:
155+
return GetLoopDescription(ins);
156+
case Instruction.DEMO:
157+
return GetDemoDescription(ins);
158+
case Instruction.DELAY:
159+
return GetDelayDescription(ins);
160+
default:
161+
return "Description not implemented.";
162+
}
163+
164+
}
165+
}
166+
}

0 commit comments

Comments
 (0)