private DeconstructionDesugaringResult DesugarPattern(deconstructor_pattern pattern, expression matchingExpression)
        {
            Debug.Assert(!pattern.IsRecursive, "All recursive patterns should be desugared into simple patterns at this point");

            var desugarResult    = new DeconstructionDesugaringResult();
            var castVariableName = NewGeneralName();

            desugarResult.CastVariableDefinition = new var_statement(castVariableName, pattern.type);

            var successVariableName = NewSuccessName();

            desugarResult.SuccessVariableDefinition = new var_statement(successVariableName, new ident("false"));

            // делегирование проверки паттерна функции IsTest
            desugarResult.TypeCastCheck = SubtreeCreator.CreateSystemFunctionCall(IsTestMethodName, matchingExpression, castVariableName);

            var parameters = pattern.parameters.Cast <var_deconstructor_parameter>();

            foreach (var deconstructedVariable in parameters)
            {
                desugarResult.DeconstructionVariables.Add(
                    new var_def_statement(deconstructedVariable.identifier, deconstructedVariable.type));
            }

            var deconstructCall = new procedure_call();

            deconstructCall.func_name     = SubtreeCreator.CreateMethodCall(DeconstructMethodName, castVariableName.name, parameters.Select(x => x.identifier).ToArray());
            desugarResult.DeconstructCall = deconstructCall;

            return(desugarResult);
        }
示例#2
0
        private expression DesugarDeconstructorPatternParameters(deconstructor_pattern pattern)
        {
            expression paramCheckExpr = null;

            for (int i = 0; i < pattern.parameters.Count; ++i)
            {
                if (pattern.parameters[i] is const_pattern_parameter constPattern)
                {
                    var constParamIdent = new ident(NewDeconstructParamId(), pattern.parameters[i].source_context);

                    var eqParams = new expression_list(
                        new List <expression>()
                    {
                        constPattern.const_param,
                        constParamIdent
                    }
                        );

                    var constParamCheck = new method_call(
                        new dot_node(new ident("object"), new ident("Equals")),
                        eqParams,
                        pattern.source_context
                        );

                    pattern.parameters[i] = new var_deconstructor_parameter(
                        constParamIdent,
                        GetTypeDefinitionForConstParam(constPattern.const_param),
                        false,
                        pattern.parameters[i].source_context);

                    paramCheckExpr = paramCheckExpr == null ? (expression)constParamCheck : bin_expr.LogicalAnd(paramCheckExpr, constParamCheck);
                }

                if (pattern.parameters[i] is wild_card_deconstructor_parameter)
                {
                    var wildCardGeneratedParamIdent = new ident(NewDeconstructParamId(), pattern.parameters[i].source_context);
                    pattern.parameters[i] = new var_deconstructor_parameter(
                        wildCardGeneratedParamIdent,
                        null,
                        false,
                        pattern.parameters[i].source_context);
                }

                if (pattern.parameters[i] is recursive_deconstructor_parameter deconstructor_param)
                {
                    if (deconstructor_param.pattern is deconstructor_pattern deconstructor_pattern)
                    {
                        var recursiveChecks = DesugarDeconstructorPatternParameters(deconstructor_pattern);
                        paramCheckExpr = paramCheckExpr == null ?
                                         recursiveChecks :
                                         bin_expr.LogicalAnd(paramCheckExpr, recursiveChecks);
                    }
                }
            }
            return(paramCheckExpr);
        }
        private expression DesugarRecursiveDeconstructor(expression expression, deconstructor_pattern pattern)
        {
            List <pattern_deconstructor_parameter> parameters = pattern.parameters;
            expression conjunction = new is_pattern_expr(expression, pattern);

            for (int i = 0; i < parameters.Count; i++)
            {
                if (parameters[i] is recursive_deconstructor_parameter parameter)
                {
                    //var parameterType = (parameter.pattern as deconstructor_pattern).type;
                    var newName      = NewGeneralName();
                    var varParameter = new var_deconstructor_parameter(newName, null);
                    parameters[i]       = varParameter;
                    varParameter.Parent = parameters[i];
                    conjunction         = bin_expr.LogicalAnd(conjunction, DesugarRecursiveDeconstructor(newName, parameter.pattern as deconstructor_pattern));
                }
            }

            return(conjunction);
        }
示例#4
0
 public virtual void visit(deconstructor_pattern _deconstructor_pattern)
 {
     DefaultVisit(_deconstructor_pattern);
 }