internal void AddPropertyAfter(ObjectProperty property, ConFileEntry afterItem) { // Make sure this entry doesnt already exist if (Entries.IndexOf(property) > 0) { throw new Exception("The specified ObjectProperty already exists in this confile."); } // Find the owner object int index = Entries.IndexOf(afterItem) + 1; if (index == 0) { throw new Exception("The specified \"afterObject\" does not exist in this ConFile"); } // Insert item if (index == Entries.Count) { Entries.Add(property); } else { Entries.Insert(index, property); } }
/// <summary> /// Gets the Reference of an Expression, that occurs before the specified /// entry. /// </summary> /// <param name="name">The name of the variable or constant expression</param> /// <param name="beforeEntry"> /// The entry where this variable or constant expression is referenced /// </param> /// <returns> /// Returns the last reference value of the specifed expression, occuring /// before the <paramref name="beforeEntry"/> /// </returns> public Expression GetExpressionReference(string name, ConFileEntry beforeEntry) { // Check to see if this expression exists if (!Expressions.ContainsKey(name)) { goto Undefined; } // Grab the last reference before this confile entry int index = Entries.IndexOf(beforeEntry); if (index == -1) { throw new Exception("The specified entry does not exist in this ConFile"); } // Now find the last set expression reference value for (int i = index - 1; i >= 0; i--) { ConFileEntry entry = Entries[i]; TokenType kind = entry.Token.Kind; if (kind == TokenType.Constant || kind == TokenType.Variable) { // cast to Expression Expression exp = entry as Expression; if (exp.Name == name) { // This expression hasnt been assigned yet.. shame /*** * We have no way yet to determine if a variable is assigned * inside an If/While statement, so skip for now * * if (String.IsNullOrWhiteSpace(exp.Value)) * throw new Exception($"Value cannot be null; Expression \"{name}\" is unassigned"); */ return(exp); } } } // If we are here, the expression was not defined yet Undefined: { string err; if (name.StartsWith("c")) { err = $"Undefined constant \"{name}\""; } else { err = $"Undefined variable \"{name}\""; } throw new Exception(err); } }
/// <summary> /// Adds a new object to this confile, and returns it's reference /// </summary> /// <param name="entry"></param> /// <param name="token"></param> /// <returns></returns> public void AddEntry(ConFileEntry entry, Token token) { if (token.Kind == TokenType.RemComment) { return; } else if (token.Kind == TokenType.ObjectStart) { Objects.Add((ConFileObject)entry); } else if (token.Kind == TokenType.ActiveSwitch) { // Create a new reference and add it var reference = new ObjectReference() { Token = token, Object = (ConFileObject)entry }; References.Add(reference); // Set entry to the object reference entry = reference; } else if (entry is Expression) { Expression exp = entry as Expression; if (!Expressions.ContainsKey(exp.Name)) { // Add the expression Expressions.Add(exp.Name, new List <Expression>() { exp }); } } // Always add the entry Entries.Add(entry); }