Exercises for Week 7#

Exercise overview#

In this exercise we get to know the language Dolphin a bit better. For this purpose, we will use the online Dolphin interpreter: https://cs.au.dk/~timany/dolphin_web/.

Syntax of Dolphin#

Throughout the course we have seen many Dolphin code snippets. Therefore, we will not repeat things in great detail. Remember, Dolphin’s syntax is heavily inspired by that of C, Java, and C#.

A Dolphin program consists of a number of function declarations and a number of record declarations. The order of these declarations does not matter. All Dolphin programs must have a main function that takes no arguments and returns an integer (the exit code of the program). Records are declared as follows using the record keyword:

record person {name : string; height : int; }

Records are referred to by their names. Given the record above, we can write the following program which finds the tallest person. Here, [person] is the type of array of person.

record person {name : string; height : int; }

person tallest(prs : [person]){
  var tlp : person = nil;

  if(length_of(prs) > 0){
    tlp = prs[0];
  }

  for(var i = 1; i < length_of(prs); i = i + 1){
    if(prs[i].height > tlp.height)
      tlp = prs[i];
  }
  return tlp;
}

int main(){
  let stdout = get_stdout();
  let prs = new person[5];
  prs[0] = new person {name = "Chris"; height = 163; };
  prs[1] = new person {name = "Pernille"; height = 152; };
  prs[2] = new person {name = "Gudmund"; height = 180; };
  prs[3] = new person {name = "Mathias"; height = 162; };
  prs[4] = new person {name = "Nina"; height = 171; };
  let tlp = tallest(prs);
  output_string("The tallest person is ", stdout);
  output_string(tlp.name, stdout);
  output_string(" who is ", stdout);
  output_string(int_to_string(tlp.height), stdout);
  output_string(" cm tall.\n", stdout);
  return 0;
}

Note how the new keyword is used to create both arrays and records. Note that arrays are not explicitly initialized by the program. Dolphin automatically initializes array entries with default values, 0 for integers, false for booleans, "" for strings, and nil for arrays and records. The let keyword creates an immutable variable, i.e., a variable that can never be updated after it’s initialization.

Run the program above in the interpreter.

Dolphin’s standard library in the interpreter#

The following standard library functions are available in the interpreter. (Things like file I/O and networking are not supported in the interpreter.)

void exit(code : int);
stream get_stdout();
stream get_stderr();
stream get_stdin();
bool flush_file(srm : stream); // returns false when errors encountered
void output_string(str : string, srm : stream);
void output_bytes_array(bta : [byte], srm : stream);
[byte] input_bytes_array(len : int; srm : stream);
bool output_byte(b : byte, srm : stream); // returns false when errors encountered
int input_byte(stream); // returns a byte as a signed integer
int string_to_int(str : string);
string int_to_string(i : int);
string substring(str : string, start : int, len : int);
string string_concat(str1 : string, str2 : string);
string ascii_chr(c : int);
int ascii_ord(string);
byte int_to_byte_signed(i : int);
byte int_to_byte_unsigned(i : int);
int byte_to_int_signed(b : byte);
int byte_to_int_unsigned(b : byte);
[byte] string_to_bytes_array(str : string);
string bytes_array_to_string(bts : [byte]);

The type stream is a type in Dolphin defined by the standard library. It is technically treated by Dolphin as a record type with no fields. However, programmers cannot create instances of this record. (Try it!) The stream type is used for file and console I/O.

The type byte is a primitive type in Dolphin of exactly 1 byte size. Programmers cannot write byte literal values. The standard library uses bytes for I/O and allows programmers to convert to and from strings and integers. Dolphin does not enforce a signedness for bytes, hence, the conversion functions to and from integers have a signed and unsigned variant.

Exercise#

Write a program for a phone directory. It should store entries as records, with two string fields, one for the name of the person, and another for the phone number. The directory data structure should be a collection of entries, e.g., an array, a linked list, etc., we leave the details to you. The data structure should support adding entries, removing entries, searching for entries (by name), and printing all entries. The latter should print entries in alphabetical order according to their names. These operations can be destructive (update in place), or non-destructive (return a new collection), again, we leave the details to you.

Write a main function that enters 20 entries, searches for a few names (test both existing and non-existing names), removes 2 entries, and prints the final directory.

This exercise gives a lot of freedom in how you choose to write your program, e.g., in the choice of presenting menu, taking user’s input, etc. The purpose is to focus on getting familiar with Dolphin, not a particular way of programming.

Bonus#

Implement a basic command-line interface (CLI), e.g., the main function should present the user with a menu like below:

Please choose one of the following actions:
1. Add an entry
2. List entries
3. Edit an entry
4. Delete an entry
5. Export as HTML
6. Exit