Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using Antlr.Runtime;
- using Antlr.Runtime.Tree;
- namespace RSD.Scripting
- {
- using Robot;
- class Program
- {
- const string CRLF = "\r\n";
- static ScorBotScriptInstructor instructor;
- static void Main(string[] args)
- {
- string filename = "Script.txt";
- var reader = new StreamReader(filename);
- var input = new ANTLRReaderStream(reader);
- var lexer = new ScorBotScriptLexer(input);
- var tokens = new CommonTokenStream(lexer);
- var parser = new ScorBotScriptParser(tokens);
- try
- {
- var result = parser.program();
- var tree = result.Tree as CommonTree;
- instructor = new ScorBotScriptInstructor(new ScorBotSimulator());
- foreach (var child in tree.Children)
- {
- Parse(child as CommonTree);
- }
- }
- catch (RecognitionException e)
- {
- Console.WriteLine("{0}: {1}",
- parser.GetErrorHeader(e),
- parser.GetErrorMessage(e, parser.TokenNames));
- }
- Console.Read();
- }
- static bool isRepeating = true;
- static void Parse(CommonTree tree)
- {
- if (tree.Token == null)
- {
- return;
- }
- switch (tree.Token.Text)
- {
- case "INIT":
- instructor.Init();
- break;
- case "HOME":
- instructor.Home(tree.GetChild(0).Text);
- break;
- case "REPEAT_START":
- {
- isRepeating = true;
- while (isRepeating)
- {
- foreach (var child in tree.Children)
- {
- Parse(child as CommonTree);
- }
- }
- }
- break;
- case "MOVE":
- instructor.Move(tree.GetChild(0).Text);
- break;
- case "MOVE_C":
- instructor.Move(
- tree.GetChild(0).Text,
- tree.GetChild(1).Text,
- tree.GetChild(2).Text,
- tree.GetChild(3).Text,
- tree.GetChild(4).Text);
- break;
- case "ROLL":
- instructor.Roll(tree.GetChild(0).Text);
- break;
- case "WAIT_FOR_CONVEYOR":
- instructor.WaitForConveyor();
- break;
- case "OPEN_GRIPPER":
- instructor.OpenGripper();
- break;
- case "CLOSE_GRIPPER":
- instructor.CloseGripper();
- break;
- case "MEASURE_JAW":
- instructor.MeasureJaw(tree.GetChild(0).Text);
- break;
- case "MEASURE_WEIGHT":
- instructor.MeasureWeight();
- break;
- case "MOVE_SORT":
- instructor.MoveToSortedLocation();
- break;
- }
- }
- static void Print(CommonTree tree, string indent)
- {
- Console.WriteLine(indent + tree.ToString());
- if (tree.Children != null)
- {
- indent += "\t";
- foreach (var child in tree.Children)
- {
- var childTree = child as CommonTree;
- if (childTree.Text != CRLF)
- {
- Print(childTree, indent);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement