Honors Compilers

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

description

Honors Compilers. An Introduction to Algol-68S Jan 24 th 2002. Closed-Clause (Block). Closed-clause is basic unit of language begin declarations and statements end begin/end can be replaced by () No termination semicolon Semicolon is a separator NOT a terminator - PowerPoint PPT Presentation

Transcript of Honors Compilers

Page 1: Honors Compilers

Honors CompilersHonors Compilers

An Introduction to Algol-68SAn Introduction to Algol-68S

Jan 24Jan 24thth 2002 2002

Page 2: Honors Compilers

Closed-Clause (Block)Closed-Clause (Block)

Closed-clause is basic unit of languageClosed-clause is basic unit of languagebeginbegin declarations and statements declarations and statements end end

begin/end can be replaced by ()begin/end can be replaced by ()No termination semicolonNo termination semicolonSemicolon is a separator NOT a terminatorSemicolon is a separator NOT a terminatorDeclarations and statements can be freely Declarations and statements can be freely

mixedmixedBegin might be written BEGIN or .beginBegin might be written BEGIN or .beginAll constructs have values (possibly void value)All constructs have values (possibly void value)

Page 3: Honors Compilers

Predefined ModesPredefined Modes

A Mode in A68S is what other A Mode in A68S is what other languages call a type.languages call a type.

Predefined modes arePredefined modes areintintreal real boolboolCharChar

Note that mode names are always Note that mode names are always boldbold

Page 4: Honors Compilers

DeclarationsDeclarations

General form isGeneral form ismode name = valuemode name = valueThis is a constant declaration, the value This is a constant declaration, the value

of the name cannot be changed, it is of the name cannot be changed, it is bound to the given value for the bound to the given value for the remainder of the block.remainder of the block.

ExampleExamplerealreal pi = 3.14159; pi = 3.14159;

Page 5: Honors Compilers

Reference modes (variables)Reference modes (variables)

The mode:The mode:refref amode amodeWhere amode is any mode, e.g. Where amode is any mode, e.g. intint is a is a

mode that represents a value reference-mode that represents a value reference-to-amodeto-amode

You can think of this as a pointerYou can think of this as a pointerOr a variable (A68 unifies the notions of Or a variable (A68 unifies the notions of

variable and pointer)variable and pointer)

Page 6: Honors Compilers

Now we can declare a Now we can declare a variablevariable

To declare an integer variable:To declare an integer variable:ref intref int ivar = ? ivar = ?But what shall we use for ?But what shall we use for ?We need an expression that returns a We need an expression that returns a

value of mode value of mode ref intref intTwo possibilities:Two possibilities:

loc intloc int (local integer allocated on stack (local integer allocated on stack frame)frame)

heap intheap int (integer allocated on GC heap) (integer allocated on GC heap)

Page 7: Honors Compilers

Declaring a VariableDeclaring a Variable

So now we can write the complete So now we can write the complete declaration of an integer variabledeclaration of an integer variableref intref int ivar = ivar = loc intloc int

That’s a bit verbose, so we allow a That’s a bit verbose, so we allow a simpler form that means exactly the simpler form that means exactly the same:same:intint ivar ivarBut you need to remember the long But you need to remember the long

formform

Page 8: Honors Compilers

Assignment StatementsAssignment Statements

Assignment is represented by the Assignment is represented by the symbol := which takes two values.symbol := which takes two values.

The value on the left must be of mode The value on the left must be of mode refref amode where amode is some mode. This amode where amode is some mode. This is also the result of the assignment is also the result of the assignment construct.construct.

The value on the right side must be of The value on the right side must be of mode amode.mode amode.

For example:For example: ivar := 13;ivar := 13; Ivar itself is not changed, but theIvar itself is not changed, but the int int which it which it

references is changed.references is changed.

Page 9: Honors Compilers

CastsCasts

Suppose we have two variables:Suppose we have two variables:intint ivar; ivar; intint jvar jvarCan we write: ivar := jvarCan we write: ivar := jvarNot according to rules so far, since jvar is Not according to rules so far, since jvar is

not of mode int, but of mode ref int.not of mode int, but of mode ref int.But if you have a ref int, how can you get But if you have a ref int, how can you get

an int? You can “cast” to int, i.e. get the an int? You can “cast” to int, i.e. get the value byvalue bydereferencing the pointer:dereferencing the pointer: ivar := ( ivar := (intint) jvar;) jvar;

Page 10: Honors Compilers

Dereferencing CoercionDereferencing Coercion

Consider again the previous assignmentConsider again the previous assignment ivar := ( ivar := (intint) jvar;) jvar;

It would be painful to require the It would be painful to require the explicit cast on every such assignment, explicit cast on every such assignment, so we can leave it out:so we can leave it out: ivar := jvar; ivar := jvar;

One level of One level of refref can be removed from can be removed from the right side automatically if necessarythe right side automatically if necessary

Page 11: Honors Compilers

The Widening CoercionThe Widening Coercion

Suppose we haveSuppose we haveintint Jvar; Jvar;floatfloat Fvar; Fvar;

We can widen an int to float with a We can widen an int to float with a castcast

Fvar := (Fvar := (floatfloat) Jvar;) Jvar;But this coercion is also allowed But this coercion is also allowed

implicitlyimplicitlyFvar := Jvar;Fvar := Jvar;

Page 12: Honors Compilers

OperatorsOperators

Operators like + (addition) can be Operators like + (addition) can be used to form expression values in a used to form expression values in a conventional manner:conventional manner:

3 + 43 + 4This is the addition operator that takes This is the addition operator that takes

two int values, and yields an int value.two int values, and yields an int value.Implicit dereferencing is permitted Implicit dereferencing is permitted here:here:

Ivar + JvarIvar + Jvar

Page 13: Honors Compilers

Value of a blockValue of a block

Statements and Declarations are Statements and Declarations are separated by semicolonsseparated by semicolons

Formally, semicolon is an operator that Formally, semicolon is an operator that “voids” (another coercion, convert to “voids” (another coercion, convert to void) its left argument and returns the void) its left argument and returns the value of its right argument.value of its right argument.

Thus the value of a block is the value of Thus the value of a block is the value of the last statement.the last statement.

Ordinary parenthesization is special Ordinary parenthesization is special casecase

Page 14: Honors Compilers

Control Structures, Control Structures, ConditionalsConditionals

ifif condition condition thenthen statements statementselseelse statements statementsfifi;;

Condition is of mode bool (dereferencing is Condition is of mode bool (dereferencing is allowed here)allowed here)

Coerced modes of both branches must be Coerced modes of both branches must be the same, value is value of appropriate the same, value is value of appropriate statement sequence.statement sequence.

Page 15: Honors Compilers

Control Structures, Control Structures, ConditionalsConditionals

ifif condition condition thenthen statements statementselifelif condition condition thenthen statements statementselifelif condition condition thenthen statements statementselseelse statements statements

fifi;;

Page 16: Honors Compilers

Control Structures, LoopsControl Structures, Loops

forfor index index fromfrom first first byby incr incr toto last lastwhilewhile condition condition dodo statements statementsodod

Can leave out any phraseCan leave out any phraseValue of loop is voidValue of loop is void

Page 17: Honors Compilers

Case StatementsCase Statements

casecase Ivar Ivar inin expr, expr, expr, … expr expr, expr, expr, … expresacesac

The expr here may be, for exampleThe expr here may be, for example

begin … begin … ifif … … fifi; …. ; …. endend;;

Page 18: Honors Compilers

LHS condition (not new LHS condition (not new feature)feature)

Consider:Consider:

ifif Ivar > Jvar Ivar > Jvar thenthen Ivar Ivar elseelse Jvar Jvar fifi := 3; := 3;

Left side has value Ivar or Jvar,Left side has value Ivar or Jvar, i.e. is of mode i.e. is of mode intint, so quite suitable , so quite suitable for for assignment. assignment.

Page 19: Honors Compilers

Multiple assign (not new Multiple assign (not new feature)feature)

Consider:Consider: Ivar := Jvar := 3; Ivar := Jvar := 3;

Which meansWhich means Ivar := (Jvar := 3); Ivar := (Jvar := 3);

Right side has mode Right side has mode ref intref int (Jvar) (Jvar)But can be dereferenced to But can be dereferenced to intintSo assignment to Ivar is fineSo assignment to Ivar is fine

Page 20: Honors Compilers

Renaming (not a new feature)Renaming (not a new feature)

ConsiderConsiderref intref int Ivar = Ivar = loc intloc int := 2; := 2;

ref intref int Jvar = Ivar; Jvar = Ivar; … …

Jvar is a renaming of IvarJvar is a renaming of Ivar

Page 21: Honors Compilers

Pointer processing (not new Pointer processing (not new feature)feature)

ConsiderConsider ref intref int Jvar = Jvar = loc intloc int;; ref ref intref ref int Iptr = Iptr = loc ref intloc ref int;; … …

Iptr := Jvar;Iptr := Jvar; Jvar := 4; Jvar := 4;

Iptr points to a ref int variable that points toIptr points to a ref int variable that points to an int that contains the value 4. an int that contains the value 4.

((refref intint) Iptr := 3; ) Iptr := 3; Jvar now contains 3Jvar now contains 3

Page 22: Honors Compilers

Complex modesComplex modes

Struct modes are like records in Struct modes are like records in Pascal or structs in CPascal or structs in Cmodemode listlist = = structstruct ( (intint val, val, ref listref list

next);next);

Row modes are like arraysRow modes are like arrays mode vectormode vector = [10] = [10] intint;; mode arraymode array = [1:N, 3:M] = [1:N, 3:M] floatfloat;;

Page 23: Honors Compilers

Union modesUnion modes

Free unions (no explicit tag)Free unions (no explicit tag)

modemode intrealintreal = = unionunion ( (intint, , realreal););

ref intrealref intreal irvar = loc irvar = loc intrealintreal;;

irvar := 3; (the uniting coercion)irvar := 3; (the uniting coercion)

irvar := ivar; (dereferencing, then unitingirvar := ivar; (dereferencing, then uniting

Page 24: Honors Compilers

ProceduresProcedures

procproc intmax ( intmax (intint a,b) a,b) intint:: ifif a > b a > b thenthen a a elseelse b b fi;fi;

All parameters passed by value, but All parameters passed by value, but we can pass parameters of type we can pass parameters of type refref amodeamode

procproc apply ( apply (refref [] [] realreal a, a, procproc ( (realreal) ) realreal f): f):forfor I I fromfrom lwblwb a a to upbto upb a a dodo a[i] := f (a[I]); a[i] := f (a[I]);odod