Decaf

PA1: Decaf Program

Objective

The goal of this project is to become familiar with the Decaf programming language.

Introduction

The semester-long project for this course is to build a compiler for the Decaf language. In this project, you will implement various functions in Decaf to become familiar with the language.

You should download the reference compiler from the "Files" tab in Canvas to test your submission for this project.

You may wish to download the starter file with function stubs: 01-stubs.decaf

Assignment

Implement the following functions in Decaf:

  • int fact(int n)
    Returns the factorial of n (i.e., "n!"). For example, "fact(3)" should return 6.
  • int fib(int n)
    Returns the nth Fibonacci number. For example, "fib(1)" should return 1 and "fib(4)" should return 3.
  • bool is_prime(int n)
    Returns true if n is prime, and false otherwise. Performance is not a concern, so you may use a naive algorithm.
  • int sum_nums(int len)
    Returns the sum of len numbers from an array called nums. For example, "sum_nums(0)" should return 0 regardless of the contents of nums, and "sum_nums(2)" should return the sum of the first two numbers.
  • void sort_nums(int len)
    Sorts len numbers from an array called nums in ascending numerical order. Sorting should be done in-place. Performance is not a concern, so you may use any sorting algorithm you wish.
  • int gcd(int a, int b)
    Returns the greatest common divisor of a and b. For example, "gcd(8,12)" should return 4. Performance is not a concern, so you may use a naive algorithm.
  • void draw_triangle(int base)
    Draw a simple text-based triangle using hash marks ('#'). The base of the triangle should be base characters long and the triangle should be oriented upwards from the base. Each level of the triangle should decrease in width by two characters per level (one on each side). Every line should end with a newline character ('\n'), but there should be no extra whitespace above or below the triangle or on its right side. Examples:
    
    base=3:
    
     #
    ###
    
    base=4:
    
     ##
    ####
    
    base=9:
    
        #
       ###
      #####
     #######
    #########
    
        

For this assignment, you may assume that all function inputs are valid by the function's mathematical definition. For example, the factorial operator is not defined for negative integers, so your fact does not need to handle negative inputs; the same holds for fib, gcd or draw_triangle.

Reflection

One of the goals of this course is to encourage introspection and thoughtful, deliberate development processes. For this project, you will compose a short (1-2 page) reflection paper, answering the following questions:

  • How does this project fit into the overall picture of our semester-long compiler project? Why is it important?
  • Describe your design and development process. Did you use a formal software development method?
  • What aspects of this project proved to be the most rewarding?
  • What aspects of this project proved to be the most challenging? How did you overcome these challenges?
  • How do you know your submission is correct? Briefly describe your testing regimen.
  • If you had to start the project again from scratch, what would you do differently?
  • What concepts from our theoretical class material or techniques from our classroom activities did you apply in this project?
  • Suppose you weren’t using any of the concepts or techniques we’ve covered in class this semester; how would your solution to this project be different?
  • What other areas of computer science (or CS courses you’ve taken) impacted your solution to this project?
  • Do you have any other feedback about this project?

Be sure to address all of the above questions in as much detail as you wish, but do not ramble. Concise answers are preferred over verbose ones.

FOR P1 ONLY: Some of the above questions (especially the ones about applying classroom concepts) may not be relevant for PA1; feel free to skip them for this assignment.

Submission

DUE DATE: Friday, September 9, 23:59 EDT (11:59PM)

Please submit your .decaf file and your reflection paper to the appropriate assignments on Canvas.

Your decaf program should be self-contained and should implement all of the project specs described above. For code review purposes, please DO NOT include your name(s) in the source code (if you do, your review will not be anonymous). If you worked in a two-person group, use Canvas's group feature to denote this.

Your reflection paper should be in a standard document format: plain text and PDF are preferred, but OpenDocument and MS Word files are also acceptable.

Code Reviews

One of the goals of this course is to encourage good software development practices, especially when building a large software system (such as a compiler). For this project, you will be assigned two other random students in the course. You must review their code and offer constructive feedback according to the given rubric.

Submit your review on Canvas by Friday, September 16. Please submit your review as a comment (not an attached file).

Grading

Here is the grading breakdown for this project:

Automated tests35
Style grading5
Reflection paper5
Peer review5
TOTAL50