COMP520 - GoLite Type Checking Specification

Similar documents
COMP520 - GoLite Type Checking Specification

COMP520 - GoLite Type Checking Specification

COMP-520 GoLite Tutorial

Lexical Considerations

Lexical Considerations

1 Lexical Considerations

CA4003 Compiler Construction Assignment Language Definition

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

The PCAT Programming Language Reference Manual

Decaf Language Reference

X Language Definition

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Begin at the beginning

Operators in java Operator operands.

(Not Quite) Minijava

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

CS115 - Module 10 - General Trees

GOLD Language Reference Manual

CMSC 330: Organization of Programming Languages. Rust Basics

Principles of Programming Languages

Slang Reference Manual

Introduction to Programming Using Java (98-388)

IC Language Specification

XQ: An XML Query Language Language Reference Manual

TML Language Reference Manual

M/s. Managing distributed workloads. Language Reference Manual. Miranda Li (mjl2206) Benjamin Hanser (bwh2124) Mengdi Lin (ml3567)

Lecture 4 Abstract syntax

CS115 - Module 8 - Binary trees

Chapter 20: Binary Trees

Project Compiler. CS031 TA Help Session November 28, 2011

FRAC: Language Reference Manual

Intermediate Code Generation

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

Programming for Engineers Iteration

ARG! Language Reference Manual

The SPL Programming Language Reference Manual

Decaf Language Reference Manual

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

1 Introduction. 2 Definition of the IR. 2.1 Types. CS134b Lab #3 January 29, 2001 Due February 12, 2001

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Type checking. Jianguo Lu. November 27, slides adapted from Sean Treichler and Alex Aiken s. Jianguo Lu November 27, / 39

GAWK Language Reference Manual

Summary of Go Syntax /

C Syntax Out: 15 September, 1995

These are reserved words of the C language. For example int, float, if, else, for, while etc.

GBIL: Generic Binary Instrumentation Language. Language Reference Manual. By: Andrew Calvano. COMS W4115 Fall 2015 CVN

Computer System and programming in C

The Typed Racket Guide

Semantic Analysis Type Checking

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Introduction to Programming (Java) 2/12

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

Implementing a VSOP Compiler. March 20, 2018

The role of semantic analysis in a compiler

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Sprite an animation manipulation language Language Reference Manual

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Language Reference Manual simplicity

Side note: Tail Recursion. Begin at the beginning. Side note: Tail Recursion. Base Types. Base Type: int. Base Type: int

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial

Semantic actions for declarations and expressions

Semantic actions for declarations and expressions. Monday, September 28, 15

Implementing a VSOP Compiler. March 12, 2018

Review of the C Programming Language for Principles of Operating Systems

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Course Outline Introduction to C-Programming

More On Syntax Directed Translation

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

CS 1428 Review. CS 2308 :: Spring 2016 Molly O Neil

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

1.3b Type Conversion

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

CS558 Programming Languages

CMSC 330: Organization of Programming Languages. Operational Semantics

Briefly describe the purpose of the lexical and syntax analysis phases in a compiler.

Fundamental of Programming (C)

CS558 Programming Languages

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Please refer to the turn-in procedure document on the website for instructions on the turn-in procedure.

The Decaf Language. 1 Lexical considerations

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

VENTURE. Section 1. Lexical Elements. 1.1 Identifiers. 1.2 Keywords. 1.3 Literals

Week 5 Tutorial Structural Induction

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

Fundamentals of Programming

Recap: Functions as first-class values

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

CSC 1351: Quiz 6: Sort and Search

Module 9: Trees. If you have not already, make sure you. Read How to Design Programs Sections 14, 15, CS 115 Module 9: Trees

Java is an objet-oriented programming language providing features that support

CS558 Programming Languages

Compilers. Compiler Construction Tutorial The Front-end

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Static Checking and Type Systems

Review of the C Programming Language

Transcription:

COMP520 - GoLite Type Checking Specification Vincent Foley February 26, 2015 1 Declarations Declarations are the primary means of introducing new identifiers in the symbol table. In Go, top-level declarations can come in any order; in GoLite, we will require that identifiers be declared before they are used. This will prevent mututally recursive functions, however it should make the type checker implementation easier. The symbol table should start with a few pre-declared mappings. These mappings may be shadowed. Identifier true false Type bool bool 1.1 Variable declarations var x int Adds the mapping x -> int to the symbol table. var x int = expr If expr is well-typed and its type is int, the mapping x -> int is added to the symbol table. var x = expr If expr is well-typed and its type is T, the mapping x -> T is added to the symbol table. In all three cases, if x is already declared in the current scope, an error is raised. If x is already declared, but in an outer scope, the new x -> T mapping will shadow the previous mapping. Note: In Go, it is an error to declare a local variable and not use it. In GoLite, we will allow unused variables. (If you wanted to comply with the Go specification, how would you make sure that all locals are used?) 1.2 Type declarations type num int 1

Adds the type mapping num -> int to the symbol table. If num is already declared in the current scope, an error is raised. If num is already declared, but in an outer scope, the new num -> int type mapping will shadow the previous mapping. 1.3 Function declarations func f(p1 T1, p2 T2,..., pn Tn) Tr { Given the declaration for f above, the mapping f -> (T1 * T2 *... * Tn -> Tr) is added to the symbol table. If f is already declared in the current scope (i.e. the global scope since we don t have nested functions), an error is raised. For each formal parameter pi, the mapping pi -> Ti is added to the symbol table. If two parameters have the same name, an error is raised. A formal parameter may have the same name as the function itself. A formal parameter or a variable or type declared in the body of the function may have the same name as the function. // Valid func f(f int) {... // Invalid func f(f int) { var f float64... // Redeclares f (the formal parameter) A function declaration type checks if the statements of its body type check. Additionally, for functions that return a value, there should be a well-typed return statement on every execution path. Hint: you may want to add a new weeding pass to check for return statements on every path. 2 Statements Type checking of a statement involves making sure that all its children are well-typed. A statement does not have a type. 2.1 Empty statement The empty statement is trivially well-typed. 2

2.2 break and continue The break and continue statements are trivially well-typed. 2.3 Expression statement expr An expression statement is well-typed if its expression child is well-typed. 2.4 return return A return statement with no expression is well-typed if the enclosing function has no return type. return expr A return statement with an expression is well-typed if its expression is well-typed and the type of this expression is the same as the return type of the enclosing function. 2.5 Short declaration v1, v2,..., vn := e1, e2,..., en A short declaration type checks if: All the expressions on the right-hand side are well-typed; At least one variable on the left-hand side is not declared in the current scope; The variables already declared in the current scope are assigned expressions of the same type. E.g. if the symbol table contains the mapping v1 -> T1, then it must be the case that typeof(e1) = T1. If these conditions are met, the mappings v1 -> typeof(e1), v2 -> typeof(e2),..., vn -> typeof(en) are added to symbol table. 2.6 Declarations Declaration statements obey the rules described in the previous section. 2.7 Assignment v1, v2,..., vn = e1, e2,..., en An assigment statement type checks if: All the expressions on the left-hand side are well-typed; 3

All the expressions on the right-hand side are well-typed; For every pair of lvalue/expression, typeof(vi) = typeof(ei). 2.8 Op-assignment v op= expr An op-assignment statement type checks if: The expression on the left-hand side is well-typed; The expression on the right-hand side is well-typed; The operator op accepts two arguments of types typeof(v) and typeof(expr) and return a value of type typeof(v). 2.9 Block { A block type checks if its statements type check. A block opens a new scope in the symbol table. 2.10 print and println print(expr) println(expr1, expr2) A print statement type checks if all its expressions are well-typed. 2.11 For loop for { An infinite for loop type checks if its body type checks. The body opens a new scope in the symbol table. for expr { A "while" loop type checks if: Its expression is well-typed and has type bool; 4

The statements type check. The body opens a new scope in the symbol table. for init; expr; post { A three-part for loop type checks if: init type check; expr is well-typed and has type bool; post type checks; the statements type check. The init statement can shadow variables declared in the same scope as the for statement. The body opens a new scope in the symbol table and can redeclare variables declared in the init statement. 2.12 If statement if init; expr { // then statements else { // else statements An if statement type checks if: init type checks; expr is well-typed and has type bool; The statements in the first block type check; The statements in the second block type check. The init statement can shadow variables declared in the same scope as the for statement. The bodies both open a new scope in the symbol table and can redeclare variables declared in the init statement. 2.13 Switch statement switch init; expr { case e1, e2,..., en: default: 5

A switch statement with an expression type checks if: init type checks; expr is well-typed; The expressions e1, e2,..., en are well-typed and have the same type as expr; The statements under the different alternatives type check. switch init; { case e1, e2,..., en: default: A switch statement without an expression type checks if: init type checks; The expressions e1, e2,..., en are well-typed and have type bool; The statements under the different alternatives type check. 3 Expressions Type checking of an expression involves making sure that all its children are well-typed and also giving a type to the expression itself. This type can should be stored (either in the AST itself or in an auxiliary data structure) as it will be queried by the expression s parent. 3.1 Literals 42 // int 1.62 // float64 X // rune "comp520" // string The different literals have obvious types: Integer literals have type int Float literals have type float64 Rune literals have type rune String literals have type string 6

3.2 Identifiers sum The type of an identifier is obtained by querying the symbol table. If the identifier cannot be found in the symbol table, an error is raised. 3.3 Unary exression unop expr A unary expression is well-typed if its sub-expression is well-typed and has the appropriate type for the operation. In GoLite, the type of a unary expression is always the same as its child. Unary plus: expr must be numeric (int, float64, rune) Arithmetic negation: expr must be numeric (int, float64, rune) Logical negation: expr must be a bool Bitwise negation: expr must be an integer (int, rune) 3.4 Binary expressions expr binop expr A binary expression is well-typed if its sub-expressions are well-typed, are of the same type and have appropriate types for the operation. The type of the binary operation is detailed in the table below. The Go specification (links below) explains which types are ordered, comparable, numeric, integer, etc. 7

arg1 op arg2 result bool bool bool bool && bool bool comparable == comparable bool comparable!= comparable bool ordered < ordered bool ordered <= ordered bool ordered > ordered bool ordered >= ordered bool numeric or string + numeric or string numeric or string numeric - numeric numeric numeric * numeric numeric numeric / numeric numeric numeric % numeric numeric integer integer integer integer & integer integer integer << integer integer integer >> integer integer integer &ˆ integer integer integer ˆ integer integer http://golang.org/ref/spec#arithmetic_operators http://golang.org/ref/spec#comparison_operators http://golang.org/ref/spec#logical_operators 3.5 Function call expr(arg1, arg2,..., argn) A function call is well-typed if: expr is well-typed and has function type (T1 * T2 *... * Tn) -> Tr; arg1, arg2,..., argn are well-typed and have types T1, T2,..., Tn respectively. The type of a function call is Tr. 3.6 Indexing expr[index] Indexing into a slice or an array is well-typed if: expr is well-typed and has type Slice<T> or Array<int, T>; index is well-typed and has type int. 8

The result of the indexing expression is T. Note: The Go specification states that the compiler should to report an error if the index of an array (not of a slice) evaluates to a statically-known constant that is outsides the bounds of the array. You do not have to implement this at compile-time in GoLite, instead we ll do the check at runtime. 3.7 Field selection expr.id Selecting a field in a struct is well-typed if: expr is well-typed and has type S; S is a struct type that has a field named id. The type of a field selection expression is the type associated with id in the struct definition. 3.8 append append(id, expr) An append expression is well-typed if: id is found in the symbol table and maps to a Slice<T>; expr is well-typed and has type T. The type of append is Slice<T>. 3.9 Type cast type(expr) A type cast expression is well-typed if: type is a int, float64, bool, rune, or a type alias that maps to one of those four; expr is well-typed and has a type listed in the previous bullet point. The type of a type cast expression is type. 9