/// <summary> /// Parses a function declaration /// Syntax: /// returntype identifer ( param_list ) /// { /// statements /// } /// </summary> public FunctionInfo ParseFunctionDeclaration() { Int32 type = -1; type = ParseType(); //Validity if (CurrentToken.Type != TokenType.Identifier) Error("Invalid function name", CurrentToken.LineStart); FunctionInfo func = new FunctionInfo(); func.Name = CurrentToken.Value; func.Type = type; List<String> paramNames = new List<String>(); NextToken(); NextToken(); if (CurrentToken.Value != ")") { while (true) { VariableInfo var = new VariableInfo(); ParseDeclaration(ref var); if (!"),".Contains(CurrentToken.Value)) Error("Expected comma as param seperator", CurrentToken.LineStart); if (paramNames.Contains(var.Name)) Error("Already got parameter", CurrentToken.LineStart, CurrentToken.CharacterPosition); paramNames.Add(var.Name); func.Parameters.Add(var); if (CurrentToken.Value.Equals(")")) break; if (CurrentToken.Value.Equals(",")) NextToken(); } } NextToken(); return func; }
public void ParseDeclaration(ref VariableInfo var) { if (!Database.CheckType(CurrentToken.Value)) Error("Invalid type", CurrentToken.LineStart, CurrentToken.CharacterPosition); Int32 type = ParseType(); //Check for validity in name if (!CurrentToken.Type.Equals(TokenType.Identifier)) Error("Invalid Name: " + CurrentToken.Value, CurrentToken.LineStart, CurrentToken.CharacterPosition); if (!Database.CheckValidName(CurrentToken.Value)) Error("Invalid Name, name already used: " + CurrentToken.Value, CurrentToken.LineStart, CurrentToken.CharacterPosition); String name = CurrentToken.Value; NextToken(); var.Name = name; var.Type = type; }
/// <summary> /// Parse a type /// Syntax: /// struct type_name /// { /// datatype identifier; /// ... /// } /// </summary> public void ParseTypeDeclaration() { //Ignore the word TYPE NextToken(); if (CurrentToken.Type != TokenType.Identifier || !Database.CheckValidName(CurrentToken.Value)) Error("Invalid Type Name", CurrentToken.LineStart); //Set type info TypeInfo type = new TypeInfo(); type.Name = CurrentToken.Value; //Keep a record of all the members to ensure none are repeated List<String> memberNames = new List<String>(); NextToken(); if (CurrentToken.Value != "{") Error("Expected member list { ... }", CurrentToken.LineStart); //NEW and DELETE of the type are functions, if they are incoming, skip and parse later while (CheckFunctionIncoming()) { SkipBlock(); NextToken(); } NextToken(); //Get member declarations do { VariableInfo var = new VariableInfo(); ParseDeclaration(ref var); //Check if valid member if (memberNames.Contains(var.Name)) Error("Type " + type.Name + " already contains a member named " + var.Name, CurrentToken.LineStart); memberNames.Add(var.Name); //If the next token isn't a semicolon we have an error if (CurrentToken.Value != ";") { Error("Expected semicolon", CurrentToken.LineStart); } NextToken(); //Skip any function while (CheckFunctionIncoming()) { SkipBlock(); NextToken(); } //Add new member to the members list type.Members.Add(var); } while (CurrentToken.Value != "}"); //Add the new type to the database Database.AddType(type); NextToken(); }
/// <summary> /// Parses all global variable declarations (not in functions or structs) /// Syntax: /// datatype identifier; /// </summary> public void ParseGlobalVariableDeclarations() { for (int Line = 0; Line < Lines && CurrentToken != null; Line++) { if (CurrentToken.Value != ";") { if (CurrentToken.Value.Equals("STRUCT") || CheckFunctionIncoming()) { SkipBlock(); PreviousToken(); } else if (Database.CheckType(CurrentToken.Value)) { VariableInfo var = new VariableInfo(); ParseDeclaration(ref var); Database.AddVariable(var, null); } } NextToken(); } }
private void ParseVariableDeclaration(ref Node node) { VariableInfo var = new VariableInfo(); ParseDeclaration(ref var); node = new Node(); node.Value = "DECLARE"; node.Attributes[0] = Database.GetVarId(var.Name, null); node.Attributes[1] = Database.GetTypeName(var.Type); }
/// <summary> /// Adds a variable to the variable list /// </summary> public static void AddVariable(VariableInfo var, Scope scope) { if (scope != null) var.Id = scope.Id + var.Name; else { scope = CurrentScope; var.Id = var.Name; } scope.Variables.Add(var); }