Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        public static ANode InterpretAndTranslate(SimpleTree <ANode> ast)
        {
            if (ast.Root.NodeValue.token_type == TokenType.Integer)
            {
                return(ast.Root.NodeValue);
            }

            var    currentNode         = GetCurrentNodeForInterpretAndTranslate(ast.Root);
            string newValue            = CalculateValueForInterpretAndTranslate(currentNode);
            string newTranslatedResult = TranslateResult(currentNode);

            currentNode.NodeValue.token_type        = TokenType.Integer;
            currentNode.NodeValue.token_value       = newValue;
            currentNode.NodeValue.translated_result = newTranslatedResult;

            return(InterpretAndTranslate(ast));
        }