[Nice-commit] Nice/src/bossa/syntax constructor.nice,NONE,1.1 importedconstructor.nice,1.2,1.3 nicec
Brought to you by:
bonniot
From: Arjan B. <ar...@us...> - 2004-12-14 22:49:24
|
Update of /cvsroot/nice/Nice/src/bossa/syntax In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30864/F:/nice/src/bossa/syntax Modified Files: importedconstructor.nice niceclass.nice tools.nice Added Files: constructor.nice Removed Files: Constructor.java Log Message: Converted Constructor. --- Constructor.java DELETED --- Index: tools.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/tools.nice,v retrieving revision 1.71 retrieving revision 1.72 diff -C2 -d -r1.71 -r1.72 *** tools.nice 14 Dec 2004 18:58:08 -0000 1.71 --- tools.nice 14 Dec 2004 22:49:12 -0000 1.72 *************** *** 217,221 **** MonoSymbol[?] getMonoSymbols(FormalParameters) = native MonoSymbol[] FormalParameters.getMonoSymbols(); mlsub.typing.AtomicConstraint[?] AtomicConstraint_substitute(java.util.Map<mlsub.typing.TypeSymbol,mlsub.typing.TypeSymbol>, AtomicConstraint[?]) = native mlsub.typing.AtomicConstraint[] mlsub.typing.AtomicConstraint.substitute(java.util.Map, mlsub.typing.AtomicConstraint[]); - NiceClass classe(Constructor) = native Constructor.classe; FormalParameters parameters(MethodDeclaration) = native MethodDeclaration.parameters; ?gnu.expr.Expression getInitializer(NiceClass) = native gnu.expr.Expression NiceClass.getInitializer(); --- 217,220 ---- *************** *** 257,260 **** --- 256,263 ---- boolean containsAlike(List<bossa.syntax.Monotype>) = native boolean bossa.syntax.Monotype.containsAlike(List); mlsub.typing.Monotype[] rawResolve(TypeMap,Collection<bossa.syntax.Monotype>) = native mlsub.typing.Monotype[] bossa.syntax.Monotype.rawResolve(TypeMap, Collection); + List<String> noMatchByName(Arguments, FormalParameters) = native List Arguments.noMatchByName(FormalParameters); + List<FormalParameters.Parameter> missingArgs(Arguments, FormalParameters) = native List Arguments.missingArgs(FormalParameters); + List<FormalParameters.Parameter> getRequiredParameters(FormalParameters) = native List FormalParameters.getRequiredParameters(); + List<FormalParameters.Parameter> asList(FormalParameters) = native List FormalParameters.asList(); // Retypings needed since java types are not strict. Index: importedconstructor.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/importedconstructor.nice,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** importedconstructor.nice 25 Nov 2004 19:28:18 -0000 1.2 --- importedconstructor.nice 14 Dec 2004 22:49:12 -0000 1.3 *************** *** 102,111 **** null : notNull(classDef.classConstraint).shallowClone(); ! let res = new ImportedConstructor(def, ! gnu.bytecode.Attribute.get(method, "default") != null, ! def.getDefinition().location(), FormalParameters.readBytecodeAttribute(attr, notNull(JavaClasses.compilation).parser), ! constraint, returnType, method: method); TypeConstructors.addConstructor(res.classe.getDefinition().getTC(), res); --- 102,112 ---- null : notNull(classDef.classConstraint).shallowClone(); ! let res = new ImportedConstructor( ! new LocatedString("<init>",def.getDefinition().location()), ! constraint, returnType, FormalParameters.readBytecodeAttribute(attr, notNull(JavaClasses.compilation).parser), ! classe: def, isDefault: gnu.bytecode.Attribute.get(method, "default") != null, ! method: method); TypeConstructors.addConstructor(res.classe.getDefinition().getTC(), res); Index: niceclass.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/niceclass.nice,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** niceclass.nice 4 Dec 2004 19:38:18 -0000 1.7 --- niceclass.nice 14 Dec 2004 22:49:12 -0000 1.8 *************** *** 551,558 **** notNull(constructorMethod)[i] = new DefaultConstructor ! (this, true, definition.location(), values, cst, Monotype.resolve(definition.getLocalScope(), values.types()), Monotype.sure(new mlsub.typing.MonotypeConstructor(definition.getTC(), definition.getTypeParameters())), ! fields: fields, parent: parent); TypeConstructors.addConstructor(definition.getTC(), notNull(constructorMethod)[i]); } --- 551,558 ---- notNull(constructorMethod)[i] = new DefaultConstructor ! (new LocatedString("<init>",definition.location()), values, cst, Monotype.resolve(definition.getLocalScope(), values.types()), Monotype.sure(new mlsub.typing.MonotypeConstructor(definition.getTC(), definition.getTypeParameters())), ! classe: this, isDefault: true, fields: fields, parent: parent); TypeConstructors.addConstructor(definition.getTC(), notNull(constructorMethod)[i]); } --- NEW FILE: constructor.nice --- /**************************************************************************/ /* N I C E */ /* A high-level object-oriented research language */ /* (c) Daniel Bonniot 2004 */ /* */ /* This program is free software; you can redistribute it and/or modify */ /* it under the terms of the GNU General Public License as published by */ /* the Free Software Foundation; either version 2 of the License, or */ /* (at your option) any later version. */ /* */ /**************************************************************************/ package bossa.syntax; import bossa.util.*; //import mlsub.typing.Constraint; //import mlsub.typing.Monotype; /** An object constructor. */ abstract class Constructor extends MethodDeclaration { NiceClass classe; final boolean isDefault; gnu.expr.Expression getInitializationCode(boolean implicitThis); void addConstructorCallSymbol() { let type = new mlsub.typing.Polytype(this.getType().getConstraint(), new mlsub.typing.FunType(this.getArgTypes(), nice.tools.typing.PrimitiveType.voidType)); classe.addConstructorCallSymbol(createConstructorCallSymbol(this, name, type)); } printInterface(s) { throw new Error("Should not be called"); } explainWhyMatchFails(arguments) { if (! isDefault) return super; String name = classe.getName().toString(); StringBuffer res = new StringBuffer(); res.append("Class ").append(name); //Arguments where none expected if (parameters.size == 0) { res.append(" has no fields. Therefore its constructor takes no arguments."); return res.toString(); } //No such field List<String> nonmatching = notNull(arguments).noMatchByName(parameters); if (!nonmatching.isEmpty()) { res.append(" has no field named "+nonmatching[0]); return res.toString(); } //Required fields missing, or else too many arguments // - three different messages depending on whether //an explanation of the syntax is necessary res = new StringBuffer(); List<FormalParameters.Parameter> missing = notNull(arguments).missingArgs(parameters); List<FormalParameters.Parameter> fieldsToList; if (notNull(arguments).size() == 0) { res.append("Fields of class ").append(name) .append(" require initial values.\n"); res.append(this.syntaxExample()) .append("Class ").append(name).append(" has the following fields:\n"); fieldsToList = parameters.asList(); } else if (missing.size() > 0) { res.append("The following fields require initial values:\n"); fieldsToList = missing; } else { res.append("Too many arguments when constructing new instance of class ") .append(name) .append(".\n") .append("The constructor accepts the following arguments:\n" ); fieldsToList = parameters.asList(); } for (field : fieldsToList) { res.append(" ") .append(field) .append("\n"); } return res.toString(); } private String syntaxExample() { String name = classe.getName().toString(); StringBuffer res = new StringBuffer(); res.append("Use the following syntax:\n") .append(" new ").append(name).append("("); let Iterator<FormalParameters.Parameter> params = parameters.getRequiredParameters().iterator(); int paramCount = 0; int len = name.length(); while(params.hasNext()) { FormalParameters.Parameter param = params.next(); if (paramCount != 0) res.append(", "); if (paramCount == 3 && params.hasNext()) { res.append('\n'); for(int i = 0; i < len; i++) {res.append(' ');} res.append(" "); paramCount = 0; } if (param instanceof FormalParameters.NamedParameter) res.append(param.getName()).append(": value"); else res.append("value"); paramCount++; } res.append(")\n\n"); return res.toString(); } } |