Homework must be submitted via D2L. All submitted files (2 for this assignment sol8A.e8 and sol8B.e8) must be submitted in the appropriate D2L directory in the drop box HW8. It is your responsibility to submit the homework in the proper format with the proper names. For example include a comment at the very top with your name and email address.
-- Homework 7 Tom Smith tom_smith@gmail.com
All programs mentioned can be downloaded from the this document
In this homework we consider a variant of simple un typed object oriented language we call E8. The language, E8, has the following features:
E8's concretized abstract syntax is given by the following grammar:
prog := { decl } 'init' exp decl := '(' 'val' id exp ')' -- a named value (immutable variable) | '(' 'ref' id [ exp ] ')' -- mutable variable | '(' 'fun' id '(' {id } ')' exp ')' -- function | '(' { 'left' | 'right' | 'non' } operator ')' -- associativity of operator field := '(' 'def' id exp ')' -- a constant field | '(' 'var' id [ exp ] ')' -- a mutable field | '(' 'method' id '(' { id } ')' exp ')' -- a method exp := id | int | bool | string | | '(' 'local' { decl } exp ')' -- Binding or naming mechanism | '(' 'write' string exp ')' -- output | '(' 'block' { exp } ')' -- sequencing | '(' ':=' id exp ')' -- assigment to mutable variables | '(' object [ 'inherits' exp ] { field } [ 'init' exp ] '}' -- object construtction | '(' exp '.' fieldselection { exp } ')' -- message request | 'self' -- the current ject | 'done' -- the unit object | 'super' -- the super class of the current object | '(' 'return' id exp ')' -- An exception, non-standard return from a method. | '(' 'if' exp exp exp ')' | '(' 'while' exp exp ')' | '(' '@' exp { exp } ')' -- function application | '(' 'lamba' '(' { id } ')' exp ')' -- lambda abstraction | '(' operator { exp } ')' -- associative "infix" operators application | '[]' -- the object with no behaviors fieldselection := id | '(' id { exp } ')' id := letter { letter | digit } operator = ["+*-/!@#$%^&~=?<>:]+ -- a sequence of these operator characters
As before, comments may be included by enclosing them between comment braces '{-' and '-}', and they may be nested.
Note that the syntax for expressions has been divided into groups, where each group supports one aspect of the language.
enter Exp> 4 4 [+,-,*,/,asString,=,<=]The valuation of the expression '3' evaluates to the object that responds to requests for '+', '-', '*', '/', 'asString', '=' and '<=' .
enter Exp> (object (def x 5) (def y 2) (method add () (+ (self.x) (self.y)))) [x,y,add]with three field, two constants and one method. Note because field definition does not bring any names into scope. The method 'add' must use '(self.x)' to access that objects 'x' field.
enter Exp> ((object (def x 5) (def y 2) (method add () (+ (self.x) (self.y)))) . add ) 7 [+,-,*,/,asString,=,<=] enter Exp> ((object (def x 5) (def y 2) (method add () (+ (self.x) (self.y)))) . bad ) Message not understood: bad [x,y,add] enter Exp> ("abc".(++ "123").len) 6 [+,-,*,/,asString,=,<=] enter Exp> ("abc".(++ "123").len.(+ 1)) 7 [+,-,*,/,asString,=,<=] enter Exp>Note that if an object does not respond to a behavior a runtime error is raised.
enter Exp> done done [asString] enter Exp> (object inherits done (def x 5) (def y 3)) done [x,y,asString] enter Exp> ((object inherits done (def x 5) (def y 3)).asString) "done" [sub,at,++,len,=,asString,<=]Note that the simple object 'done' has only one behavior 'asString'. The anonymous object inherits that behavior. And if it is called on the new object it behaves exactly as its parent object.
(val i3 (object inherits done (def x 5) (def y 3) (method asString () (++ (super.asString) " + " (self.x.asString) " + " (self.y.asString))))) enter Exp> i3 done[x 5, y 3] enter Exp> (i3.asString) "done + 5 + 3" enter Exp>
(val cell (object (var count 0) (method new (x) (object (def head x) (def number (cell.count)) (var tail) init (:= (cell.count) (+ 1 (cell.count))))) )) enter Exp> (cell.new 4) [head 4, number 0, tail@6] enter Exp> (cell.new 5) [head 5, number 1, tail@7]Note that every new cell has a new (and different) number.
| '(' 'if' exp exp exp ')' | '(' 'while' exp exp ')' | '(' '@' exp { exp } ')' -- function application | '(' 'lambda' '(' { var } ')' exp ')' -- lambda abstraction | '(' operator { exp } ')' -- associative "infix" operators application | '[]' -- the object with no behaviorsOne can observe this using the :m expression in the interactive loop.
enter Exp> :m (if tst thencl elsecl) -- if expressions (if tst thencl elsecl) --> (tst.ifTrueFalse (local (fun lam () thencl) lam) (local (fun lam () elsecl) lam)) enter Exp> :m (lambda (x y) (+ x y)) -- lambda abstarction (\ (x y) (+ x y)) --> (local (fun lam (x y) (+ x y)) lam) enter Exp> :m (@ f 4 5 6) -- function application (@ f 4 5 6) --> (f.apply 4 5 6) enter Exp> :m (while tst body) -- while loops (while tst body) --> (local (fun loop () (tst.ifTrueFalse (local (fun lam () (block body (loop.apply) )) lam) (local (fun lam () done) lam))) (loop.apply)) enter Exp> :m (+ 2 3 4 5) -- left associative 'infix' operator (+ 2 3 4 5) --> (((2.+ 3).+ 4).+ 5) enter Exp> :m (++ "a" "b" "c" "d") -- right associative 'infix' operator (++ "a" "b" "c" "d") --> ((("d".++ "c").++ "b").++ "a") enter Exp> :m [] -- the empty object with no behavior [] --> (object)
Your job in this homework is to write 2 small object oriented programs
(data (Tree a) (#leaf) (#Tip Int a) (#node Int a (Tree a) (Tree a)))Note the extra tip node. Your tree should be implemented using objects since their are no data declarations in E8. Define a named class, tree, along the lines of the list class in test1.e8. The tree class should have exactly 3 fields.