; Problem Set 9 Answers (include-book "irun") (in-package "M5") ; Problem 9-1. ; The following constant (which contains a blank to be filled in) defines a ; class named "Cons" with two fields, "car" and "cdr", and one method, named ; "main" which takes no arguments. Fill in the :code component of the "main" ; method so that executing the main method constructs a linked list ; representing the (1 2 3 . -1). That is, the topmost Object is a "Cons" with 1 ; in the "car" and a reference to an Object representing (2 3 . -1) in the ; "cdr". The last "Cons" contains a -1 in the "cdr". You may not store any ; locals. At the conclusion of your program, the stack should contain exactly ; one item, a reference to the "Cons" in question. (defconst *ps9-ct* (make-ct (list (make class :name "Cons" :supers '("Object") :fields '("car" "cdr") :methods (list (make method :name "main" :formals nil :sync nil :code ; Answer: '((new "Cons") (dup) (const 1) (putfield ("Cons" "car")) (dup) (new "Cons") (dup) (const 2) (putfield ("Cons" "car")) (dup) (new "Cons") (dup) (const 3) (putfield ("Cons" "car")) (dup) (const -1) (putfield ("Cons" "cdr")) (putfield ("Cons" "cdr")) (putfield ("Cons" "cdr")) (halt)) )))))) ; The following is a call of irun that should allow you to step through your ; program starting at the 0th instruction in it. (irun (make state :tt (list (make thread :id 0 :cs (push (make frame :pc 0 :locs nil :stk nil :mloc '("Cons" "main" 0)) nil) :stat 'active :ref nil)) :hp nil :ct *ps9-ct*)) #| Here is an irun session that shows the program running to completion: M5 !>(irun (make state :tt (list (make thread :id 0 :cs (push (make frame :pc 0 :locs nil :stk nil :mloc '("Cons" "main" 0)) nil) :stat 'active :ref nil)) :hp nil :ct *ps9-ct*)) Welcome to irun. Type :help for help. ----------------------------------------------------------------- Scene 0 Current thread = (cid 0) = 0 Current state = (s 0) = (MODIFY 0 (S PROTO) :PC 0 :LOCS NIL :STK NIL :NEXT-INST '(NEW "Cons") :MLOC '("Cons" "main" 0)) cmd: :ret Note: The state below is stuck! :Ret did not get back to the caller's frame! ----------------------------------------------------------------- Scene 1 Current thread = (cid 1) = 0 Current state = (s 1) = (MODIFY 0 (S 0) :PC 19 :STK '((REF 0)) :NEXT-INST '(HALT) :HP '(((REF 0) (("Cons" (("car" 1) ("cdr" (REF 1)))) ("Object" (("monitor" 0) ("mcount" 0) ("wait-set" 0))))) ((REF 1) (("Cons" (("car" 2) ("cdr" (REF 2)))) ("Object" (("monitor" 0) ("mcount" 0) ("wait-set" 0))))) ((REF 2) (("Cons" (("car" 3) ("cdr" -1))) ("Object" (("monitor" 0) ("mcount" 0) ("wait-set" 0))))))) cmd: :show-sched (REPEAT 0 20) cmd: :q To re-enter this scene, type (irun). M5 !> |#