in ,

ChocoPy: A Programming Language for Compilers Courses, Hacker News


ChocoPyis a programming language designed for classroom use in undergraduate compilers courses. ChocoPy is a restricted subset ofPython 3, which can easily be compiled to a target such asRISC-V. The language isfully specified using formal grammar, typing rules, and operational semantics. ChocoPy is used to teachCS 164 at UC Berkeley. ChocoPy has been designed byRohan PadhyeandKoushik Sen, with substantial contributions fromPaul Hilfinger.

New!A short paper on ChocoPy will appear atSPLASH-E ’19. Check out thepreprint.

At a glance, ChocoPy is:

  • Familiar: ChocoPy programs can be executed directly in a Python (3.6 ) interpreter. ChocoPy programs can also be edited using standard Python syntax highlighting.
  •     

  • Safe: ChocoPy uses Python 3.6typeannotationsto enforce static type checking. Thetype systemsupports nominal subtyping.
  •     

  • Concise: A full compiler for ChocoPy be implemented in about 12 weeks by undergraduate students of computer science. This can be a hugely rewarding exercise for students.
  •     

  • Expressive: One can write non-trivial ChocoPy programs using lists, classes, and nested functions. Suchlanguage featuresalso lead to interesting implications for compiler design .

Bonus: Due to static type safety and ahead-of-time compilation, most student implementations outperform the reference Python implementation on non-trivial benchmarks.

Try ChocoPy

# Search in a list def contains (items: [int], x: int) ->bool:     i: int=0     while i

Teaching Resources

The following resources are available for conducting a compilers course with ChocoPy:

  • Alanguage reference manual, complete with formal grammar, typing rules, and operational semantics.
  •     

  • Animplementation guidedescribing calling conventions and runtime support in RISC-V.
  •     

  • A Java-based framework for conducting three Programming Assignments (PAs):         
    1. Lexing and Parsing ChocoPy into Abstract Syntax Trees (ASTs)
    2.             

    3. Semantic analysis and type checking on ASTs
    4.             

    5. Code generation from typed ASTs to (RV) IM assembly
    6.         

            The framework usesJSONas an intermediate representation for piping results between compiler stages. The framework includes auto-grading support. All artifacts are platform independent and only require Java 8 and Apache Maven.

  •     

  • A modified version of theVenus simulatorfor executing RISC-V programs. Venus can be deployed to the web (for online debugging) or compiled to the JVM (for testing and auto-grading).
  •     

  • A Java-based reference compiler that compiles ChocoPy programs toauto-documentedRISC-V assembly. Students have access to a binary version of this compiler, for use in debugging their own assignments. The reference compiler also powers the online examples on this web page. The source code of the reference compilercan be made available to instructors.
  •     

Language Features

Here are some code examples demonstrating various features of ChocoPy. Edit and compile the programs below to observe the generated RISC-V assembly, which can itself be executed online. A detailed description of the ChocoPy language is available in the officiallanguage reference manual.

Static Type Checking

# A broken program def is_even (x: int) ->bool:     if x% 2==1:         return 0 # FIXME     else:         return True  print (is_even ("3")) # FIXME

Nested functions with global / nonlocal variables

# Compute x ** y def exp (x: int, y: int) ->int:     a: int=0     global invocations # Count calls to this function      def f (i: int) ->int:         nonlocal a         def geta () ->int:             return a         if i

Lists, classes, and dynamic dispatch

# A resizable list of integers class Vector (object):     # Attributes     items: [int]=None     size: int=0      # Constructor     def __init __ (self: "Vector"):         self.items=[0]      # Returns current capacity     def capacity (self: "Vector") ->int:         return len (self.items)      # Increases capacity of vector by one element     def increase_capacity (self: "Vector") ->int:         self.items=self.items   [0]         return self.capacity ()      # Appends one item to end of vector     def append (self: "Vector", item: int):         if self.size==self.capacity ():             self.increase_capacity ()          self.items [self.size]=item         self.size=self.size   1  # A faster (but more memory-consuming) implementation of vector Class DoublingVector (Vector):     doubling_limit: int=16      # Overriding to do fewer resizes     def increase_capacity (self: "DoublingVector") ->int:         if (self.capacity ()  ChocoPydoes not supportmodules and imports, higher order functions, native dictionaries, and exceptions.  

Want to use ChocoPy to run your own compilers course? Send an email to ([email protected]).

  

Brave Browser
Read More
Payeer

What do you think?

Leave a Reply

Your email address will not be published.

GIPHY App Key not set. Please check settings

Cubed wombat poop, why your left scrotum runs hot, among Ig Nobel winners, Ars Technica

Cubed wombat poop, why your left scrotum runs hot, among Ig Nobel winners, Ars Technica

Toyota Is Trying to Figure Out How to Make a Car Run Forever, Hacker News