Honors Compilers An Introduction to Algol-68S Jan 24 th 2002.

Slides:



Advertisements
Similar presentations
Modern Programming Languages, 2nd ed.
Advertisements

ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Chapter 6 Type Checking. The compiler should report an error if an operator is applied to an incompatible operand. Type checking can be performed without.
Programming Languages and Paradigms
Programming Languages and Paradigms The C Programming Language.
Parameter Passing. Variables: lvalues and rvalues In the assignment statement “X=Y”, variables X and Y are being used in different ways. Y is being used.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Kernighan/Ritchie: Kelley/Pohl:
ECE 103 Engineering Programming Chapter 11 One Minute Synopsis Herbert G. Mayer, PSU CS Status 7/1/2014.
COEN Expressions and Assignment
Copyright © by Curt Hill Expressions and Assignment Statements Part 2.
Arrays in CCS-2301, B-Term Arrays in C (including a brief introduction to pointers) CS-2301, System Programming for Non-Majors (Slides include materials.
Objective Revision of data structures and their implementation.
ISBN Chapter 7 Expressions and Assignment Statements.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Arrays in CCS-2301 B-term Arrays in C (with a brief Introduction to Pointers) CS-2301, System Programming for Non-majors (Slides include materials.
PSUCS322 HM 1 Languages and Compiler Design II Parameter Passing Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Parameter Passing. Expressions with lvalues and rvalues An expression has an lvalue/rvalue if it can be placed on the left/right side of an assignment.
ISBN Chapter 7 Expressions and Assignment Statements.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Expressions, Evaluation and Assignments
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
CS 363 Comparative Programming Languages Expressions and Assignment Statements.
Lecture 2 Arrays, Pointers, and Structures. Objective In this chapter, we will discuss several concepts: Arrays (first-class arrays, using vector) Strings.
Runtime Environments Compiler Construction Chapter 7.
Compiler Construction
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Basic Semantics Associating meaning with language entities.
CSI 3120, Expressions and assignment, page 1 Expressions and assignment Points for discussion Arithmetic expressions Overloading Logical expressions Assignment.
Programming Languages and Paradigms Imperative Programming.
Ch. 5 Ch. 51 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (more notes) Dr. Carter Tiernan.
Arithmetic Expressions
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Chapter 7 © 1998 by Addison -Wesley Longman, Inc Arithmetic Expressions - Their evaluation was one of the motivations for the development of the.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Chapter 7 Expressions and Assignment Statements. Outline Introduction Arithmetic Expressions Overloaded Operators Type Conversions Assignment Statements.
LOOP & Type Conversion. do – while Loop In the while loop, the test expression is evaluated at the beginning of the loop. If the test condition is false.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Arrays in CCIS 1057 Fall Arrays in C (with a brief Introduction to Pointers) CIS 1057 Computer Programming in C Fall 2013 (Slides include materials.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX.
1 CS Programming Languages Class 09 September 21, 2000.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
Ch. 5 Ch. 51 jcmt Summer 2003Programming Languages CSE3302 Programming Languages (more notes) Summer 2003 Dr. Carter Tiernan.
March 31, ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Programming Languages (ICE 1341) Lecture #11 Programming Languages (ICE 1341)
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
C SC 520 Principles of Programming Languages 1 Principles of Programming Languages Lecture 10 Variables and Assignment.
Constructs for Data Organization and Program Control, Scope, Binding, and Parameter Passing. Expression Evaluation.
Names, Scope, and Bindings Programming Languages and Paradigms.
Learners Support Publications Constructors and Destructors.
Windows Programming Lecture 03. Pointers and Arrays.
Constructors and Destructors
7.2 Arithmetic Expressions
Chapter 7: Expressions and Assignment Statements
Names and Attributes Names are a key programming language feature
Programming Languages and Paradigms
Principles of programming languages 4: Parameter passing, Scope rules
Chapter 7: Expressions and Assignment Statements
Subprograms The basic abstraction mechanism.
Constructors and Destructors
Type Conversion It is a procedure of converting one data type values into another data type In C programming language we are having two types of type conversion.
Programming Languages and Paradigms
PRESENTED BY ADNAN M. UZAIR NOMAN
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Honors Compilers An Introduction to Algol-68S Jan 24 th 2002

Closed-Clause (Block) Closed-clause is basic unit of language Closed-clause is basic unit of language begin declarations and statements end begin declarations and statements end begin/end can be replaced by () begin/end can be replaced by () No termination semicolon No termination semicolon Semicolon is a separator NOT a terminator Semicolon is a separator NOT a terminator Declarations and statements can be freely mixed Declarations and statements can be freely mixed Begin might be written BEGIN or.begin Begin might be written BEGIN or.begin All constructs have values (possibly void value) All constructs have values (possibly void value)

Predefined Modes A Mode in A68S is what other languages call a type. A Mode in A68S is what other languages call a type. Predefined modes are Predefined modes are int int real real bool bool Char Char Note that mode names are always bold Note that mode names are always bold

Declarations General form is General form is mode name = value mode name = value This is a constant declaration, the value of the name cannot be changed, it is bound to the given value for the remainder of the block. This is a constant declaration, the value of the name cannot be changed, it is bound to the given value for the remainder of the block. Example Example real pi = ; real pi = ;

Reference modes (variables) The mode: The mode: ref amode ref amode Where amode is any mode, e.g. int is a mode that represents a value reference-to-amode Where amode is any mode, e.g. int is a mode that represents a value reference-to-amode You can think of this as a pointer You can think of this as a pointer Or a variable (A68 unifies the notions of variable and pointer) Or a variable (A68 unifies the notions of variable and pointer)

Now we can declare a variable To declare an integer variable: To declare an integer variable: ref int ivar = ? ref int ivar = ? But what shall we use for ? But what shall we use for ? We need an expression that returns a value of mode ref int We need an expression that returns a value of mode ref int Two possibilities: Two possibilities: loc int (local integer allocated on stack frame) loc int (local integer allocated on stack frame) heap int (integer allocated on GC heap) heap int (integer allocated on GC heap)

Declaring a Variable So now we can write the complete declaration of an integer variable So now we can write the complete declaration of an integer variable ref int ivar = loc int ref int ivar = loc int That’s a bit verbose, so we allow a simpler form that means exactly the same: That’s a bit verbose, so we allow a simpler form that means exactly the same: int ivar int ivar But you need to remember the long form But you need to remember the long form

Assignment Statements Assignment is represented by the symbol := which takes two values. Assignment is represented by the symbol := which takes two values. The value on the left must be of mode ref amode where amode is some mode. This is also the result of the assignment construct. The value on the left must be of mode ref amode where amode is some mode. This is also the result of the assignment construct. The value on the right side must be of mode amode. The value on the right side must be of mode amode. For example: For example: ivar := 13; ivar := 13; Ivar itself is not changed, but the int which it references is changed. Ivar itself is not changed, but the int which it references is changed.

Casts Suppose we have two variables: Suppose we have two variables: int ivar; int jvar int ivar; int jvar Can we write: ivar := jvar Can we write: ivar := jvar Not according to rules so far, since jvar is not of mode int, but of mode ref int. Not according to rules so far, since jvar is not of mode int, but of mode ref int. But if you have a ref int, how can you get an int? You can “cast” to int, i.e. get the value by dereferencing the pointer: ivar := (int) jvar; But if you have a ref int, how can you get an int? You can “cast” to int, i.e. get the value by dereferencing the pointer: ivar := (int) jvar;

Dereferencing Coercion Consider again the previous assignment ivar := (int) jvar; Consider again the previous assignment ivar := (int) jvar; It would be painful to require the explicit cast on every such assignment, so we can leave it out: ivar := jvar; It would be painful to require the explicit cast on every such assignment, so we can leave it out: ivar := jvar; One level of ref can be removed from the right side automatically if necessary One level of ref can be removed from the right side automatically if necessary

The Widening Coercion Suppose we have int Jvar; float Fvar; Suppose we have int Jvar; float Fvar; We can widen an int to float with a cast Fvar := (float) Jvar; We can widen an int to float with a cast Fvar := (float) Jvar; But this coercion is also allowed implicitly Fvar := Jvar; But this coercion is also allowed implicitly Fvar := Jvar;

Operators Operators like + (addition) can be used to form expression values in a conventional manner: Operators like + (addition) can be used to form expression values in a conventional manner: This is the addition operator that takes two int values, and yields an int value. Implicit dereferencing is permitted here: Ivar + Jvar This is the addition operator that takes two int values, and yields an int value. Implicit dereferencing is permitted here: Ivar + Jvar

Value of a block Statements and Declarations are separated by semicolons Statements and Declarations are separated by semicolons Formally, semicolon is an operator that “voids” (another coercion, convert to void) its left argument and returns the value of its right argument. Formally, semicolon is an operator that “voids” (another coercion, convert to void) its left argument and returns the value of its right argument. Thus the value of a block is the value of the last statement. Thus the value of a block is the value of the last statement. Ordinary parenthesization is special case Ordinary parenthesization is special case

Control Structures, Conditionals if condition then statements else statements fi; if condition then statements else statements fi; Condition is of mode bool (dereferencing is allowed here) Condition is of mode bool (dereferencing is allowed here) Coerced modes of both branches must be the same, value is value of appropriate statement sequence. Coerced modes of both branches must be the same, value is value of appropriate statement sequence.

Control Structures, Conditionals if condition then statements elif condition then statements elif condition then statements else statements if condition then statements elif condition then statements elif condition then statements else statements fi; fi;

Control Structures, Loops for index from first by incr to last while condition do statements od for index from first by incr to last while condition do statements od Can leave out any phrase Can leave out any phrase Value of loop is void Value of loop is void

Case Statements case Ivar in expr, expr, expr, … expr esac case Ivar in expr, expr, expr, … expr esac The expr here may be, for example begin … if … fi; …. end; The expr here may be, for example begin … if … fi; …. end;

LHS condition (not new feature) Consider: if Ivar > Jvar then Ivar else Jvar fi := 3; Consider: if Ivar > Jvar then Ivar else Jvar fi := 3; Left side has value Ivar or Jvar, i.e. is of mode int, so quite suitable for assignment. Left side has value Ivar or Jvar, i.e. is of mode int, so quite suitable for assignment.

Multiple assign (not new feature) Consider: Ivar := Jvar := 3; Consider: Ivar := Jvar := 3; Which means Ivar := (Jvar := 3); Which means Ivar := (Jvar := 3); Right side has mode ref int (Jvar) Right side has mode ref int (Jvar) But can be dereferenced to int But can be dereferenced to int So assignment to Ivar is fine So assignment to Ivar is fine

Renaming (not a new feature) Consider ref int Ivar = loc int := 2; ref int Jvar = Ivar; … Consider ref int Ivar = loc int := 2; ref int Jvar = Ivar; … Jvar is a renaming of Ivar Jvar is a renaming of Ivar

Pointer processing (not new feature) Consider ref int Jvar = loc int; ref ref int Iptr = loc ref int; … Consider ref int Jvar = loc int; ref ref int Iptr = loc ref int; … Iptr := Jvar; Jvar := 4; Iptr := Jvar; Jvar := 4; Iptr points to a ref int variable that points to an int that contains the value 4. Iptr points to a ref int variable that points to an int that contains the value 4. (ref int) Iptr := 3; (ref int) Iptr := 3; Jvar now contains 3 Jvar now contains 3

Complex modes Struct modes are like records in Pascal or structs in C Struct modes are like records in Pascal or structs in C mode list = struct (int val, ref list next); Row modes are like arrays mode vector = [10] int; mode array = [1:N, 3:M] float; Row modes are like arrays mode vector = [10] int; mode array = [1:N, 3:M] float;

Union modes Free unions (no explicit tag) Free unions (no explicit tag) mode intreal = union (int, real); ref intreal irvar = loc intreal; irvar := 3; (the uniting coercion) irvar := ivar; (dereferencing, then uniting

Procedures proc intmax (int a,b) int: if a > b then a else b fi; proc intmax (int a,b) int: if a > b then a else b fi; All parameters passed by value, but we can pass parameters of type ref amode All parameters passed by value, but we can pass parameters of type ref amode proc apply (ref [] real a, proc (real) real f): for I from lwb a to upb a do a[i] := f (a[I]); od proc apply (ref [] real a, proc (real) real f): for I from lwb a to upb a do a[i] := f (a[I]); od