Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/release-alpha-0.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiashuo Li committed Apr 13, 2015
2 parents dfcfe78 + 2e2e850 commit ac845f0
Show file tree
Hide file tree
Showing 80 changed files with 1,162 additions and 1,344 deletions.
29 changes: 25 additions & 4 deletions HighLevelProgram/HLProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ public HLProgram(String programString)
{
program = new List<Instruction>();

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

// Ignore comment line
if (Instruction.IsValidInstructionLine(insStrArray[i]))
program.Add(new Instruction(insStrArray[i]));
// Remove comment and trim
string unifiedStr = Instruction.UnifyInstructionString(insStrArray[i]);
if (unifiedStr.Length > 0)
program.Add(new Instruction(unifiedStr));
}
}

Expand Down Expand Up @@ -70,7 +72,7 @@ public Instruction this[int index]
/// </summary>
/// <param name="ifLoopIns">The starting IF or LOOP instruction.</param>
/// <returns>The completed IF or LOOP block.</returns>
public static HLProgram GetIfLoopBlock(Instruction ifLoopIns)
public static HLProgram GetDefaultIfLoopBlock(Instruction ifLoopIns)
{
HLProgram result = new HLProgram();
result.Add(ifLoopIns);
Expand Down Expand Up @@ -254,11 +256,30 @@ public HLProgram SubProgram(int startIndex, int endIndex)
return hlp;
}

/// <summary>
/// Get the HLProgram string.
/// </summary>
/// <returns></returns>
public override string ToString()
{
// Use Windows style CRLF
// http://stackoverflow.com/questions/15433188/r-n-r-n-what-is-the-difference-between-them
return string.Join("\r\n", program);
}

/// <summary>
/// Get the HLProgram string, specifying if to attach the description comment.
/// </summary>
/// <param name="withDescription">If each instruction is trailed by description comment. </param>
/// <returns></returns>
public string ToString(bool withDescription)
{
StringBuilder sb = new StringBuilder();
foreach(Instruction ins in program)
{
sb.AppendLine(ins.ToString(withDescription));
}
return sb.ToString();
}
}
}
3 changes: 2 additions & 1 deletion HighLevelProgram/HighLevelProgram.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
Expand All @@ -48,6 +48,7 @@
<Compile Include="Operator.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Sensor.cs" />
<Compile Include="TextDescription.cs" />
<Compile Include="Validator.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
63 changes: 46 additions & 17 deletions HighLevelProgram/Instruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace iRobotGUI
Expand Down Expand Up @@ -82,19 +83,21 @@ public class Instruction

#region Constants

public const int STRAIGHT1 = 0x8000; // -32768
public const int STRAIGHT2 = 0x7FFF; // 32767
public const int STRAIGHT1 = 0x7FFF; // 32767
public const int STRAIGHT2 = 0x8000; // 32768
public const int TURN_IN_PLACE_CLOCKWISE = 0xFFFF;
public const int TURN_IN_PLACE_COUNTER_CLOCKWISE = 0x0001;

#endregion



/// <summary>
/// To construct the Instruction. The instruction string must be unified using UnifyInstructionString first.
/// </summary>
/// <param name="insStr"></param>
public Instruction(string insStr)
{
// Remove leading indent.
insStr = insStr.Trim(new char[] { ' ', '\t' });
// The string is already unified.

string opcode;
string[] paramArray;
Expand Down Expand Up @@ -201,23 +204,28 @@ public static Instruction CreatFromOpcode(string opcode)


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

// Empty line
if (insStr.Length == 0) return false;
// Trim the '\t' and ' '
insStr = insStr.Trim(new char[] { '\t', ' ' });

// Command line
if (insStr[0] == '/') return false;
else return true;
return insStr;
}

/// <summary>
Expand All @@ -231,5 +239,26 @@ public override string ToString()
sb.Append(" ").Append(string.Join(",", paramList));
return sb.ToString();
}

/// <summary>
/// Get the instruction string with the description.
/// </summary>
/// <param name="withDescription">If to have the description attached as comment.</param>
/// <returns></returns>
public string ToString(bool withDescription)
{
if (withDescription)
return this.ToString() + " // " + this.GetTextDescription();
else return this.ToString();
}

/// <summary>
/// Get the human-readable text description of the Instruction.
/// </summary>
/// <returns></returns>
public string GetTextDescription()
{
return TextDescription.GetTextDescription(this);
}
}
}
166 changes: 166 additions & 0 deletions HighLevelProgram/TextDescription.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace iRobotGUI
{
/// <summary>
/// This class generates the text description for an <see cref="Instruction"/>.
/// </summary>
/// <remarks>
/// The implementation of this class is similar to that of the Translator in TranslatorLib,
/// since it translates the instruction to human-readable format instead of C code.
/// </remarks>
public static class TextDescription
{

private static string GetMoveDescription(Instruction ins)
{
if (ins.paramList[0] < 0)
return string.Format("Move backward {0}cm in {1}s.", -ins.paramList[0], ins.paramList[1]);
else if (ins.paramList[0] > 0)
return string.Format("Move forward {0}cm in {1}s.", ins.paramList[0], ins.paramList[1]);
else if (ins.paramList[0] == 0)
return "Stop moving.";

// For return check.
return "";
}

private static string GetRotateDescription(Instruction ins)
{
if (ins.paramList[0] < 0)
return string.Format("Turn right by {0} degree(s).", -ins.paramList[0]);
else if (ins.paramList[0] > 0)
return string.Format("Turn left by {0} degree(s).", ins.paramList[0]);
else if (ins.paramList[0] == 0)
return "Stop rotating.";

// For return check.
return "";
}

private static string GetDriveDescription(Instruction ins)
{
if (ins.paramList[0] < 0 && ins.paramList[1] == 32767)
return string.Format("Drive forward straightly at {0}mm/s.", -ins.paramList[0]);
else if (ins.paramList[0] > 0 && ins.paramList[1] == 32767)
return string.Format("Drive backward straightly at {0}mm/s.", ins.paramList[0]);
else if (ins.paramList[0] < 0 && ins.paramList[1] < 0)
return string.Format("Drive backward while turning right at {0}mm/s with radius {1}mm.", -ins.paramList[0], -ins.paramList[1]);
else if (ins.paramList[0] < 0 && ins.paramList[1] > 0)
return string.Format("Drive backward while turning left at {0}mm/s with radius {1}mm.", ins.paramList[0], ins.paramList[1]);
else if (ins.paramList[0] > 0 && ins.paramList[1] < 0)
return string.Format("Drive forward while turning right at {0}mm/s with radius {1}mm.", -ins.paramList[0], -ins.paramList[1]);
else if (ins.paramList[0] > 0 && ins.paramList[1] > 0)
return string.Format("Drive forward while turning left at {0}mm/s with radius {1}mm.", ins.paramList[0], ins.paramList[1]);
else if (ins.paramList[0] == 0)
return "Stop driving.";

// For return check.
return "";
}

private static string GetLedDescription(Instruction ins)
{
if (ins.paramList[0] == 0)
return string.Format("Turn off both Play LED and Advance LED.");
else if (ins.paramList[0] == 2)
return string.Format("Turn on Play LED.");
else if (ins.paramList[0] == 8)
return "Turn on Advance LED.";
else if (ins.paramList[0] == 10)
return "Turn on both Play LED and Advance LED.";

// For return check.
return "";
}

private static string GetSongDescription(Instruction ins)
{
return string.Format("Song");
}

private static string GetIfDescription(Instruction ins)
{
return string.Format("If");
}

private static string GetLoopDescription(Instruction ins)
{
return string.Format("Loop");
}

private static string GetDemoDescription(Instruction ins)
{
switch (ins.paramList[0])
{
case 0:
return string.Format("Play demo Cover");
case 1:
return string.Format("Play demo Cover and Dock");
case 2:
return string.Format("Play demo Spot Cover");
case 3:
return string.Format("Play demo Mouse");
case 4:
return string.Format("Play demo Drive Figure Eight");
case 5:
return string.Format("Play demo Wimp");
case 6:
return string.Format("Play demo Home");
case 7:
return string.Format("Play demo Tag");
case 8:
return string.Format("Play demo Pachelbel");
case 9:
return string.Format("Play demo Banjo");
case -1:
return string.Format("Stop the demo");
}

// For return check.
return "";
}

private static string GetDelayDescription(Instruction ins)
{
return string.Format("Delay for {0}ms.", ins.paramList[0]);
}

/// <summary>
/// Get the text description for the spefified Instruction.
/// </summary>
/// <param name="ins"></param>
/// <returns></returns>
public static string GetTextDescription(Instruction ins)
{
switch (ins.opcode)
{
case Instruction.MOVE:
return GetMoveDescription(ins);
case Instruction.ROTATE:
return GetRotateDescription(ins);
case Instruction.DRIVE:
return GetDriveDescription(ins);
case Instruction.LED:
return GetLedDescription(ins);
case Instruction.SONG:
return GetSongDescription(ins);
case Instruction.IF:
return GetIfDescription(ins);
case Instruction.LOOP:
return GetLoopDescription(ins);
case Instruction.DEMO:
return GetDemoDescription(ins);
case Instruction.DELAY:
return GetDelayDescription(ins);
default:
return "Description not implemented.";
}

}
}
}
Loading

0 comments on commit ac845f0

Please sign in to comment.