11
Design of OO language Georgiana Tache CSCE 550

Design of OO language

Embed Size (px)

Citation preview

Page 1: Design of OO language

Design of OO languageGeorgiana Tache

CSCE 550

Page 2: Design of OO language

Idea

● The program happens inside a begin .. end block

● After each expression inside the block, the environment is enriched with a new pair (key, value) if an assignment happened

● Classes and procedures are all proc-val● Objects (instantiating classes) are of type obj-

val

Page 3: Design of OO language

Abstract syntaxExpression

::= init Identifier = class (Identifier) Expression ;; class creation::= (Expression Expression) ;; regular call-exp for a method::= var Identifier = new class () Expression ;; creating object from anonymous

class::= var2 Identifier = new Identifier (Expression) ;; creating object from a

known class

::= public-f Identifier = Expression ;; declaring public fields in class::= private-f Identifier = Expression ;; decl. private fields in class::= public-m Identifier = method (Identifier) Expression ;; declaring public

methods in class::= private-m Identifier = method(Identifier) Expression ;; decl. Private

methods in class

::= set Expression . Identifier = Expression ::= access Expression . Identifier ;; accessing a public field from an object::= call Identifier . Identifier (Expression) ;; accessing a public method from

an object

Page 4: Design of OO language

Abstract syntax (2)::= proto-f Identifier . Identifier = Expression ;; adding a prototype field for an

OBJECT::= proto-m Identifier . Identifier = method (Identifier) Expression ;; adding a

new prototype method for an object::= proto-c Identifier = Identifier (Expression) ;; adding a new prototype

method for an object

::= prototype-f Identifier . Identifier = Expression ;; adding a prototype field to a CLASS

::= prototype-m Identifier. Identifier = method (Identifier) Expression ;; adding a new prototype method for a class

::= prototype-c Identifier = new class () Expression ;; adding a new prototype class for a class

::= clone Identifier from Identifier ;;returns a new (independent) copy to an obj

Page 5: Design of OO language

Concrete syntax (expression ("init" identifier "=" "class" "(" identifier ")" expression) class-exp)

(expression("var" identifier "=" "new" "class" "(" ")" expression)

new1-exp)

(expression("var2" identifier "=" "new" identifier "(" expression ")")

new2-exp)

(expression ("public-f" identifier "=" expression) assign-publicf-exp)

;; etc

(expression("proto-f" identifier "." identifier "=" expression)

proto-f-obj-exp)

;; etc in lang.scm

Page 6: Design of OO language

Expressed & Denoted values

● ExpVal = Int + Bool + Proc + Object + Entry + Entry-list + Dummy

● DenVal = ExpVal

(Proc = for both methods and classes)

Page 7: Design of OO language

ADT for expressed values

Entry> make-entry : symbol x expval x boolean-> entry

symbol = field/method/class nameexpval = value (whatever value for a field; proc-val for a method and class)boolean = privacy (#t for public, #f for private)

Entry-list --- useful for storing an environment in an object> entry-list is a listOf (entry)

Object> make-object : symbol x listOf(symbol) x expval -> object

symbol = object namelistOf(symbol) = list of classes inheritedexpval = own environment (an entry-list-val)

Proc changes:> procedure : symbol x symbol x expval x envir → proc

● the first symbol represents the name of procedure/class● the rest has the same meaning

Page 8: Design of OO language

Data structures(define-datatype object object?

(make-object(objName symbol?)(clsInherit (list-of symbol?))(envir expval?) ;;; more precisely it should be entry-list-val

))

(define-datatype entry entry?(make-entry

(fieldName symbol?)(value expval?)(privacy boolean?) ; #t public, #f private

))

ETC

Page 9: Design of OO language

Inheritance (1)1) proto-f id1 . id2 = exprproto-f-obj-exp : identifier x identifier x expression -> undefinedFinds the field or method denoted by an identifier within an object.

Some steps: myObj = find object named id1myClass = get class of myObj myfield = find field/function named id2 in myObj

-> if id2 already exists in myObj, then no change happenselse, myObj creates a new field id2 = value, privacy #t

and myClass creates a new field id2 = value (default value)

2) proto-c id = id2 (param)proto-c-obj-exp : identifier x identifier x expression -> undefined

Steps:myObj = find object named idmyClass = get class named id2for each field f from myClass:

if f exists in myObj then nothing;else copy (f, value of f, privacy) into myObj

Page 10: Design of OO language

Inheritance (2)3) prototype-m id1 . id2 = method (param) exprprototype-m-class-exp : identifier x identifier x identifier x expression -> undefined

myclass = get the class denoted by id1mymethod = create the new method (proc-val) create new field in myclass as an entry (methodname, proc, privacy true)

4) prototype-c id = exprprototype-c-class-exp : identifier x expression -> undefined

Same mechanism as in 3) , just that on the right side there is a class with more fields, they are adopted in class denoted by id.

;; etc, in interp.scm

Page 11: Design of OO language

Others

● clone-exp : identifier x identifier → object

The resulting identifier points to a new object (deep copy, the 2 objects won't depend on each other)