예제 #1
0
        private bool AstAssignsToSameVariable(Ast ast)
        {
            ParameterAst ast2 = ast as ParameterAst;

            if (ast2 != null)
            {
                return(this.VariablePath.IsUnscopedVariable && ast2.Name.VariablePath.UnqualifiedPath.Equals(this.VariablePath.UnqualifiedPath, StringComparison.OrdinalIgnoreCase));
            }
            ForEachStatementAst ast3 = ast as ForEachStatementAst;

            if (ast3 != null)
            {
                return(this.VariablePath.IsUnscopedVariable && ast3.Variable.VariablePath.UnqualifiedPath.Equals(this.VariablePath.UnqualifiedPath, StringComparison.OrdinalIgnoreCase));
            }
            AssignmentStatementAst ast4 = (AssignmentStatementAst)ast;
            ExpressionAst          left = ast4.Left;
            ConvertExpressionAst   ast6 = left as ConvertExpressionAst;

            if (ast6 != null)
            {
                left = ast6.Child;
            }
            VariableExpressionAst ast7 = left as VariableExpressionAst;

            if (ast7 == null)
            {
                return(false);
            }
            System.Management.Automation.VariablePath variablePath = ast7.VariablePath;
            return(variablePath.UserPath.Equals(this.VariablePath.UserPath, StringComparison.OrdinalIgnoreCase) || (this.VariablePath.IsScript && this.VariablePath.UnqualifiedPath.Equals(variablePath.UnqualifiedPath, StringComparison.OrdinalIgnoreCase)));
        }
예제 #2
0
 public override AstVisitAction VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
 {
     this.CheckAssignmentTarget(assignmentStatementAst.Left, assignmentStatementAst.Operator == TokenKind.Equals, delegate(Ast ast) {
         this._parser.ReportError(ast.Extent, ParserStrings.InvalidLeftHandSide, new object[0]);
     });
     return(AstVisitAction.Continue);
 }
예제 #3
0
        public override AstVisitAction VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
        {
            if (_symbolTable.IsInMethodScope())
            {
                var targets = assignmentStatementAst.GetAssignmentTargets().ToArray();
                foreach (var expressionAst in targets)
                {
                    var expression            = expressionAst;
                    var variableExpressionAst = expression as VariableExpressionAst;
                    while (variableExpressionAst == null && expression != null)
                    {
                        var convertExpressionAst = expression as ConvertExpressionAst;
                        if (convertExpressionAst != null)
                        {
                            expression            = convertExpressionAst.Child;
                            variableExpressionAst = convertExpressionAst.Child as VariableExpressionAst;
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (variableExpressionAst != null && variableExpressionAst.VariablePath.IsVariable)
                    {
                        var ast            = _symbolTable.LookupVariable(variableExpressionAst.VariablePath);
                        var propertyMember = ast as PropertyMemberAst;
                        if (propertyMember != null)
                        {
                            if (propertyMember.IsStatic)
                            {
                                var typeAst = _symbolTable.GetCurrentTypeDefinitionAst();
                                Diagnostics.Assert(typeAst != null, "Method scopes can exist only inside type definitions.");

                                string typeString = string.Format(CultureInfo.InvariantCulture, "[{0}]::", typeAst.Name);
                                _parser.ReportError(variableExpressionAst.Extent,
                                                    nameof(ParserStrings.MissingTypeInStaticPropertyAssignment),
                                                    ParserStrings.MissingTypeInStaticPropertyAssignment,
                                                    typeString,
                                                    propertyMember.Name);
                            }
                            else
                            {
                                _parser.ReportError(variableExpressionAst.Extent,
                                                    nameof(ParserStrings.MissingThis),
                                                    ParserStrings.MissingThis,
                                                    "$this.",
                                                    propertyMember.Name);
                            }
                        }
                    }
                }
                // TODO: static look for alias and function.
            }

            return(AstVisitAction.Continue);
        }
예제 #4
0
        public override AstVisitAction VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
        {
            var variableExpressionAst = assignmentStatementAst.Left as VariableExpressionAst;
            if (variableExpressionAst == null) throw new NotImplementedException(assignmentStatementAst.ToString());

            var variable = this._context.SessionState.SessionStateGlobal.SetVariable(variableExpressionAst.VariablePath.UserPath, EvaluateAst(assignmentStatementAst.Right));
            this._pipelineCommandRuntime.WriteObject(variable);

            return AstVisitAction.SkipChildren;
        }
예제 #5
0
    public System.Object VisitAssignmentStatement(System.Management.Automation.Language.AssignmentStatementAst assignmentStatementAst)
    {
        IScriptExtent mappedExtent = MapExtent(assignmentStatementAst.Extent);

        IScriptExtent mappedErrorExtent = MapExtent(assignmentStatementAst.ErrorPosition);

        ExpressionAst mappedLeft  = _VisitExpression(assignmentStatementAst.Left);
        StatementAst  mappedRight = _VisitStatement(assignmentStatementAst.Right);

        return(new AssignmentStatementAst(mappedExtent, mappedLeft, assignmentStatementAst.Operator, mappedRight, mappedErrorExtent));
    }
        public override AstVisitAction VisitAssignmentStatement(AssignmentStatementAst ast)
        {
            if (!used)
            {
                if (ast.Operator == TokenKind.Equals && ast.Left is VariableExpressionAst)
                {
                    if (((VariableExpressionAst) ast.Left).VariablePath.UserPath == varName)
                    {
                        LastAssignmentStatementAst = ast;
                    }
                }
            }

            return AstVisitAction.Continue;
        }
예제 #7
0
 public object VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
 {
     assignmentStatementAst.Right.Accept(this);
     foreach (ExpressionAst ast in GetAssignmentTargets(assignmentStatementAst.Left))
     {
         bool          flag  = false;
         int           num   = 0;
         ExpressionAst child = ast;
         while (child is AttributedExpressionAst)
         {
             num++;
             if (!(child is ConvertExpressionAst))
             {
                 flag = true;
             }
             child = ((AttributedExpressionAst)child).Child;
         }
         if (child is VariableExpressionAst)
         {
             if (flag || (num > 1))
             {
                 VariablePath variablePath = ((VariableExpressionAst)child).VariablePath;
                 if (variablePath.IsAnyLocal())
                 {
                     VariableAnalysisDetails details = this._variables[GetUnaliasedVariableName(variablePath)];
                     details.LocalTupleIndex = -2;
                 }
             }
             else
             {
                 this._currentBlock.AddAst(new AssignmentTarget(ast));
             }
         }
         else
         {
             ast.Accept(this);
         }
     }
     return(null);
 }
예제 #8
0
 /// <summary/>
 public virtual object VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst) { return null; }
예제 #9
0
 public object VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
 {
     return(false);
 }
예제 #10
0
파일: Compiler.cs 프로젝트: nickchal/pash
 private Expression CompileAssignment(AssignmentStatementAst assignmentStatementAst, MergeRedirectExprs generateRedirectExprs = null)
 {
     ArrayLiteralAst left = assignmentStatementAst.Left as ArrayLiteralAst;
     if (assignmentStatementAst.Left is ParenExpressionAst)
     {
         left = ((ParenExpressionAst) assignmentStatementAst.Left).Pipeline.GetPureExpression() as ArrayLiteralAst;
     }
     Expression expression = this.CaptureStatementResults(assignmentStatementAst.Right, (left != null) ? CaptureAstContext.Enumerable : CaptureAstContext.Assignment, generateRedirectExprs);
     if (left != null)
     {
         expression = Expression.Dynamic(PSArrayAssignmentRHSBinder.Get(left.Elements.Count), typeof(IList), expression);
     }
     List<Expression> expressions = new List<Expression> {
         this.UpdatePosition(assignmentStatementAst),
         this.ReduceAssignment((ISupportsAssignment) assignmentStatementAst.Left, assignmentStatementAst.Operator, expression)
     };
     return Expression.Block(expressions);
 }
예제 #11
0
 public object VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
 {
     return false;
 }
        public AssignmentTarget(AssignmentStatementAst ast)
        {
            this._targetAst = ast.Left;
            this._rightAst = ast.Right;

            if (_rightAst != null)
            {
                Constant = Undetermined.UndeterminedConstant;
                Type = typeof(Undetermined);
            }

            CommandExpressionAst cmExAst = _rightAst as CommandExpressionAst;

            if (cmExAst != null)
            {
                ExpressionAst exprAst = cmExAst.Expression;
                Type = exprAst.StaticType;

                if (exprAst is ConvertExpressionAst)
                {
                    ConvertExpressionAst convertAst = exprAst as ConvertExpressionAst;

                    Type = DeepestRelatedDerivedClass(convertAst.StaticType, Type);

                    if (convertAst.Child is ConstantExpressionAst)
                    {
                        Constant = (convertAst.Child as ConstantExpressionAst).Value;
                    }
                }
                else if (exprAst is BinaryExpressionAst)
                {
                    BinaryExpressionAst binAst = exprAst as BinaryExpressionAst;
                    if (binAst != null && binAst.Operator == TokenKind.As && binAst.Right is TypeExpressionAst)
                    {
                        Type = DeepestRelatedDerivedClass((binAst.Right as TypeExpressionAst).TypeName.GetReflectionType(),
                            binAst.Left.StaticType);

                        if (binAst.Left is ConstantExpressionAst)
                        {
                            Constant = (binAst.Left as ConstantExpressionAst).Value;
                        }
                        else if (binAst.Left is VariableExpressionAst)
                        {
                            _rightHandSideVariable = new VariableTarget(binAst.Left as VariableExpressionAst);
                        }
                    }
                }
                else if (exprAst is ConstantExpressionAst)
                {
                    Constant = (cmExAst.Expression as ConstantExpressionAst).Value;
                }
                else if (exprAst is VariableExpressionAst)
                {
                    _rightHandSideVariable = new VariableTarget(cmExAst.Expression as VariableExpressionAst);
                    if (String.Equals((exprAst as VariableExpressionAst).VariablePath.UserPath, "this", StringComparison.OrdinalIgnoreCase))
                    {
                        Constant = SpecialVars.ThisVariable;
                    }
                }
                //Store the type info for variable assignment from .Net type
                else if (exprAst is MemberExpressionAst)
                {

                    Type = DeepestRelatedDerivedClass(Type, GetTypeFromMemberExpressionAst(exprAst as MemberExpressionAst));
                }
            }
            // We'll consider case where there is only 1 pipeline element for now
            else if (_rightAst is PipelineAst && (_rightAst as PipelineAst).PipelineElements.Count == 1)
            {
                #region Process New-Object command
                CommandAst cmdAst = (_rightAst as PipelineAst).PipelineElements[0] as CommandAst;

                if (cmdAst != null && cmdAst.CommandElements.Count > 1)
                {
                    StringConstantExpressionAst stringAst = cmdAst.CommandElements[0] as StringConstantExpressionAst;

                    if (stringAst != null && String.Equals(stringAst.Value, "new-object", StringComparison.OrdinalIgnoreCase))
                    {
                        CommandParameterAst secondElement = cmdAst.CommandElements[1] as CommandParameterAst;
                        StringConstantExpressionAst typeName = null;

                        if (secondElement != null)
                        {
                            if (String.Equals(secondElement.ParameterName, "TypeName", StringComparison.OrdinalIgnoreCase))
                            {
                                if (secondElement.Argument != null)
                                {
                                    typeName = secondElement.Argument as StringConstantExpressionAst;
                                }
                                else
                                {
                                    if (cmdAst.CommandElements.Count > 2)
                                    {
                                        typeName = cmdAst.CommandElements[2] as StringConstantExpressionAst;
                                    }
                                }
                            }
                        }
                        else
                        {
                            typeName = cmdAst.CommandElements[1] as StringConstantExpressionAst;
                        }

                        if (typeName != null)
                        {
                            Type = System.Type.GetType(typeName.Value) ?? typeof(object);
                        }
                    }
                }

                #endregion
            }

            SetVariableName();
        }
 /// <summary/>
 public virtual object VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
 {
     return _decorated.VisitAssignmentStatement(assignmentStatementAst);
 }
예제 #14
0
 public override AstVisitAction VisitAssignmentStatement(AssignmentStatementAst ast)
 {
     return this.Check(ast);
 }
예제 #15
0
        public override AstVisitAction VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
        {
            ExpressionAst expressionAst = assignmentStatementAst.Left;
            var isEquals = assignmentStatementAst.Operator == TokenKind.Equals;
            var isVariableAssignment = expressionAst is VariableExpressionAst;
            var rightValueRes = EvaluateAst(assignmentStatementAst.Right);
            // a little ugly, but we need to stay dynamic. It's crucial that a psobject isn't unpacked if it's simply
            // assigned to a variable, otherwise we could lose some important properties
            // TODO: this is more like a workaround. PSObject should implement implicit casting,
            // then no checks and .BaseObject calls should be necessary anymore
            bool unpackPSObject = !isEquals || !isVariableAssignment;
            dynamic rightValue = (rightValueRes is PSObject && unpackPSObject) ?
                ((PSObject)rightValueRes).BaseObject : rightValueRes;
            object newValue = rightValue;

            dynamic currentValueRes = isEquals ? null : EvaluateAst(assignmentStatementAst.Left);
            dynamic currentValue = (currentValueRes != null && currentValueRes is PSObject && unpackPSObject) ?
                                   ((PSObject)currentValueRes).BaseObject : currentValueRes;

            if (assignmentStatementAst.Operator == TokenKind.Equals)
            {
                newValue = rightValue;
            }
            else if (assignmentStatementAst.Operator == TokenKind.PlusEquals)
            {
                newValue = currentValue + rightValue;
            }
            else if (assignmentStatementAst.Operator == TokenKind.MinusEquals)
            {
                newValue = currentValue - rightValue;
            }
            else if (assignmentStatementAst.Operator == TokenKind.MultiplyEquals)
            {
                newValue = currentValue * rightValue;
            }
            else if (assignmentStatementAst.Operator == TokenKind.DivideEquals)
            {
                newValue = currentValue / rightValue;
            }
            else if (assignmentStatementAst.Operator == TokenKind.RemainderEquals)
            {
                newValue = currentValue % rightValue;
            }
            else
            {
                throw new NotImplementedException("Unsupported operator: " + assignmentStatementAst.Operator.ToString());
            }

            if (this._writeSideEffectsToPipeline)
            {
                this._pipelineCommandRuntime.WriteObject(newValue);
            }

            if (isVariableAssignment)
            {
                _context.SetVariable(((VariableExpressionAst) expressionAst).VariablePath.UserPath, newValue);
            }
            else if (expressionAst is MemberExpressionAst)
            {
                SetMemberExpressionValue((MemberExpressionAst) expressionAst, newValue);
            }
            else
            {
                var msg = String.Format("The expression type '{0}' is currently not supported for assignments",
                                        expressionAst.GetType().ToString());
                throw new NotImplementedException(msg);
            }

            return AstVisitAction.SkipChildren;
        }
 public object VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst) { throw new UnexpectedElementException(); }
 public override AstVisitAction VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
 {
     this.ReportError(assignmentStatementAst, () => ParserStrings.AssignmentStatementNotSupportedInDataSection, new object[0]);
     return(AstVisitAction.Continue);
 }
예제 #18
0
        public override AstVisitAction VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
        {
            // Make sure LHS is something that can be assigned to.
            CheckAssignmentTarget(assignmentStatementAst.Left, assignmentStatementAst.Operator == TokenKind.Equals,
                ast => _parser.ReportError(ast.Extent, () => ParserStrings.InvalidLeftHandSide));

            return AstVisitAction.Continue;
        }
예제 #19
0
        public override AstVisitAction VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
        {
            // Assignments are never allowed.
            ReportError(assignmentStatementAst, () => ParserStrings.AssignmentStatementNotSupportedInDataSection);

            return AstVisitAction.Continue;
        }
예제 #20
0
 /// <summary/>
 public virtual AstVisitAction VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst) => DefaultVisit(assignmentStatementAst);
예제 #21
0
 public override AstVisitAction VisitAssignmentStatement(AssignmentStatementAst ast) { return CheckParent(ast); }
예제 #22
0
 // Capture modules that add themselves to the path (so they generally package their functionality
 // as loose PS1 files)
 public override AstVisitAction VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
 {
     // $env:PATH += "";$psScriptRoot""
     if (String.Equals("$env:PATH", assignmentStatementAst.Left.ToString(), StringComparison.OrdinalIgnoreCase) &&
         Regex.IsMatch(assignmentStatementAst.Right.ToString(), "\\$psScriptRoot", RegexOptions.IgnoreCase))
     {
         ModuleIntrinsics.Tracer.WriteLine("Module adds itself to the path.");
         AddsSelfToPath = true;
     }
     return AstVisitAction.SkipChildren;
 }
예제 #23
0
 public override AstVisitAction VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
 {
     this.ReportError(assignmentStatementAst, () => ParserStrings.AssignmentStatementNotSupportedInDataSection, new object[0]);
     return AstVisitAction.Continue;
 }
예제 #24
0
        public override AstVisitAction VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
        {
            var rightValue = EvaluateAst(assignmentStatementAst.Right);
            object newValue = rightValue;

            ExpressionAst expressionAst = assignmentStatementAst.Left;
            var variableExpressionAst = expressionAst as VariableExpressionAst;
            if (variableExpressionAst == null) throw new NotImplementedException(expressionAst.ToString());

            if (assignmentStatementAst.Operator == TokenKind.Equals)
            {
                _context.SetVariable(variableExpressionAst.VariablePath.UserPath, rightValue);
            }

            else if (assignmentStatementAst.Operator == TokenKind.PlusEquals)
            {
                dynamic currentValue = _context.GetVariableValue(variableExpressionAst.VariablePath.UserPath);
                dynamic assignmentValue = ((PSObject)rightValue).BaseObject;
                newValue = currentValue + assignmentValue;
                _context.SetVariable(variableExpressionAst.VariablePath.UserPath, newValue);
            }

            else if (assignmentStatementAst.Operator == TokenKind.MinusEquals)
            {
                dynamic currentValue = _context.GetVariableValue(variableExpressionAst.VariablePath.UserPath);
                dynamic assignmentValue = ((PSObject)rightValue).BaseObject;
                newValue = currentValue - assignmentValue;
                _context.SetVariable(variableExpressionAst.VariablePath.UserPath, newValue);
            }

            else if (assignmentStatementAst.Operator == TokenKind.MultiplyEquals)
            {
                dynamic currentValue = _context.GetVariableValue(variableExpressionAst.VariablePath.UserPath);
                dynamic assignmentValue = ((PSObject)rightValue).BaseObject;
                newValue = currentValue * assignmentValue;
                _context.SetVariable(variableExpressionAst.VariablePath.UserPath, newValue);
            }

            else if (assignmentStatementAst.Operator == TokenKind.DivideEquals)
            {
                dynamic currentValue = _context.GetVariableValue(variableExpressionAst.VariablePath.UserPath);
                dynamic assignmentValue = ((PSObject)rightValue).BaseObject;
                newValue = currentValue / assignmentValue;
                _context.SetVariable(variableExpressionAst.VariablePath.UserPath, newValue);
            }

            else if (assignmentStatementAst.Operator == TokenKind.RemainderEquals)
            {
                dynamic currentValue = _context.GetVariableValue(variableExpressionAst.VariablePath.UserPath);
                dynamic assignmentValue = ((PSObject)rightValue).BaseObject;
                newValue = currentValue % assignmentValue;
                _context.SetVariable(variableExpressionAst.VariablePath.UserPath, newValue);
            }

            if (this._writeSideEffectsToPipeline) this._pipelineCommandRuntime.WriteObject(newValue);

            return AstVisitAction.SkipChildren;
        }
예제 #25
0
 public object VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
 {
     assignmentStatementAst.Right.Accept(this);
     foreach (ExpressionAst ast in GetAssignmentTargets(assignmentStatementAst.Left))
     {
         bool flag = false;
         int num = 0;
         ExpressionAst child = ast;
         while (child is AttributedExpressionAst)
         {
             num++;
             if (!(child is ConvertExpressionAst))
             {
                 flag = true;
             }
             child = ((AttributedExpressionAst) child).Child;
         }
         if (child is VariableExpressionAst)
         {
             if (flag || (num > 1))
             {
                 VariablePath variablePath = ((VariableExpressionAst) child).VariablePath;
                 if (variablePath.IsAnyLocal())
                 {
                     VariableAnalysisDetails details = this._variables[GetUnaliasedVariableName(variablePath)];
                     details.LocalTupleIndex = -2;
                 }
             }
             else
             {
                 this._currentBlock.AddAst(new AssignmentTarget(ast));
             }
         }
         else
         {
             ast.Accept(this);
         }
     }
     return null;
 }
예제 #26
0
 public override AstVisitAction VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
 {
     if (_symbolTable.IsInMethodScope())
     {
         var targets = assignmentStatementAst.GetAssignmentTargets().ToArray();
         foreach (var expressionAst in targets)
         {
             var expression = expressionAst;
             var variableExpressionAst = expression as VariableExpressionAst;
             while (variableExpressionAst == null && expression != null)
             {
                 var convertExpressionAst = expression as ConvertExpressionAst;
                 if (convertExpressionAst != null)
                 {
                     expression = convertExpressionAst.Child;
                     variableExpressionAst = convertExpressionAst.Child as VariableExpressionAst;
                 }
                 else
                 {
                     break;
                 }
             }
             if (variableExpressionAst != null && variableExpressionAst.VariablePath.IsVariable)
             {
                 var ast = _symbolTable.LookupVariable(variableExpressionAst.VariablePath);
                 var propertyMember = ast as PropertyMemberAst;
                 if (propertyMember != null)
                 {
                     if (propertyMember.IsStatic)
                     {
                         var typeAst = _symbolTable.GetCurrentTypeDefinitionAst();
                         Diagnostics.Assert(typeAst != null, "Method scopes can exist only inside type definitions.");
                         _parser.ReportError(variableExpressionAst.Extent, () => ParserStrings.MissingTypeInStaticPropertyAssignment,
                             String.Format(CultureInfo.InvariantCulture, "[{0}]::", typeAst.Name), propertyMember.Name);
                     }
                     else
                     {
                         _parser.ReportError(variableExpressionAst.Extent, () => ParserStrings.MissingThis, "$this.", propertyMember.Name);
                     }
                 }
             }
         }
         // TODO: static look for alias and function.
     }
     return AstVisitAction.Continue;
 }
 /// <summary>
 /// Visit assignmentstatement
 /// </summary>
 /// <param name="assignmentStatementAst"></param>
 /// <returns></returns>
 public object VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
 {
     if (assignmentStatementAst == null) return null;
     assignmentStatementAst.Right.Visit(this.Decorator);
     return null;
 }
예제 #28
0
        internal override IEnumerable <PSTypeName> GetInferredType(CompletionContext context)
        {
            if (this.VariablePath.IsVariable)
            {
                Ast parent = this.Parent;
                if (this.VariablePath.IsUnqualified && (this.VariablePath.UserPath.Equals("_", StringComparison.Ordinal) || this.VariablePath.UserPath.Equals("PSItem", StringComparison.OrdinalIgnoreCase)))
                {
                    while (parent != null)
                    {
                        if (parent is ScriptBlockExpressionAst)
                        {
                            break;
                        }
                        parent = parent.Parent;
                    }
                    if (parent != null)
                    {
                        if ((parent.Parent is CommandExpressionAst) && (parent.Parent.Parent is PipelineAst))
                        {
                            if (parent.Parent.Parent.Parent is HashtableAst)
                            {
                                parent = parent.Parent.Parent.Parent;
                            }
                            else if ((parent.Parent.Parent.Parent is ArrayLiteralAst) && (parent.Parent.Parent.Parent.Parent is HashtableAst))
                            {
                                parent = parent.Parent.Parent.Parent.Parent;
                            }
                        }
                        if (parent.Parent is CommandParameterAst)
                        {
                            parent = parent.Parent;
                        }
                        CommandAst iteratorVariable1 = parent.Parent as CommandAst;
                        if (iteratorVariable1 != null)
                        {
                            PipelineAst iteratorVariable2 = (PipelineAst)iteratorVariable1.Parent;
                            int         iteratorVariable3 = iteratorVariable2.PipelineElements.IndexOf(iteratorVariable1) - 1;
                            if (iteratorVariable3 >= 0)
                            {
                                foreach (PSTypeName iteratorVariable4 in iteratorVariable2.PipelineElements[0].GetInferredType(context))
                                {
                                    if (iteratorVariable4.Type != null)
                                    {
                                        if (iteratorVariable4.Type.IsArray)
                                        {
                                            yield return(new PSTypeName(iteratorVariable4.Type.GetElementType()));

                                            continue;
                                        }
                                        if (typeof(IEnumerable).IsAssignableFrom(iteratorVariable4.Type))
                                        {
                                            IEnumerable <Type> iteratorVariable5 = from t in iteratorVariable4.Type.GetInterfaces()
                                                                                   where t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(IEnumerable <>))
                                                                                   select t;
                                            foreach (Type iteratorVariable6 in iteratorVariable5)
                                            {
                                                yield return(new PSTypeName(iteratorVariable6.GetGenericArguments()[0]));
                                            }
                                            continue;
                                        }
                                    }
                                    yield return(iteratorVariable4);
                                }
                            }
                            goto Label_0833;
                        }
                    }
                }
                if (this.VariablePath.IsUnqualified)
                {
                    for (int i = 0; i < SpecialVariables.AutomaticVariables.Length; i++)
                    {
                        if (this.VariablePath.UserPath.Equals(SpecialVariables.AutomaticVariables[i], StringComparison.OrdinalIgnoreCase))
                        {
                            Type type = SpecialVariables.AutomaticVariableTypes[i];
                            if (!type.Equals(typeof(object)))
                            {
                                yield return(new PSTypeName(type));

                                break;
                            }
                            break;
                        }
                    }
                }
                while (parent.Parent != null)
                {
                    parent = parent.Parent;
                }
                if (parent.Parent is FunctionDefinitionAst)
                {
                    parent = parent.Parent;
                }
                IEnumerable <Ast> source             = AstSearcher.FindAll(parent, ast => (((ast is ParameterAst) || (ast is AssignmentStatementAst)) || (ast is ForEachStatementAst)) && this.AstAssignsToSameVariable(ast), true);
                ParameterAst      iteratorVariable10 = source.OfType <ParameterAst>().FirstOrDefault <ParameterAst>();
                if (iteratorVariable10 != null)
                {
                    PSTypeName[] iteratorVariable11 = iteratorVariable10.GetInferredType(context).ToArray <PSTypeName>();
                    if (iteratorVariable11.Length > 0)
                    {
                        foreach (PSTypeName iteratorVariable12 in iteratorVariable11)
                        {
                            yield return(iteratorVariable12);
                        }
                        goto Label_0833;
                    }
                }
                AssignmentStatementAst[] iteratorVariable13 = source.OfType <AssignmentStatementAst>().ToArray <AssignmentStatementAst>();
                foreach (AssignmentStatementAst iteratorVariable14 in iteratorVariable13)
                {
                    ConvertExpressionAst left = iteratorVariable14.Left as ConvertExpressionAst;
                    if ((left != null) && (left.StaticType != null))
                    {
                        yield return(new PSTypeName(left.StaticType));

                        goto Label_0833;
                    }
                }
                ForEachStatementAst iteratorVariable16 = source.OfType <ForEachStatementAst>().FirstOrDefault <ForEachStatementAst>();
                if (iteratorVariable16 != null)
                {
                    foreach (PSTypeName iteratorVariable17 in iteratorVariable16.Condition.GetInferredType(context))
                    {
                        yield return(iteratorVariable17);
                    }
                }
                else
                {
                    int startOffset        = this.Extent.StartOffset;
                    int iteratorVariable19 = 0x7fffffff;
                    AssignmentStatementAst iteratorVariable20 = null;
                    foreach (AssignmentStatementAst ast in iteratorVariable13)
                    {
                        int endOffset = ast.Extent.EndOffset;
                        if ((endOffset < startOffset) && ((startOffset - endOffset) < iteratorVariable19))
                        {
                            iteratorVariable19 = startOffset - endOffset;
                            iteratorVariable20 = ast;
                        }
                    }
                    if (iteratorVariable20 != null)
                    {
                        foreach (PSTypeName iteratorVariable21 in iteratorVariable20.Right.GetInferredType(context))
                        {
                            yield return(iteratorVariable21);
                        }
                    }
                }
            }
Label_0833:
            yield break;
        }
예제 #29
0
        public override AstVisitAction VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
        {
            // first check if it's a assignable expression, fail otherwise. later on we also need support for 
            // arrays of variables
            ExpressionAst expressionAst = assignmentStatementAst.Left;
            if (!SettableExpression.SupportedExpressions.Contains(expressionAst.GetType()))
            {
                var msg = String.Format("The expression type '{0}' is currently not supported for assignments",
                                        expressionAst.GetType().ToString());
                throw new NotImplementedException(msg);
            }
            var assignableExpression = SettableExpression.Create(expressionAst, this);
            var rightValue = EvaluateAst(assignmentStatementAst.Right);
            object newValue = rightValue;

            bool isSimpleAssignment = assignmentStatementAst.Operator == TokenKind.Equals;
            // safe the effort to get the value of the left side if it's replaced anyway
            dynamic currentValue = isSimpleAssignment ? null : assignableExpression.GetValue();


            // compute the new value
            if (isSimpleAssignment)
            {
                newValue = rightValue;
            }
            else if (assignmentStatementAst.Operator == TokenKind.PlusEquals)
            {
                newValue = ArithmeticOperations.Add(currentValue, rightValue);
            }
            else if (assignmentStatementAst.Operator == TokenKind.MinusEquals)
            {
                newValue = ArithmeticOperations.Subtract(currentValue, rightValue);
            }
            else if (assignmentStatementAst.Operator == TokenKind.MultiplyEquals)
            {
                newValue = ArithmeticOperations.Multiply(currentValue, rightValue);
            }
            else if (assignmentStatementAst.Operator == TokenKind.DivideEquals)
            {
                newValue = ArithmeticOperations.Divide(currentValue, rightValue);
            }
            else if (assignmentStatementAst.Operator == TokenKind.RemainderEquals)
            {
                newValue = ArithmeticOperations.Remainder(currentValue, rightValue);
            }
            else
            {
                throw new NotImplementedException("Unsupported operator: " + assignmentStatementAst.Operator.ToString());
            }

            if (this._writeSideEffectsToPipeline)
            {
                this._pipelineCommandRuntime.WriteObject(newValue);
            }

            // set the new value
            assignableExpression.SetValue(newValue);

            return AstVisitAction.SkipChildren;
        }
예제 #30
0
파일: Compiler.cs 프로젝트: nickchal/pash
 public object VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
 {
     return this.CompileAssignment(assignmentStatementAst, null);
 }
예제 #31
0
 /// <summary/>
 public virtual object VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
 {
     return(null);
 }
예제 #32
0
 public override AstVisitAction VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
 {
     this.CheckAssignmentTarget(assignmentStatementAst.Left, assignmentStatementAst.Operator == TokenKind.Equals, delegate (Ast ast) {
         this._parser.ReportError(ast.Extent, ParserStrings.InvalidLeftHandSide, new object[0]);
     });
     return AstVisitAction.Continue;
 }
예제 #33
0
 public virtual AstVisitAction VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
 {
     return AstVisitAction.Continue;
 }
예제 #34
0
 public object VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst) { return AutomationNull.Value; }
예제 #35
0
 public object VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst) { throw PSTraceSource.NewArgumentException("ast"); }
예제 #36
0
파일: Compiler.cs 프로젝트: 40a/PowerShell
 public object VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
 {
     return CompileAssignment(assignmentStatementAst);
 }
예제 #37
0
 public override AstVisitAction VisitAssignmentStatement(AssignmentStatementAst ast)
 {
     return(Check(ast));
 }
예제 #38
0
파일: Compiler.cs 프로젝트: 40a/PowerShell
        private Expression CompileAssignment(
            AssignmentStatementAst assignmentStatementAst,
            MergeRedirectExprs generateRedirectExprs = null)
        {
            var arrayLHS = assignmentStatementAst.Left as ArrayLiteralAst;
            var parenExpressionAst = assignmentStatementAst.Left as ParenExpressionAst;
            if (parenExpressionAst != null)
            {
                arrayLHS = parenExpressionAst.Pipeline.GetPureExpression() as ArrayLiteralAst;
            }

            // If assigning to an array, then we prefer an enumerable result because we use an IList
            // to generate the assignments, no sense in converting to object[], or worse, returning a
            // single object.
            // We should not preserve the partial output if exception is thrown when evaluating right-hand-side expression.
            Expression rightExpr = CaptureStatementResults(assignmentStatementAst.Right,
                arrayLHS != null ? CaptureAstContext.Enumerable : CaptureAstContext.AssignmentWithoutResultPreservation, generateRedirectExprs);

            if (arrayLHS != null)
            {
                rightExpr = DynamicExpression.Dynamic(PSArrayAssignmentRHSBinder.Get(arrayLHS.Elements.Count), typeof(IList), rightExpr);
            }

            var exprs = new List<Expression>
                            {
                            // Set current position in case of errors.
                            UpdatePosition(assignmentStatementAst),
                            ReduceAssignment((ISupportsAssignment)assignmentStatementAst.Left,
                                             assignmentStatementAst.Operator,
                                             rightExpr)
                        };

            return Expression.Block(exprs);
        }
예제 #39
0
 public object VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
 {
     throw PSTraceSource.NewArgumentException("ast");
 }
예제 #40
0
        /// <summary>
        /// Visit assignment statement
        /// </summary>
        /// <param name="assignmentStatementAst"></param>
        /// <returns></returns>
        public override object VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
        {
            if (assignmentStatementAst == null)
            {
                return null;
            }

            base.VisitAssignmentStatement(assignmentStatementAst);

            foreach (var assignTarget in GetAssignmentTargets(assignmentStatementAst.Left))
            {
                var leftAst = assignTarget;
                while (leftAst is AttributedExpressionAst)
                {
                    leftAst = ((AttributedExpressionAst)leftAst).Child;
                }

                if (leftAst is VariableExpressionAst)
                {
                    var varPath = ((VariableExpressionAst)leftAst).VariablePath;

                    if (_variables.ContainsKey(AssignmentTarget.GetUnaliasedVariableName(varPath)))
                    {
                        var details = _variables[AssignmentTarget.GetUnaliasedVariableName(varPath)];
                        details.AssignedBlocks.Add(Current);
                    }

                    Current.AddAst(new AssignmentTarget(assignmentStatementAst));
                }
                else
                {
                    // We skip things like $a.test = 3. In this case we will just test
                    // for variable $a
                    assignTarget.Visit(this.Decorator);
                }
            }

            return null;
        }
예제 #41
0
 /// <summary/>
 public virtual AstVisitAction VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
 {
     return(AstVisitAction.Continue);
 }
예제 #42
0
        public object VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
        {
            assignmentStatementAst.Right.Accept(this);

            foreach (var assignTarget in GetAssignmentTargets(assignmentStatementAst.Left))
            {
                bool anyAttributes = false;
                int convertCount = 0;
                ConvertExpressionAst convertAst = null;
                var leftAst = assignTarget;
                while (leftAst is AttributedExpressionAst)
                {
                    convertCount += 1;
                    convertAst = leftAst as ConvertExpressionAst;
                    if (convertAst == null)
                    {
                        anyAttributes = true;
                    }
                    leftAst = ((AttributedExpressionAst)leftAst).Child;
                }

                if (leftAst is VariableExpressionAst)
                {
                    // Two below if statements are similar, but there is a difference:
                    // The first one tells us about the dynamic nature of the local type.
                    // The second one tells us about the assignment, that happens in this block.
                    // Potentially there could be more complicated cases like [int[]]($a, $b) = (1,2)
                    // We don't handle them at all in the variable analysis.

                    if (anyAttributes || convertCount > 1 ||
                        (convertAst != null && convertAst.Type.TypeName.GetReflectionType() == null))
                    {
                        var varPath = ((VariableExpressionAst)leftAst).VariablePath;
                        if (varPath.IsAnyLocal())
                        {
                            var details = _variables[GetUnaliasedVariableName(varPath)];
                            details.LocalTupleIndex = ForceDynamic;
                        }
                    }

                    if (!anyAttributes && convertCount <= 1)
                    {
                        _currentBlock.AddAst(new AssignmentTarget(assignTarget));
                    }
                }
                else
                {
                    // We're not assigning to a simple variable, so visit the left so that variable references get
                    // marked with their proper tuple slots.
                    assignTarget.Accept(this);
                }
            }
            return null;
        }
예제 #43
0
 public object VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
 {
     return(AutomationNull.Value);
 }
예제 #44
0
 public override AstVisitAction VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
 {
     if (string.Equals("$env:PATH", assignmentStatementAst.Left.ToString(), StringComparison.OrdinalIgnoreCase) && Regex.IsMatch(assignmentStatementAst.Right.ToString(), @"\$psScriptRoot", RegexOptions.IgnoreCase))
     {
         ModuleIntrinsics.Tracer.WriteLine("Module adds itself to the path.", new object[0]);
         this.AddsSelfToPath = true;
     }
     return AstVisitAction.Continue;
 }