/// <summary> /// Calls GetNextChar() to skip over comment characters. /// </summary> public void SkipComment(Regex commentEndRegex) { while (!commentEndRegex.IsMatch(CurrentChar.ToString() + JavaFile.PeekNextChar()) && !JavaFile.EndOfFile()) { JavaFile.GetNextChar(); } }
/// <summary> /// Loads the Lexeme until the regular expression no longer matches. /// </summary> public void LoadLexeme(Regex lexemeRegex) { while (lexemeRegex.IsMatch(JavaFile.PeekNextChar().ToString())) { JavaFile.GetNextChar(); Lexeme += CurrentChar; } }
/// <summary> /// Determines type of comment, the comment is then skipped. /// </summary> public void ProcessComment() { if (JavaFile.PeekNextChar() == '/') { SkipComment(oneLineCommentEndRegex); } else if (JavaFile.PeekNextChar() == '*') { SkipComment(multiLineCommentEndRegex); JavaFile.GetNextChar(); JavaFile.GetNextChar(); } }
/// <summary> /// Processes tokens until end of stream has been hit. /// </summary> public void GetNextToken() { JavaFile.SkipWhitespace(); if (!JavaFile.EndOfFile()) { ProcessToken(); } else { Token = Tokens.EofT; Lexeme = "End of file"; } }
/// <summary> /// Reads a literal string until the ending quote is found. /// Adds 3 tokens: 2 quote tokens, 1 literal token. /// </summary> /// <example> /// "literal" => QuoteT -> LiteralT -> QuoteT /// </example> public void ProcessLiteral() { Token = Tokens.LiteralT; Lexeme = CurrentChar.ToString(); while (JavaFile.PeekNextChar() != '\"' && JavaFile.PeekNextChar() != '\n' && !JavaFile.EndOfFile()) { JavaFile.GetNextChar(); Lexeme += CurrentChar; } if (JavaFile.PeekNextChar() == '\"') { JavaFile.GetNextChar(); Lexeme += CurrentChar.ToString(); } }
/// <summary> /// Reads next character into Lexeme, then validates correct /// double token. /// </summary> public void ProcessDoubleToken() { JavaFile.GetNextChar(); Lexeme += CurrentChar; if (CurrentChar == '=') { Token = Tokens.RelOpT; } else if (CurrentChar == '&') { Token = Tokens.MulOpT; } else if (CurrentChar == '|') { Token = Tokens.AddOpT; } else { Token = Tokens.UnknownT; } }
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(); }
/// <summary> /// Reads the first token char into Lexeme, which is then /// used to determine which function to use for processing. /// </summary> public void ProcessToken() { JavaFile.GetNextChar(); Lexeme = CurrentChar.ToString(); if (commentStartRegex.IsMatch(Lexeme + JavaFile.PeekNextChar())) { // skip the comment then continue processing the token ProcessComment(); GetNextToken(); } else if (char.IsLetter(Lexeme[0])) { ProcessWordToken(); } else if (char.IsDigit(Lexeme[0])) { ProcessNumToken(); } else if (comparisonRegex.IsMatch(Lexeme) && lookAheadCharRegex.IsMatch(JavaFile.PeekNextChar().ToString())) { ProcessDoubleToken(); } else if (specialCharRegex.IsMatch(Lexeme)) { ProcessSingleToken(); } else if (Lexeme == "\"") { ProcessLiteral(); } else { Token = Tokens.UnknownT; } }