Beispiel #1
0
 private void Actor(AST node)
 {
     Console.Write("  Adding actor " + node.Token.Text);
     _currentActor = _world.CreateActor(node.Token.Text);
     node.Children.ForEach(n => Build(n));
     Console.WriteLine("");
 }
Beispiel #2
0
 protected void AddCurrentTokenAndSetAsCurrentNode(int tokenType)
 {
     var newNode = new AST(CurrentToken);
     _currentNode.Children.Add(newNode);
     _currentNode = newNode;
     Match(tokenType);
 }
Beispiel #3
0
 private void AddEventActions(EventAction<EventState> eventAction, AST whenNode)
 {
     for (int i = 1; // Skip first, which is event type
         i < whenNode.Children.Count; i++)
     {
         eventAction.Add(GetAction(whenNode.Children[i]));
     }
 }
Beispiel #4
0
 private string GetDisplayText(AST node)
 {
     string tokenName = Tokens.TokenNames[node.Token.Type];
     string tokenText = node.Token.Text.Replace('"', '\'');
     if (string.IsNullOrEmpty(tokenText) || tokenName.Equals(tokenText.ToUpper()))
         return string.Format("\"{0}: {1}\"", _id.Next(), tokenName);
     else
         return string.Format("\"{0}: {1} \\\"{2}\\\"\"", _id.Next(), tokenName, tokenText);
 }
Beispiel #5
0
 public static string ToString(AST node)
 {
     if (node.Children.Count > 0)
     {
         var children = node.Children.Aggregate("", (acc, cn) => acc + ToString(cn));
         return string.Format(" ({0}{1})", node.Token, children);
     }
     else
         return " " + node.Token;
 }
Beispiel #6
0
 public void Build(AST node)
 {
     switch (node.Token.Type)
     {
         case Tokens.PROGRAM: Program(node); break;
         case Tokens.WHEN: When(node); break;
         case Tokens.COUNT: Count(node); break;
         default:
             break;
     }
 }
Beispiel #7
0
        private void AddNode(AST node)
        {
            node.Children.ForEach(c =>
            {
                _dot.AppendFormat("{0}->{1};{2}",
                    node.Token.Text,
                    c.Token.Text,
                    Environment.NewLine);

                AddNode(c);
            });
        }
Beispiel #8
0
        public string ToDot(AST node)
        {
            _id = new IntGenerator();
            RewriteTree(node);

            _dot = new StringBuilder();
            _dot.AppendLine("digraph { ");
            _dot.AppendLine("node [color=black, style=filled, fillcolor=wheat]");
            AddNode(node);
            _dot.AppendLine("}");

            return _dot.ToString();
        }
Beispiel #9
0
 private Action<EventState> GetAction(AST line)
 {
     switch (line.Token.Type)
     {
         case Tokens.PRINT:
             var print = PrintAction.Create(line);
             return state => print.Execute(state);
         case Tokens.PING:
             return state => state.World.Ping(line.Children[0].Token.Text);
         case Tokens.WAIT:
             int sleepInMilliseconds = Int32.Parse(line.Children[0].Token.Text) * Unit(line.Children[1].Token.Text);
             return state => Thread.Sleep(sleepInMilliseconds);
         case Tokens.SEND:
             var send = SendAction.Create(line);
             return state => send.Execute();
         case Tokens.RESET:
             return state => state.Self.ResetCounter();
         default:
             throw new Exception("Unrecognized action " + line.Token);
     }
 }
Beispiel #10
0
        public static PrintAction Create(AST printNode)
        {
            var p = new PrintAction();
            printNode.Children.ForEach(node =>
            {
                switch (node.Token.Type)
                {
                    case Tokens.STRING:
                        p.AddPrint(state => Console.Write(node.Token.TextWithoutQuotes()));
                        break;
                    case Tokens.COUNTER:
                        p.AddPrint(state => Console.Write(state.Self.Counter));
                        break;
                    case Tokens.INT:
                        p.AddPrint(state => Console.Write(node.Token.Text));
                        break;

                    //TODO: add message ..
                }
            });
            return p;
        }
Beispiel #11
0
 private void RewriteTree(AST node)
 {
     node.Token.Text = GetDisplayText(node);
     node.Children.ForEach(c => RewriteTree(c));
 }
Beispiel #12
0
        private void WhenCounter(AST node)
        {
            Console.Write(" with counter event ");

            if(node.Children[0].Children[0].Token.Type != Tokens.GT)
                throw new Exception("Only supports 'greater then' counter predicate at the moment!");

            if(node.Children[0].Children[1].Token.Type != Tokens.INT)
                throw new Exception("Last argument to counter event predicate must be an integer!");

            var intArg = Int32.Parse(node.Children[0].Children[1].Token.Text);
            _currentActor.WhenCounter = counter => counter > intArg;

            AddEventActions(_currentActor.OnCounter, node);
        }
Beispiel #13
0
 private void When(AST node)
 {
     var eventType = node.Children.First();
     switch (eventType.Token.Type)
     {
         case Tokens.STARTING: WhenStarting(node); break;
         case Tokens.PINGED: WhenPinged(node); break;
         case Tokens.COUNTER: WhenCounter(node); break;
         default:
             break;
     }
 }
Beispiel #14
0
 private void WhenStarting(AST node)
 {
     Console.Write(" with starting event ");
     AddEventActions(_currentActor.OnStart, node);
 }
Beispiel #15
0
 private void WhenPinged(AST node)
 {
     Console.Write(" with pinged event ");
     AddEventActions(_currentActor.OnPing, node);
 }
Beispiel #16
0
 protected void SetRoot(Token root)
 {
     AST = new AST(root);
     _currentNode = AST;
 }
Beispiel #17
0
 protected void PreserveCurrentNode(Action a)
 {
     var tempNode = _currentNode;
     a.Invoke();
     _currentNode = tempNode;
 }
Beispiel #18
0
 private void Program(AST node)
 {
     node.Children.ForEach(n => Actor(n));
 }
Beispiel #19
0
 public static SendAction Create(AST sendNode)
 {
     return new SendAction(
         Int32.Parse(sendNode.Children[1].Token.Text),
         sendNode.Children[0].Token.Text);
 }
Beispiel #20
0
        private void Count(AST node)
        {
            int countInMilliseconds = Int32.Parse(node.Children[0].Token.Text) * Unit(node.Children[1].Token.Text);

            _currentActor.CountEvery(countInMilliseconds);
        }