コード例 #1
0
ファイル: ParserUtils.cs プロジェクト: sh4nnongoh/Dynamo
        /// <summary>
        /// Parses desginscript code with specified core and returns a 
        /// ProtoAST CodeBlockNode
        /// </summary>
        /// <param name="code"></param>
        /// <param name="core"></param>
        /// <returns></returns>
        public static ParseResult ParseWithCore(string code, ProtoCore.Core core)
        {
            var p = CreateParser(code, core);
            p.Parse();

            ParseResult result = new ParseResult();
            result.CodeBlockNode = p.root as AST.AssociativeAST.CodeBlockNode;
            result.CommentBlockNode = p.commentNode as AST.AssociativeAST.CodeBlockNode;
            return result;
        }
コード例 #2
0
ファイル: CompilerUtils.cs プロジェクト: yaoclee/Dynamo
        private static void ParseUserCodeCore(Core core, string expression, out List <AssociativeNode> astNodes, out List <AssociativeNode> commentNodes)
        {
            astNodes = new List <AssociativeNode>();

            core.ResetForPrecompilation();
            core.IsParsingCodeBlockNode = true;
            core.ParsingMode            = ParseMode.AllowNonAssignment;

            ParseResult parseResult = ParserUtils.ParseWithCore(expression, core);

            commentNodes = ParserUtils.GetAstNodes(parseResult.CommentBlockNode);
            var nodes = ParserUtils.GetAstNodes(parseResult.CodeBlockNode);

            Validity.Assert(nodes != null);

            int index           = 0;
            int typedIdentIndex = 0;

            foreach (var node in nodes)
            {
                var n = node as AssociativeNode;
                Validity.Assert(n != null);

                // Append the temporaries only if it is not a function def or class decl
                bool isFunctionOrClassDef = n is FunctionDefinitionNode || n is ClassDeclNode;

                if (n is ImportNode)
                {
                    core.BuildStatus.LogSemanticError(Resources.ImportStatementNotSupported);
                }
                else if (isFunctionOrClassDef)
                {
                    // Add node as it is
                    astNodes.Add(node);
                }
                else
                {
                    // Handle temporary naming for temporary Binary exp. nodes and non-assignment nodes
                    var ben = node as BinaryExpressionNode;
                    if (ben != null && ben.Optr == Operator.assign)
                    {
                        var lNode = ben.LeftNode as IdentifierNode;
                        if (lNode != null && lNode.Value == Constants.kTempProcLeftVar)
                        {
                            string name    = Constants.kTempVarForNonAssignment + index;
                            var    newNode = new BinaryExpressionNode(new IdentifierNode(name), ben.RightNode);
                            astNodes.Add(newNode);
                            index++;
                        }
                        else
                        {
                            // Add node as it is
                            astNodes.Add(node);
                            index++;
                        }
                    }
                    else
                    {
                        if (node is TypedIdentifierNode)
                        {
                            // e.g. a : int = %tTypedIdent_<Index>;
                            var ident = new IdentifierNode(Constants.kTempVarForTypedIdentifier + typedIdentIndex);
                            NodeUtils.CopyNodeLocation(ident, node);
                            var typedNode = new BinaryExpressionNode(node as TypedIdentifierNode, ident, Operator.assign);
                            NodeUtils.CopyNodeLocation(typedNode, node);
                            astNodes.Add(typedNode);
                            typedIdentIndex++;
                        }
                        else
                        {
                            string name    = Constants.kTempVarForNonAssignment + index;
                            var    newNode = new BinaryExpressionNode(new IdentifierNode(name), n);
                            astNodes.Add(newNode);
                            index++;
                        }
                    }
                }
            }
        }