Ejemplo n.º 1
0
        private static string CalculateValueForInterpretAndTranslate(SimpleTreeNode <ANode> currentNode)
        {
            var arg1   = int.Parse(currentNode.Children[0].NodeValue.token_value);
            var arg2   = int.Parse(currentNode.Children[1].NodeValue.token_value);
            var result = 0;

            switch (currentNode.NodeValue.token_value)
            {
            case "/":
                result = arg1 / arg2;
                break;

            case "*":
                result = arg1 * arg2;
                break;

            case "-":
                result = arg1 - arg2;
                break;

            case "+":
                result = arg1 + arg2;
                break;

            default:
                throw new Exception();
            }
            return(result.ToString());
        }
Ejemplo n.º 2
0
        public static SimpleTree <ANode> Build(List <ANode> nodes)
        {
            var currentNode = new SimpleTreeNode <ANode>(null, null);
            var result      = new SimpleTree <ANode>(currentNode);

            foreach (var node in nodes)
            {
                if (node.token_type == TokenType.Bracket && node.token_value == "(")
                {
                    var leftEmpty = new SimpleTreeNode <ANode>(null, currentNode);
                    result.AddChild(currentNode, leftEmpty);
                    currentNode = leftEmpty;
                }
                if (node.token_type == TokenType.Bracket && node.token_value == ")")
                {
                    currentNode = currentNode.Parent;
                }
                if (node.token_type == TokenType.Integer)
                {
                    currentNode.NodeValue = node;
                    currentNode           = currentNode.Parent;
                }
                if (node.token_type == TokenType.Operation)
                {
                    currentNode.NodeValue = node;
                    var rightEmpty = new SimpleTreeNode <ANode>(null, currentNode);
                    result.AddChild(currentNode, rightEmpty);
                    currentNode = rightEmpty;
                }
            }
            return(result);
        }
Ejemplo n.º 3
0
        private static string TranslateResult(SimpleTreeNode <ANode> currentNode)
        {
            var arg1 = string.IsNullOrEmpty(currentNode.Children[0].NodeValue.translated_result) ? currentNode.Children[0].NodeValue.token_value : currentNode.Children[0].NodeValue.translated_result;
            var arg2 = string.IsNullOrEmpty(currentNode.Children[1].NodeValue.translated_result) ? currentNode.Children[1].NodeValue.token_value : currentNode.Children[1].NodeValue.translated_result;

            return($"({arg1}{currentNode.NodeValue.token_value}{arg2})");
        }
Ejemplo n.º 4
0
 public void AddChild(SimpleTreeNode <T> ParentNode, SimpleTreeNode <T> NewChild)
 {
     // ваш код добавления нового дочернего узла существующему ParentNode
     NewChild.Parent = ParentNode;
     if (ParentNode.Children == null)
     {
         ParentNode.Children = new List <SimpleTreeNode <T> >();
     }
     ParentNode.Children.Add(NewChild);
 }
Ejemplo n.º 5
0
 private static void FillResult(SimpleTreeNode <T> node, List <SimpleTreeNode <T> > result)
 {
     result.Add(node);
     if (node.Children == null)
     {
         return;
     }
     foreach (var nodeChild in node.Children)
     {
         FillResult(nodeChild, result);
     }
 }
Ejemplo n.º 6
0
 private static SimpleTreeNode <ANode> GetCurrentNodeForInterpretAndTranslate(SimpleTreeNode <ANode> node)
 {
     if (node.Children[0].NodeValue.token_type == TokenType.Integer && node.Children[1].NodeValue.token_type == TokenType.Integer)
     {
         return(node);
     }
     if (node.Children[0].NodeValue.token_type == TokenType.Operation)
     {
         return(GetCurrentNodeForInterpretAndTranslate(node.Children[0]));
     }
     if (node.Children[1].NodeValue.token_type == TokenType.Operation)
     {
         return(GetCurrentNodeForInterpretAndTranslate(node.Children[1]));
     }
     return(null);
 }
Ejemplo n.º 7
0
        public SimpleTreeNode <T> Root; // корень, может быть null

        public SimpleTree(SimpleTreeNode <T> root)
        {
            Root = root;
        }
Ejemplo n.º 8
0
        public List <SimpleTreeNode <T> > Children; // список дочерних узлов или null

        public SimpleTreeNode(T val, SimpleTreeNode <T> parent)
        {
            NodeValue = val;
            Parent    = parent;
            Children  = null;
        }