Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. using Antlr.Runtime;
  8. using Antlr.Runtime.Tree;
  9.  
  10. namespace RSD.Scripting
  11. {
  12.     using Robot;
  13.  
  14.     class Program
  15.     {
  16.         const string CRLF = "\r\n";
  17.  
  18.         static ScorBotScriptInstructor instructor;
  19.  
  20.         static void Main(string[] args)
  21.         {
  22.             string filename = "Script.txt";
  23.  
  24.             var reader = new StreamReader(filename);
  25.             var input = new ANTLRReaderStream(reader);
  26.             var lexer = new ScorBotScriptLexer(input);
  27.             var tokens = new CommonTokenStream(lexer);
  28.             var parser = new ScorBotScriptParser(tokens);
  29.  
  30.             try
  31.             {
  32.                 var result = parser.program();
  33.                 var tree = result.Tree as CommonTree;
  34.  
  35.                 instructor = new ScorBotScriptInstructor(new ScorBotSimulator());
  36.  
  37.                 foreach (var child in tree.Children)
  38.                 {
  39.                     Parse(child as CommonTree);
  40.                 }
  41.             }
  42.             catch (RecognitionException e)
  43.             {
  44.                 Console.WriteLine("{0}: {1}",
  45.                     parser.GetErrorHeader(e),
  46.                     parser.GetErrorMessage(e, parser.TokenNames));
  47.             }
  48.  
  49.             Console.Read();
  50.         }
  51.  
  52.         static bool isRepeating = true;
  53.  
  54.         static void Parse(CommonTree tree)
  55.         {
  56.             if (tree.Token == null)
  57.             {
  58.                 return;
  59.             }
  60.  
  61.             switch (tree.Token.Text)
  62.             {
  63.                 case "INIT":
  64.                     instructor.Init();
  65.                     break;
  66.                 case "HOME":
  67.                     instructor.Home(tree.GetChild(0).Text);
  68.                     break;
  69.                 case "REPEAT_START":
  70.                     {
  71.                         isRepeating = true;
  72.                         while (isRepeating)
  73.                         {
  74.                             foreach (var child in tree.Children)
  75.                             {
  76.                                 Parse(child as CommonTree);
  77.                             }
  78.                         }
  79.                     }
  80.                     break;
  81.                 case "MOVE":
  82.                     instructor.Move(tree.GetChild(0).Text);
  83.                     break;
  84.                 case "MOVE_C":
  85.                     instructor.Move(
  86.                         tree.GetChild(0).Text,
  87.                         tree.GetChild(1).Text,
  88.                         tree.GetChild(2).Text,
  89.                         tree.GetChild(3).Text,
  90.                         tree.GetChild(4).Text);
  91.                     break;
  92.                 case "ROLL":
  93.                     instructor.Roll(tree.GetChild(0).Text);                    
  94.                     break;
  95.                 case "WAIT_FOR_CONVEYOR":
  96.                     instructor.WaitForConveyor();
  97.                     break;
  98.                 case "OPEN_GRIPPER":
  99.                     instructor.OpenGripper();
  100.                     break;
  101.                 case "CLOSE_GRIPPER":
  102.                     instructor.CloseGripper();
  103.                     break;
  104.                 case "MEASURE_JAW":
  105.                     instructor.MeasureJaw(tree.GetChild(0).Text);
  106.                     break;
  107.                 case "MEASURE_WEIGHT":
  108.                     instructor.MeasureWeight();
  109.                     break;
  110.                 case "MOVE_SORT":
  111.                     instructor.MoveToSortedLocation();
  112.                     break;
  113.             }
  114.         }
  115.  
  116.         static void Print(CommonTree tree, string indent)
  117.         {
  118.             Console.WriteLine(indent + tree.ToString());
  119.  
  120.             if (tree.Children != null)
  121.             {
  122.                 indent += "\t";
  123.  
  124.                 foreach (var child in tree.Children)
  125.                 {
  126.                     var childTree = child as CommonTree;
  127.  
  128.                     if (childTree.Text != CRLF)
  129.                     {
  130.                         Print(childTree, indent);
  131.                     }
  132.                 }
  133.             }
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement