. p.1/23. Today. 1. Questions and discussion from lecture. 2. Type-checking Functions Arrays Records (maybe)

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

Final CSE 131B Spring 2004

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

FORM 2 (Please put your name and form # on the scantron!!!!)

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Lecture 15 CIS 341: COMPILERS

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Announcements. Working on requirements this week Work on design, implementation. Types. Lecture 17 CS 169. Outline. Java Types

CS558 Programming Languages

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

Lexical Considerations

CS111: PROGRAMMING LANGUAGE II

CS313D: ADVANCED PROGRAMMING LANGUAGE

Lexical Considerations

VARIABLES AND TYPES CITS1001

Lecture 16: Static Semantics Overview 1

Lecture #23: Conversion and Type Inference

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

(Not Quite) Minijava

Computer Science II Lecture 1 Introduction and Background

CS558 Programming Languages

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CS558 Programming Languages

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Exam 3 Chapters 7 & 9

Final CSE 131B Spring 2005

CS558 Programming Languages

Goals of this Lecture

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

CS558 Programming Languages

University of Palestine. Mid Exam Total Grade: 100

Types and Type Inference

Reasoning About Imperative Programs. COS 441 Slides 10

Arrays. myints = new int[15];

Introduction to Programming (Java) 4/12

ENERGY 211 / CME 211. Functions

Types, Expressions, and States

1KOd17RMoURxjn2 CSE 20 DISCRETE MATH Fall

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

CSC324 Principles of Programming Languages

CS558 Programming Languages

CS536 Spring 2011 FINAL ID: Page 2 of 11

About this exam review

Today: Revisit some objects. Programming Languages. Key data structure: Dictionaries. Using Dictionaries. CSE 130 : Winter 2009

CS558 Programming Languages. Winter 2013 Lecture 3

COS 126 General Computer Science Spring Written Exam 1

Design Patterns: State, Bridge, Visitor

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

CS61B Lecture #8: Object-Oriented Mechanisms

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

CS 0449 Sample Midterm

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

COMP-520 GoLite Tutorial

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Introduction to Programming using C++

1 Lexical Considerations

Programming Problems 22nd Annual Computer Science Programming Contest

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

Java Programming Basics. COMP 401, Spring 2017 Lecture 2

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

Computer Science 1 Ah

Java Review. Fundamentals of Computer Science

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output

pointers + memory double x; string a; int x; main overhead int y; main overhead

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

Lecture06: Pointers 4/1/2013

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

Syntax Errors; Static Semantics

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

C++ (Non for C Programmer) (BT307) 40 Hours

Compiler Construction Lent Term 2015 Lectures 10, 11 (of 16)

APCS Semester #1 Final Exam Practice Problems

Basic Java Syntax, cont d. COMP 401, Fall 2015 Lecture 3 1/15/2015

Programming Languages

Topics Introduction to Microprocessors

Lecture Topics. Administrivia

Symbol Tables. ASU Textbook Chapter 7.6, 6.5 and 6.3. Tsan-sheng Hsu.

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Next: What s in a name? Programming Languages. Data model in functional PL. What s in a name? CSE 130 : Fall Lecture 13: What s in a Name?

COP4020 Programming Languages. Subroutines and Parameter Passing Prof. Robert van Engelen

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

QUIZ Lesson 4. Exercise 4: Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20, otherwise y is unchanged.

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Lecture Set 4: More About Methods and More About Operators

Lecture Questions. Types (binary expressions, etc). Refactoring, abstraction, better engineering. Testing. Error message generation/suppression.

ENGI 1020 Introduction to Computer Programming J U L Y 5, R E Z A S H A H I D I

Instructions. This exam has 7 questions, worth 10 points each. You have 50 minutes.

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

Department of Computer Science COMP The Programming Competency Test

CS61B Lecture #12. Today: Various odds and ends in support of abstraction.

CSE 374 Programming Concepts & Tools

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Functions in C C Programming and Software Tools

Transcription:

. p.1/23 Today 1. Questions and discussion from lecture. 2. Type-checking Functions Arrays Records (maybe)

. p.2/23 Type-checking functions Aspects: Overview of checks 6, 7 Declaration header Declaration body Procedure call (extra!) How to do overloading

. p.3/23 Check 6 Correct number of arguments Compatible types for non-var Same type for VAR Addressable VAR Distinction between procedures and functions.

. p.4/23 Check 7 Illegal return Missing return (This could be hard...) PROCEDURE f(a : BOOLEAN) : INTEGER; BEGIN IF a THEN RETURN 1; ELSE RETURN 0; END; END f; Does this have a return?

. p.5/23 Illegal returns...but luckily you don t need to do that. Instead, just check for at least one return anywhere: PROCEDURE f(a : BOOLEAN) : INTEGER; BEGIN END f; IF FALSE THEN END; RETURN 1; Sadly, this has a return...

. p.6/23 Function header PROCEDURE f(x: INTEGER): REAL; (*... *) What needs to be checked for each part? What actions need to be performed? Procedure name Return type Parameters

. p.7/23 Header checks Duplicate procedure name? Duplicate parameter name? Valid types? Where can we do this more generally?

. p.8/23 Header actions Create a type (What is included?) Create a STO (What are its data and methods?) Add to the symbol table (How? Which? Checks?)

. p.9/23 Procedure types Return type (optional) Argument types What about VAR parameters? Argument names? Why or why not? How do we represent this? What about type equality tests?

. p.10/23 Procedure STOs Argument names? Local variables? Why keep STO and type separate?

. p.11/23 Function body PROCEDURE f(x: INTEGER): REAL; BEGIN END f; Parts: VAR z: REAL; CONST c = 3; z := c * x; RETURN z; Variable declarations. Body. Nota bene: main is like a function.

. p.12/23 Variable declarations Same as parameters? Add to symbol table (How? Which?)

. p.13/23 Function body Symbol table organization? Symbol lookup procedure? Globals, locals, and shadowing Recursive procedures Return statement: Check type and valuelessness. Check for missing return in functions.

. p.14/23 Function call PROCEDURE f(x: INTEGER): REAL; (*... *) VAR x : REAL; BEGIN x := f(5); END. Parameter passing Return value Gramma perversions

. p.15/23 Return value In Oberon, statements are not expressions = procedures are not functions. C int f(int); a = f(3); // expression f(3); Oberon // statement PROCEDUREˆ f(x: INTEGER): INTEGER; a := f(3); (* expression *) f(3); (* does not compute *)

. p.16/23 Argument passing: non-var Arguments must be assignment-compatible with parameters. PROCEDURE f(x: REAL); BEGIN (*... *) END; VAR x : INTEGER; VAR y : REAL; f(y); (* 1 *) f(x); (* 2 *) f(3); (* 3 *) f(3.5); (* 4 *)

. p.17/23 Argument passing: non-var Why assignment-compatible? Because there s an assignment! PROCEDURE f(x: REAL); BEGIN (* use x *) END; PROCEDURE f(init); BEGIN x := init; (* use x *) END;

. p.18/23 Argument passing: VAR Arguments must: have the same type (why not compatible type?) be valid designators (what s this?) PROCEDUREˆ f(var x: REAL); VAR r : REAL; VAR i : INTEGER; f(r); (* 1 *) f(i); (* 2 *) f(3.5); (* 3 *)

. p.19/23 Argument passing: VAR PROCEDURE swap(var x, y: REAL); VAR t : REAL; BEGIN t := x; x := y; y := t; END f; VAR r,s : REAL; VAR i,j : INTEGER; BEGIN swap(r,s); (* 1 *) swap(r,r); (* 2 *) swap(r,3.5); (* 3 *) swap(i,r); (* 4 *) END.

. p.20/23 VAR hints What happens (in memory) for float x = 5? seti %i0, 5 itof %f0, %i0 st x, %f0 What is being assigned to? Relation to C pointers? Java references?

. p.21/23 Ew... No-argument calls: x := f; x := f(); Procedures vs. functions: f(1,2,3); x := f(1,2,3);

. p.22/23 Aside: overloading How are overloaded STOs different? Multiple types? Yes, and multiple operations! How does op.compatible(args) change? Check for any compatible operation. What order? (think about + s return type) Could we overload on return type?

. p.23/23 Review: operator types What are the types of the following operators? How would each be declared as a procedure/function? + (addition) := (assignment) [] (array subscript) [] := (assignment to array subscript)

. p.24/23 Array declarations They re easy! VAR A : ARRAY 10 OF REAL; What are the parts of this type? Element type. Length (why is this good or bad?)

. p.25/23 Array uses VAR A : ARRAY 10 OF REAL; A[1] := A[2]; What are the types? How is A[1] different from A[2]? Can A[i] be passed to a procedure? As VAR?

. p.26/23 Arrays vs. Functions VAR A : ARRAY 10 OF REAL; A[1] := A[2]; What would A s type be as a function? Can we assign to A[i]? What about to procedure results?