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(""); }
protected void AddCurrentTokenAndSetAsCurrentNode(int tokenType) { var newNode = new AST(CurrentToken); _currentNode.Children.Add(newNode); _currentNode = newNode; Match(tokenType); }
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])); } }
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); }
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; }
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; } }
private void AddNode(AST node) { node.Children.ForEach(c => { _dot.AppendFormat("{0}->{1};{2}", node.Token.Text, c.Token.Text, Environment.NewLine); AddNode(c); }); }
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(); }
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); } }
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; }
private void RewriteTree(AST node) { node.Token.Text = GetDisplayText(node); node.Children.ForEach(c => RewriteTree(c)); }
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); }
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; } }
private void WhenStarting(AST node) { Console.Write(" with starting event "); AddEventActions(_currentActor.OnStart, node); }
private void WhenPinged(AST node) { Console.Write(" with pinged event "); AddEventActions(_currentActor.OnPing, node); }
protected void SetRoot(Token root) { AST = new AST(root); _currentNode = AST; }
protected void PreserveCurrentNode(Action a) { var tempNode = _currentNode; a.Invoke(); _currentNode = tempNode; }
private void Program(AST node) { node.Children.ForEach(n => Actor(n)); }
public static SendAction Create(AST sendNode) { return new SendAction( Int32.Parse(sendNode.Children[1].Token.Text), sendNode.Children[0].Token.Text); }
private void Count(AST node) { int countInMilliseconds = Int32.Parse(node.Children[0].Token.Text) * Unit(node.Children[1].Token.Text); _currentActor.CountEvery(countInMilliseconds); }