예제 #1
0
 /// <summary>
 /// Prog -> MoreClasses MainClass
 /// </summary>
 public void Prog()
 {
     MoreClasses();
     MainClass();
     symbolTable.DeleteDepth(Depth);
     TACFile.CreateTACFile();
 }
예제 #2
0
        private void GenerateOperationLineASM(char opChar, string axReg, string tacWord)
        {
            TACFile.GetNextWord(); // skip operator

            if (opChar == '+')
            {
                AssemblyFile.AddLine($"     mov ax, {axReg}");
                AssemblyFile.AddLine($"     add ax, {GetAndFormatWord()}");
                AssemblyFile.AddLine($"     mov {tacWord}, ax");
            }
            else if (opChar == '-')
            {
                AssemblyFile.AddLine($"     mov ax, {axReg}");
                AssemblyFile.AddLine($"     sub ax, {GetAndFormatWord()}");
                AssemblyFile.AddLine($"     mov {tacWord}, ax");
            }
            else if (opChar == '/')
            {
                AssemblyFile.AddLine($"     mov ax, {axReg}");
                AssemblyFile.AddLine($"     cwd");
                AssemblyFile.AddLine($"     mov bx, {GetAndFormatWord()}");
                AssemblyFile.AddLine($"     idiv bx");
                AssemblyFile.AddLine($"     mov {tacWord}, ax");
            }
            else if (opChar == '*')
            {
                AssemblyFile.AddLine($"     mov ax, {axReg}");
                AssemblyFile.AddLine($"     mov bx, {GetAndFormatWord()}");
                AssemblyFile.AddLine($"     imul bx");
                AssemblyFile.AddLine($"     mov {tacWord}, ax");
            }
        }
예제 #3
0
        private string GetAndFormatWord()
        {
            string word = TACFile.GetNextWord();

            if (word[0] == '_')
            {
                word  = word.Replace('_', '[');
                word += ']';
            }

            return(word);
        }
예제 #4
0
        private void GenerateProcBodyASM()
        {
            string tacWord = GetAndFormatWord();

            while (tacWord != "endp")
            {
                if (TACKeywords.Contains(tacWord))
                {
                    GenerateProcLineUsingKeywordASM(tacWord);
                }
                else
                {
                    GenerateProcLineUsingVariableASM(tacWord);
                }

                tacWord = GetAndFormatWord();
            }

            TACFile.GetNextWord();
        }
예제 #5
0
        static void Main(string[] args)
        {
            try
            {
                JavaFile.ReadLines(args[0]);

                Parser parser = new Parser();
                Console.WriteLine("TAC File:");
                Console.WriteLine("---------");
                parser.Prog();

                if (Token == Tokens.EofT)
                {
                    Console.WriteLine("");
                    Console.WriteLine("successful compilation!");
                    Console.WriteLine("");
                    Console.WriteLine("");
                }
                else
                {
                    Console.WriteLine($"error - line {JavaFile.lineNum} - unused tokens, please check for correct Java syntax");
                }

                JavaFile.CloseReader();
            }
            catch (Exception ex)
            {
                Console.WriteLine("error - the compiler encountered an unknown error, please try compiling again");
                Console.WriteLine("note - be sure you have the file being input into the program as an application argument");
                Environment.Exit(100);
            }

            Console.WriteLine("Assembly File:");
            Console.WriteLine("--------------");
            AssemblyGenerator assemblyGenerator = new AssemblyGenerator();

            TACFile.ReadLinesFromFile();
            assemblyGenerator.GenerateASMFile();
        }
예제 #6
0
        private void GenerateProcLineUsingVariableASM(string tacWord)
        {
            if (tacWord == "_ax")
            {
                TACFile.GetNextWord();
                AssemblyFile.AddLine($"     mov ax, {GetAndFormatWord()}");
            }
            else
            {
                TACFile.GetNextWord(); // skip equal sign
                string axReg  = GetAndFormatWord();
                char   opChar = TACFile.PeekNextChar();

                if (opChar == '*' || opChar == '/' || opChar == '-' || opChar == '+')
                {
                    GenerateOperationLineASM(opChar, axReg, tacWord);
                }
                else
                {
                    GenerateAssignmentLineASM(axReg, tacWord);
                }
            }
        }
예제 #7
0
 private void GenerateProcLineUsingKeywordASM(string tacWord)
 {
     if (tacWord == "proc")
     {
         TACFile.GetNextWord();
     }
     else if (tacWord == "wrs")
     {
         AssemblyFile.AddLine($"     mov dx, offset {TACFile.GetNextWord()}");
         AssemblyFile.AddLine("     call writestr");
     }
     else if (tacWord == "wri")
     {
         string bpWord = GetAndFormatWord();
         AssemblyFile.AddLine($"     mov dx, {bpWord}");
         AssemblyFile.AddLine($"     call writeint");
     }
     else if (tacWord == "wrln")
     {
         AssemblyFile.AddLine($"     call writeln");
     }
     else if (tacWord == "rdi")
     {
         string bpWord = GetAndFormatWord();
         AssemblyFile.AddLine($"     call readint");
         AssemblyFile.AddLine($"     mov {bpWord}, bx");
     }
     else if (tacWord == "call")
     {
         AssemblyFile.AddLine($"     call {TACFile.GetNextWord()}");
     }
     else if (tacWord == "push")
     {
         AssemblyFile.AddLine($"     mov ax, {GetAndFormatWord()}");
         AssemblyFile.AddLine($"     push ax");
     }
 }
예제 #8
0
        private void GenerateAssignmentLineASM(string axReg, string tacWord)
        {
            if (axReg.Contains("ax"))
            {
                AssemblyFile.AddLine($"     mov {tacWord}, ax");
            }
            else if (tacWord.Contains("ax"))
            {
                AssemblyFile.AddLine($"     mov ax, {axReg}");
            }
            else
            {
                AssemblyFile.AddLine($"     mov ax, {axReg}");
                AssemblyFile.AddLine($"     mov {tacWord}, ax");

                if (!axReg.Contains("bp") && !int.TryParse(axReg, out int result))
                {
                    tacWord = GetAndFormatWord();
                    TACFile.GetNextWord(); // skip equal sign
                    AssemblyFile.AddLine($"     mov ax, {GetAndFormatWord()}");
                    AssemblyFile.AddLine($"     mov {tacWord}, ax");
                }
            }
        }
예제 #9
0
 /// <summary>
 /// Adds the generated code to the list of lines. Line is passed by value.
 /// </summary>
 public void GenerateLineOfTAC(string line)
 {
     TACFile.AddLine(line);
 }
예제 #10
0
 /// <summary>
 /// Adds the generated code to the list of lines. Line is passed by reference and is
 /// reset after being added.
 /// </summary>
 public void GenerateLineOfTAC(ref string line)
 {
     TACFile.AddLine(line);
     line = "";
 }