public bool Parse(List<Token> tokens) { //check for the correct start to our mini pascal program Tokens = tokens; //check for 'program <name> header' //should move this into statement probably if (Tokens[currentToken].Type != TokenType.ProgramSym) { WriteError("Missing 'Program' symbol", Tokens[currentToken]); AdvanceToken(); } //next token should be an identifier... AdvanceToken(); if (Tokens[currentToken].Type != TokenType.Identifier) { WriteError("Missing program identifier", Tokens[currentToken]); } assembler = new Assembler(Tokens[currentToken].Value); //eat the input until semicolon while (Tokens[currentToken].Type != TokenType.Semicolon) { AdvanceToken(); if (currentToken >= Tokens.Count-1) { WriteError("Expected Semicolon end of first line", Tokens[Tokens.Count-1]); return false; } } AdvanceToken(); //parse variables if (Tokens[currentToken].Type == TokenType.VarSym) { if (!ParseVariables()) { WriteError("Error parsing variables", Tokens[Tokens.Count - 1]); return false; } } if (Tokens[currentToken].Type != TokenType.BeginSym) { WriteError("Missing 'Begin' symbol", Tokens[currentToken]); } AdvanceToken(); Statement(); while (Tokens[currentToken].Type == TokenType.Semicolon ) { AdvanceToken(); Statement(); } if (Tokens[currentToken].Type != TokenType.EndSym) { WriteError("Expected 'End' symbol", Tokens[currentToken]); } if (parseSuccessful) { assembler.FinalizeCode(); } else { assembler.RemoveFile(); } return parseSuccessful; }
public bool Parse(List <Token> tokens) { //check for the correct start to our mini pascal program Tokens = tokens; //check for 'program <name> header' //should move this into statement probably if (Tokens[currentToken].Type != TokenType.ProgramSym) { WriteError("Missing 'Program' symbol", Tokens[currentToken]); AdvanceToken(); } //next token should be an identifier... AdvanceToken(); if (Tokens[currentToken].Type != TokenType.Identifier) { WriteError("Missing program identifier", Tokens[currentToken]); } assembler = new Assembler(Tokens[currentToken].Value); //eat the input until semicolon while (Tokens[currentToken].Type != TokenType.Semicolon) { AdvanceToken(); if (currentToken >= Tokens.Count - 1) { WriteError("Expected Semicolon end of first line", Tokens[Tokens.Count - 1]); return(false); } } AdvanceToken(); //parse variables if (Tokens[currentToken].Type == TokenType.VarSym) { if (!ParseVariables()) { WriteError("Error parsing variables", Tokens[Tokens.Count - 1]); return(false); } } if (Tokens[currentToken].Type != TokenType.BeginSym) { WriteError("Missing 'Begin' symbol", Tokens[currentToken]); } AdvanceToken(); Statement(); while (Tokens[currentToken].Type == TokenType.Semicolon) { AdvanceToken(); Statement(); } if (Tokens[currentToken].Type != TokenType.EndSym) { WriteError("Expected 'End' symbol", Tokens[currentToken]); } if (parseSuccessful) { assembler.FinalizeCode(); } else { assembler.RemoveFile(); } return(parseSuccessful); }