Theses
Thesis/Dissertation Collections
1989
HOPS: A Hierarchical/object-oriented
programming environment system
Ryuichiro Kodama
Follow this and additional works at:
http://scholarworks.rit.edu/theses
This Thesis is brought to you for free and open access by the Thesis/Dissertation Collections at RIT Scholar Works. It has been accepted for inclusion in Theses by an authorized administrator of RIT Scholar Works. For more information, please [email protected].
Recommended Citation
School of Computer Science
HOPS:
A Hierarchical/Object-Oriented
Programming Environment System
by
Ryuichiro Kodama
A thesis, submitted to
The Faculty of
the School of Computer Science and Technology,
in partial fulfillment of the requirements for the degree of
Master of Science in Computer Science.
Approved by:
.Professor James E. Heliotis
Professor Peter G. Anderson
Professor John A. Biles
Date
Acknowledgements
I would like to thank Hitachi, Ltd. for its scholarship during the study in the MS
program at Rochester Institute of Technology.
Being a foreign student, I greatly
appreciate Janene Oettel's help in pointing out the grammatical mistakes in English.
Finally, I would like to
thank
my wife, Kazuko, for her support. This paper could not
have been completed without her cheerful encouragement.
About the Reproduction of This Thesis
Title of Thesis:
HOPS: A Hierarchical I Object-Oriented Programming Environment System
I,
Ryuichiro Kodama, hereby grant permission to the Wallace Memorial Library, of
RTf,
to
reproduce my thesis in whole. Any reproduction will not be for commercial use of profit.
Theconcept of object-orientedprogrammingis
becoming
animportantparadigminwhichprogrammersrepresent and solve problems. HOPS (Hierarchical
/
Object-orientedProgramming
EnvironmentSystem),
developed inthis thesisproject,employsthisconcepttorepresent and manipulatetheresourcesofaprogrammingenvironment. Alltheresource
objects(such as
files, directories,
simpleinteger dataand soon)arehierarchically
arranged for easyaccess andmanagement, as theusermoves through atree structurejustasinthe UNLX file system. HOPL (Hierarchical/
Object-orientedProgramming
Language),
whosegrammaris similarto C
language,
has been designed so that theexpression of resourceobjects canbe
directly
embeddedinastatement, whichis translatedinto intermediatecodes and then is executed. The system allows the user to store the intermediate codes asmethods and to inherit methods from objects which already contain methods.
HOPS,
functioning
nowundertheUNLXenvironment,can sharetheUNIXdirectories andfilesas its resources, so that it can be an object-oriented front-end processor for UNIX shellcommands. An experimental screen editor, HOPE (Hierarchical
/
Object-orientedProgramming
Editor),
has also been developed. It is written partly in HOPLto facilitatecustomization of someeditingcommands.
Computing
Review Subject CodesThe
following
are the primary and secondary classification codes for this thesis,accordingto theACM's
Computing
Reviews. The primaryclassificationcode:D3.3 Language Constructs
The secondaryclassificationcodes:
F3.3 StudiesofProgramConstructs
Acknowledgements
2Abstract .-...3
Computing
Review Subject Codes 31. Introduction 6
1.1. More Precise Abstraction through HOPS 7
1.2. PreviousWork 8
1.3. Theoretical and Conceptual Development 10
1.3.1. Mechanism of HOPS Inheritance 10 1.3.2. Evaluation Process intheHOPL Interpreter 13
1.3.3. Hierarchical Arrangement of Objects 14
1.3.4. RepresentationofObjects 14
1.3.5.
Continuity
ofConventional SystemsandHOPS 151.3.6. TTY-basedSystem 16
1.4. HOPS
Glossary
162. Functional Specification 18
2.1. Object Management 18
2.2. HOPL
(Hierarchical
/
Object-OrientedProgramming
Language)
182.3. HOPE
(Hierarchical
/
Object-OrientedProgramming Editor)
263. Program Description 28
3.1. Basic Structureof anObject 28
3.1.1. BasicOperationson anObject 32 3.1.2. PhysicalStructureofSome Primitive Objects 32
3.1.3. CreationofObjects 36
3.1.4. Reference Counts 36
3.1.5. C Language Interface 39
3.2. Lexical AnalyzerandParser 39
3.2.1. TTYInterfaceandCommandFile Interface 39
3.2.2 Syntax Analysis 41
3.3. ImplementationofControl Structure 43
3.3.1. An Environment Object 43
3.3.2. The Evaluation Process and
Message-Passing
44 3.3.3. InstanceVariablesandLocal Variables 473.3.4. Method Objects 47
3.3.5. A Method List 49
3.3.6. AssignmentStatements 50
3.3.7. ConditionalStatementsandIterationStatements 51
3.3.8.
Jump
StatementsandInterruption 523.3.9. A Block Object 53
3.3.10. ScopeRules 57
3.5.1. A FileObject 66
3.5.2. Coordination with the UNLX File System 66
3.6. HOPE 67
3.6.1. The Line Editor 67
3.6.2. The Curses
Library
in C language 68 3.6.3. The HOPE Main Program Structure 683.7. Software Developmentusing HOPS 69
3.7.1.
Prototyping
693.7.2. A"setup"
Message 70
3.7.3. A Project
Using
Inheritance 734. Sample Programs in HOPL
find,
grep, andawk 765. Conclusions 79
5.1.
Summary
795.2. Alternative Approaches foranImproved System 79
5.3. Suggestionsfor Future Extensions 80
Bibliography
82Appendix A: C Language Interface A-l
Appendix B:
Setting Up
HOPSunderUNLX A-6One of the primary goals of an interactive programming environment is to
enhance theproductivity of software programs.
Easy
manipulation of system resources(such as
files, disks,
and other input/outputdevices)
is essential for productivity. TheUNLXt
operating system
[7],
basedonahierarchical filesystem, isasuccessfulinteractiveprogrammingenvironmentwhichmakesit easyandunderstandabletouse system resources
by
representingthemasfiles. Thatis,
UNLXenables ustoabstract resourcessuch asI/O devicesby
read/write operations. This project develops an interactive programmingenvironmentwhichintroducesobject-oriented methodsintotherepresentationofresources,
and arranges every object hierarchically. For
instance,
even adirectory,
which itselfcontains objects, is regarded as an object that can accept messages to manipulate its
embedded objects. Becauseobject-oriented methodsprovidemore preciseabstraction of
resourcesthanjustread/write abstraction,the resources, acquiringthe abilitytostandardize
their
interfaces,
gain greater connectivity. Mostimportantly,
thehierarchical arrangementofobjects makesit easyto access and manage objects.
The environment system developed in this project is called HOPS
(Hierarchical/Object-oriented
Programming
EnvironmentSystem). HOPS isasingle-user,single-tasksystem. Thesystem includestwocomponents:
(1)
aninterpreterofthenewly-designed object-oriented
language,
HOPL (Hierarchical/Object-orientedProgramming
Language),
whichhandlesobjectsby internally
interpreting
messages senttothem,and(2)
a screen editor, HOPE (Hierarchical/Object-oriented
Programming
Editor),
whose instructionsarewritteninHOPLsothat theusercan customize some ofitsfunctions. Theeditor is also an example of an application program written in
HOPL;
therefore, anunderstandingoftheeditorwill
help
thereader understandHOPLlanguage.IntheUNIXoperating system, all peripheraldevicesarerepresentedas files inthe
hierarchical file system [7]. Most of the device-dependent issues are
concealed insidethe device
drivers,
so that application programs can communicate withthosedevicesthrough
basically
onlyread and write operations. Eventhecommunications betweenprocesses aredonethrougha pseudofilecalled a'pipe'
orthrough standardinput and output. While this standardization ofresourceconnections
by
a fileprovides greatflexibility,
there are many resources that cannot be abstractedby
only read and writeoperations. A
directory,
implemented as afile inUNIX,
holds some files or someotherdirectories.The
directory
obviouslyneeds some operationsotherthanreadandwrite, suchas a
lookup
operation, which could test if a file specified as an argument exists in thedirectory
ornot.As an alternative to file abstraction, thispaper advocates hierarchically-arranged
objectswhichrepresent alltheresourcesinaninteractive programmingenvironment.
By
regarding theresources as objects whichcan acceptmessages, it is possibletorepresent
many different types of resources as objects. A
directory
object can acceptthelookup
operation as amessage. A
directory
objectcan accept a messageX,
then execute itby
sendinga message such as
"evaluate"
to themethod objectXthat the
directory
associateswithX.
Thus,
customized commands canreside onadirectory
as objects. An interpreterobjectcan acceptamessagelike "changetheworkingdirectory." Andan editor object can
evenacceptall themessages toedit a text,so that theuserscancreate customizededitor
commandsusingthose messages.
This system is based on object-oriented methods
[3],
which standardize allexecutions as messages sent toobjects, so that we can take advantage ofdynamic high
connectivity betweenobjects. This featureisalso referredto as an advantageofatypeless language. Suppose that an object is attached as an argument to the message
EAT,
andOne
day
you create an objectLAMB and wanttoinvoke EATwithLAMB. Atthattime,you don'tneedtocreate a specializedversion oftheEATmessage, such as EAT-LAMB. Allyou needtodo is justadd a methodfortheGRILLmessageto theobject
LAMB,
thenyou can send amessage, EATLAMB. Inthis way, theconnectivityofEATisenhanced.
In
HOPS,
everynamed objectis locatedinatleastonedirectory
and alldirectoriesare arranged
hierarchically,
so that theobjects relatedtoaproject canbecollectedinonedirectory
foradministrative convenience andthey
can be searched in ahierarchical way.Basically,
there are no globally-named objects other than a rootdirectory
and a currentworkingdirectory. Theroot
directory
representstheHOPLinterpreter;
it holdsthevariable objects usedin theinterpreter,
such as a previous input statement. Thecurrent workingdirectory
istheobjecttowhichmosttyped-inmessages are sent.Every
commandin HOPSstartswitha messagesymbol whichmay be followed
by
somearguments,ifany. Youcanchangethat
directory by
sending theinterpreteramessagetochangethecurrent workingdirectory.
1.2. Previous Work
The Smalltalk-80t programming environment
[3]
is apioneer system which notonlyprovidesbutis also described in anobject-orientedlanguage. This systemhas been developedthroughseveral versions:
Smalltalk-72,
Smalltalk-76,
andfinally
Smalltalk-80 in 1980. In the Smalltalk-80system,awindowcalledabrowserenablesthe userstofind aspecificclass andmessage[4]. Bothclassesandmessages arecategorized,sothat theusers
canfindthe code of a specific method
by
selecting(1)
aclasscategory,(2)
a class inthatcategories,
(3)
amessagecategory inthatclass, and,finally, (4)
a messageinthatcategory.1"
HOPS doesn'trelyon anything butaTTY-type
device,
whilethe Smalltalk-80 systemisdependent on a bitmapped display. In a sense,HOPS provides more primitive functions
which would be basic operations for even using the bitmapped display. The Little
Smalltalk system
[2]
largely
supports thespecification ofthe Smalltalk-80 under UNIX.Thesystem, writteninC languageforportability, shares one ofits purposeswithHOPS in
the sensethatitpursues a TTY-version of a object-orientedlanguage. Butwhen youlook
atthescopingrule of object variablesinthose two systems,Little Smalltalk appears notto
have such a structural scope as the hierarchical arrangement of variables that HOPS
provides.
Directories playa significant rolein
hierarchically
arrangingtheobjectsin HOPS.Objectsare connectedtoa
dictionary
inwhichinstancevariables anduser-definedmethodsarelookedup. Thereis nodirect waytoaccessinstancevariables;a method boundto the
symbol '.'
(period),
receiving an instancevariable nameas an argument,returns thevalueof aninstancevariable.
Among
currently investigatedobject-orientedlanguages,
Self
[17]
employs a similar structure foran object's
dictionary,
whoseslots contain bothvariablesand procedures. Thereis another conceptthatSelfandHOPS share: eliminationof a class
object. While
Self
gets rid of class objectsby
exploiting prototype-based objects(substituting
instantiationby
cloningorcopyinganobject),a methoddictionary
works asifit were a class object in HOPS. For
instance,
if a message 'new' is sent to a methoddictionary,
thedictionary
object creates an instance that has operations in the methoddictionary. With eithermethod, this eliminationreduces complexities (such as thosein
metaclassconnections)oftheobject-orientedsystem.
For a compiled and strongly typed object-oriented
language,
there is C++[15]
developedatAT&T. Theobjectsinthatlanguageare scopedbasedonthescopingrulefor
functions,
localtoafunction,
orlocaltoanobject. The HOPLisinteractively
interpretedaswell astranslatableinto
intermediate
codes.UNIXprovides a lotof useful built-incommandsthatcanmanipulatefilesor their
contents. Anew command canbe built
by
combiningthose built-incommands in whatiscalled a shell script. Ontheother
hand,
there are alotoflibrary
functionswhichdirectly
accessthe structure of afileor allow thequickmanipulationof system resources. Those functions
help
build up an application programwrittenin C language.Thus,
UNIX hastwobasicinterfaces: shell script commands andC
library
functions. Forexample, thereis both a command called 'rm' and a C languagelibrary
function called'unlink'
which are
usedtodelete afile. In HOPLthose two interfacesare standardizedinone
format;
HOPLjust sends a messagetoa object. The deletionofafile is done
by
sending a messagelike"deletethefile xyz"
to the
directory
containingxyz.In addition to the HOPL
interpreter,
another system component in HOPS is thescreeneditor,
HOPE,
which can becustomizedby
modifying HOPEitself,
becauseit iswrittenin HOPL. Emacs
[13]
hasa similarfeature inthatit iswrittenin itsownlanguage calledMlisp
(Mock lisp). The users can create a customized editing environmentby
changingsomefunctions in Emacs. Forexample, itcansimulateanenvironment similarto theUNLX editor called'vi'. The users canalso add a newfunctionto theEmacsand save
it as a
library
function. While Emacstakes advantage ofalisp-like programming style,such as the dynamic scope of variables, HOPE is built up based on object-oriented
methods; it consists of several chief objects and executes edit commands
by
sendingmessagestothose objects.
1.3. Theoretical and Conceptual Development
1.3.1. Mechanism of HOPS Inheritance
In the Smalltalk-80 system, when theusers want torun a simple demo program,
While this indirectprocedure preserves programs as assets ofthe system, itimplies that
theremay bea more
direct,
quicker proceduretoruna prototype program. InHOPS,
theclassisreplaced
by
a simpledictionary. Theobjectin HOPSis categorizedintotwo types: a simple object and a compound object. Asimple objecthas onlyonedictionary,
sothatit isusedforrepresentinga primitive objectwhichdoesnotinherit anypropertiesfromotherobjects,exceptfroma publicdictionary. Thepublic
dictionary
includesmethods(such asa logical-oroperator "II"and alogical-andoperator"&&")
whicharecommonlyusedby
anyobject. "Compound
object"
isanother namefor
"directory
object", whichis connectedtoadirectory-method
dictionary,
a user-customizeddictionary,
and zero or more inheriteddictionaries. When theuserssee andmodifythecontentofthe current
directory,
theuser-customized
dictionary
isused tolook up aninstancevariable.Therefore,
whentheuserswanttorun ademoprogram, alltheyneedtodo is justcreate a new methodina
directory
andtype inthatmethod name. Ifa messageis sentto adirectory,
the systemlooks it upfirst, (1)
in the directory-methoddictionary,
(2)
intheuser-customizeddictionary,
(3)
inany inherited
dictionaries, (4)
inthedictionaries fortheHOPLinterpreter,
thenfinally
(5)
in thepublicdictionary
describedabove. Thereisa methodinthedirectory-methoddictionary
HOPL interpreter
parent
Hierarchical Arrangement
'child
Directory-method
dictionary
(common
to all
directories)
User-customized
dictionary
kpwd^b^
',*
m?M.
T^m*
wmi.
A
_z/
or>
m+
methods
written in
C
language!me !pa
X
Increment
Inheritance
dictionary
(implemented
by
alist,
whose element
has no name)
literal string "project"
parent
directory
= "!"integer"1"
methodfor Increment
Directorieswhose user-customized
dictionary
is used for inheritanceFigure 1.1 Anexample ofdirectories in HOPS
Figure 1.1 showsan exampleofdirectories in HOPS.
Every directory
shares onedirectory-method
dictionary
which consists of tables between a message symbol and amethodthatis
typically
writteninClanguage. Eachdirectory
has itsown user-customizeddictionary
and inheritance dictionary. The user-customizeddictionary
contains twoinherited
directories.Only
the user-customizeddictionary
connectedto thosedirectories isusedfor
inheriting
methods.Oneoftheimportantcharacteristicsin HOPS directoriesisthat thereisnodifference
betweenaninstancevariable'slocationand a method'slocation. Thereferencefields inthe
dictionary
pointtoboth instancevariables and methods. Forexample,aninstancevariablex (which holds an integer
1)
and a method Increment reside on a user-customizeddictionary
in Figure 1.1. This structure makes it easy to manage bindings for instancevariables, becausean instance variable can be
dynamically
obtainedby
carrying out thesame procedure as a methodis lookedup.
1.3.2. Evaluation Process in the HOPL Interpreter
MostversionsoftheSmalltalksysteminterpretanintermediatecode(bytecode)
by
incorporating
stack operations [3]. Thiswasdemonstratedby
a simpleSmalltalk interpreterwritten in Smalltalk-76 itself [5]. HOPS applies lisp-like evaluation to the process of interpretation instead ofusing byte code. Figure 2.1 shows that process, in which the
message-sending actionis interpretedasanobject(methodobject),then the systemsends a special message 'eval'to the method objectwithno arguments. This design allows usto
instance
variables
X
)
ieatj
target objecttarget Qbject meSsage
Figure 1.2 Interpretationofsendinga messagein HOPS
1.3.3. Hierarchical Arrangement of Objects
The hierarchical arrangement of objects plays a significant role in HOPS. In a
hierarchicalstructure, objects canbe locatedin a well-organizedwaysothatmoredetailed
components
(objects)
can be accessed as theleafofthe tree structure is searched. Thisstructure is also suitable for project management because separated subprojects can be
developedin directories independently.
1.3.4. Representation of Objects
In
HOPS,
themannerofspecifyingahierarchicalobject retainstheconsistencyofobject-oriented methods: the execution of a program
by
sending a message. HOPSrepresentseverything inthe systemas an object. Therearethreewaystospecifyanobject:
(1)
by
sendingamessage '.'withan argument,objectname,to thecurrent
directory
(togetanobjectembeddedinthat
directory),
(2)
by
sendinga message 'A'to thecurrentdirectory
(toget theobject
holding
thatdirectory),
or(3)
by
sending a message '.'withanargument,
objectname,to theroot
directory
(to get an objectembeddedinthatdirectory). Becausenamedoes nothavetobetypedinwhen a message issent to
it;
every statement in HOPLstartswith a message symbol whichissenttothecurrent
directory
(ifthedirectory
doesn'thave such amessage, then itwillbe sent to the root
directory,
HOPLinterpreter),
unlessthe root
directory
is specified first in the statement. Instead oftyping
the statementinteractively,
if itiswrittenin a method (aprogrambody
invokedby
amessage) andtheroot
directory
isnot specifiedfirst inthestatement,thenthefirst identifier appearing inthe statement means a messageidentifiersentto'selfinthesense ofSmalltalk-80terminology.If such a message symbol cannot be found in the message
dictionary
of'self,
a specialobject calledILLEGAL isreturned after evaluation process. ILLEGALisusedincase an
errorconditionis satisfied:forexample,thecaseinwhichaspecifiedmessageis notfound.
ILLEGALrespondsILLEGAL to any kindofmessage except the 'print'message which
willprint out the name "ILLEGAL" to standard output. ILLEGALmustnotbe boundto
any variables; if one tries to assign it to an instance variable, the assigned value is
automaticallychangedtoa system constantobject,
NIL,
whichmeansfalse inconditionalexpressions.
1.3.5.
Continuity
of Conventional Systems and HOPSThesyntactic sugardescribedabove givestheusersa sense ofcontinuity betweena
non-object-oriented,conventional systemandHOPS. Aconventional system allowsfora
set of globally-accessiblecommands, which consist of
first,
a commandname, and thensomearguments,ifany. Whenafile is
deleted,
thecommand'rmxyz'
istyped
in,
where'rm'
is a command name and xyz is the argument that indicates the file to be deleted.
Behind this systemis theideathat thesystem works as a"servant" acceptingcommands.
HOPSdoesnot changethisideaexceptthatitemploys asmanydifferent"servants"
(which
can understand different sets ofcommands) as possible. The "servant"
in HOPS is a
directory
object, in which (in addition tothe standard set of commands forthedirectory
directories.
In thisway,as theusers movethrough thehierarchy,
HOPS provides acustomizedcommand setthatisconnectedto thecurrentworking directory.
1.3.6.
TTY-based
SystemGenerally,
anadvancedprogrammingenvironment requiresuserstospend alotoftime at the keyboard
typing
in text and debugging. Aslong
as the chief task inprogramming istoassign a symbol to an
idea,
thekeyboardwillnotbe eliminated. Evenwhen a system employs menus andicons a great
deal,
programmers stillhave to typeina newly-created name relatedto theconceptthatthey
aretrying
tomodel. Useof apointingdevice,
ormouse,whichis inevitablewithabitmappeddisplay,
interruptsthattypingwork;theprogrammers haveto
keep
switching theirhands between keyboards and mice. So forthis reason, andtomakethesystem as portable aspossible,itwasdesirabletouse aTTY
ratherthan bitmapped
display
forthe basic components ofthe system.However,
ifonewanted to make the user interface
friendly
tonovice programmers,it is still possible toconstruct abitmap-based systemon
HOPS,
becauseofitsprimitivefunctionality.1.4. HOPS
Glossary
The
following
list showsHOPSterminology
inthispaper, most ofwhich is fromGoldberg
[3]. Thepurpose ofthissectionistoclarifythedifferencebetweenSmalltalk-80termsandHOPS terms. Most
importantly,
HOPSimplementsa"class"in adifferentway.object Acomponent oftheHOPS system represented
by
someprivatememoryand a set of operations.
message A request for an object to carry out one of its
operations.
class Anobjectthatdescribestheimplementationof a set of similar objects. This is represented
by
a simpledictionary
inHOPS.instance variable A variable available to a single object for the entire
Ufetime
ofthe object; instancevariablescanbenamed.method
receiver
A description of how to perform one of object's
operations. While a message is an external
representation of the request to an object, a method
describes aninternal specification abouthow tocarry
outthatrequest.
Theobjecttowhicha messageissent.
message symbol The name ofthe typeof operation a messagerequests
ofitsreceiver.
message argument An object that specifies additional informationforan
operation.
block A descriptionof adeferredsequence of actions.
block argument Aparameterthatmustbesupplied when certainblocks
are evaluated.
simple object
compound object
An object that does not inherit any properties from
other objects andhas onlyone method-dictionary. This
isoriginal
terminology
in HOPS.Anothernameof a
directory
in HOPS. Incontrasttoasimpleobject, this object caninheritspropertiesfrom
other objects; therefore it can hold several
method-dictionaries in it. This is also original
terminology
in2.
FunctionalSpecification
2.1.
Object Management
Because everythingin HOPS isrepresented as anobject,theobject structureplays a
significant role in
determining
the system's performance and basic properties, such asprimitive methods. Thebasicideaofanobjectisthatit isacontainerof pointers toobjects.
In
HOPS,
therequirementsforanobjectisthat thenumber of pointers mustbeexpandable(as faras thesystem's resources allow) and reducible atany time,andthattheobjectbe a
sequence of those pointers or simple integer values. A reference counter method is
employedtorecoverfreespace.
2.2. HOPL (Hierarchical
/
Object-OrientedProgramming
Language)
Below isthegrammar ofHOPL. Herenon-terminal symbols arewritteninun-bold
fonts,
andterminal symbols arein bold face. Alternativecategories arelistedonseparatelines;
inafewcases, along
set ofnarrow alternativesispresented on oneline,
markedby
the phrase "one of". An optional terminal or nonterminal symbol carries the subscript
compilation-unit:
method-definition-unit
interactive-unit
method-definition-unit:
method-structure method-definition-unit
method-structure
method-definition:
instance-variable
:= method-structureinstance-variable:
. identifier
. character-string-object
method-structure:
local-declaration-list
opf{
local-declaration-listopt statement-listopt}
interactive-unit:
local-declaration-list
opt statement-listlocal-declaration-list:
local-declaration local-declaration-list local-declaration
local-declaration:
var identifier-list ;
identifier-list:
identifier , identifier-list
identifier
Therearetwocompilation units: a methoddefinitionunitand ainteractiveunit. The
methoddefinitionunitcontains alistofmethod
definitions,
whichdefinehowtorespondtoa message. This unitis normally stored in a permanent
file,
and iscompiled as needed.The current working
directory
gets to accommodate methods with names after thecompilation. Because in
HOPS,
instancevariables and methods sharetheirlocation (in adirectory),
themethod-definitionhastheformof aninstancevariableassignment.The interactive unit is a collection ofHOPL statements. This unit specifies the
format of commands which are typed in from a TTY. This unit can be saved in a
permanent
file,
which is compiled and executedjust like a command batch file. Thestatement:
expression-statement
if-statement
iteration-statement
jump-statement
compound-statementcompound-statement:
{
statement-list opt}
statement-list:
statement statement-list statement
expression-statement: expression opt ;
if-statement:
if
(
expression)
statementif
(
expression)
statement else statementiteration-statement:
while
(
expression)
statementfor
(
expressionopt ; expression opt ; expressionopt)
statementjump-statement:
return term-expressionovt ;
Astatement is similartoaC languagestatement exceptthata switch-statementis
not included in the HOPL statement and that a compound-statementin HOPL does not
containalocal variabledeclaration. IfanexpressionisnotNIL
(constant-object;
describedlater),
thenit isregardedasTRUEinaconditional expression ofif-statementanditeration-statement. The solution of the
"dangling-else"
problem is the same as C language's:
associatingtheelsewiththeclosestpreviouse/se-lessif.
expression:
instance-variable = expression local-variable = expression instance-variable := expression local-variable := expression
message-expression
local-variable:
$
identifierAssignment
statements change the value ofinstance variables or local variables.There are two assignmentoperators:"="
called simply an assignment operatorand ":="
called a quoted assignment operator. Ifthequoted assignmentoperatorisused, the
right-hand- side expression,
remaining not evaluated, is assigned to the variable which is
specified
by
theleft-hand-side
expression.Tocreate aninstance variable,theseassignment operators areusednormally. Even
if the specified instance variable does not exist, the assignment operators create that
variable.
There are three ways to reference a localvariable. In the most formal way, the
dollar sign '$' is prefixed before the variable name identifier. This gives the safest
grammar. Another wayis the dollarsign plus positive number, which indexes the local
variable. The easiest way to represent a local variable is to usejust thevariable name
identifierwithoutthedollarsign. The HOPLcompilertries tomatchtheidentifiertosome
localvariable. Ifitfailstodoso,an errormessagewillbeprinted.
message-expression:
term-expression cascaded-messageopt
cascaded-message
cascaded-message:
special-message : 0pt cascaded-message
keyword-message : cascaded-message
special-message
keyword-message
keyword-message:
identifier multiple-arguments0pt
special-message:
special-identifier object-expressionopt
special-identifier identifieropl
Aspecialidentifierisa sequence ofthe
following
characters:#%&*
buttheidentifier consistingofonlytheequalsign,
'=',
isnota specialidentifier (theequalsignisusedforanassignment statement).
Using
cascadedmessages, several messages can be typed in one statement. Forexample,
1+2factorial:/(2+1 xor
3)
istransformedintothe
following
parsingtree (Figure2.1).Parsed
resultfor"1+2
factorial:
/(2+1 xor3)"tObj
msgSym /
argl
______
tObj
msgSym factorial
tObj
1msgSym +
argl 2
method object method object
method object
tObj
msgSym xor
argl 3
tObj
2msgSym +
argl 1
method object
method object
Figure 2.1 Parsedresultof1+2 factorial: /(2+1xor
3)
A parsing tree, consistingofthemethodobjects,receives the "eval"messagefrom
theHOPL
interpreter,
so that the message propagates through the leaves in thetree andmultiple-arguments:
single-argument , multiple-arguments single-argument
single-argument:
term-expression
term-expression:
object-expression cascaded-
special-message^,
cascaded-special-message
cascaded-special-message:
special-message :opt cascaded-special-message special-message
When akeyword message has several arguments, some special messages can be included ineach argument. Forexample,thekeywordmessage,
1 send 1+2+3,1-3
hastwo arguments(l+2)+3 and 1-3.
object-expression:
constant-object
(
expression)
block-expression local-variable hierarchical-object identifierhierarchical-object:
instance-variable hierarchical-object
A hierarchical-object
instance-variable
A
block-expression:
[
local-declaration-list0pf statement-listopt]
The hierarchical-objectenables us toreference any instance variable
by
movingthroughthehierarchicalstructure. The
following
expressionspecifies aninstancevariable ins2of an objectboundtoaninstancevariableins1 of a parent object oftheparent object ofthecurrentworking directory.
The block-expression is ablock in Smalltalk-80: adescription of adeferred sequence of
actions. Itwillaccept a message"value" witharguments,ifany. Thosearguments should
be declared inside
[
].constant-object: number-object
character-string-object character-object
! hierarchical-objectopt system-constant-object
number-object:
-number-object positive-integer-object
positive-float-object
system-constant-object: one of
TRUE FALSE NIL SELF SUPER
Thecharacter-string-objectandcharacter-objectarerepresentedinthesameway as
inC
language;
thecharacter-string-objectis enclosedby
doublequotesandthecharacter-object
by
single quotes. Theexclamation mark "!"standsfortheroot
directory,
orHOPLinterpreter. It ispossibletospecifya
directory
objectfromtherootdirectory. Thesystem-constant-object FALSE is another name for
NIL;
those internal representations are thesame. SELFandSUPERare pseudovariablesinthesense ofSmalltalk-80terminology.
Miscellaneous LexicalConventions
Acommentbeginswith/* andterminateswith
*/,
orbeginswith//
and terminateswith new-line character orend offile.Anidentifierisasequence oflettersanddigits. The firstcharacter
mustbea
letter;
theunderscorecountsasaletter.Asemicolon ';'
is automaticallyaddedtotheendofastatement
The HOPLinterpreter
interactively
acceptsonlyexpression-statement. Astatementexecuted
interactively
ends with a new-line characterwhich can be an alternative to thecharacter';'. Alltheinterpreterdoes isread an objectfroma
TTY,
evaluateit,
andwritetheresult of theevaluationtoaTTY. The
following
are examples ofconstant-object,wherethesentence
following
"//" isa comment.152
//
integer-object-234.34
//
float-objectwithanunaryoperator
"-"
"helloworld\n"
//character-string-object
//(sameastheonein C language
[6])
'
z' //character-object
//(sameastheoneinClanguage
[6])
The
following
example shows how to work on a customized object(directory),
where"HOSP> " isa system prompt message.
HOPS> 1
project_A project_B
//The '1'message willbe sentto thecurrentdirectory.
//Therearetwoobjects(botharedirectories
//inthiscase).
HOPS> cd .project A
//CcTwill be interpreted
by
therootdirectory,
because//thecurrent
directory
doesnot acceptthatmessage.//The dot'.' issentto thecurrent
directory
with//theargument, project_A,andthen the
directory
object//boundto thename'project_A'willbereturned.
//That
directory
ispassedas an argumentforthemessage//'cd'.
Finally,
theinterpreter (rootdirectory)
changes//thecurrent
directory
to theproject_Adirectory.HOPS> 1
x Increment //The Tmessagewillbesentto thecurrent
//directory
"project_A".//Supposethat'x' is anintegerand
'Increment'
is bound //toa method.
HOPS> .x = 1+2+3
6 //X is boundto6. Theplus
'+'
is sent to 1 and
//theresult of1+2.
HOPS> . Increment type
//ThemethodIncrementreceivesthemessage
//"type",
whichdisplaysthemethod content.var
i;
{.x =
.x +
$i;
}HOPS> Increment 3
HOPS> . Increment vi
//Themessagevi(theUNIXscreeneditor)issentto
//'.Increment'.
Themethodfor"vi"invokestheUNIXvi //ifthereisadocumentation fileattachedto the'.Increment'. //Youcan edit a methodinthisway.2.3. HOPE (Hierarchical
/
Object-OrientedProgramming
Editor)
There are two main objects that support the screen editor HOPE: a line
display
object called "curses"and a scratch fileobject called"ed". Cursesorganizestextlineson a
TTY
display
and manages a current cursor position inwhichinputcharacters areinserted.In order to enhance its portability among many kinds ofTTY
displays,
curses employsUNIX
library
curses asthe nameliterally
describes. It includes some informationabouthow many linesaredisplayedand where eachlinestarts or endsinascreen, sothatitcan
preventthecursorfrom movingaroundthescreenareainwhichnolinesaredisplayed.
Edisa simplelineeditorwhich canbeused withoutthe line
display
object. Itcanretrieve oneline specified
by
a linenumberfromatextfileand edittheline. Itcan alsomove, copy, and delete some lines specified
by
two linenumbers. The scratchfile is atemporary
filewhich isusedforkeeping
up all theeditedlines of atextfile accordingtoKernighan [8].
HOPE works
by
sending messagesto these two objects, curses and ed. The usercan edit atextfile
by invoking
twokindsofcommands:keyboard-mappedcommands andnamed commands. Whenyou open
HOPE,
everykey
typed in first is a command as inUNIX vi editor. For example, if you type in the character
'i',
then you can insertcharactersinto the locationpointed to
by
the cursor.By
typing
in the named commandfollowing
':', you can send messages to HOPE. The customization of the namedcommands is done
by
adding methods onto thedirectory
for HOPE orby inheriting
Figure2.2 shows thestructure ofHOPE as describedabove. Thefile with which
HOPE deals is also an object representing a UNIX ASCII file. The user can handle a
UNIX ASCII file as an object that can accept messages such as concatenate, append a
string, showitscontents, and so on.
screen
Figure 2.2 StructureofHOPE
3
. Program Description3.1. Basic Structure of an Object
Because everyresource in HOPS is an object, it is necessaryto
identify
anobjectsomehow. The identification number for an object consists of 16
bits,
whose positiverepresentation (15 bits withthe most significantbit
being
zero) indicates theindexofanarrayofbasicobjectblocks. The lengthoftheobjectblock is 16 bytesanditcontainssome
informationabout anobject, asdescribed later. The negativerepresentationis usedforthe
identification of special objects, which do not needto beallocated space when
they
arecreated;
they
includesystem constant objects such asNILandTRUE.Thestructureof anobject canbeexplained
following
threesteps,eachofwhichhasaconsiderably independent function.
Inordertoconveniently fulfilltheneedtoallocate new spaceintheprocessofobject
creation, allthepositiveidentificationnumbers are
initially
queuedinafree block stack.Whennew space is neededfor objectcreation, anumberpoppedfromthis stackis used,
andwhendestruction ofobjectsisneeded, theidentificationnumbersofdestroyedobjects
are pushedto this stack.
At that step, every block is considered to be transparent. That
is,
there is nodifference in information among the blocks dequeuedor enqueued. In thenext step, an
object array whose volume can be changed as needed is implemented using these
transparentblocks. Heretheblocksare categorizedintotwo types: header blocksand node
blocks. Figure 3.1 shows howthevolume-changeablearray isconstructed. One halfofa
header block (8
bytes)
is dedicatedtopointingtoup tofourobjects, since eachobject isrepresentedas 16bits(2 bytes). Ifthevolumeof anarraybecomesgreaterthanfourobject
elements, node blocksare addedundertheheader block. Thenodeblockpointstoup to
maximum of32 (4 x
8)
elements. Ifthenumberof elementsis greaterthan32,
thentheaddition ofnodeblocksto thetree-likestructure canbecontinuedinasimilar manner until
thefreeblockstackisexhausted. This is how thepractically infinite lengthofaninstance
objectis attained. This givesHOPS another advantage: the averagetime toaccess each
elementisproportionalto thelogarithmoftheelementnumber(basedon2). This access
speedis relatively
fast,
comparedwith the implementation usingsequentiallists,
whoseelement access coststheorder oftheelement number.
up
to 4(0
depth)
up
to8
(1
depth)
Q
up
to16
(1
depth)
up
to 32(1
depth)
?
up
to40
(2
depths)
up
to 64(2
depths)
up
to 1
28
(2
depths)
?
?
up to 256
(2
depths)
up
to 264(3
depths)
header
block
I
I
node blockBy
taking
advantageoftheunbounded size ofthe tree structure mentionedabove,itispossibleto representan object regardless ofits length. Ontheother
hand,
thereis moreinformation tobeaddedinthis structure. Theupperhalfof a 16-byte headerblockisused
tostoretheinformationwhichthesystem needstohandleanobject,such asthe
flag
whichindicateswhetherthecontents ofanobject are pointersornot.
Figure 3.2illustratesthestructure of aheaderblock. Thefirstslot(2-byte
field)
inthisblock iscalledmagic,whichisactuallytheindexof a method
dictionary
object. Ifthemessageissenttothis object,that
dictionary
objectisusedtolook upthemessage symbol.The second specifies how many words (16
bits)
anobject hasas its slots. Note that thisnumberuniquelydecides the tree structure. Forexample,ifthelength is
15,
then the treeconsists of oneheaderblockandtwonodeblocks. Thethirdisa referencecounter,which
is describedin alatersectionforreference counts. The fourth isa set ofbit
flags,
fourofwhichare explainedhere.
(1)
GC flag: Thisflag
indicates whether the elements heldby
aheader block areobject pointersornot. Itisusedforreference countswhenan objectis discarded.
(2)
KILL flag: Whentheobject spaceiscollected asgarbage,thesystem checksforthis
flag
to decide whether or not a special message is to be sent to this object beforereturning it backto the free blockstack. Thisis used, forexample, todelete a UNLX file
connectedtoaFileobject(anobject
behaving
likeanASCIIfile)
whichisbeing
discarded.(3)
EVALflag: The HOPL interpreter evaluatesevery objectby
sending a specialmessage"eval". This
flag
eliminatesthefrequentsearchforthe "eval"method; iftheflag
isoff, then the objectisregardedas aconstantthroughouttheevaluationprocess,andifthe
flag
ison, thesystemstartstolookforthe "eval"method. Mostoftheobjectsdonothave
eval method in the
dictionary;
their EVAL flags are all off becausethey
are constant(4)
MOUNT flag: Ifadirectory
objectismarked with thisflag
on whencreated, then the fileobjects(objectsbehaving
likeafile containing ASCIIcharacters)created underthat
directory
areautomatically linkedto theUNIXfiles. Whenadirectory
objectiscreatedunderthat
directory,
thesame nameddirectory
iscreatedundertheUNIXfilesystem. Thisis how the projectdevelops programs written in
HOPL;
aFile object containing HOPLsource codesis automatically savedintheUNIXfile. Thismechanism willbe discussed
later indetail.
Header Block
Structure
(16Bytes)
objectindex
magic
length
refc
oattr
objjDtr
[0]
obj_ptr[1]
obj_ptr[2]
obj_ptr
[3]
: message
dictionary
?
(corresponding
toa class
)
: word size of an object
: reference counter
:object attributes:
GC,
KILLEVAL,
MOUNTobject indices or
node block indices
2
bytes
Figure 3.2 A header blockstructure
Table 3.1 summarizestheprocessofconstructingan object. Itis
interesting
tonotethat the process can be compared to the reference model [16]. Each layer's function
described in Table 3.1 is implemented
by
using the lower layer's services. Objectscommunicate to each other
during
theirlifetime,
driving
the services providedby
theTable 3.1 Referencemodelin
implementing
an objectlayer
#
Function
Implementation
Illustration0
To supply
transparent,
regular-size
blocks
as needed
Astack
containing
free
blocks
___-* >E__
1
To
implement
a variable length objectsAtreestructure
consisting
ofa header blockand
some node blocks
ZwWZw77\
2
To represent
any
kindsofobjects,
considering
thecharacteristics ofeach
(positive)
block indices forordinary
objects andnegative numbers
forsystem reserved objects
An object
3.1.1. Basic Operations on an Object
An object in HOPS is a tree structure consisting ofblocks as described above.
Thereareseveralbasicoperations onthis tree structure. Thetreestructureis
invisible;
allthatis observedfromoutsideisavalue's sequence,whoselengthis
infinitely
enlarged orreduced.
Therefore,
twobasicoperations,insertionanddeletionofelements,are provided.Itis possibletoplacean elementanywhereinthesequence. Another importantoperationis
the transformationofdata between atree structure and a
flat,
contiguous memory. Mostprimitive objects are implemented
by
Clanguage,
in which data is represented in flatmemory. In a sense, that transformation is a basic interface between HOPL and C
language.
3.1.2. Physical Structure of Some Primitive Objects
All theobjectsin HOPS areimplemented usingthestructure mentioned above. In
this section, let us take a look at thephysical structures ofsome primitive objects. The
categoryname (class name)oftheprimitive object shownbelowstarts with a capitalletter.
class at all,althoughthesystemdoesnot requireit. Thisconvention comesfrom
Smalltalk-80's notation [3].
(1)
Integer: It isrepresentedphysically in fourbytes,
whichiscalledlong
integer'
inC language terminology. Becauseobject pointer slots of aheader blockare eight
bytes,
enoughtocontainthosefour
bytes,
anintegerobject occupies 16 bytes for justoneheaderblock.
(2)
Float: Itoccupies 8bytes,
whose format is the same as 'double' in UNIX Clanguage.
(3)
String
and Character: It is a sequence ofbytecharacters; ifthelength isodd,thenit is adjustedtoeven byte length
by
terminating
with the nullcharacter. Theobjectlengthkeptinaheader blockstillindicateswordsize,not character size.
A Characterobjectisrepresented innegative 16-bitnumbers. That
is,
when it iscreated,the systemdoesnothavetoallocate spacetoholdthecharacter value. Thenegative
indexnumberitselftellsus whatitrepresents. Therangefrom-100to-355isreservedfor
aCharacterobject, so that those objectscanbe convertedtoASCII characters
by
adding100totheindexnumberinC language.
(4)
C function method (Cmethod): The content of this object consists of a Clanguage function address and atoken ID (2-byte integer). Thetoken ID is supposedto
identify
each messagesymbolin a simple object. Ifthisobject receives a message symbol"eval",
then theCfunctionpointedtoby
thefunctionaddress('fnc')
wouldbeasfollows:(*fnc)(obj,
token, env) OBJobj, env; shorttoken;Typically,
a switch statement insidethefunctionpointedtoby
'fnc' switches Clanguage-written methods
by
using the token ID 'token'. 'Obj'is a target object in the caller's
activation recordobject. This type of object is added toa
dictionary
in theinitializationphase.
(5)
Method: Thisincludesthreeobject pointers: atargetobject,a messagesymbol,and an argument list. Ifthis method receives a message symbol
"eval",
first,
the targetobject andeachargument areevaluated,thenanewactivation recordiscreated,and thenthe
message symbolincludedinthemethod objectissentto theevaluatedtargetobjectwiththe
new activation record
holding,
if any, evaluated arguments. The algorithm will bedescribedin detaillater.
(6)
Method list: Thisconsistsofalistofmethod objectsand aFileobject(anASCIIfilelinkedtoaUNLX
file,
explained moreinthesection"Coordination withUNLX"). Thisistheplace where translatedcodesfor statements separatedwith a semicolon are stored.
The Fileobjectisusedtoreferto thedocumentationofthe translatedcodes. Itisattachedto
aMethodlistwhentranslationstarts,rightaftertheFileobject receivesthemessage"trans".
Theusercan still seetheoriginal source
by
sending'type'to theMethod list.(7)
Dictionary: This isan even-lengthlist inwhich even-numbered(2n)
slotsholdkey
objects andthe correspondingodd-numbered(2n+l)
slotsholdvalueobjects. Whenthe
Dictionary
object receives a message "lookup" with an argumentKEY,
the list isscanned only for even-numbered slots to look up KEY and the corresponding
odd-numbered slot's valueisreturnedif it is found.
Otherwise,
theDictionary
object returnsthespecial objectILLEGAL.
(8)
Directory: Itconsists of a listofkey
objects and alist of value objects. Bothlists havethesamesize,becauseeach
key
object correspondstoeach value object. Noticethat a
Directory
objectis anothernameforacompoundobject,whichis theonlyinstanceobject thatcan inherit methods fromother objects. In that sense, the
key
object andthevalueobject are supposedtobetheinstancevariablename
(using
aString
object) andtheIn
HOPS,
Directory
objects arehierarchically
arranged. To implement this,specialinstancevariables called"!pa" and"!me"are employed. Thevariable "!pa"holdsapointer
toa parent
Directory
object, andthevariable" !me"holdsthenameof aselfDirectory
objectratherthan the self
Directory
objectitself,
inorderto avoidthecyclicalstructure. Figure3.3 illustratesan exampleoftheseinstancevariable connections.
Root
directory (!)
key
value "!pa""!me"
"x"
"aDir"
23
Directory
!.p
"jpa"key
value
'!me" "aDir"
45.6
Directory
S.p.aDir
Figure 3.3 Anexampleof
directory
connections: adirectory !.p
hasaninteger instancevariable"x"and a
directory
instancevariable"aDir",
while adirectory
l.p.aDir hasanfloat instancevariable"y".A
Directory
object, namely a compound object, is not always connected to theparent
directory;
it couldbe assignedto alocal variable in a statement withouthaving
aparentdirectory. Thegeneralrule of "!pa"
and "!me"
connectionsisthat theconnections
arepreserved unless a
directory
is disconnected from its original parentdirectory. If adirectory
isassignedtoalocalvariablerightafterit iscreated,theneventhevariables"!pa"and "!me"
will not existin that directory. There is also a rule aboutdisconnection: ifa
directory
is disconnected fromaninstancevariableandthatvariablebelongstoadirectory
pointedto
by
a "!pa"variableofthedisconnected
directory,
thenthe "!pa"variableofthedirectory
whose connectionisnot completed andthattheconsistencyofthehierarchical
structureispreserved.3.1.3.
Creation
ofObjects
Constantobjects(suchas
integer, float,
orstring)are createdby
a parser. Therearetwo ways tocreate other types of objects. One is to send a message "new" to a method
dictionary
(calledmagic)which is attachedto thefirstslotofaheaderblock in anobjectbeing
created. HOPSinitially
creates a specialdirectory
"l.dict",
under which all themagicsforprimitive objects are boundto thecorrespondingclassname. Forexample, the
instancevariable ".Directory"in"Ldict" holds a method
dictionary
foraDirectory
object.Sothe
following
statement creates a newdirectory
named"aDir"
underthecurrentworking
directory.
.aDir=
!.dict.Directory
new;The otherway to create anobject is to send a message "new" to the same-class instance
object. The
following
statement expressesthesameideaasthatabove..aDir=new;
The message
"new"
is received
by
the current workingdirectory
which is aDirectory
instanceobject. This waygivesthemost concise expression ofcreatingadirectory.
Althoughthese twoarebasicmeans tocreate an object, thereare other waystodo
so,
depending
on thecharacteristics oftheobject. Forexample,anIntervalobjectiscreatedby
sendinga message "to"toanIntegerobject asfollows:.a= 1 to
5;
//
createsanIntervalobjectwhichisa rangefrom 1 to5.3.1.4. Reference Counts
HOPS adopts reference counts
[11]
as memory management algorithms. Thismethodcalledgarbage collection. On theother
hand,
thedisadvantageofthismethodis thatit cannot recover a cyclical structurein whichoneoftheslotsin aobject,directly
orindirectly,
points to the self object.Therefore,
whendesigning
the basic structure ofobjects in
HOPS,
some solutions wereconsideredtoavoid a cyclical structure. The "!me"connection of a
directory,
described in the previous section, is an example of thoseconsiderations.
Two basic operations are provided for the reference counts method:
IncObj
and DecObj.IncObj
simplyincreasesby
onethe 'refc'valuelocated intheheader block ofanobject.
DecObj
decreases therefc valueby
one and ifthevalue becomes zero, then the objectisregarded asdiscarded becausethevalue zero meansthatnothingisconnectedtothisobject. Before collecting blocks used
by
thediscardedobject asgarbage,DecObj
alsostartstodecreasetherefc values of objects held inthe slots ofthediscardedobject.
Thus,
alltheblocksthat thediscardedobject uses arerecursivelycollected and pushedto thetop
ofthefree blockstack.
The
following
three paragraphs show techniques fordealing
with the referencecounts method.
(1)
Referencecounts in an assignmentstatement: Suppose wehavethefollowing
simple assignment statement.
$a=
$b;
An object is boundto each variable before the assignment.
IncObj
will operate on theobjectboundto
$b
andDecObj
willoperate ontheobjectboundto$a.Here,
IncObj
must becalledfirsttoallowforthenextspecial statement.If
DecObj
is firstandtheobjectboundto$aisnot connectedto theothers,thenitisthrownoutbefore
IncObj
operatesonit.(2)
Objectsattainedin anactivationrecord: The lifetimeofanyobjectis determinedby
thereference counter, so it should be bound to something in orderto have a positivereference count. An activationrecord, implemented as anEnvironmentobject in
HOPS,
playstheroleof
holding
allthelocally-bound
objects andanyreturnedobjects. Ifa methodreturns an objectto the caller, the activation recordforthe methodis destroyed and the
returned object is bound to the activation record for the caller. In the process of
destruction,
the objects usedonlyinside the method arediscarded because thereferencecounter goestozero.
(3)
Garbage collection in aparsing phase: Because methods are implemented asobjectsin
HOPS,
theparsingprogram(written in Clanguage)
createsmanyobjectswhichremainunboundtoobject slots.Ifthereis noerror, thenthoseobjects arebundled intoone
methodormethod
list,
whichiscollectedasgarbageafteritsexecution. Butit is difficulttocollectthoseobjects as garbagewhen a syntax erroris found.
Specifically,
HOPSapplies asyntax-directedtranslationscheme
[1]
toparsing,so eachClanguage functioncorrespondstoa nonterminalintheHOPLsyntax. Ifacontrolisaddedtocollectgarbage,theflow of
eachfunctionwillbesodistortedthat thesystem couldlosethe advantage oftheclearflows
of a syntax-directedtranslationscheme.
HOPS solves thisproblem
by
collecting in advance all the objects created in aparsingphase. Thoseobjects areboundto a simpleListobject asits memberswhen
they
are created. Thislistobjectis discardedrightbefore thenextparsingphase starts.
Thus,
3.1.5. C
Language
InterfaceHOPS enables the system to be expanded
by linking
a new object written in Clanguage. It isan unfortunate factthat themorepurelyobject-orientedthe system
is,
themore slowly it runs. The reason is that a method
dictionary
search isfundamentally
essential. HOPS provides the programming environment in which everything can be
written using HOPS objects. But this codingmanner is not always recommendedifthe
message-passing overhead gets intolerable. The user is encouraged to find out the
functional borderwhichsplits programsin HOPLandprogramsin C
language,
consideringthespeed efficiency.
Appendix Ashows an example of an objectwrittenin C language for HOPS.
3.2. Lexical Analyzer and Parser
3.2.1. TTY Interface and Command File Interface
'
UNIX
file
descriptor
VI7
Line Buffer
characters
send "eval"
message
Lexical
Analyzer
token
objects
(1)
method definition(2)
statement listFigure 3.4 A lexicalanalyzerand parserin HOPS
Figure 3.4 shows adiagramofdata flowthroughthelexicalanalyzerand parserin
HOPS. Inputcharacters arepooledinaline buffer.
They
aretranslatedintotokenobjectseventokensarerepresentedasobjects. Tokensareusuallyimplemented
by
twoprimitives:a tokenidentification number and therelated attribute values. Forexample, the lexical
analyzerproducesINTEGER (definedas anumbersomewhere)andits
binary
value12,
sothatusing
INTEGER,
theparser can switchthesyntaxtreebranches correctly and passthevalue 12to thecode generation phase. Butanintegertokenin HOPS is simplyanInteger
object. Its magic (see the section 3.1 and Figure
3.2)
plays the role of the tokenidentificationnumber,andthevalueisembeddedin it.
HOPS has threeinput
interfaces,
as shownin Figure3.4:TTY,
String
Object,
andUNIX file descriptor. The
String
object accepts a message "getm" and returns a parsedmethod object for the character string. Although the TTY interface seems to be
implemented
by
givinga standardinput filedescriptorto theUNIXfile descriptorinterface,
it is differentinseveralrespectsfromtheUNLXfile descriptor interface. Ifa curses mode
(inwhichUNIXcurses
library
isavailable, andthe terminalmodeis switchedtoechoandcbreak) ison,thenTTYinterfaceechoes eachinputcharacter, simulates a
backspace,
and invokes a commandline editor whenreceiving an escape character. Thecommand lineeditorenablesthe usertoedit theinput line whichrefers to the
history
ofrecently-typedcommands,justasin theUNLXKorn Shell [9]. Another difference isthat TTYinterface adds a semicolon at the end of an input line.
According
to the HOPL syntax, everystatement should terminate with a semicolon. The typed-in command
has, however,
traditionally usedaCARRAGE-RETURNorNEW-LINEcharacter asitsterminator. The
compromiseofthe twoideasyieldstheautomaticadditionof a semicolon.
Theoutputoftheparserisalways aMethodlistobjector,if only one statementis
parsed, aMethodobject. Buttherearesyntacticallytwocategoriestobeparsed: a method definitionand a statementlist. There is ordinarily aFileobject under a
directory,
whichcontains allthedefinitionsof methodsinthedirectory. Theparsertranslates thisFileobject intoa methoddefinition. Thestatementlist istypedin fromaTTYterminalorisstoredina
3.2.2 Syntax Analysis
Intheinitialstage ofthesyntaxdesign for
HOPL,
theHOPL grammarwascleanin a sense that the parser needed to look ahead at only one token in order to decide thedeviationtree.
However,
inits finalformtwo token look-aheadwas used. Thestructure oftheparser program wouldhavebeentoocomplicatedtoallow onetokenlookahead,
duetothe problems
described
below.(1)
Localvariablenamingproblem: Localvariablesincludeargumentvariablesandtemporarily-definedvariablesinsidea compound statement.
They
areoriginallyreferenced withadollarsignprefix, '$'.However,
after some programs are writteninHOPL,
adding the '$' sometimes turns out to be cumbersome. For example, consider thefollowing
statementin HOPL.for
(
$i=0;$i<10;$i=$i+l)
$iprint;The use of theprefix gives us an easy-to-readformat in which the variable 'i'
is quickly
found to be a local variable. On the other
hand, typing
the dollar sign on a standard keyboardrequires twokeys,
the'$'key
andtheshiftkey. Thisisnotefficient,considering the frequentuse of thelocal variables in a program.Therefore,
a statement such as thefollowing
is desirable.for
(i=0;i<10;i=i+l)
iprint;Unfortunately
thereisaprobleminthisstatement. Itisagainstthebasicconceptin HOPLthat every starting identifier is to be regarded as a message symbol sent to the current
working directory.
Accordingly,
the original grammar interprets the"i"
as a message
symbol to the current working
directory
in the two expressions, "i+1" and "i print".Finally,
therule addedtothegrammaristhatiftheparsermeets a special character(suchasfor
(i=0;i<10;i=i+l)
$iprint;Ifakeywordmessage is sentto an objectbound to alocal variable, thedollar signmust
precedethelocalvariable name.
(2)
Negativenumberparsingproblem: Intheoriginalgrammar, thelexicalanalyzerrecognized negativenumbers,whosedigitsmust
immediately
followa minus signwithoutany space characters. As aresult,the
following
statementshave syntaxerrors.5
//
Sending
a message'-'
to thecurrentworking
directory
with'5'.//
(There isa space characterbetween'-' and'5'.)
3-5
//
Nomessage symbolbetweentheobject'3'
andtheobject '-5'.
3- -5
//
'3'receives a message ' ' with anargument '5'.
Thereasonfortheerrors originatesinthefactthatthegrammaradopts
binary
operatorsandpostfixoperators,notprefixoperators. Whatwasdonetoeliminatetheabove errorswas
tomaketheminus sign'-' one separatedtokeneventhough the
subsequentcharacterisalso aspecialcharacter,
to
lexically
analyzeonlypositive numbers (without'-'),
*
torecognize negative numbersifa minustokenfollowsa positivenumber
tokenin the HOPL's
'expression',
andtoaddthe
following
productionrules.number-object:
-number-object
positive-integer-object
3.3.
Implementation
ofControl Structure
3.3.1. An
Environment Object
An
Environment
objectisan activation recordin HOPS. Thepurpose ofthisrecordis principallytogive an appropriatescopeto thecurrentlyrunningmethod andtomaintain
the current working directory. At the same time, its practical role is to point to
every
locally-used object andtohave itsreference countbeatleastone,inordertonot permitthe
reference counts algorithmtocollectitas garbage. An Environmentobject iscreatedjust
before a messageis senttoanobject andit is destroyed justafter a valueisreturnedto the
caller'sEnvironmentobject. Whenit is
destroyed,
theDecObj
operatortries toconsume alltheobjectsboundtoitsslots(orinstancevariables)
by decrementing
theirreference counts.