public MIPS Visitor(IFunctionCil function, ProgramContext program)
        {
            var mipsResult = new MIPS()
            {
                Functions = Utils.AcomodarLocales(function)
            };
            var functionsVisited = function.ThreeDirInses.Select(x => {
                return(Selector(x, function, Cil));
            }).ToList();

            for (int i = 0; i < functionsVisited.Count; i++)
            {
                var current = functionsVisited[i];
                if (current == null)
                {
                    mipsResult.Functions.Add("##-----\t" + function.ThreeDirInses.ToList()[i].ToString().TrimEnd() + "\t-----");
                }
                else
                {
                    mipsResult.Data.AddRange(current.Data);
                    if (current.Functions.Count > 0)
                    {
                        current.Functions[0] = current.Functions[0] + "\t\t##" + function.ThreeDirInses.ToList()[i].ToString().Trim();
                    }
                    mipsResult.Functions.AddRange(current.Functions);
                    mipsResult.Text.AddRange(current.Text);
                }
            }
            return(mipsResult);
        }
        public MIPS Visit(ProgramContext program)
        {
            var emptyString = "\"\"";
            var functions   = Cil.CilAst.FunctionCils;
            var types       = Cil.CilAst.TypeCils;
            var data        = Cil.CilAst.dataStringCils;
            //var functionsMIPS = functions.Select(x => Visitor(x));
            var mipsResult = new MIPS()
            {
                Data = new List <string>()
                {
                    "heap: .space 2000000"
                }
            };

            mipsResult.Data.Add("heapPointer: .word 0");
            mipsResult.Data.Add("newLine: .asciiz \"\\n\"");
            mipsResult.Data.AddRange(data.Select(x => $"{x.varCil}: .asciiz {(x.stringCil.Name.Length!=0 ? x.stringCil.Name : emptyString)}"));
            foreach (var t in types)
            {
                mipsResult.Data.Add($"type_{t.Name}:");
                mipsResult.Data.Add($"\ttype_{t.Name}_Length: .byte {t.Attributes.Count *4}");
                foreach (var f in t.Functions)
                {
                    mipsResult.Data.Add($"\ttype_{t.Name}_{f.CilName}: .word {f.Function.Name}");
                }
                mipsResult.Data.Add($"\ttype_{t.Name}_count_methods: .word {t.Functions.Count}");
                string parent;
                if (t.inherit != null)
                {
                    parent = $"type_{ t.inherit.Name}_parent";
                }
                else
                {
                    parent = "0";
                }
                mipsResult.Data.Add($"\ttype_{t.Name}_parent: .word {parent}");
                mipsResult.Data.Add($"\ttype_{t.Name}_name: .asciiz \"{t.Name}\"");
            }
            var entry = functions.Single(x => x.Name == "entry");

            functions.Remove(entry);
            mipsResult.Add(Visitor(entry, program));
            for (int i = 0; i < mipsResult.Functions.Count; i++)
            {
                if (mipsResult.Functions[i].StartsWith("jr"))
                {
                    mipsResult.Functions.RemoveAt(i);
                    mipsResult.Functions.InsertRange(i, new List <string>()
                    {
                        "li $v0, 10", "syscall"
                    });
                }
            }
            var functionsMIPS = (from i in functions
                                 select Visitor(i, program)).ToList();

            for (int i = 0; i < functionsMIPS.Count; i++)
            {
                var current = functionsMIPS[i];
                mipsResult.Add(current);
            }
            mipsResult.Functions.AddRange(CreateEqualFunction());
            mipsResult.Functions.AddRange(CreateConformFunction());
            mipsResult.Functions.AddRange(CreateLengthFunction());
            mipsResult.Functions.AddRange(CreateConcatFunction());
            mipsResult.Functions.AddRange(CreateSubstringFunction());
            return(mipsResult);
            //var functionsMIPS = Visitor(functions.ToList()[17]);
        }
Example #3
0
        }                                           //

        internal void Add(MIPS mIPS)
        {
            Data.AddRange(mIPS.Data);
            Text.AddRange(mIPS.Text);
            Functions.AddRange(mIPS.Functions);
        }