UP | HOME

SimpleC language overview
Lecture 6

Table of Contents

Review

Questions about the last class?

Quiz

What is the utility of the visitor pattern for compilers?

Quiz Discussion

SimpleC programs

program

  • A list of declarations or definitions
  • Declarations may be variables or functions
    • Distinguished by its type

declaration

  • Symbol name followed by type
  • Only one declaration per name per scope
    • Unique names in global scope and function scope
  • Function local names may shadow global scope
    • Reference to name first checks nearest scope

definitions

  • Function definitions
  • Declare parameter and return types
  • Body contains declarations first, then statements
  • Definitions may define already-declared function symbols

types

  • primitive: int, bool, string
  • compound: function (type, type, …) -> return_type
    • Corresponds to C function pointer when used as a value

SimpleC statements

assignment

  • x = 1 + y
    • First computes RHS
    • Then stores resulting value in memory associated with x
    • x must be declared already
    • Types of x and RHS must match

expression statement

  • f(1);
    • An expression by itself
    • Useful for function calls where return value is irrelevant
      • (Function calls are expressions in this language)
int x;
x*y;
typedef int x;
x*y;

return

  • return 0;
    • Resumes control to caller of function
    • Replacing function call with value of return expression
    • Type of return expression must match function return type

while

  • while (condition) statement
    • condition expression must evaluate to a bool
    • while takes only a single statement
      • (compound statement allow multiple statements)
    • Checks condition for truth, then executes statement
    • Executes statement until condition is no longer true

conditional statements (if-then, if-then-else)

  • if (condition) statement or if (condition) statement else statement
    • Evaluates condition first, when positive, execute if-branch statements
    • When negative skips if-branch (and optionally goes to else-branch if there)
    • Produces a Void type, i.e., has no resulting value

compound statement is a single statement syntactically

goto fail bug

compound statement

  • { statement* }
  • Executes multiple statements in sequence
  • Useful for control-flow statements to execute many statements

SimpleC expressions

  • Expressions are used in certain statements
  • Some statements enforce type requirements on statements
    • E.g., bool on while condition

function call

  • f(1)
    • Transfers control to function's statements
      • Calling function's local state is frozen
    • Replaces call with return value of function

variable usage

  • x
    • Replaces value with current value associated with the symbol

arithmetic operations

  • add +, subtract -, multiply *, divide /
    • Only operates on int types
    • Passes through semantics to C
      • Overflows possible

relational operations

  • greater than > (or equal to >=), less than < (or equal to <=), equals ==, not equals !=
    • Left and right operators must match in type
    • Resulting value is a bool
    • Passes through semantics to C

boolean operators

  • and &&, or ||, not !
    • Operands are bool
    • Produces a bool
    • Passes through semantics to C

literals

  • numbers
  • strings

Setting up your git repo

Project

(1 week)

  • Download the skeleton repository
  • Setup your repo replacing NAME with your own (using SSH keys recommended)

    git clone git@github.com:cop5621spring23/project-NAME.git
    
  • Untar the skeleton to the root of your repository replacing NAME with your own

    tar -C project-NAME/ -xvf path/to/simplec.tar
    
  • Add the skeleton files to the repo replacing NAME with your own

    cd project-NAME
    git add ./
    git commit -m "Added compiler skeleton"
    git push
    
  • Make sure that the compiler template builds and runs

    # make sure your environment is setup
    source configure.sh
    # compile the code template
    cd src/simplec
    make
    java Compiler ../../tests/example.simplec
    
  • Create two example SimpleC programs, and add them to your repository's tests/ folder.

Homework

(1 week)

  • Submit the text parse trees of the two example test programs you created for the project in your tests/ folder.

Reading

(due next class)

Author: Paul Gazzillo

Created: 2023-04-13 Thu 14:59