CSC 533: Programming Languages
Spring 2025

HW3: Implementing Strings


The SILLY interpreter you wrote for HW2 extended the starter version to allow for Boolean operations, characters and repeat statements. For this third SILLY assignment, you are to further extend your interpreter to implement the string type and corresponding string operations. A string literal is any sequence of non-whitespace, non-quote characters inside of double quotes, e.g., "foo". The operations that currently apply to lists (len, get and cat) should also apply to strings. In fact, it is convenient to think of a string as a list of characters, with the same operations as any other list.

SAMPLE CODE (output in red)
>>> word = "foo"
>>> print word
foo
>>> print (len word)
3 
>>> print (get word 0)
f   
>>> index = 0
>>> while (< index (len word)) {
        print (get word index)
        index = (+ index 1)
    }
f
o
o 
>>> s = "racecar"
>>> isPal = true
>>> i = 0
>>> while (<= i (/ (len s) 2)) {
        if (!= (get s i) (get s (+ (len s) -1 (* i -1)))) {
            isPal = false
        } else { }
        i = (+ i 1)
    }
>>> print isPal
true 
>>> letters = (cat "a" "b" "c")
>>> print letters 
abc
>>> repeat 3 {
        letters = (cat letters letters)
    }
>>> print letters
abcabcabcabcabcabcabcabc
>>> num = 3.2
>>> numstr = (str num)
>>> print (cat "num=" numstr)
num=3.2 
>>> print (cat (str true) "-" (str 'A') "-" (str [1 2]))
true-A-[1 2] 
>>> long = (cat word "bar" "biz")
>>> print long
foobarbiz
>>> rev = ""
>>> index = (+ (len long) -1)
>>> while (>= index 0) {
        ch = (get long index)
        rev = (cat rev (str ch))
        index = (+ index -1)        
    }
>>> print rev
zibraboof 
To complete this assignment, you must do the following:
  1. Define the StringValue class that implements the SILLY string data type. To take advantage of the similarity between strings and lists, you are to utilize inheritance when defining this new class. That is, StringValue should extend the ListValue class. The constructor for StringValue should call the ListValue constructor to store the individual characters of the string. The class should utilize inherited methods when possible. You should also add an entry to the DataValue.Type enumeration for this new data type.
  2. Modify the Expression class so that string literals are recognized as valid expressions. Also, revise the definitions of the list operations (len, get and cat) so that they can be applied to strings as well as lists. Take advantage of polymorphism (StringValue IS_A ListValue) whenever possible to avoid redundancy.
  3. Implement the str operation that acts like toString in Java. That is, it takes a value of any data type as input and returns a string representation of that value.
  4. To test your implementation of the string data type, write a sequence of SILLY statements that initialize a variable to a list of strings, then processes that list to determine the shortest word, longest word, and whether there are any duplicates in the list. For example, if the first line of your code was:

    list = ["dog" "cat" "ox" "fish" "zyzzyva" "cat"]

    then your code would display:

    shortest=ox
    longest=zyzzyva
    dupes?yes

    Save your SILLY code in a text file named hw3.txt and submit it along with your project code.