160 likes | 264 Views
Classi:. variabili di istanza :. class Foo { public int x; public int y; public void swap (boolean z) { if (z) {int temp = x; x = y; y = temp;}; }; }. metodi: operazioni sugli oggetti. Ambienti per le classi: Cenv
E N D
Classi: variabili di istanza: class Foo { public int x; public int y; public void swap (boolean z) { if (z) {int temp = x; x = y; y = temp;}; }; } metodi: operazioni sugli oggetti Ambienti per le classi: Cenv Cenv = {Ide Menv x x Menv} c ={<Foo , (,{x->, y->} , m)>} m={<swap,<z,if(z){int temp=x; x=y;y=temp;}>>
Oggetti: • istanze di classe • ogni oggetto una differente istanza: • istanze differiscono per:: • i valori nel frame • i metodi non cambiamo ({x->, y->} , m) Class Foo {....}; Foo obj1 = new Foo; Foo obj2 = new Foo; .... frame per obj1: 1 = {x->, y->} frame per obj2: 2 = {x->, y->} Heap per gli oggetti: Heap Heap = {Loc Ide x }
Valori, Classi e Oggetti: una vista grafica oj1 Foo oj2 b tt altri frames dello stato Fie y x y x x y Class Foo {public int x; public int y; public void swap (boolean z){ if (z) {int temp = x; x = y; y = temp;}; }; } Class Fie { public int y;} } Foo oj1 = new Foo; Fiepp oj2 = new Fie; boolean b = true; .... Foo L1 foo L2 Fie fie c Il Nuovo Stato: Class_Env x x Heap < c , , >
Sistema di transizioni per: Semantica dei Comandi S com={< com , Tcom , com } com= {<C ,<c ,, >| C Com, , c Cenv, Heap} {<,>| , Heap} Tcom = {<,>| , Heap} com = {(=), ..., tutte le regole che vedremo }
Comandi (nuova semantica) <E, <c , , > > expv (x)≠ <x=E; <c,,> > com <[v/x], > (=) <StmtList, <c , . , > > com < .’, ’> <{StmtList}, <c , , > > com <’, ’> <S, <c,,> > com<”,”> <StmtList , <c,”,”> com <’,’> <S StmtList , <c , , > > com <’, ’> (Block) (StmL)
Condizionale <E , <c , , > > exp tt <C1,> cmd <’ ,’> <if(E) C1 else C2 , <c , , > > com <’, ’> <E , <c , , > > exp ff <C2,> cmd <’, ’> <if(E) C1 else C2 , <c , , > > com <’ ’> (if-tt) (if-ff)
Iterazione (while) <E , <c , , > > exp ff <while(E) C, <c , , > > com< , > <E , <c , , > > exp tt <C, <c , , >> cmd<” ”> <while(E) C, <c, ”, ”> >com <’,’> <while(E) C, <c , , > > com<’,’> (W-ff) (W-tt)
La sintassi del nucleo Progr::=prog {ClassDeclList {StmtList}} ClassDecl_List::=ClassDecl | ClassDecl ClassDeclList ClassDecl::=class Ide {StatMetDecList InstVarDecList MetDecList} InstVarDecList::=InstVarDec | InstVarDec InstVarDecList InstVarDec::=public Type Ide StmtList::= Stmt | Stmt StmtList Stmt::= Com | Decl Decl::= Type Ide | Type Ide= Exp; | Type ide = new Ide(); Type::= int | boolean | char | Ide Block::= {StmtList} Exp::= Ide.Ide | Ide | NumExp | BoolExp | Ide.Idem(Exp)
La sintassi del nucleo MetDecList::= MetDec MetDecList | MetDec::=public void Ide(T Ide){StmtList} StatMetDecList::= StatMetDec StaticMetDecList | StatMetDec::=public static TypeR Ide(Type Ide){StmtList} TypeR::=Type | void Com::= Ide= Exp; | Ide.Ide=Exp; | Ide = new Ide(); | Block |Ide.Idem(Exp) | if (BoolExp) Com else Com; | while (BoolExp) Com;
Esempio programma Java publicclass volume { publicstaticclass Foo { public int x ; public int y;} publicstaticclass Fie { public int x ;} publicstatic void main (String [ ] args) { Foo oj1=new Foo(); Fie oj2=new Fie(); oj1.x=5; oj1.y=oj1.x+4; oj2.x=oj1.x+oj1.y; }} prog{ class Foo { public int x; public int y;} class Fie { public int x;} {Foo oj1= new Foo(); Fie oj2= new Fie(); oj1.x=5; oj1.y=oj1.x+4; oj2.x=oj1.x+oj1.y;}}
Semantica: il programma <ClassDeclList, >classdeclc <StmtList,<c , . , >> com<,> <prog {ClassDecList {StmtList} }> classdecl<,> (prog)
Semantica: classi <ClassDecl , c> c’<ClassDecList , c’> c” <ClassDecl ClassDecList,c c” <SMDL,>istdecls <InsVarDecLis, >istdecl <MetDefLis,>mdlm <class c {SMDL InsVarDecLis MetDefLis}, c> c[< s,, m>/c] (classdeclList) (classdecl)
Semantica: istanza e metodi d’istanza <public Typer m(Type x) B,s> s[<x,B>/m] <MetDef,s> s’ < MetDefList , s’> s” < MetDef MetDefList,s s” <public T x;, > istdecl [ /x] <InstVarDecl, > ’ <InstVarDecList, ’ > ” <InstVarDecl InstVarDecList, > ” (MetDef) (MDL) (instdecl) (instdeclist)
Semantica: dichiarazioni di oggetti c(c) =< s,, m>l =newloc ( ) <new c(),<c,,>> <l ,[<c,>/l]> <new c(),<c,,>> <l ,’>= .’ <c x= new c(); <c,,>> < [l /x]. ’,’> = .’ <c x;<c,,>> < [null/x]. ’,> new (declnew) (declnonew)
Semantica: assegnamento ed espressioni <new c(),<c,,>> <l ,’> <x=new c();,<c,,>> < [l /x],’> <E,<c,,>> v (o)=l(l)=<c, > ’= [v/x] <o.x= E; <c,,>> <,[<c, ’/l ]> (o)=l(l)=<c, > <o.x;<c,,>> (x) (com=new) (como.x=) exp o.x
Semantica: metodi d’istanza invocazione (Ipotesi che le espressioni non modifichino lo stato) <Exp,<c,,>> v (o)=l(l)=<c, ’> c(c)=< s,, m> m(m)=<x,B> <B, < c ,[l / this, v/x]. , > <’,’> <o.m(Exp);, <c,,>> <,’> invoc