[Nice-commit] Nice/src/bossa/syntax enum.nice,NONE,1.1 AST.java,1.59,1.60 Definition.java,1.23,1.24
Brought to you by:
bonniot
Update of /cvsroot/nice/Nice/src/bossa/syntax In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10969/F:/nice/src/bossa/syntax Modified Files: AST.java Definition.java MonoSymbol.java literalarray.nice pattern.nice tools.nice Added Files: enum.nice Removed Files: EnumDefinition.java Log Message: Converted EnumDefinition. Index: Definition.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/Definition.java,v retrieving revision 1.23 retrieving revision 1.24 diff -C2 -d -r1.23 -r1.24 *** Definition.java 7 Nov 2004 01:07:26 -0000 1.23 --- Definition.java 21 Nov 2004 18:33:17 -0000 1.24 *************** *** 83,86 **** --- 83,96 ---- } + boolean isEnumDefinition() + { + return false; + } + + ClassDefinition getEnumClass() + { + return null; + } + /**************************************************************** * Name and location of the definition Index: AST.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/AST.java,v retrieving revision 1.59 retrieving revision 1.60 diff -C2 -d -r1.59 -r1.60 *** AST.java 7 Nov 2004 01:07:26 -0000 1.59 --- AST.java 21 Nov 2004 18:33:17 -0000 1.60 *************** *** 65,70 **** else if (node instanceof MethodBodyDefinition) methodImplementations.add(node); ! else if (node instanceof EnumDefinition) ! classes.add(((EnumDefinition)node).classDef); else if (node.isGlobalVarDeclaration()) globals.add(node); --- 65,70 ---- else if (node instanceof MethodBodyDefinition) methodImplementations.add(node); ! else if (node.isEnumDefinition()) ! classes.add(node.getEnumClass()); else if (node.isGlobalVarDeclaration()) globals.add(node); Index: pattern.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/pattern.nice,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** pattern.nice 7 Nov 2004 01:07:26 -0000 1.8 --- pattern.nice 21 Nov 2004 18:33:17 -0000 1.9 *************** *** 244,250 **** addValues(values) { ! let EnumDefinition.EnumSymbol esym = cast(atValue.value); ! let EnumDefinition edef = cast(esym.getDefinition()); ! for (sym : edef.symbols) values.add(new ConstantExp(null, tc, sym, sym.name.toString(), this.location)); } --- 244,249 ---- addValues(values) { ! let EnumSymbol esym = cast(atValue.value); ! for (sym : esym.getDefinition().symbols) values.add(new ConstantExp(null, tc, sym, sym.name.toString(), this.location)); } *************** *** 468,472 **** for (sym : Node.getGlobalScope().lookup(refName)) if (sym instanceof GlobalVarSymbol || ! sym instanceof EnumDefinition.EnumSymbol ) return sym; --- 467,471 ---- for (sym : Node.getGlobalScope().lookup(refName)) if (sym instanceof GlobalVarSymbol || ! sym instanceof EnumSymbol ) return sym; *************** *** 507,511 **** return pattern; ! if (symbol instanceof EnumDefinition.EnumSymbol) { NewExp val = cast(symbol.getValue()); --- 506,510 ---- return pattern; ! if (symbol instanceof EnumSymbol) { NewExp val = cast(symbol.getValue()); Index: literalarray.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/literalarray.nice,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** literalarray.nice 30 Jul 2004 18:31:10 -0000 1.2 --- literalarray.nice 21 Nov 2004 18:33:17 -0000 1.3 *************** *** 20,27 **** The array type is infered as the minimal possible type. */ - public class LiteralArrayExp extends Expression { - Expression[] elements; --- 20,25 ---- --- EnumDefinition.java DELETED --- Index: MonoSymbol.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/MonoSymbol.java,v retrieving revision 1.30 retrieving revision 1.31 diff -C2 -d -r1.30 -r1.31 *** MonoSymbol.java 23 Jun 2004 19:38:09 -0000 1.30 --- MonoSymbol.java 21 Nov 2004 18:33:17 -0000 1.31 *************** *** 156,159 **** --- 156,162 ---- public String toString() { + if (type == null) + return syntacticType+" "+name; + return type+" "+name; } Index: tools.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/tools.nice,v retrieving revision 1.60 retrieving revision 1.61 diff -C2 -d -r1.60 -r1.61 *** tools.nice 18 Nov 2004 17:31:16 -0000 1.60 --- tools.nice 21 Nov 2004 18:33:17 -0000 1.61 *************** *** 199,203 **** Stack<bossa.link.Alternative> sortedAlternatives(MethodDeclaration) = native Stack bossa.link.Alternative.sortedAlternatives(MethodDeclaration); LinkedList<VarSymbol> getConstructors(mlsub.typing.TypeConstructor) = native LinkedList TypeConstructors.getConstructors(mlsub.typing.TypeConstructor); - List<VarSymbol> symbols(EnumDefinition) = native EnumDefinition.symbols; void addValues(Pattern, List<ConstantExp>) = native void Pattern.addValues(List); ?mlsub.typing.TypeConstructor getRuntimeTC(Pattern) = native mlsub.typing.TypeConstructor Pattern.getRuntimeTC(); --- 199,202 ---- *************** *** 238,241 **** --- 237,242 ---- void printInterface(ClassImplementation, java.io.PrintWriter) = native void ClassImplementation.printInterface(java.io.PrintWriter); ?gnu.bytecode.Type TypeImport_lookup(LocatedString) = native gnu.bytecode.Type nice.tools.code.TypeImport.lookup(LocatedString); + ClassDefinition.Class ClassDefinition_makeClass(LocatedString, boolean, boolean, ?MethodContainer.Constraint, List<?boolean>, MonotypeConstructor, ?List<TypeIdent>, ?List<TypeIdent>) = native ClassDefinition.Class ClassDefinition.makeClass(LocatedString, boolean, boolean, MethodContainer.Constraint, List, MonotypeConstructor, List, List); + MethodBodyDefinition MethodBodyDefinition(NiceClass, LocatedString, ?Collection<LocatedString>, List<Pattern>, Statement) = native new MethodBodyDefinition(NiceClass, LocatedString, Collection, List, Statement); // Retypings needed since java types are not strict. --- NEW FILE: enum.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.*; /** Definition of an enum */ public class EnumDefinition extends Definition { ClassDefinition classDef; List<EnumSymbol> symbols; String representation; { this.addChild(classDef); } void addElement(EnumSymbol sym) { symbols.add(sym); bossa.util.Debug.println("adding: "+sym); this.addChild(sym); } isEnumDefinition() = true; getEnumClass() = classDef; resolve() { for (symbol : symbols) symbol.resolveValue(notNull(scope), notNull(typeScope)); } typecheck() { for (symbol : symbols) symbol.typecheckValue(); } printInterface(s) { s.print(representation+"\n"); } compile() { for (symbol : symbols) symbol.compileValue(); } toString() = representation; } public EnumDefinition createEnumDefinition(LocatedString name, List<LocatedString> elements, List<MonoSymbol> fields, List<List<Expression>> argsList, List<Definition> globalDefs, ?List<TypeIdent> interfaces) { let shortName = name.toString(); let classDef = ClassDefinition.makeClass (name,true,false, null, new ArrayList(), new MonotypeConstructor(new TypeIdent(new LocatedString("nice.lang.Enum", name.location())), null, name.location()), interfaces, null); let impl = createNiceClass(classDef); for (fieldsym : fields) impl.addField(fieldsym, null, true, false, false, null); classDef.setImplementation(impl); if (! notNull(Definition.currentModule).interfaceFile()) // create the method implementation of family() globalDefs.add(new MethodBodyDefinition(impl, new LocatedString("family"), null, new LinkedList(), createReturnStmt(createLiteralArrayExp( elements.map(LocatedString elem => new IdentExp(elem))), true))); bossa.util.Debug.println("name: "+shortName); //generate the prettyprint string String repr = "enum " + shortName; if (! fields.isEmpty()) repr += Util.map("(", ", ", ")", fields); if (interfaces != null) repr += " implements " + Util.map(""," , ","", interfaces); if (fields.isEmpty()) repr += Util.map(" {", " , ", " }", elements); else { repr += " {"; for (int i = 0; i < elements.size(); i++) { if (i != 0) repr += ", "; repr += elements[i] + Util.map("(", ", ", ")", argsList[i]); } repr += "}"; } let res = new EnumDefinition(name, Node.global, classDef: classDef, symbols: new ArrayList(), representation: repr); for (int ord = 0; ord<elements.size(); ord++) { if (argsList[ord].size() != fields.size()) User.error(elements[ord], "the number of arguments doesn't match the number of enum fields"); res.addElement(createEnumSymbol(res, name, elements[ord], ord, fields, argsList[ord])); } return res; } class EnumSymbol extends MonoSymbol { EnumDefinition definition; Expression value; isAssignable() = false; getDeclaration() { ?gnu.expr.Declaration res = super; if (res == null) { res = new gnu.expr.Declaration(name.toString(), nice.tools.code.Types.javaType(type)); this.setDeclaration(res); definition.module.addGlobalVar(res, true); } return res; } void resolveValue(VarScope scope, TypeScope typeScope) { value = analyse(value, scope, typeScope); } void typecheckValue() { try { value = value.resolveOverloading(this.getType()); typecheck(value); mlsub.typing.Typing.leq(value.getType(), this.getType()); } catch(TypingEx e) { Internal.error(this,"Typing error in enum:"); } } void compileValue() { let declaration = this.getDeclaration(); notNull(declaration).setFlag(gnu.expr.Declaration.IS_CONSTANT); notNull(declaration).noteValue(value.compile()); } Expression getValue() { return value; } override EnumDefinition getDefinition() = definition; } EnumSymbol createEnumSymbol(EnumDefinition def, LocatedString enumName, LocatedString name, int ordinal, List<MonoSymbol> fields, List<Expression> argExps) { Monotype type = new TypeIdent(enumName); type.nullness = Monotype.absent; List<Arguments.Argument> args = new ArrayList(2 + fields.size()); args.add(new Arguments.Argument(createStringConstantExp(name.toString()), new LocatedString("name",name.location))); args.add(new Arguments.Argument(new ConstantExp(PrimitiveType.intTC, ordinal, ordinal.toString(), name.location()), new LocatedString("ordinal", name.location))); for (int i = 0; i < fields.size(); i++) args.add(new Arguments.Argument(argExps[i], fields[i].getName())); return new EnumSymbol(name, type, definition: def, value: createNewExp(new TypeIdent(enumName), new Arguments(args))); } |