示例#1
0
        public static IApiMethodBuilderResult CreateTestExpression(ApiBaseFunction func, ExpressionBuilderParams p,
                                                                   FunctionCallStatement functionCallStatement, TestExpressionCreator createTestExpression)
        {
            func.AssertParameters(p, functionCallStatement.Parameters);

            var result = createTestExpression(p, functionCallStatement);

            if (p.UsageContext is IfElseStatement)
            {
                return(new ApiMethodBuilderRawResult(new ExpressionResult(
                                                         func.TypeDescriptor,
                                                         result.Expression,
                                                         result.Template
                                                         )));
            }

            p.NonInlinePartWriter.WriteLine(result.Expression);

            var varName = BashVariableDefinitionStatementTranspiler.WriteLastStatusCodeStoreVariableDefinition(
                p.Context, p.Scope,
                p.NonInlinePartWriter, $"{functionCallStatement.Fqn}_Result");

            return(new ApiMethodBuilderRawResult(new ExpressionResult(
                                                     func.TypeDescriptor,
                                                     $"${varName}",
                                                     new VariableAccessStatement(varName, functionCallStatement.Info),
                                                     ExpressionBuilderBase.PinRequiredNotice
                                                     )));
        }
        protected override ExpressionResult CreateExpressionRecursive(ExpressionBuilderParams p,
                                                                      EvaluationStatement statement)
        {
            switch (statement)
            {
            case LogicalEvaluationStatement logicalEvaluationStatement:
            {
                if (p.UsageContext is IfElseStatement)
                {
                    break;
                }

                switch (logicalEvaluationStatement.Operator)
                {
                case EqualOperator _:
                case NotEqualOperator _:
                {
                    using (var writer = new StringWriter())
                    {
                        var expressionBuilderParams = new ExpressionBuilderParams(p, writer);

                        var leftResult = CreateExpressionRecursive(expressionBuilderParams,
                                                                   logicalEvaluationStatement.Left);
                        var rightResult = CreateExpressionRecursive(expressionBuilderParams,
                                                                    logicalEvaluationStatement.Right);

                        if (leftResult.TypeDescriptor.IsString() && rightResult.TypeDescriptor.IsString())
                        {
                            HandleNotices(expressionBuilderParams, ref leftResult, ref rightResult);

                            expressionBuilderParams.NonInlinePartWriter.WriteLine(
                                $"[ {leftResult.Expression} {logicalEvaluationStatement.Operator} {rightResult.Expression} ]");

                            string varName;

                            if (logicalEvaluationStatement.ParentStatement is AssignmentStatement ||
                                logicalEvaluationStatement.ParentStatement is VariableDefinitionStatement)
                            {
                                varName = UnixBashPlatform.LastStatusCodeStoreVariableName;
                            }
                            else
                            {
                                varName = BashVariableDefinitionStatementTranspiler
                                          .WriteLastStatusCodeStoreVariableDefinition(expressionBuilderParams.Context,
                                                                                      expressionBuilderParams.Scope,
                                                                                      expressionBuilderParams.NonInlinePartWriter, "cmp_strings");
                            }

                            var template = new VariableAccessStatement(
                                varName,
                                logicalEvaluationStatement.Info
                                );

                            p.NonInlinePartWriter.Write(writer);

                            return(new ExpressionResult(
                                       TypeDescriptor.Boolean,
                                       $"${varName}",
                                       template,
                                       PinRequiredNotice
                                       ));
                        }
                    }

                    break;
                }
                }

                break;
            }

            case ArithmeticEvaluationStatement arithmeticEvaluationStatement:
            {
                switch (arithmeticEvaluationStatement.Operator)
                {
                case AdditionOperator _:
                {
                    using (var writer = new StringWriter())
                    {
                        var expressionBuilderParams = new ExpressionBuilderParams(p, writer);

                        var leftResult = CreateExpressionRecursive(expressionBuilderParams,
                                                                   arithmeticEvaluationStatement.Left);
                        var rightResult = CreateExpressionRecursive(expressionBuilderParams,
                                                                    arithmeticEvaluationStatement.Right);

                        if (leftResult.TypeDescriptor.IsString() || rightResult.TypeDescriptor.IsString())
                        {
                            HandleNotices(expressionBuilderParams, ref leftResult, ref rightResult);

                            var leftExp  = leftResult.Expression;
                            var rightExp = rightResult.Expression;

                            if (leftResult.Template is VariableAccessStatement)
                            {
                                leftExp = FormatStringConcatenationVariableAccess(leftExp);
                            }

                            if (rightResult.Template is VariableAccessStatement)
                            {
                                rightExp = FormatStringConcatenationVariableAccess(rightExp);
                            }


                            if (!leftResult.TypeDescriptor.IsString() &&
                                !(leftResult.Template is VariableAccessStatement))
                            {
                                leftExp = $"$(({leftExp}))";
                            }

                            if (!rightResult.TypeDescriptor.IsString() &&
                                !(rightResult.Template is VariableAccessStatement))
                            {
                                rightExp = $"$(({rightExp}))";
                            }

                            var concatExp = $"{leftExp}{rightExp}";

                            var newTemp = new ArithmeticEvaluationStatement(
                                leftResult.Template,
                                arithmeticEvaluationStatement.Operator,
                                rightResult.Template,
                                arithmeticEvaluationStatement.Info
                                );

                            leftResult.Template.ParentStatement  = newTemp;
                            rightResult.Template.ParentStatement = newTemp;

                            p.NonInlinePartWriter.Write(writer);

                            return(new ExpressionResult(
                                       TypeDescriptor.String,
                                       concatExp,
                                       newTemp
                                       ));
                        }
                    }

                    break;
                }
                }

                break;
            }

            case ArrayStatement arrayStatement:
            {
                string sourceName;

                if (arrayStatement.ParentStatement is VariableDefinitionStatement variableDefinitionStatement)
                {
                    sourceName = variableDefinitionStatement.Name;
                }
                else if (arrayStatement.ParentStatement is AssignmentStatement assignmentStatement)
                {
                    if (!(assignmentStatement.LeftSide is VariableAccessStatement variableAccessStatement))
                    {
                        throw new InvalidStatementStructureCompilerException(assignmentStatement,
                                                                             assignmentStatement.Info);
                    }

                    sourceName = variableAccessStatement.VariableName;
                }
                else if (arrayStatement.ParentStatement is ReturnStatement returnStatement)
                {
                    sourceName = p.Context.GetLastFunctionCallStorageVariable(arrayStatement.Type, p.MetaWriter);
                }
                else
                {
                    sourceName = p.Scope.NewHelperVariable(arrayStatement.Type, "array_helper");
                }

                if (arrayStatement.Elements != null)
                {
                    for (var i = 0; i < arrayStatement.Elements.Length; i++)
                    {
                        p.NonInlinePartWriter.Write($"{sourceName}[{i}]=");

                        var element = CreateExpression(p, arrayStatement.Elements[i]);

                        p.NonInlinePartWriter.WriteLine(element);
                    }

                    return(ExpressionResult.EmptyResult);
                }

                if (arrayStatement.Length != null)
                {
                    var sourceNameEval = new VariableAccessStatement(sourceName, arrayStatement.Info);

                    return(CallApiFunction <ApiArray.Initialize>(p, new[] { sourceNameEval, arrayStatement.Length },
                                                                 arrayStatement.ParentStatement, arrayStatement.Info));
                }

                throw new InvalidStatementStructureCompilerException(arrayStatement, arrayStatement.Info);
            }
            }

            return(base.CreateExpressionRecursive(p, statement));
        }