isp Implementtion Hndout Pge 1 o 10 Quik Guide to isp Implementtion Representtion o si dt strutures isp dt strutures re lled S-epressions. The representtion o n S-epression n e roken into two piees, the tg (whih stores tpe inormtion) nd the tul dt. There re two kinds o S- epression, toms nd lists. The s-epression r (the tom r) would e stored s: r With the '' tg signiing tht this is n tom (s opposed to list), nd the smol r eing the tul tom (in most lisp sstems, the string "r" would e stored somewhere else, nd the tom would just e pointer to tht lotion, ut to keep things simple we will ssume tht the string n e stored in the dt setion o the lisp dt struture) Sine lisp stores everthing s pointers, the tom r would rell e pointer to the ove dt struture. r ists re uilt up using list struture tht hs two pointers pointer to the irst element o the list, nd pointer to the rest o the list. So, the list ( ) would e represented the ollowing: et's get little more omplited, nd look t lists inside o lists. For emple, the representtion o the s- epression ( ( ) ()) is
isp Implementtion Hndout Pge 2 o 10 ist within ists within ists Here re even more emples o lists tht ontin lists, inluding the empt list: ( ()) ( ( ( d) e) ( ) ( ) ) e d Our Friend Rell tht '<sepr> is just shorthnd or ( <sepr>), so tht ' is shorthnd or ( ) nd '( ) is shorthnd or ( ( )) Hene, the internl representtion o ' is the internl representtion o ( ), whih is:
isp Implementtion Hndout Pge 3 o 10 ikewise, the internl representtion o '( ) is the sme s the internl representtion o ( ( )), whih is: Red-Evl-Print loop The lisp interpreter is elegnt in its simpliit. n s-epression is red in (nd onverted to the internl representtion), evluted, nd the result is printed out. So, it the user tped in: ( ' '( )) First, the interpreter would onvert this s-epression to the pproprite internl representtion [ rememer tht ( ' '( )) is shorthnd or ( ( ) ( ( ))) ] : Net, the epression is evluted. Sine the epression is list, the interpreter irst looks t the irst element o the list (whih is ) to see i it is vlid untion nme. Sine is vlid untion nme, eh o the rguments to re evluted. The 2 rguments to re the 2 nd nd 3 rd elements o the list, whih re:
isp Implementtion Hndout Pge 4 o 10 nd Tking the irst rgument irst, this is list. whih mens it is untion ll. Chek the irst element o the list to mke sure tht it is vlid untion nme nd is vlid single rgument untion nme. We re lling with single rgument, so we re OK so r. Quote is speil untion tht returns its rgument unevluted. So, this ll to will return its one rgument (the seond element o the list) whih is: OK, so now we evlute the seond rgument to, whih is list, whih mens untion ll. First, hek the irst element o the list (the tom ), whih is vlid untion nme. The untion returns its single rgument (the seond element in the list) unevluted, to get: So, we n ll, with the ove two rguments. Cons retes new list element, sets the r equl to the irst element nd the dr equl to the seond element, to get:
isp Implementtion Hndout Pge 5 o 10 Finll, the interpreter prints out the result, whih is ( ). Getting new memor the ree list How do we get new memor lotion when we need one? When is lled, where does tht memor lotion ome rom? ll ville memor is stored in ree list. The ree list is list o memor lotions tht re ville. This list is little dierent thn stndrd lisp list, like ( ). To store the list ( ), we need spe or the three toms,, nd, s well s 3 list truts to glue everthing together. It seems wste to use 6 memor lotions to store 3 memor lotions or ree list, so we will use slightl dierent method. The ree list pointer will point to the irst element o the ree list, nd the r o tht element will point to the net element in the ree list, nd so on. So, the ree list might look something like: Free ist n time new piee o memor is needed, the irst element o the ree list is used, nd the ree list pointer is dvned. Memor lotion to use or, et Free ist O ourse, eventull we re going to run out o memor. Wht hppens then? The interpreter will do mrk nd sweep grge olletion. Ever piee o memor tht is urrentl eing used is mrked. Then the interpreter does sweep through memor, dding ever unmrked element to the ree list. Then the ree list should no longer e empt, nd the interpreter will ontinue. et's emine omplete emple. Consider the untion : (deun ( ) ( ( ( ( ))))) et's s the ree list looks like the ollowing:
isp Implementtion Hndout Pge 6 o 10 Free ist nd we mke the untion ll ( ' ' ') [whih is shorthnd or ( ( ) ( ) ( )) ] The ll ( ' ' ') is irst red into memor: Free ist Evl. ep Now we hve to evlute the epression. "" is untion tht hs een deined, so we evlute eh o the rguments, ple them on the stk, then ll the untion: Free ist Evl. ep The irst rgument is ( ), whih evlutes to, the seond rgument is ( ), whih evlutes to, nd the third rgument is ( ), whih evlutes to
isp Implementtion Hndout Pge 7 o 10 Now we ll the untion. Tht untion returns o two rguments: Free ist Evl. ep The irst rgument is, ut the seond rgument is nother, whih needs to e evluted (the rther lrge grphil spe lloted or the irst on the stk is merel to mke the pointers esier to see) The irst rgument to this is, ut the seond rgument is nother Free ist Evl. ep
isp Implementtion Hndout Pge 8 o 10 The rguments to this re nd () : Free ist Evl. ep Now tht we hve evluted the rguments or the inl, we n eeute the untion od. Cons tkes new piee o memor rom the ree list, opies the irst rgument into the r nd the seond rgument into the dr, nd returns this lok o memor, whih is used s the seond rgument to the seond : Free ist Evl. ep
isp Implementtion Hndout Pge 9 o 10 Now tht we hve evluted oth rguments or the seond, we n eeute the untion od. Cons tkes new memor lotion o the ree list ut the ree list is empt! So, we need to do grge olletion step. First we go through the stk nd mrk ever piee o memor tht is rehle rom some pointer on the stk, s ollows: Free ist Evl. ep O ourse, eh memor lotion will need to hve n etr it reserved to store these mrks. Now, we sweep through memor, dding everthing tht is not mrked to the ree list (we might s well lso ler the mrks on this pss, to e red or the net time we wnt to do grge olletion). Free ist Evl. ep
isp Implementtion Hndout Pge 10 o 10 Now the ree list is no longer empt, so we n inish the ll to the seond : Free ist Evl. ep Finll, we n omplete the ll to the irst, nd return tht vlue (whew!) Free ist Evl. ep Now we n print out the vlue o the epression, whih is ( ), s epeted. Note tht now the ree list is gin empt, nd there is whole lot o grge. The net time we wnt to evlute n epression, we will need to run mrk-nd-sweep grge olletion gin, to ree up some more memor.