uio-jpt / pt-compiler

5 stars 0 forks source link

Virker lister av lister-eksempelet? #9

Closed eyvindwa closed 12 years ago

eyvindwa commented 12 years ago
template LinkedList{
    class List{
        Elem first, last;
        void addLast(Elem e){...}
        Elem removeFirst(){...}
    }
    class Elem{
        Elem nextElem;
        Elem next(){ return nextElem;}
    }
}

package Program{
    inst LinkedLists
    with List => ListOfLists, Elem => ElemList;
    class ListOfLists adds{...}
    class Elem adds{...}
    inst LinkedLists
    with List => ElemList, Elem => Person;
    class Person adds{...}
}
svk commented 12 years ago

Dette ser ut til å virke fint, skal være som eksempelet over.

template LinkedLists {
    class List{
        Elem first, last;
        void addLast(Elem e){
            if( first == null ) {
                first = last = e;
            } else {
                last.setNext( e );
            }
        }
        Elem removeFirst(){
            Elem rv = first;
            first = first.next();
            rv.setNext( null );
            if( last == rv ) {
                last = null;
            }
            return rv;
        }
        boolean isEmpty() {
            return first == null;
        }
    }
    class Elem{
        Elem nextElem;
        Elem next() {
            return nextElem;
        }
        void setNext(Elem e) {
            nextElem = e;
        }
    }
}
package Program {
    inst LinkedLists with List => ListOfGroups, Elem => Group;
//    class ListOfGroups adds { } // trivial/optional
    class Group adds {
        String groupName;
        Group(String groupName) {
            this.groupName = groupName;
        }
    }
    inst LinkedLists with List => Group, Elem => Person;
    class Person adds{
        String name;
        Person(String name) {
            this.name = name;
        }
        String getName() {
            return name;
        }
    }
    class ListOfListsTest {
        public static void main(String args[]) {
            ListOfGroups g = new ListOfGroups();
            Group c = new Group("Group C");
            c.addLast( new Person( "Percy" ) );
            c.addLast( new Person( "John" ) );
            c.addLast( new Person( "Wendy" ) );
            g.addLast( c );
            Group d = new Group("Group D");
            d.addLast( new Person( "Maria" ) );
            d.addLast( new Person( "Thomas" ) );
            d.addLast( new Person( "Nina" ) );
            g.addLast( d );
        }
    }
}

Tror kanskje ikke det er noe problem her allikevel.

Det er vel følgende tester jeg har lurt på, der noe instansieres to ganger og en klasse merges med samme klasse i en annen instansiering: https://github.com/uio-jpt/pt-compiler/blob/master/PTFrontend/test/compiler_semantic_tests/single_file/merge/MergeOneClassWithSelf.javapt https://github.com/uio-jpt/pt-compiler/blob/master/PTFrontend/test/compiler_semantic_tests/single_file/merge/MergeOneClassWithSelfNoConflicts.javapt https://github.com/uio-jpt/pt-compiler/blob/master/PTFrontend/test/compiler_semantic_tests/single_file/merge/MergeOneClassWithSelfNoConflictsExplicit.javapt Det går ikke bra foreløpig.

svk commented 12 years ago

Dette er vel fikset nå.