-------------------------------------------------------------------- -- a3.ads -- This file is the specification file for the types -- and functions used in the environment simulation -- of Assignment 3, CS 152, Spring 2000. -------------------------------------------------------------------- with TEXT_IO; use TEXT_IO; package a3 is -- These are the record types and access types -- used by the test program. Note that the -- implementation of the "binding" type must -- be public in order to allow the test program -- to convert integer literals to this type. -- Also "frame_ptr" must be identified as an -- access type so that the test program can -- verify that NULL is a permissible value -- for that type. type binding is new integer; type environment_ptr is private; type frame is private; type frame_ptr is access frame; -- These are the procedures and functions used -- in the test program -- These two functions substitute for constructors, giving -- a little more control over the initialization, etc. -- The first function creates an empty environment. -- The second creates a frame (with no bindings), given its -- static link. function new_environment return environment_ptr; function new_frame(fr:frame_ptr) return frame_ptr; -- These three functions modify frames or environments -- The first adds a given binding to a given frame -- The second adds (i.e., pushes) a given frame to a given environment. -- The third removes a frame from (the front of) an -- environment. procedure add_binding(f:frame_ptr; bb:binding); procedure add_frame(e:environment_ptr; fr:frame_ptr); procedure delete_frame(e:environment_ptr); -- This function returns the (value of) the binding found -- in environment e, at position j in the frame found -- at i links down the chain of static links from the -- top frame. procedure lookup_and_print(e:environment_ptr; i,j:integer); private -- An environment consists of a display (an array of frame pointers), -- a display size (how many positions of the array are used) -- and a pointer to a stack of frames of type "environment_node_ptr" -- An environment_node is a linked stack of frame pointers, with a -- header node. Note that the static pointers are kept in the -- frame nodes, while the dynamic pointers are kept in the -- environment nodes. MAX_DISPLAY_SIZE:constant integer:=10; type display is array(0..MAX_DISPLAY_SIZE-1) of frame_ptr; type environment_node; type environment_node_ptr is access environment_node; type environment is record d:display:=(others=>null); display_size:integer:=0; frames:environment_node_ptr; end record; type environment_node is record frame:frame_ptr; next:environment_node_ptr; end record; type environment_ptr is access environment; -- A frame consists of a static link and a linked list of bindings. -- The binding_node type represents a node in a linked list of bindings. -- It has a link field "next", so these nodes know that they are in -- a linked list. type binding_node; type binding_node_ptr is access binding_node; type binding_node is record data:binding; next:binding_node_ptr; end record; -- Here is the definition of a frame type frame is record static_link:frame_ptr; bindings: binding_node_ptr; end record; end a3;