Exemplo n.º 1
0
        public static ICFunction CreateNode(string label, NodeLabelCallback callBack)
        {
            label = removeWhitespace(label);
            ICFunction ret;

            // if it has an '=' then it's a variable.
            // Rearrange X = Y into Var(X,Y) format.
            if (label.Contains("="))
            {
                string[] pars = label.Split(new char[] { '=' });
                label = "Var(" + pars[0] + "," + pars[1] + ")";
            }

            // Split on first '('
            string[] split = label.Split(new char[] { '(' }, 2);
            ret = GetFunctionFromLabel(split[0]);
            // If null, then it's either a constant or node.
            if (split.Length < 2 || ret == null)
            {
                ret = ConstantFunction.CreateConstantFunction(label, split[0]);
                if (ret != null)
                {
                    return(AddNodeToList(ret));
                }
                ret = OutsourcedFunction.CreateOutsourcedFunction(split[0], callBack);
                if (ret != null)
                {
                    return(AddNodeToList(ret));
                }
            }
            // Now need to find children represented by remainder of split: split[1]
            // eg "4,5,Sub(3,4))"
            List <ICFunction> children = new List <ICFunction>();
            string            child    = "";
            int relevance = 0;

            foreach (char c in split[1])
            {
                if (c == '(')
                {
                    relevance++;
                }
                if (c == ')')
                {
                    relevance--;
                }
                child += c;
                if (relevance == 0)
                {
                    if (c == ')')
                    {
                        child = child.TrimEnd(new char[] { ')' });
                        if (child.Length > 0)
                        {
                            ICFunction icf = CreateNode(child, callBack);
                            children.Add(icf);
                        }
                        child = "";
                    }
                    if (c == ',')
                    {
                        child = child.TrimEnd(new char[] { ',' });
                        if (child.Length > 0)
                        {
                            ICFunction icf = CreateNode(child, callBack);
                            children.Add(icf);
                        }
                        child = "";
                    }
                }
            }
            child = child.TrimEnd(new char[] { ',', ')' });
            if (child.Length > 0)
            {
                ICFunction icff = CreateNode(child, callBack);
                children.Add(icff);
            }
            ret.Children = children;

            return(AddNodeToList(ret));
        }