Week 1

Week 2 – ChocoPy, Types, and Functions

Readings & Due Dates

Read:

TODOs:

Lecture Materials

Lecture 3 (Tues 04/05) Handout

Lecture 4 (Thu 04/07) Handout

Assignment 2

PA2: ChocoPy Functions, Types, and Control Flow

In this PA, you’ll design and implement a compiler for all but the heap-manipulating parts of ChocoPy.

There is some support code and examples that can help you:

  • From lecture3, basics of functions and type-checking (you’ll need to extend this, but it has simple starting points) https://github.com/ucsd-cse231-s22/lecture3

You can use any code from lecture directly or for inspiration (like lecture3 or code we publish on Piazza from office hours). It is by no means guaranteed to perfectly match the ChocoPy spec, but it does run and provide some valuable code structure suggestions you will find useful. You might choose to base your implementation on how you approached PA1, or take a different approach entirely based on what you learned. Feel free to use snippets of TypeScript, WASM, and so on that you find online as long as you’re sure it has a permissive license. Link to the source for code you use if you find it. Don’t try to find or use past solutions from last year (it’s bad for your learning, this assignment isn’t exactly the same, etc).

Feel free to discuss the specification, approaches, implementation strategies and tricks, etc. Try to do the programming on your own, but don’t be afraid of seeing someone else’s code or learning a useful TypeScript pattern from them. Focus on doing what you feel is best for one anothers’ learning. If you struggle on this assignment and don’t get all the credit, there will be opportunities to make it up, so focus on developing your skills rather than trying to put together a complete solution you don’t understand.

So that we can test your code and for the examples described at the end, you need to support:

  • A web-based text box with a run button and output for successful compilation and for errors, using npm run build-web
  • A node-main-like interface for running programs from the command line, using npm run build-cli
  • Some way to write unit tests for your code, which runs with npm test

We highly recommend using some repository we’ve given you to get the packages and basics in place, but you’re free to (and responsible for!) making any updates and changes to meet the specification below.

Language Specification

You’ll be implementing the following sub-grammar of ChocoPy:

program := <var_def | func_def>* <stmt>*
var_def := <typed_var> = <literal>
typed_var := <name> : <type>
func_def := def <name>([<typed_var> [, <typed_var>]*]?) [-> <type>]? : <func_body>
func_body := <var_def>* <stmt>+
stmt := <name> = <expr>
      | if <expr>: <stmt>+ [elif <expr>: <stmt>+]? [else: <stmt>+]?
      | while <expr>: <stmt>+
      | pass
      | return <expr>?
      | <expr>
expr := <literal>
      | <name>
      | <uniop> <expr>
      | <expr> <binop> <expr>
      | ( <expr> )
      | <name>([<expr> [, <expr>]*]?)
uniop := not | -
binop := + | - | * | // | % | == | != | <= | >= | < | > | is                 
literal := None
         | True
         | False
         | <number>
type := int | bool
number := 32-bit integer literals

The grammar above is a strict subset of ChocoPy’s. Namely, the grammar above excludes:

  • lists
  • strings
  • classes
  • nested functions
  • for loops
  • global and nonlocal declarations inside a function

Your compiler should have the same output and error messages as ChocoPy for programs in this subset. If you need to test out a program to check its behavior, you can do so at ChocoPy’s web site.

Handin

You will turn in two deliverables, a repository containing your implementation, and an informative README PDF.

Turn in your codebase to pa2-code and your writeup to pa2-pdf on Gradescope.

There is no autograder for this assignment. You are responsible for testing your implementation and ensuring that it matches the ChocoPy reference implementation’s behavior on the relevant sub-language for this PA.

Your README should include the following components:

  1. A description of the representation of values (integers, booleans, and None) in your implementation. Show how the representation is used to print True/False for boolean results rather than numeric output.
  2. Give an example of a program that uses
    • At least one global variable
    • At least one function with a parameter
    • At least one variable defined inside a function

    By linking to specific definitions and code in your implementation, describe where and how those three variables are stored and represented throughout compilation.

  3. Write a ChocoPy program that goes into an infinite loop. What happens when you run it on the web page using your compiler?
  4. For each of the following scenarios, show a screenshot of your compiler running the scenario in the browser:
    1. A program that reports a type error for adding a number and a boolean where one operand is a call expression and the other operand is a variable (for example, f(1) + x)
    2. A program that has a type error in a conditional position (the condition part of an if or while), where that position is a non-global identifier (function parameter or local variable)
    3. A program that with a loop that has multiple iterations, and calls a function on each iteration (it can be the same function)
    4. A program that returns from the body of a loop, and not on the first iteration of the loop
    5. Printing an integer and a boolean
    6. A recursive function that terminates (e.g. no stack overflow)
    7. Two mutually-recursive functions that terminate (e.g. no stack overflow)
  5. Choose one of example (1) or (2) above, show a few lines of code around the line that reports the error and describe the relevant parts of the type-checking environment at that point.

If for any of the above items you don’t complete it, write a short explanation of why it was hard/what you think needs to be done in order to finish it (just a few sentences), which will help us understand your overall learning and give the best feedback.