/
Chapter 6: From an assignment core to Java Chapter 6: From an assignment core to Java

Chapter 6: From an assignment core to Java - PowerPoint Presentation

test
test . @test
Follow
346 views
Uploaded On 2019-06-21

Chapter 6: From an assignment core to Java - PPT Presentation

Xinming Simon Ou CIS 505 Programming Languages Kansas State University Fall 2010 1 Core imperative language 2 P Program D Declaration T TypeStructure C Command E Expression ID: 759562

int var array struct var int struct array howmany database proc table size time class balance index idnum tick 100 module clock

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Chapter 6: From an assignment core to Ja..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Chapter 6: From an assignment core to Java

Xinming (Simon) OuCIS 505: Programming LanguagesKansas State UniversityFall 2010

1

Slide2

Core imperative language

2

P : Program

D : Declaration

T :

TypeStructure

C : Command

E : Expression

L :

NameExpression

N : Numeral

I : Identifier

P ::= D ; C

E ::= N | L | E1 + E2 | E1 - E2 | E1 > E2 | E1 == E2 | not E |

new T

C ::= L = E | print L | C1 ; C2 | if E { C1 } else { C2 } | while E { C }

D ::=

var

I = E | D1 ; D2

T ::=

int

|

struct

(I :

int

)+ end

|

array[N

] of

int

L ::= I | L.I | L[E]

N ::= 0 | 1 | 2 | ...

I ::= alphanumeric strings beginning with a letter, not including keywords

Slide3

Storage Layout

3

var x = 2 - 1;var r = new array[3] of int;var s = new struct a:int; b:int end;var y = new int;r[0] = x;r[x] = r[0] + r[r[0] - 1];if not(r[1] > x) { s.a = 0 }else { s.a = 1 };s.b = r[s.a];print s

Slide4

Data-structure extensions

4

T ::= int | struct (I : int)+ end | array[N] of int

T ::= int | struct D end | array[N] of T

D ::=

var

I = E | D1 ; D2

Slide5

Example

5

var

database = new

struct

var

howmany

= 0;

var

table = new array[100] of

struct

var

idnum

= new

int

;

var

balance = 0

end

end;

if

not(database.howmany

== 100) {

database.table[howmany].idnum

= 999999;

database.table[howmany].balance

= 25;

database.howmany

=

database.howmany

+ 1

}

Slide6

Alternatively

6

T ::= int | struct D end | array[N]

T ::= int | struct D end | array[N] of T

Slide7

Example

7

var

database = new

struct

var

howmany

= 0;

var

table = new array[100]

end;

if

not(database.howmany

== 100) {

database.table[howmany

] =

new

struct

var

idnum

= 999999;

var

balance = 25

end;

database.howmany

=

database.howmany

+ 1

}

Slide8

Functions inside structs

8

D ::=

var

I = E | D1 ; D2 |

proc I() { C }

C ::= L = E | C1 ; C2 | ... |

L()

T ::=

int

|

struct

D end |

array[E

] of T

E ::= N | ... | new T |

L()

L ::= I | L.I | L[E]

Slide9

Example

9

var

clock = new struct var time = 0; proc tick(){ time = time + 1 }; proc display(){ print time }; proc reset(){ time = 0 } endclock.tick();clock.tick();clock.display()

Slide10

Example

10

var

clock = new struct var time = 0; proc tick(){ time = time + 1 }; proc display(){ print time }; proc reset(){ time = 0 } endclock.tick();clock.tick();clock.display()

Slide11

Class as a type-structure abstract

11

D ::= var I = E | D1 ; D2 | proc I() { C } | class I = TT ::= int | struct D end | array[E] of T | L

e.g.

class Entry =

struct

var

idnum

= new

int

;

var

balance = 0

end;

class DB =

struct

var

howmany

= 0;

var

table = new array[100] of Entry

end;

var

database = new DB

Slide12

Modules

12

D ::= var I = E | D1 ; D2 | ... | module I = D | import L

e.g.

module M =

var

x

= new

int

;

class C =

struct

var

a = 0 end;

var

y

= new array[10] of C;

proc initialize(){

x

= 0;

for

i

= 0

upto

9 {

y[i].a

=

i

}

}

import M;

initialize();

var

z

= new C;

z.a

=

x

+ y[0].a

Slide13

Avoiding namespace confusion

13

module M =

newstruct

var

x

= 7 end;

module N =

newstruct

var

x

= 99 end;

import M;

import N;

N.x

=

M.x

+ 1

Slide14

Parameterization

14

Provide parameters to class. Example:

class

Entry =

struct

var

idnum

= new

int

;

var

balance = 0

end;

class

DB

(size

)

=

struct

var

howmany

= 0;

var

table = new

array[

size

] of Entry

end;

var

database = new DB(

100

)

Slide15

Type Templates

15

module DataBase(size, recordTemplate) = var howmany = 0; var table = new array[size] of recordTemplate; proc initialize(){ for i = 0 upto size - 1 { table[i].init() } }; proc find(index): if index >= 0 and index < size { answer = table[index].getVal() return answer }

class Entry = struct var idnum = new int; var balance = new int; proc init(){ idnum = -1; balance = 0 }; fun getVal(){ return balance } end;

Slide16

Specifying “type template”

16

interface

RecordInterface

=

struct

init: void -> command;

getVal

: void ->

int

end

;

module

DataBase(size

:

int

,

recordTemplate

:

RecordInterface

) =

var

howmany

= 0

;

var

table = new

array[size

] of

recordTemplate

;

proc

initialize(){

for

i

= 0

upto

size - 1 {

table[i].

init

()

}

};

proc

find(index

):

if index >= 0 and index < size {

answer =

table[index].

getVal

()

return answer

}