internal override Expression Resolve(Parser parser) { string className = this.NameToken.Value; if (parser.IsTranslateMode) { StructDefinition structDefinition = parser.GetStructDefinition(className); if (structDefinition != null) { if (this.Args.Length != structDefinition.Fields.Length) { throw new ParserException(this.FirstToken, "Args length did not match struct field count for '" + structDefinition.Name.Value + "'."); } StructInstance si = new StructInstance(this.FirstToken, this.NameToken, this.Args, this.FunctionOrClassOwner); si = (StructInstance)si.Resolve(parser); return si; } } for (int i = 0; i < this.Args.Length; ++i) { this.Args[i] = this.Args[i].Resolve(parser); } ConstructorDefinition cons = this.Class.Constructor; if (this.Args.Length < cons.MinArgCount || this.Args.Length > cons.MaxArgCount) { // TODO: show the correct arg count. throw new ParserException(this.FirstToken, "This constructor has the wrong number of arguments."); } return this; }
protected override void TranslateStructInstance(List<string> output, StructInstance structInstance) { output.Add("["); for (int i = 0; i < structInstance.Args.Length; ++i) { if (i > 0) output.Add(this.Shorten(", ")); this.TranslateExpression(output, structInstance.Args[i]); } output.Add("]"); }
internal override Expression Resolve(Parser parser) { string className = this.NameToken.Value; if (parser.IsTranslateMode) { StructDefinition structDefinition = parser.GetStructDefinition(className); if (structDefinition != null) { if (this.Args.Length != structDefinition.Fields.Length) { throw new ParserException(this.FirstToken, "Args length did not match struct field count for '" + structDefinition.Name.Value + "'."); } StructInstance si = new StructInstance(this.FirstToken, this.NameToken, this.Args, this.FunctionOrClassOwner); si = (StructInstance)si.Resolve(parser); return(si); } } for (int i = 0; i < this.Args.Length; ++i) { this.Args[i] = this.Args[i].Resolve(parser); } if (this.Class == null) { throw new ParserException(this.FirstToken, "No class named '" + this.Name + "'"); } if (this.Class.StaticToken != null) { throw new ParserException(this.FirstToken, "Cannot instantiate a static class."); } ConstructorDefinition cons = this.Class.Constructor; if (cons.PrivateAnnotation != null) { if (this.Class != this.FunctionOrClassOwner.FunctionOrClassOwner) { string errorMessage = "The constructor for " + this.Class.NameToken.Value + " is private and cannot be invoked from outside the class."; if (cons.PrivateAnnotation.Args.Length > 0) { StringConstant stringMessage = cons.PrivateAnnotation.Args[0] as StringConstant; if (stringMessage != null) { errorMessage += " " + stringMessage.Value.Trim(); } } throw new ParserException(this.FirstToken, errorMessage); } } if (this.Args.Length < cons.MinArgCount || this.Args.Length > cons.MaxArgCount) { string message = "This constructor has the wrong number of arguments. "; if (cons.MinArgCount == cons.MaxArgCount) { message += "Expected " + cons.MinArgCount + " but found " + this.Args.Length; } else if (this.Args.Length < cons.MinArgCount) { message += " At least " + cons.MinArgCount + " are required but found only " + this.Args.Length + "."; } else { message += " At most " + cons.MaxArgCount + " are allowed but found " + this.Args.Length + "."; } throw new ParserException(this.FirstToken, message); } return(this); }
protected abstract void TranslateStructInstance(List<string> output, StructInstance structInstance);