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);
        }
Exemple #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));
        }
        private static CodeMemberMethod CreateSetTemplate()
        {
            var method = new CodeMemberMethod
            {
                Name       = "SetTemplate",
                Attributes = MemberAttributes.Public | MemberAttributes.Static
            };

            method.Parameters.Add(
                new CodeParameterDeclarationExpression(
                    new CodeTypeReference("AlexaRequestInformation<Alexa.NET.Request.APLSkillRequest>"),
                    CodeConstants.RequestVariableName));
            method.Parameters.Add(new CodeParameterDeclarationExpression(
                                      new CodeTypeReference(typeof(string)), "templateName"));

            method.Statements.Add(CodeGeneration_Instructions.SetVariable("scene_template", new CodeVariableReferenceExpression("templateName"), false));

            return(method);
        }
        private static CodeMemberMethod CreateSetDataProperty()
        {
            var method = new CodeMemberMethod
            {
                Name       = "SetDataProperty",
                Attributes = MemberAttributes.Public | MemberAttributes.Static
            };

            method.Parameters.Add(
                new CodeParameterDeclarationExpression(
                    new CodeTypeReference("AlexaRequestInformation<Alexa.NET.Request.APLSkillRequest>"),
                    CodeConstants.RequestVariableName));
            method.Parameters.Add(new CodeParameterDeclarationExpression(
                                      new CodeTypeReference(typeof(string)), "property"));
            method.Parameters.Add(new CodeParameterDeclarationExpression(
                                      new CodeTypeReference(typeof(string)), "value"));

            method.Statements.Add(CodeGeneration_Instructions.SetVariable(
                                      new CodeBinaryOperatorExpression(new CodePrimitiveExpression("scene_"), CodeBinaryOperatorType.Add, new CodeVariableReferenceExpression("property")), new CodeVariableReferenceExpression("value")));

            return(method);
        }
        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));
        }
        private static CodeMemberMethod CreateGenerateSpeech()
        {
            var method = new CodeMemberMethod
            {
                Name       = "GenerateSpeech",
                Attributes = MemberAttributes.Static,
                ReturnType = new CodeTypeReference("SkillResponse")
            };

            method.Parameters.Add(
                new CodeParameterDeclarationExpression(
                    new CodeTypeReference("AlexaRequestInformation<Alexa.NET.Request.APLSkillRequest>"),
                    CodeConstants.RequestVariableName));

            var items  = new CodePropertyReferenceExpression(new CodeVariableReferenceExpression(CodeConstants.RequestVariableName), "Items");
            var speech = new CodePrimitiveExpression("speech");

            method.Statements.Add(new CodeVariableDeclarationStatement(typeof(string), "fullSpeech", new CodePrimitiveExpression(null)));
            method.Statements.Add(new CodeConditionStatement(
                                      new CodeMethodInvokeExpression(new CodePropertyReferenceExpression(CodeConstants.RequestVariableRef, "Items"), "ContainsKey", speech),
                                      new CodeAssignStatement(
                                          new CodeVariableReferenceExpression("fullSpeech"),
                                          new CodeMethodInvokeExpression(
                                              new CodeTypeReferenceExpression(typeof(string)),
                                              "Concat",
                                              new CodeMethodInvokeExpression(
                                                  new CodeCastExpression("List<string>", new CodeIndexerExpression(items, speech)),
                                                  "ToArray")))));

            method.Statements.Add(CodeGeneration_Instructions.SetVariable(
                                      new CodePrimitiveExpression("scene_lastSpeech"), new CodeVariableReferenceExpression("fullSpeech")));


            method.Statements.Add(new CodeVariableDeclarationStatement(
                                      new CodeTypeReference("var"),
                                      "speech",
                                      new CodeMethodInvokeExpression(_refOutput, "CreateOutput", new CodeVariableReferenceExpression("fullSpeech"))
                                      ));

            method.Statements.Add(new CodeVariableDeclarationStatement(new CodeTypeReference("SkillResponse"), "response"));
            var checkForCandidates = new CodeConditionStatement(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("Navigation"),
                                                                                               "HasCandidates", new CodeVariableReferenceExpression(CodeConstants.RequestVariableName)));

            checkForCandidates.TrueStatements.Add(new CodeVariableDeclarationStatement(
                                                      new CodeTypeReference("var"),
                                                      "reprompt",
                                                      new CodeMethodInvokeExpression(_refOutput, "CreateOutput",
                                                                                     new CodeMethodInvokeExpression(
                                                                                         new CodeVariableReferenceExpression(CodeConstants.RequestVariableName), "GetValue<string>",
                                                                                         new CodePrimitiveExpression("scene_reprompt")))));
            checkForCandidates.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("response"),
                                                                          new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("ResponseBuilder"), "Ask", new CodeVariableReferenceExpression("speech"), new CodeSnippetExpression("new Reprompt{OutputSpeech=reprompt == null ? speech : reprompt}"))));
            checkForCandidates.FalseStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("response"),
                                                                           new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("ResponseBuilder"), "Tell", new CodeVariableReferenceExpression("speech"))));

            method.Statements.Add(checkForCandidates);
            //response.SessionAttributes = request.State.Session.Attributes;
            method.Statements.Add(new CodeAssignStatement(
                                      new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("response"),
                                                                          "SessionAttributes"),
                                      new CodePropertyReferenceExpression(new CodePropertyReferenceExpression(new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("request"), "State"), "Session"), "Attributes")));
            method.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("response")));

            return(method);
        }