Design of OO language

Page 1

Design of OO language Georgiana Tache CSCE 550


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 objval


Abstract syntax Expression ::= 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


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


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


Expressed & Denoted values ●

ExpVal = Int + Bool + Proc + Object + Entry + Entry-list + Dummy DenVal = ExpVal (Proc = for both methods and classes)


ADT for expressed values Entry > make-entry : symbol x expval x boolean-> entry symbol = field/method/class name expval = 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 name listOf(symbol) = list of classes inherited expval = 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


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


Inheritance (1) 1) proto-f id1 . id2 = expr proto-f-obj-exp : identifier x identifier x expression -> undefined Finds the field or method denoted by an identifier within an object. Some steps: myObj = find object named id1 myClass = get class of myObj myfield = find field/function named id2 in myObj -> if id2 already exists in myObj, then no change happens else, 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 id myClass = get class named id2 for each field f from myClass: if f exists in myObj then nothing; else copy (f, value of f, privacy) into myObj


Inheritance (2) 3) prototype-m id1 . id2 = method (param) expr prototype-m-class-exp : identifier x identifier x identifier x expression -> undefined myclass = get the class denoted by id1 mymethod = create the new method (proc-val) create new field in myclass as an entry (methodname, proc, privacy true) 4) prototype-c id = expr prototype-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


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)


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.