/// <summary> /// Check if a function declaration is incoming /// </summary> /// <returns></returns> public Boolean CheckFunctionIncoming() { //Starting with a type can mean a function if (!Database.CheckType(CurrentToken.Value)) return false; //Record the index of the present token so we look forward and come back again Int32 currentIndex = TokenIndex; NextToken(); //Skip the *s //while (CurrentToken.Value.Equals("*")) //NextToken(); NextToken(); //A '(' after a type name mean's the function's parameter list is incoming, so this is a function //declaration Boolean retVal; retVal = CurrentToken.Value.Equals("("); //Reset the tokenIndex TokenIndex = currentIndex; CurrentToken = Tokens[currentIndex]; return retVal; }
/// <summary> /// Resets the tokens /// </summary> protected void ResetToken() { TokenIndex = 0; CurrentToken = Tokens[TokenIndex]; }
protected void PreviousToken() { TokenIndex--; CurrentToken = Tokens[TokenIndex]; }
/// <summary> /// Moves onto the next token /// </summary> protected void NextToken() { TokenIndex++; try { CurrentToken = Tokens[TokenIndex]; } catch { CurrentToken = new Token(); CurrentToken.LineStart = Int32.MaxValue; CurrentToken.Type = TokenType.Symbol; } }
/// <summary> /// Reads the next token in the program /// </summary> TokenType GetNextToken() { CurrentToken = new Token(); SkipWhitespace(); //Setup the base of the token CurrentToken.LineStart = LineStart; CurrentToken.CharacterPosition = CharacterPosition - LineStart + 1; //EOL token if (Program[CharacterPosition].Equals('\n')) { CurrentToken.Type = TokenType.EOL; return TokenType.EOL; } //Number token if ((char.IsDigit(Program[CharacterPosition])) || (Program[CharacterPosition].Equals('.') && char.IsDigit(Program[CharacterPosition + 1]))) return GetNumber(); //Identifier token if ((char.IsLetterOrDigit(Program[CharacterPosition])) || (Program[CharacterPosition].Equals('_') && char.IsLetterOrDigit(Program[CharacterPosition + 1]))) return GetIdentifier(); //Character token if (Program[CharacterPosition].Equals('\"')) return GetString(); //Symbol token if ("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~".Contains(Program[CharacterPosition])) return GetSymbol(); Error("Unrecognised Token", LineStart, CharacterPosition - LineStart + 1); return TokenType.Unknown; }
/// <summary> /// Does all the preparsing of the three sections /// </summary> public void Preparse() { ResetToken(); ParseTypesDeclarations(); ResetToken(); ParseGlobalVariableDeclarations(); //Wrap in void main Token v = new Token(TokenType.Identifier, "VOID"); Token m = new Token(TokenType.Identifier, "MAIN"); Token pl = new Token(TokenType.Symbol, "("); Token pr = new Token(TokenType.Symbol, ")"); Token bl = new Token(TokenType.Symbol, "{"); Token br = new Token(TokenType.Symbol, "}"); Tokens.Insert(0, bl); Tokens.Insert(0, pr); Tokens.Insert(0, pl); Tokens.Insert(0, m); Tokens.Insert(0, v); Tokens.Add(br); ResetToken(); ParseFunctionDeclarations(); }