コード例 #1
0
ファイル: ArrayGetNode.cs プロジェクト: zdimension/Hassium
        public static ArrayIndexerNode Parse(Parser.Parser parser)
        {
            var ret = new ArrayIndexerNode();
            parser.ExpectToken(TokenType.Bracket, "[");

            while (!parser.MatchToken(TokenType.Bracket, "]"))
            {
                ret.Children.Add(ExpressionNode.Parse(parser));
            }
            parser.ExpectToken(TokenType.Bracket, "]");

            return ret;
        }
コード例 #2
0
ファイル: ArrayGetNode.cs プロジェクト: zdimension/Hassium
        public static ArrayIndexerNode Parse(Parser.Parser parser)
        {
            var ret = new ArrayIndexerNode();

            parser.ExpectToken(TokenType.Bracket, "[");

            while (!parser.MatchToken(TokenType.Bracket, "]"))
            {
                ret.Children.Add(ExpressionNode.Parse(parser));
            }
            parser.ExpectToken(TokenType.Bracket, "]");

            return(ret);
        }
コード例 #3
0
        /// <summary>
        /// Create BinaryExpressionNode
        /// </summary>
        /// <param name="node"></param>
        /// <param name="outnode"></param>
        private void EmitBlockNode(Block node, out AssociativeNode outnode)
        {
            Validity.Assert(node != null);

            // TODO: Confirm that these children are returned in the order that they
            // appear in the code
            Dictionary <int, Node> childNodes = node.GetChildrenWithIndices();

            // Parse program statement in node.Name
            string code = node.Name + ";";

            ProtoCore.AST.AssociativeAST.CodeBlockNode commentNode   = null;
            ProtoCore.AST.AssociativeAST.CodeBlockNode codeBlockNode = (ProtoCore.AST.AssociativeAST.CodeBlockNode)GraphUtilities.Parse(code, out commentNode);
            Validity.Assert(codeBlockNode != null);

            List <ProtoCore.AST.AssociativeAST.AssociativeNode> astList = codeBlockNode.Body;

            Validity.Assert(astList.Count == 1);

            if (astList[0] is ProtoCore.AST.AssociativeAST.IdentifierNode)
            {
                ProtoCore.AST.AssociativeAST.BinaryExpressionNode ben = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode();
                ben.LeftNode = astList[0];
                ben.Optr     = ProtoCore.DSASM.Operator.assign;
                ProtoCore.AST.AssociativeAST.AssociativeNode statement = null;
                foreach (KeyValuePair <int, Node> kvp in childNodes)
                {
                    DFSTraverse(kvp.Value, out statement);
                }
                ben.RightNode = statement;
                astList[0]    = ben;
            }

            //I don't know what I am doing
            if (astList[0] is BinaryExpressionNode)
            {
                BinaryExpressionNode tempBen = astList[0] as BinaryExpressionNode;
                if (tempBen.LeftNode is IdentifierNode)
                {
                    IdentifierNode identitiferNode = tempBen.LeftNode as IdentifierNode;
                    if (identitiferNode.ArrayDimensions != null)
                    {
                        ArrayIndexerNode arrIndex = new ArrayIndexerNode();
                        arrIndex.ArrayDimensions = identitiferNode.ArrayDimensions;
                        arrIndex.Array           = identitiferNode;
                        tempBen.LeftNode         = arrIndex;
                    }
                }
                if (tempBen.RightNode is IdentifierNode)
                {
                    IdentifierNode identitiferNode = tempBen.RightNode as IdentifierNode;
                    if (identitiferNode.ArrayDimensions != null)
                    {
                        ArrayIndexerNode arrIndex = new ArrayIndexerNode();
                        arrIndex.ArrayDimensions = identitiferNode.ArrayDimensions;
                        arrIndex.Array           = identitiferNode;
                        tempBen.RightNode        = arrIndex;
                    }
                }
                astList[0] = tempBen;
            }
            //it should be correct, if not, debug?

            ProtoCore.AST.AssociativeAST.BinaryExpressionNode bNode = astList[0] as ProtoCore.AST.AssociativeAST.BinaryExpressionNode;
            Validity.Assert(bNode != null);
            bNode.Guid = node.Guid;
            //bNode.SplitFromUID = node.splitFomUint;

            // Child nodes are arguments to expression in bNode.RightNode.
            // Match child nodes with IdentifierNode's in bNode.RightNode - pratapa
            foreach (Node n in childNodes.Values)
            {
                AssociativeNode argNode = null;
                DFSTraverse(n, out argNode);

                // DFS traverse the bNode.RightNode and check for IdentifierNode's
                // and if their names match with the names of argNode, then replace
                // the IdentifierNode in bNode.RightNode with argNode
                BinaryExpressionNode ben = argNode as BinaryExpressionNode;
                Validity.Assert(ben != null);
                //ProtoCore.CodeGenDS codeGen = new ProtoCore.CodeGenDS(ben);
                AstCodeBlockTraverse sourceGen = new AstCodeBlockTraverse(ben);
                ProtoCore.AST.AssociativeAST.AssociativeNode right = bNode.RightNode;
                sourceGen.DFSTraverse(ref right);
                bNode.RightNode = right;
            }

            //(AstRootNode as CodeBlockNode).Body.Add(expressionNode);

            Validity.Assert(gc != null);
            gc.HandleNewNode(bNode);

            outnode = bNode;
        }
コード例 #4
0
        private void TraverseToSplit(AssociativeNode node, out AssociativeNode outNode, ref List <AssociativeNode> splitList)
        {
            if (node is BinaryExpressionNode)
            {
                BinaryExpressionNode ben = node as BinaryExpressionNode;

                BinaryExpressionNode newNode = new BinaryExpressionNode();
                AssociativeNode      lNode   = null;
                TraverseToSplit(ben.LeftNode, out lNode, ref splitList);
                newNode.LeftNode = lNode;
                newNode.Optr     = ben.Optr;

                AssociativeNode rNode = null;
                TraverseToSplit(ben.RightNode, out rNode, ref splitList);
                newNode.RightNode = rNode;

                if (ben.Optr == ProtoCore.DSASM.Operator.assign)
                {
                    if (NotEnlisted(splitList, newNode))
                    {
                        splitList.Add(newNode);
                    }
                    outNode = lNode;
                }
                else
                {
                    outNode = newNode;
                }
            }
            else if (node is FunctionCallNode)
            {
                FunctionCallNode funcCallNode = node as FunctionCallNode;
                AssociativeNode  statement    = null;
                foreach (AssociativeNode argNode in funcCallNode.FormalArguments)
                {
                    TraverseToSplit(argNode, out statement, ref splitList);
                }
                for (int i = 0; i < funcCallNode.FormalArguments.Count; i++)
                {
                    AssociativeNode argNode = funcCallNode.FormalArguments[i];
                    if (argNode is BinaryExpressionNode)
                    {
                        funcCallNode.FormalArguments[i] = (argNode as BinaryExpressionNode).LeftNode;
                    }
                }
                //if (statement is BinaryExpressionNode)
                //{
                //    splitList.Add(statement);
                //}
                outNode = funcCallNode;
            }
            else if (node is FunctionDotCallNode)
            {
                FunctionDotCallNode funcDotNode = node as FunctionDotCallNode;
                AssociativeNode     statement   = null;
                TraverseToSplit(funcDotNode.FunctionCall, out statement, ref splitList);
                funcDotNode.FunctionCall = (statement as FunctionCallNode);
                TraverseToSplit(funcDotNode.DotCall.FormalArguments[0], out statement, ref splitList);
                if (statement is BinaryExpressionNode)
                {
                    funcDotNode.DotCall.FormalArguments[0] = (statement as BinaryExpressionNode).LeftNode;
                }
                else
                {
                    funcDotNode.DotCall.FormalArguments[0] = statement;
                }
                outNode = funcDotNode;
            }
            else if (node is ProtoCore.AST.AssociativeAST.ImportNode)
            {
                outNode = node;
                splitList.Add(outNode);
            }
            else if (node is ProtoCore.AST.AssociativeAST.ArrayIndexerNode)
            {
                ArrayIndexerNode arrIdxNode = node as ArrayIndexerNode;
                AssociativeNode  statement  = null;
                TraverseToSplit(arrIdxNode.Array, out statement, ref splitList);
                arrIdxNode.Array = statement;
                outNode          = arrIdxNode;
            }
            else if (node is ProtoCore.AST.AssociativeAST.ExprListNode)
            {
                ExprListNode    exprListNode = node as ExprListNode;
                AssociativeNode statement    = null;
                //for (int i=0; i<exprListNode.list.Count; i++)
                foreach (AssociativeNode listNode in exprListNode.list)
                {
                    TraverseToSplit(listNode, out statement, ref splitList);
                }
                for (int i = 0; i < exprListNode.list.Count; i++)
                {
                    AssociativeNode argNode = exprListNode.list[i];
                    if (argNode is BinaryExpressionNode)
                    {
                        exprListNode.list[i] = (argNode as BinaryExpressionNode).LeftNode;
                    }
                }
                outNode = exprListNode;
            }
            //else if (node is ProtoCore.AST.AssociativeAST.ArrayNode)
            //{
            //    k
            //}
            else if (node is ProtoCore.AST.AssociativeAST.RangeExprNode)
            {
                RangeExprNode   rangeExprNode = node as RangeExprNode;
                AssociativeNode statement     = null;
                TraverseToSplit(rangeExprNode.FromNode, out statement, ref splitList);
                TraverseToSplit(rangeExprNode.StepNode, out statement, ref splitList);
                TraverseToSplit(rangeExprNode.ToNode, out statement, ref splitList);
                if (rangeExprNode.FromNode is BinaryExpressionNode)
                {
                    rangeExprNode.FromNode = (rangeExprNode.FromNode as BinaryExpressionNode).LeftNode;
                }
                if (rangeExprNode.StepNode is BinaryExpressionNode)
                {
                    rangeExprNode.StepNode = (rangeExprNode.StepNode as BinaryExpressionNode).LeftNode;
                }
                if (rangeExprNode.ToNode is BinaryExpressionNode)
                {
                    rangeExprNode.ToNode = (rangeExprNode.ToNode as BinaryExpressionNode).LeftNode;
                }
                outNode = rangeExprNode;
            }
            else
            {
                outNode = node;
            }
        }