示例#1
0
        private void CreateTestNodes()
        {
            // create test nodes
            var testNode  = new NodeStringXOR(new Point(100, 50));
            var testNode2 = new NodeString(new Point(-350, 40));
            var testNode3 = new NodeChar(new Point(-300, -150));
            var testNode4 = new NodeStringXOR(new Point(350, 150));
            var testNode5 = new NodeLogicOutput(new Point(-280, 300));
            var testNode6 = new NodeMonitor(new Point(600, 290));

            // add nodes to control
            nodeGraphControl.AddNode(testNode);
            nodeGraphControl.AddNode(testNode2);
            nodeGraphControl.AddNode(testNode3);
            nodeGraphControl.AddNode(testNode4);
            nodeGraphControl.AddNode(testNode5);
            nodeGraphControl.AddNode(testNode6);

            // connect nodes
            nodeGraphControl.Connect((SocketOut)testNode3.Sockets[0], (SocketIn)testNode.GetSocketByName("Input key"));
            nodeGraphControl.Connect((SocketOut)testNode2.Sockets[0], (SocketIn)testNode.GetSocketByName("Input string"));
            nodeGraphControl.Connect((SocketOut)testNode.GetSocketByName("Output string"), (SocketIn)testNode4.GetSocketByName("Input string"));
            nodeGraphControl.Connect((SocketOut)testNode3.Sockets[0], (SocketIn)testNode4.GetSocketByName("Input key"));
            nodeGraphControl.Connect((SocketOut)testNode5.GetSocketByName("Output H"), (SocketIn)testNode.GetSocketByName("Input enabled"));
            nodeGraphControl.Connect((SocketOut)testNode5.GetSocketByName("Output H"), (SocketIn)testNode4.GetSocketByName("Input enabled"));
            nodeGraphControl.Connect((SocketOut)testNode4.GetSocketByName("Output string"), (SocketIn)testNode6.GetSocketByName("Input"));
        }
示例#2
0
        private static IEnumerable <string> BFS(NodeString root)
        {
            var queue = new Queue <NodeString>();

            queue.Enqueue(root);

            while (queue.Count > 0)
            {
                var v = queue.Dequeue();

                yield return(v.Value);

                foreach (var child in v.Children)
                {
                    queue.Enqueue(child);
                }
            }
        }
示例#3
0
        private static IEnumerable <string> DFS(NodeString root)
        {
            var stack = new Stack <NodeString>();

            stack.Push(root);

            while (stack.Count > 0)
            {
                var v = stack.Pop();

                yield return(v.Value);

                foreach (var child in v.Children)
                {
                    stack.Push(child);
                }
            }
        }
示例#4
0
        public void Go()
        {
            var root =
                new NodeString("7",
                               new NodeString("1",
                                              "0",
                                              new NodeString("3",
                                                             "2",
                                                             new NodeString("5",
                                                                            "4",
                                                                            "6"))),
                               new NodeString("9",
                                              "8",
                                              "10"));

            Console.WriteLine(root);
            Console.WriteLine();

            Console.WriteLine("DFS");
            Console.WriteLine(string.Join(", ", DFS(root)));
            Console.WriteLine();

            Console.WriteLine("BFS");
            Console.WriteLine(string.Join(", ", BFS(root)));
            Console.WriteLine();

            Console.WriteLine("Binary tree, preorder");
            Console.WriteLine(string.Join(", ", BinaryTree_Preorder(root)));
            Console.WriteLine("7, 1, 0, 3, 2, 5, 4, 6, 9, 8, 10    <- Expected");
            Console.WriteLine();

            Console.WriteLine("Binary tree, inorder");
            Console.WriteLine(string.Join(", ", BinaryTree_Inorder(root)));
            Console.WriteLine("0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10    <- Expected");
            Console.WriteLine();

            Console.WriteLine("Binary tree, postorder");
            Console.WriteLine(string.Join(", ", BinaryTree_Postorder(root)));
            Console.WriteLine("0, 2, 4, 6, 5, 3, 1, 8, 10, 9, 7    <- Expected");
            Console.WriteLine();
        }
示例#5
0
文件: HtmlDoc.cs 项目: alvaka/ucml
        private static NodeString ParseNodeStringByName(string text, ref int index, string nodeName)
        {
            if (text == "")
            {
                return(null);
            }
            NodeString nodeStr = new NodeString();

            while (index < text.Length)
            {
                int start = text.IndexOf('<', index);
                if (start == -1)
                {
                    return(null);
                }
                int end = text.IndexOf('>', start);
                if (end == -1)
                {
                    return(null);
                }
                int length = end - start + 1;
                if (length < 3)
                {
                    continue;
                }
                else
                {
                    string tagBlock = text.Substring(start, length);
                    string tagName  = HtmlNode.GetNodeName(tagBlock);
                    index = end + 1;                                                                  //索引前进
                    if (HtmlNode.IsValidHtmlNode(tagName) && tagName.ToLower() == nodeName.ToLower()) //检验标签名是否合法
                    {
                        nodeStr.Type      = HtmlNode.GetNodeStringType(tagBlock);
                        nodeStr.NodeBlock = tagBlock;
                        return(nodeStr);
                    }
                }
            }
            return(null);
        }
示例#6
0
        private static IEnumerable <string> BinaryTree_Inorder(NodeString root)
        {
            var hasChild = root.Children.Count == 2;

            if (hasChild)
            {
                foreach (var node in BinaryTree_Inorder(root.Children[0]))
                {
                    yield return(node);
                }
            }

            yield return(root.Value);

            if (hasChild)
            {
                foreach (var node in BinaryTree_Inorder(root.Children[1]))
                {
                    yield return(node);
                }
            }
        }
示例#7
0
 internal void Accept(NodeString s)
 {
     builder.currentLineNumber = s.EndLine;
     builder.OpSConst(s.value);
 }
示例#8
0
 void ASTVisitor.Accept(NodeString value)
 {
     Accept(value);
 }
示例#9
0
文件: HtmlDoc.cs 项目: alvaka/ucml
        /// <summary>
        /// 解析第一个节点(包括文本节点)
        /// </summary>
        /// <param name="text"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        private static NodeString ParseNodeString(string text, ref int index)
        {
            if (text == "")
            {
                return(null);
            }
            NodeString nodeStr = new NodeString();
            int        start   = text.IndexOf('<', index);

            if (start == -1)//文本节点
            {
                nodeStr.Type      = 3;
                nodeStr.NodeBlock = text.Substring(index);
                index             = text.Length - 1;
                return(nodeStr);
            }
            else if (start > index)//前面有文本节点
            {
                nodeStr.Type      = 3;
                nodeStr.NodeBlock = text.Substring(index, start - index);//保存文本节点
                index             = start;
                int        tmpIndex = index;
                NodeString tmp      = ParseNodeString(text, ref tmpIndex);
                if (tmp.Type == 3)
                {
                    nodeStr.NodeBlock = nodeStr.NodeBlock + tmp.NodeBlock;
                    index             = tmpIndex;
                }
                //如果不是文本节点,则还原index到上一次位置
                return(nodeStr);
            }
            int end = text.IndexOf('>', start); //若Tag内出现>会出错

            if (end == -1)                      //无>标记表示为文本节点
            {
                index             = text.Length - 1;
                nodeStr.Type      = 3;
                nodeStr.NodeBlock = text.Substring(index);
                return(nodeStr);
            }
            int length = end - start + 1;

            if (length < 3)//一个标签长度最少为3
            {
                nodeStr.Type      = 3;
                nodeStr.NodeBlock = text.Substring(index, length);
                index             = end + 1;
                int        tmpIndex = index;
                NodeString tmp      = ParseNodeString(text, ref tmpIndex);
                if (tmp.Type == 3)
                {
                    nodeStr.NodeBlock = nodeStr.NodeBlock + tmp.NodeBlock;
                    index             = tmpIndex;
                }
                return(nodeStr); //若文本里出现<>继续搜索
            }
            else//解析到类常规标签,可能是<123>等不合法标签
            {
                string tagBlock = text.Substring(start, length);
                string tagName  = HtmlNode.GetNodeName(tagBlock);
                if (HtmlNode.IsValidHtmlNode(tagName)) //检验标签名是否合法
                {
                    index             = end + 1;       //索引前进
                    nodeStr.Type      = HtmlNode.GetNodeStringType(tagBlock);
                    nodeStr.NodeBlock = tagBlock;
                    return(nodeStr);
                }
                else//标签不合法则视为文本
                {
                    nodeStr.Type      = 3;
                    nodeStr.NodeBlock = text.Substring(index, length);
                    index             = end + 1;
                    int        tmpIndex = index;
                    NodeString tmp      = ParseNodeString(text, ref tmpIndex);
                    if (tmp.Type == 3)
                    {
                        nodeStr.NodeBlock = nodeStr.NodeBlock + tmp.NodeBlock;
                        index             = tmpIndex;
                    }
                    return(nodeStr); //若文本里出现<>继续搜索
                }
            }
        }
示例#10
0
文件: HtmlDoc.cs 项目: alvaka/ucml
        public static List <HtmlNode> ParseNodeByName(string text, string nodeName)
        {
            if (text == "")
            {
                return(null);
            }
            List <HtmlNode> nodeList  = new List <HtmlNode>();
            int             nodeIndex = 0;

            while (nodeIndex < text.Length - 1)
            {
                NodeString nodeStr = ParseNodeStringByName(text, ref nodeIndex, nodeName);
                if (nodeStr == null)
                {
                    break;
                }
                else if (nodeStr.Type == 2)
                {
                    nodeList.Add(HtmlNode.ParseNode(nodeStr.NodeBlock));
                }
                else if (nodeStr.Type == 0)
                {
                    int         tmpIndex = nodeIndex;
                    string      tagName  = HtmlNode.GetNodeName(nodeStr.NodeBlock);
                    NodeString  tmpNode  = null;
                    Stack <int> cntStack = new Stack <int>();
                    cntStack.Push(0);
                    while (cntStack.Count != 0)
                    {
                        if (tmpIndex >= text.Length)
                        {
                            break;                         //到文本结尾仍未找到结束标签,则跳出
                        }
                        tmpNode = ParseNodeString(text, ref tmpIndex);
                        string tmpTagName = HtmlNode.GetNodeName(tmpNode.NodeBlock);
                        if (tmpTagName.ToLower() == nodeName.ToLower())
                        {
                            if (tmpNode.Type == 0)
                            {
                                cntStack.Push(0);
                            }
                            else if (tmpNode.Type == 1)
                            {
                                cntStack.Pop();
                            }
                        }
                    }
                    if (cntStack.Count == 0)//找到结束标签
                    {
                        HtmlNode node      = HtmlNode.ParseNode(nodeStr.NodeBlock);
                        string   innerNode = text.Substring(nodeIndex, tmpIndex - nodeIndex - tmpNode.NodeBlock.Length);
                        if (innerNode != "")//递归解析子节点
                        {
                            node.Childs = ParseNode(innerNode);
                        }
                        nodeList.Add(node);
                        nodeIndex = tmpIndex;
                    }
                    else
                    {
                        break;
                    }
                }
            }//循环结束
            return(nodeList);
        }
示例#11
0
文件: HtmlDoc.cs 项目: alvaka/ucml
        /// <summary>
        /// 解析传入的字符串,并返回最上层节点列表
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static List <HtmlNode> ParseNode(string text)
        {
            if (text == "")
            {
                return(null);
            }
            List <HtmlNode> nodeList  = new List <HtmlNode>();
            int             nodeIndex = 0;

            while (nodeIndex < text.Length - 1)
            {
                NodeString nodeStr = ParseNodeString(text, ref nodeIndex);
                if (nodeStr.Type == 3 || nodeStr.Type == 2)//文本节点和闭合标签
                {
                    nodeList.Add(HtmlNode.ParseNode(nodeStr.NodeBlock));
                }
                else if (nodeStr.Type == 0)//处理开放节点,寻找结束标记
                {
                    int         tmpIndex = nodeIndex;
                    string      tagName  = HtmlNode.GetNodeName(nodeStr.NodeBlock);
                    NodeString  tmpNode  = null;
                    Stack <int> cntStack = new Stack <int>();
                    cntStack.Push(0);
                    while (cntStack.Count != 0)
                    {
                        if (tmpIndex >= text.Length)
                        {
                            break;                        //到文本结尾仍未找到结束标签,则跳出
                        }
                        tmpNode = ParseNodeString(text, ref tmpIndex);
                        string tmpTagName = HtmlNode.GetNodeName(tmpNode.NodeBlock);
                        if (tmpTagName.ToLower() == tagName.ToLower())
                        {
                            if (tmpNode.Type == 0)
                            {
                                cntStack.Push(0);
                            }
                            else if (tmpNode.Type == 1)
                            {
                                cntStack.Pop();
                            }
                        }
                    }
                    if (cntStack.Count == 0)//找到结束标签
                    {
                        HtmlNode node      = HtmlNode.ParseNode(nodeStr.NodeBlock);
                        string   innerNode = text.Substring(nodeIndex, tmpIndex - nodeIndex - tmpNode.NodeBlock.Length);
                        if (innerNode != "")//递归解析子节点
                        {
                            node.Childs = ParseNode(innerNode);
                        }
                        nodeList.Add(node);
                        nodeIndex = tmpIndex;
                    }
                    else
                    {
                        HtmlDocParseExeption err = new HtmlDocParseExeption("Bad Document,Miss enclosed tag for " + nodeStr.NodeBlock);
                        throw err;
                    }
                }
            }//循环结束
            return(nodeList);
        }
示例#12
0
文件: Parser.cs 项目: LayeLang/Laye
 private Node PrimaryExpression(bool allowPostfixInvocation = true)
 {
     if (!TokensRemain)
         return null;
     var loc = Location;
     Node node;
     switch (Current.type)
     {
         case WILDCARD:
             // If we aren't in a truly anon function, we ignore this
             if (CurrentFunction != null && CurrentFunction.fullAnon && CurrentFunction.paramNames.Count == 0)
                 CurrentFunction.paramNames.Add(null);
             node = new NodeWildcard(loc);
             Advance();
             break;
         case PARAM_INDEX:
             var index = (uint)Current.intValue;
             // If we aren't in a truly anon function, we ignore this
             if (CurrentFunction != null && CurrentFunction.fullAnon)
                 while (CurrentFunction.paramNames.Count <= index)
                     CurrentFunction.paramNames.Add(null);
             node = new NodeParamIndex(loc, index);
             Advance();
             break;
         case IDENTIFIER:
             node = new NodeIdentifier(loc, Current.image);
             Advance();
             break;
         case SYMBOL:
             node = new NodeSymbol(loc, Current.image);
             Advance();
             break;
         case NULL:
             node = new NodeNull(loc);
             Advance();
             break;
         case TRUE:
         case FALSE:
             node = new NodeBool(loc, Check(TRUE));
             Advance();
             break;
         case ENDL:
             node = new NodeEndL(loc);
             Advance();
             break;
         case THIS:
             node = new NodeThis(loc);
             Advance();
             break;
         case SELF:
             node = new NodeSelf(loc);
             Advance();
             break;
         case INT:
             node = new NodeInt(loc, Current.intValue);
             Advance();
             break;
         case FLOAT:
             node = new NodeFloat(loc, Current.floatValue);
             Advance();
             break;
         case STRING:
             node = new NodeString(loc, Current.image);
             Advance();
             break;
         case OPEN_BRACE:
             Advance();
             if (Check(CLOSE_BRACE))
             {
                 // 0-length tuple:
                 node = new NodeTuple(loc, new List<Node>());
                 node.EndLine = Current.location.line;
                 Advance();
             }
             else
             {
                 var values = CommaExpressions();
                 if (values.Count != 1)
                 {
                     // This is a tuple :D
                     node = new NodeTuple(loc, values);
                     Expect(CLOSE_BRACE, "Closing brace (')') expected to end tuple expression.");
                     node.EndLine = tokens[-1].location.line;
                 }
                 else
                 {
                     Expect(CLOSE_BRACE, "Closing brace (')') expected to surround expression.");
                     // Just an expression, carry on:
                     node = values[0];
                 }
             }
             break;
         case OPEN_SQUARE_BRACE:
             // This is a list :D
             Advance();
             node = new NodeList(loc, (TokensRemain && Current.type != CLOSE_SQUARE_BRACE) ? CommaExpressions() : new List<Node>());
             Expect(CLOSE_SQUARE_BRACE, "Closing square brace (']') expected to end list expression.");
             node.EndLine = tokens[-1].location.line;
             break;
         case OPEN_CURLY_BRACE:
             // This is a code block :D
             Advance();
             var exprs = new List<Node>();
             while (!Check(CLOSE_CURLY_BRACE))
             {
                 if (!TokensRemain)
                 {
                     log.Error(tokens[-1].location, "Unfinished block. A closing curly brace ('}') should be used to end blocks.");
                     break;
                 }
                 var expr = Expression();
                 if (expr != null)
                     exprs.Add(expr);
             }
             Expect(CLOSE_CURLY_BRACE, "Closing curly brace ('}') expected to end block.");
             node = new NodeBlock(loc, exprs);
             node.EndLine = tokens[-1].location.line;
             break;
         case TAILREC:
             Advance();
             Expect(OPEN_BRACE, "Expected an open brace ('(') to begin the tailrec argument list.");
             List<Node> args;
             if (!Check(CLOSE_BRACE))
                 args = CommaExpressions();
             else args = new List<Node>();
             Expect(CLOSE_BRACE, "Expected a close brace (')') to end the tailrec argument list.");
             node = new NodeTailRec(loc, args);
             break;
         // These can't be postfix'd, so we don't allow it.
         case OPERATOR:
             var prefix = Current.image;
             Advance();
             if (!TokensRemain)
                 log.Error(loc, "An expression is expected after prefix operator, but the end of the file was reached.");
             var prefixValue = PrimaryExpression();
             return new NodePrefix(loc, prefix, prefixValue);
         case NOT:
             Advance();
             if (!TokensRemain)
                 log.Error(loc, "An expression is expected after the 'not' keyword, but the end of the file was reached.");
             return new NodeNot(loc, Expression());
         case THROW:
             Advance();
             if (!TokensRemain)
                 log.Error(loc, "An expression is expected after the 'throw' keyword, but the end of the file was reached.");
             return new NodeThrow(loc, Expression());
         case YIELD:
             Advance();
             if (!TokensRemain)
                 log.Error(loc, "An expression is expected after the 'yield' keyword, but the end of the file was reached.");
             return new NodeYield(loc, Expression());
         case RES:
             Advance();
             if (!TokensRemain)
                 log.Error(loc, "An expression is expected after the 'res' keyword, but the end of the file was reached.");
             return new NodeRes(loc, Expression());
         case BREAK:
             Advance();
             string bLabel = null;
             if (Check(IDENTIFIER) && Current.location.line == loc.line)
             {
                 bLabel = Current.image;
                 Advance();
             }
             return new NodeBreak(loc, bLabel);
         case CONT:
             Advance();
             string cLabel = null;
             if (Check(IDENTIFIER) && Current.location.line == loc.line)
             {
                 cLabel = Current.image;
                 Advance();
             }
             return new NodeCont(loc, cLabel);
         case FN: return ParseFn();
         case GEN: return ParseFn(true);
         case VAR: return ParseVar();
         case IF: return ParseIf();
         case WHILE: return ParseWhile();
         case TRY: return ParseTry();
         case ITER: return ParseIter();
         case EACH: return ParseEach();
         // And here, we don't know what they want... oops.
         default:
             log.Error(Location, "Unexpected token '{0}', skipping...", Current.ToString());
             // We don't know what to do with this, let's skip and try to recover.
             Advance();
             // Error saying we couldn't understand the token:
             // return a new primary expression.
             return PrimaryExpression();
     }
     // TODO postfix
     return Postfix(node, allowPostfixInvocation);
 }
示例#13
0
文件: HtmlDoc.cs 项目: alvaka/ucml
 private static NodeString ParseNodeStringByName(string text, ref int index,string nodeName)
 {
     if (text == "") return null;
     NodeString nodeStr = new NodeString();
     while (index < text.Length)
     {
         int start = text.IndexOf('<', index);
         if (start == -1) return null;
         int end = text.IndexOf('>', start);
         if (end == -1) return null;
         int length = end - start + 1;
         if (length < 3) continue;
         else
         {
             string tagBlock = text.Substring(start, length);
             string tagName = HtmlNode.GetNodeName(tagBlock);
             index = end + 1;//索引前进
             if (HtmlNode.IsValidHtmlNode(tagName)&&tagName.ToLower()==nodeName.ToLower())//检验标签名是否合法
             {
                 nodeStr.Type = HtmlNode.GetNodeStringType(tagBlock);
                 nodeStr.NodeBlock = tagBlock;
                 return nodeStr;
             }
         }
     }
     return null;
 }
示例#14
0
文件: HtmlDoc.cs 项目: alvaka/ucml
 /// <summary>
 /// 解析第一个节点(包括文本节点)
 /// </summary>
 /// <param name="text"></param>
 /// <param name="index"></param>
 /// <returns></returns>
 private static NodeString ParseNodeString(string text, ref int index)
 {
     if (text == "") return null;
     NodeString nodeStr = new NodeString();
     int start = text.IndexOf('<',index);
     if (start == -1)//文本节点
     {
         nodeStr.Type = 3;
         nodeStr.NodeBlock = text.Substring(index);
         index = text.Length - 1;
         return nodeStr;
     }
     else if (start > index)//前面有文本节点
     {
         nodeStr.Type = 3;
         nodeStr.NodeBlock = text.Substring(index, start - index);//保存文本节点
         index = start;
         int tmpIndex=index;
         NodeString tmp =ParseNodeString(text,ref tmpIndex);
         if(tmp.Type==3)
         {
             nodeStr.NodeBlock=nodeStr.NodeBlock+tmp.NodeBlock;
             index = tmpIndex;
         }
         //如果不是文本节点,则还原index到上一次位置
         return nodeStr;
     }
     int end = text.IndexOf('>', start);//若Tag内出现>会出错
     if (end == -1)//无>标记表示为文本节点
     {
         index = text.Length - 1;
         nodeStr.Type = 3;
         nodeStr.NodeBlock = text.Substring(index);
         return nodeStr;
     }
     int length = end - start+1;
     if (length < 3)//一个标签长度最少为3
     {
         nodeStr.Type = 3;
         nodeStr.NodeBlock = text.Substring(index, length);
         index = end + 1;
         int tmpIndex = index;
         NodeString tmp = ParseNodeString(text, ref tmpIndex);
         if (tmp.Type == 3)
         {
             nodeStr.NodeBlock = nodeStr.NodeBlock + tmp.NodeBlock;
             index = tmpIndex;
         }
         return  nodeStr; //若文本里出现<>继续搜索
     }
     else//解析到类常规标签,可能是<123>等不合法标签
     {
         string tagBlock = text.Substring(start, length);
         string tagName = HtmlNode.GetNodeName(tagBlock);
         if (HtmlNode.IsValidHtmlNode(tagName))//检验标签名是否合法
         {
             index = end + 1;//索引前进
             nodeStr.Type = HtmlNode.GetNodeStringType(tagBlock);
             nodeStr.NodeBlock = tagBlock;
             return nodeStr;
         }
         else//标签不合法则视为文本
         {
             nodeStr.Type = 3;
             nodeStr.NodeBlock = text.Substring(index, length);
             index = end + 1;
             int tmpIndex = index;
             NodeString tmp = ParseNodeString(text, ref tmpIndex);
             if (tmp.Type == 3)
             {
                 nodeStr.NodeBlock = nodeStr.NodeBlock + tmp.NodeBlock;
                 index = tmpIndex;
             }
             return nodeStr; //若文本里出现<>继续搜索
         }
     }
 }