datatype s_espressione = NUM of int | STRINGA of string | T | F | NIL | DOT of s_espressione * s_espressione ; datatype Sexpr = Var of string | Quote of s_espressione | Op of string * Sexpr list | If of Sexpr * Sexpr * Sexpr | Lambda of string list * Sexpr | Call of Sexpr * Sexpr list | Let of Sexpr * string list * Sexpr list | Letrec of Sexpr * string list * Sexpr list; datatype secdexpr = ADD | SUB | MUL | DIV | REM | EQ | LEQ | CAR | CDR | CONS | ATOM | JOIN| RTN| AP | DUM| RAP| STOP | LD of int*int | LDC of s_espressione | SEL of secdexpr list * secdexpr list | LDF of secdexpr list ; exception NonTrovato ; fun find(nil,y)= raise NonTrovato | find(x1::L,y) = if x1=y then 0 else 1+find(L,y); fun location(x,i,nil) = raise NonTrovato | location(x,i,n1::L) = let val temp = find(n1,x) handle NonTrovato => ~1 in if temp = ~1 then location(x,i+1,L) handle NonTrovato => (~1,~1) else (i,temp) end; (* COMP *) fun COMP(e: Sexpr, n: string list list, c: secdexpr list): secdexpr list= case e of Var x => LD(location(x,0,n))::c | Quote k => LDC(k)::c | Op(operator,sl) => (case operator of (* hd -> restituisce la testa della lista tl -> restituisce il resto della lista hd(tl()) -> restituisce la testa del resto della lista *) (* OPERATORI BINARI *) "ADD" => COMP(hd(sl),n,COMP(hd(tl(sl)),n,(ADD::c))) | "SUB" => COMP(hd(sl),n,COMP(hd(tl(sl)),n,(SUB::c))) | "MUL" => COMP(hd(sl),n,COMP(hd(tl(sl)),n,(MUL::c))) | "DIV" => COMP(hd(sl),n,COMP(hd(tl(sl)),n,(DIV::c))) | "REM" => COMP(hd(sl),n,COMP(hd(tl(sl)),n,(REM::c))) | "EQ" => COMP(hd(sl),n,COMP(hd(tl(sl)),n,(EQ::c))) | "LEQ" => COMP(hd(sl),n,COMP(hd(tl(sl)),n,(LEQ::c))) | (* OPERATORI UNARI *) "CAR" => COMP(hd(sl),n,(CAR::c)) | "CDR" => COMP(hd(sl),n,(CDR::c)) | "ATOM" => COMP(hd(sl),n,(ATOM::c)) | (* non ne sono sicuro !!! *) "CONS" => COMP(hd(tl(sl)),n,COMP(hd(sl),n,(CONS::c))) ) | If(cond,e1,e2) => let val thenpt = COMP(e1,n,[JOIN]) val elsept = COMP(e2,n,[JOIN]) in COMP(cond,n,SEL(thenpt,elsept)::c) end | Lambda(varLegate,sl) => [ LDF ( COMP(sl,varLegate::n,[RTN]) ) ] | Call(exp,lista_var) => let fun Funzione(nil: Sexpr list , n: string list list): secdexpr list= nil | Funzione(Testa::Coda: Sexpr list , n: string list list): secdexpr list= Funzione(Coda,n)@COMP(Testa,n,[])@[CONS] in LDC(NIL)::Funzione(lista_var,n)@COMP(exp,n,[AP])@c end | Let(e,v,lista) => let fun Funzione(nil: Sexpr list , n: string list list): secdexpr list=nil | Funzione(Testa::Coda: Sexpr list , n: string list list): secdexpr list=Funzione(Coda,n)@COMP(Testa,n,[])@[CONS] in LDC(NIL)::Funzione(lista,n)@ LDF( COMP(e,v::n,[RTN]))::[AP]@c end | Letrec(e,v,lista) => let fun Funzione(nil: Sexpr list , n: string list list): secdexpr list=nil | Funzione(Testa::Coda: Sexpr list , n: string list list): secdexpr list=Funzione(Coda,n)@COMP(Testa,n,[])@[CONS] in DUM::LDC(NIL)::Funzione(lista,v::n)@LDF( COMP(e,v::n,[RTN]))::[RAP]@c end ;