private static CodeMemberMethod CreateFallback()
        {
            var method = new CodeMemberMethod
            {
                Name       = "Fallback",
                Attributes = MemberAttributes.Public | MemberAttributes.Static,
                ReturnType = CodeConstants.AsyncTask
            };

            method.AddRequestParam();

            method.Statements.Add(new CodeVariableDeclarationStatement(CodeConstants.Var, "recap",
                                                                       CodeGeneration_Instructions.GetVariable("scene_recap", typeof(string), false)));

            method.Statements.Add(new CodeConditionStatement(
                                      new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(string)), "IsNullOrWhiteSpace", new CodeVariableReferenceExpression("recap")),
                                      new CodeVariableDeclarationStatement(CodeConstants.Var, "lastSpeech", CodeGeneration_Instructions.GetVariable("scene_lastSpeech", typeof(string), false)),
                                      new CodeExpressionStatement(new CodeMethodInvokeExpression(
                                                                      new CodeTypeReferenceExpression("Output"), "AddSpeech", new CodeVariableReferenceExpression(CodeConstants.RequestVariableName), new CodeVariableReferenceExpression("lastSpeech")))));

            method.Statements.Add(new CodeMethodInvokeExpression(
                                      new CodeTypeReferenceExpression("Output"),
                                      "AddSpeech",
                                      new CodeVariableReferenceExpression(CodeConstants.RequestVariableName), new CodeVariableReferenceExpression("recap")));

            return(method);
        }
Пример #2
0
        private static CodeExpression[] VariableSplitArray(string content)
        {
            var currentPosition = 0;
            var matches         = GetVariables(content);

            if (!matches.Any())
            {
                return(new CodeExpression[] { new CodePrimitiveExpression(content) });
            }

            var list = new List <CodeExpression>();

            foreach (var match in matches)
            {
                if (match.Index != currentPosition)
                {
                    list.Add(new CodePrimitiveExpression(content.Substring(currentPosition, match.Index - currentPosition)));
                }

                var variable = match.Groups["name"].Value;
                list.Add(CodeGeneration_Instructions.GetVariable(variable, typeof(string)));
                currentPosition = match.Index + match.Length;
            }

            if (currentPosition != content.Length)
            {
                list.Add(new CodePrimitiveExpression(content.Substring(currentPosition, content.Length - currentPosition)));
            }
            return(list.ToArray());
        }
        public static CodeExpression Generate(Value condition, Value comparisonType = null)
        {
            switch (condition)
            {
            case Variable variable:
                var typeArg = ComparisonType(comparisonType);
                return(CodeGeneration_Instructions.GetVariable(variable.Name, typeArg));

            case LiteralValue literal:
                return(new CodePrimitiveExpression(literal.Value));

            case ValueWrapper wrapper:
                return(Generate(wrapper.Value));

            case Equal equal:
                return(Binary(equal, CodeBinaryOperatorType.ValueEquality));

            case GreaterThan gt:
                return(Binary(gt, CodeBinaryOperatorType.GreaterThan));

            case LessThan lt:
                return(Binary(lt, CodeBinaryOperatorType.LessThan));

            case GreaterThanEqual gte:
                return(Binary(gte, CodeBinaryOperatorType.GreaterThanOrEqual));

            case LessThanEqual lte:
                return(Binary(lte, CodeBinaryOperatorType.LessThanOrEqual));

            case Not not:
                return(new CodeBinaryOperatorExpression(
                           Generate(not.Condition),
                           CodeBinaryOperatorType.ValueEquality,
                           new CodePrimitiveExpression(false)));

            case And and:
                return(Binary(and, CodeBinaryOperatorType.BooleanAnd));

            case Or or:
                return(Binary(or, CodeBinaryOperatorType.BooleanOr));
            }
            return(new CodePrimitiveExpression("Unable to convert " + condition.GetType().Name));
        }
        protected override Task Render(SceneInstruction instruction, CodeGeneratorContext context)
        {
            CodeStatementCollection statements;

            switch (context.CodeScope.Peek())
            {
            case CodeMemberMethod member:
                statements = member.Statements;
                break;

            case CodeTypeDeclaration codeType:
                statements = codeType.GetMainMethod().Statements;
                break;

            case CodeConditionStatement stmt:
                statements = stmt.TrueStatements;
                break;

            default:
                return(Noop(context));
            }

            CodeGeneration_Instructions.EnsureStateMaintenance(context);
            switch (instruction)
            {
            case Clear clear:
                statements.Clear(clear.Variable);
                break;

            case ClearAll clearAll:
                statements.ClearAll();
                break;

            case Decrease decrease:
                statements.Decrease(decrease.Variable, decrease.Amount);
                break;

            case Flag flag:
                statements.SetVariable(flag.Variable, true);
                break;

            case GoTo gto:
                statements.Add(CodeGeneration_Navigation.GoToScene(gto.SceneName));
                statements.Add(new CodeMethodReturnStatement());
                break;

            case GoToAndReturn goToAndReturn:
                statements.Add(CodeGeneration_Navigation.GoToScene(goToAndReturn.SceneName));
                break;

            case Increase increase:
                statements.Increase(increase.Variable, increase.Amount);
                break;

            case Set set:
                statements.SetVariable(set.Variable, set.Value);
                break;

            case SlotAssignment slotAssignment:
                context.SetSlotType(slotAssignment.SlotName, slotAssignment.SlotType);
                break;

            case Unflag unflag:
                statements.SetVariable(unflag.Variable, false);
                break;

            case End end:
                statements.Reset();
                statements.Add(new CodeMethodReturnStatement());
                break;

            case Repeat repeat:
                statements.Add(new CodeVariableDeclarationStatement(CodeConstants.Var, "lastSpeech",
                                                                    CodeGeneration_Instructions.GetVariable("scene_lastSpeech", typeof(string), false)));
                statements.Add(new CodeMethodInvokeExpression(
                                   new CodeTypeReferenceExpression("Output"),
                                   "AddSpeech", CodeConstants.RequestVariableRef,
                                   new CodeVariableReferenceExpression("lastSpeech"), new CodePrimitiveExpression(true)));
                statements.Add(new CodeMethodReturnStatement());
                break;

            case Restart restart:
                statements.ClearAll("scene_");
                statements.ClearAll("_scene");
                CodeGeneration_Navigation.GoToScene("start");
                statements.Add(new CodeMethodReturnStatement());
                break;

            case Resume resume:
                statements.Add(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("await Navigation"),
                                                              "Resume", CodeConstants.RequestVariableRef, new CodePrimitiveExpression(true)));
                statements.Add(new CodeMethodReturnStatement());
                break;

            case Return returnCmd:
                statements.Add(new CodeMethodReturnStatement());
                break;

            case Reprompt reprompt:
                statements.Add(new CodeVariableDeclarationStatement(CodeConstants.Var, "reprompt",
                                                                    CodeGeneration_Instructions.GetVariable("scene_reprompt", typeof(string), false)));
                statements.Add(new CodeMethodInvokeExpression(
                                   new CodeTypeReferenceExpression("Output"),
                                   "AddSpeech", CodeConstants.RequestVariableRef,
                                   new CodeVariableReferenceExpression("reprompt"), new CodePrimitiveExpression(true)));
                statements.Add(new CodeMethodReturnStatement());
                break;

            case Back back:
                statements.Add(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("await Navigation"),
                                                              "Back", CodeConstants.RequestVariableRef));
                statements.Add(new CodeMethodReturnStatement());
                break;

            case Pause pause:
                statements.Add(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("await Navigation"),
                                                              "Pause", CodeConstants.RequestVariableRef));
                statements.Add(new CodeMethodReturnStatement());
                break;
            }
            return(base.Render(instruction, context));
        }