示例#1
0
 public override string Generate(CompileContext outer)
 {
     using (TextWriter writer = new StringWriter())
     {
         bool first = true;
         foreach (var block in blocks)
         {
             EvaluationStack unhandled = null;
             if (block.condition != null)
             {
                 string nothing = outer.GenerateWithoutWriting(block.condition.first, block.condition.last, ref unhandled);
                 Debug.Assert(string.IsNullOrEmpty(nothing));
                 if (unhandled == null)
                 {
                     unhandled = outer.GetEvaluationStack();
                 }
                 writer.WriteLine(CompileContext.Format(first ? "if({0} == 0)" : "else if({0} == 0)", unhandled.Pop()));
             }
             else
             {
                 writer.WriteLine("else");
             }
             writer.WriteLine(CompileContext.Format("{{{0}}}", outer.GenerateWithoutWriting(block.body.first, block.body.last, ref unhandled)));
             first = false;
         }
         return(writer.ToString());
     }
 }
示例#2
0
 internal string GenerateWithoutWriting(Instruction first, Instruction last, ref EvaluationStack unhandled)
 {
     using (TextWriter writer = new StringWriter())
     {
         return(GenerateInternal(first, last, writer, ref unhandled));
     }
 }
示例#3
0
        private string GenerateInternal(Instruction first, Instruction last, TextWriter specific, ref EvaluationStack unhandled)
        {
            int evaluationStackPreviousCount = evaluationStack.Count;
            var writer = this.writer;

            this.writer = specific;
            for (var instruction = first; instruction != last.Next;)
            {
                try
                {
                    instruction = instruction.Step(last, this);
                }
                catch (System.Reflection.TargetInvocationException e)
                {
                    Debug.LogFormat("Inner exception \"{0}\" occured in {1}, when generating the following codes:", e.InnerException, instruction);
                    var f = first;
                    while (f.Previous != null)
                    {
                        f = f.Previous;
                    }
                    var l = last;
                    while (l.Next != null)
                    {
                        l = l.Next;
                    }
                    for (var i = f; i != l.Next; i = i.Next)
                    {
                        Console.WriteLine(i);
                    }
                    throw new NotImplementedException();
                }
            }
            this.writer = writer;
            int evaluationStackCount = evaluationStack.Count;

            if (evaluationStackCount > evaluationStackPreviousCount)
            {
                int count = evaluationStackCount - evaluationStackPreviousCount;
                unhandled = new EvaluationStack();
                Stack <Value> tmp = new Stack <Value>();
                while ((count--) != 0)
                {
                    tmp.Push(evaluationStack.Pop());
                }
                while (tmp.Count != 0)
                {
                    unhandled.Push(tmp.Pop());
                }
            }
            else
            {
                unhandled = null;
            }
            return(specific.ToString());
        }
示例#4
0
        public string Generate()
        {
            EvaluationStack unhandled = null;

#if false  // DEBUG
            for (var instruction = first; instruction != last.Next; instruction = instruction.Next)
            {
                Console.WriteLine(instruction);
            }
#endif
            unnecessaryLocalVariableIDs = ScanUnnecessaryLocalVariableIDs();
            return(GenerateInternal(this.first, this.last, this.writer, ref unhandled));
        }
示例#5
0
        public override string Generate(CompileContext outer)
        {
            // Condition
            EvaluationStack unhandled = null;
            string          nothing   = outer.GenerateWithoutWriting(this.condition.first, this.condition.last, ref unhandled);

            Debug.Assert(string.IsNullOrEmpty(nothing));
            if (unhandled == null)
            {
                unhandled = outer.GetEvaluationStack();
            }
            var    condition = unhandled.Pop();
            string body      = outer.GenerateWithoutWriting(this.body.first, this.body.last, ref unhandled);

            Debug.Assert(unhandled == null);
            return(CompileContext.Format("while({0} != 0) {{\n{1}\n}}\n", condition, body));
        }