Declarations

In Parham, a component is made of two things: a set of classes and a set of declarations.

The classes define the roles of the objects involved in the component and the declarations define the behavior of them.

Parham supports the usual attribute declarations (field, constructor and method). In addition, it has some specific declarations: instance, aggregation and inclusion declarations.

Aggregation declaration is discussed in the lesson. Instance and inclusion declarations are discussed in future lessons.

Attribute and Aggregation Declarations

In Parham, components are stateless. Therefore, it is not possible to define any attribute for them.

It means that a component can define attributes only for its classes. The usual way is to do attribute declaration. In this way, the type and the name of the attribute is specified. In an attribute declaration the type of the declaration can be primitive (int, char, float, etc.) or an internal class. In other words, the type of an attribute cannot be an interface class. Moreover, in an attribute declaration, space is allocated to store the value.

For example, in the below declaration, attribute c is declared for class Class1. In this case, the object c is part of Class 1.

Class1

{

    Class2 c;

}

If it is required to store a pointer to an object in another object ,aggregation declaration should be used.

Class1 -> Class2 c;

In the above example a reference (pointer) to Class2 is stored in class Class1. In this case, Class1 and Class2 can be internal or interface classes.

The example defines a Queue component. It is an implementation of queue data structure. It then uses it to store three persons and then print them.

It is made from three files.

  • Queue component
  • App (main) component
  • I/O functions
Queue component

Queue component is a doubly linked list. It has two interface classes: Root and Node. Root denotes the queue itself and Node represents a node of the queue.

Queue[Root,Node]

After providing the component name and its interface classes, there are four aggregation declarations: head, tail, next and prev.

head and tail are members of Root class. head points to the first node of the list and tail points to the last node of the list.

next and prev are members of Node class. next points to the next node of this node in the list and prev points to the previous node of this node in the list.

Root -> Node head, tail;

Node -> Node next, prev;

The of the component defines a constructor and three methods for Root class. The constructor initializes a Root object. The methods are enqueue, deque and remove.

enqueue adds a node to the tail of the queue, deque remvoes and returns the first node of the queue and remove removes the given node from the queue.

App component

App is the main component of the application. It has two internal classes: People and Person.

In addition, it has an instance of Queue.

Queue[People,Person];

It then defines the attributes of Person: id and name.

    Person

    {

        int id;

        char[] name;

    }

Then, there is a constructor and a method (print) for Person.

Finally, the main function of the application creates a People object, inserts three person in it and then gets and prints them.

I/O functions

Finally, I/O functions are developed in a C file.

Queue[Root, Node]


Root -> Node head, tail;

Node -> Node next, prev;


Root ()

{

   head = tail = null;

}


public void Root.enqueue (Node n)

{

   if (!head)

   {

       head = tail = n;

       n.next = n.prev = null;

       return;

   }

   n.next = null;

   n.prev = tail;

   tail.next = n;

   tail = n;

}



public Node Root.deque ()

{

   Node tmp;


   if (!head)

       return null;

   tmp = head;

   head = head.next;

   if (head)

       head.prev = null;

   else

       tail = null;

   return tmp;

}


public void Root.remove (Node n)

{

   if (head == n)

       head = head.next;

   if (tail == n)

       tail = tail.prev;

   if (n.next)

       n.next.prev = n.prev;

   if (n.prev)

       n.prev.next = n.next;

}

Queue component

App


foreign void cprint (int id, char[] name);


People[];

Person[];


Queue[People, Person];


Person

{

    int id;

    char[] name;

}


Person (int _id, char[] _name)

{

    id = _id;

    name = _name;

}


void Person.print ()

{

    cprint (id, name);

}

foreign int main ()

{

    People people;

    Person person;


    people = new People ();


    person = new Person (1, “C”);

    people.enqueue (person);


    person = new Person (2, “C++”);

    people.enqueue (person);


    person = new Person (3, “Java”);

    people.enqueue (person);


    while (person = people.deque ())

        person.print ();

    return 0;

}

App component

#include <stdio.h>


void cprint (int id, char *name)

{

    printf (“%d: %sn”, id, name);

}


I/O functions in C