Ejemplo n.º 1
0
        private List<ZInstruction> ConvertMacros(PassageContent content, ref int currentLink, List<Tuple<string, Assign>> links, string currentPassage)
        {
            List<ZInstruction> instructions = new List<ZInstruction>();

            PassageMacroSet setMacro = content as PassageMacroSet;
            PassageMacroBranch branchMacro = content as PassageMacroBranch;
            PassageMacroPrint printMacro = content as PassageMacroPrint;
            PassageMacroDisplay displayMacro = content as PassageMacroDisplay;

            if (setMacro != null)
            {
                instructions.AddRange(ConvertAssignExpression((Assign)setMacro.Expression, currentPassage));
            }

            else if (printMacro != null)
            {
                if (printMacro.Expression is StringValue)
                    instructions.AddRange(StringToInstructions(((StringValue)(printMacro.Expression)).Value.Replace('\"', ' ').Trim()));
                else
                {
                    instructions.AddRange(ConvertValueExpression(printMacro.Expression, currentPassage));
                    instructions.Add(new PrintNum(new ZStackVariable()));
                }
            }

            else if (displayMacro != null)
            {
                instructions.AddRange(ConvertPassageContent(_passages.Single(passage =>
                    passage.Name == ((StringValue)(displayMacro.Expression)).Value.Replace('\"', ' ').Trim()).PassageContentList, ref currentLink, links, currentPassage));
            }

            else if (branchMacro != null)
            {
                Guid endIfGuid = Guid.NewGuid();
                string endIf = "endIf_" + endIfGuid;

                //if
                PassageMacroIf ifMacro = (PassageMacroIf)branchMacro.BranchNodeList.First();
                Guid ifGuid = Guid.NewGuid();
                string ifTrue = "if_True_" + ifGuid;
                string ifFalse = "if_False_" + ifGuid;

                instructions.AddRange(ConvertBranchExpression(ifMacro.Expression, ifTrue, ifFalse, currentPassage));

                // if expression is true
                instructions.Add(new Nop() { Label = new ZLabel(ifTrue) });
                foreach (var instruction in ConvertPassageContent(ifMacro.PassageContentList, ref currentLink, links, currentPassage))
                {
                    instructions.Add(instruction);
                }
                instructions.Add(new Jump(new ZJumpLabel(endIf)));

                // if expression is false
                instructions.Add(new Nop() { Label = new ZLabel(ifFalse) });

                //else if
                foreach (PassageMacroElseIf elseIfMacro in branchMacro.BranchNodeList.OfType<PassageMacroElseIf>())
                {
                    Guid elseIfGuid = Guid.NewGuid();
                    string elseIfTrue = "elseIf_True_" + elseIfGuid;
                    string elseIfFalse = "elseIf_False_" + elseIfGuid;

                    instructions.AddRange(ConvertBranchExpression(elseIfMacro.Expression, elseIfTrue, elseIfFalse, currentPassage));

                    // if expression is true
                    instructions.Add(new Nop() { Label = new ZLabel(elseIfTrue) });
                    foreach (var instruction in ConvertPassageContent(elseIfMacro.PassageContentList, ref currentLink, links, currentPassage))
                    {
                        instructions.Add(instruction);
                    }
                    instructions.Add(new Jump(new ZJumpLabel(endIf)));

                    // if expression is false
                    instructions.Add(new Nop() { Label = new ZLabel(elseIfFalse) });
                }

                //else
                PassageMacroElse elseMacro = branchMacro.BranchNodeList.Last() as PassageMacroElse;

                if (elseMacro != null)
                {
                    foreach (var instruction in ConvertPassageContent(elseMacro.PassageContentList, ref currentLink, links, currentPassage))
                    {
                        instructions.Add(instruction);
                    }
                }

                //endif
                instructions.Add(new Nop() { Label = new ZLabel(endIf) });
            }

            else
            {
                throw new NotSupportedException("This macro is not supported yet: " + content.GetType().Name);
            }

            return instructions;
        }