Ejemplo n.º 1
0
        /// <summary>
        /// Exports the program to the given text writer
        /// </summary>
        /// <param name="program">program to export</param>
        /// <param name="writer">text writer to export to</param>
        public void Export(Program program, TextWriter writer)
        {
            // Get labels dictionary
            Dictionary<short, string> labels = GenerateLabels(program);

            // Process each instruction
            bool hasGap = false;
            WritingVisitor visitor = new WritingVisitor(labels, writer);

            for (short i = 0; i < program.Instructions.Count; i++)
            {
                var instruction = program.Instructions[i];

                // Valid instruction?
                if (instruction != null)
                {
                    bool spacerUsed = false;

                    // Add address directive if there was a gap
                    if (hasGap)
                    {
                        writer.WriteLine();
                        if (i > 255)
                            writer.WriteLine("ADDRESS {0:X4}", i);
                        else
                            writer.WriteLine("ADDRESS {0:X2}", i);

                        hasGap = false;
                        spacerUsed = true;
                    }

                    // Label here?
                    string label;

                    if (labels.TryGetValue(i, out label))
                    {
                        if (!spacerUsed)
                            writer.WriteLine();

                        writer.WriteLine(label + ":");
                    }

                    // Write instruction
                    instruction.Accept(visitor);
                }
                else
                {
                    // Ignore and mark gap
                    hasGap = true;
                }
            }
        }
        public static string DynamicAsString(
            HRONObject hronObject
            )
        {
            if (hronObject == null)
            {
                return "";
            }

            var v = new WritingVisitor();
            VisitDynamic(hronObject, v);
            return v.StringBuilder.ToString();
        }