cs 345 project presentation ooh a more object-oriented hmm++ alysha behn jesse vera

33
CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Upload: jonathan-roy-cooper

Post on 17-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

CS 345 Project Presentation

OOHA More Object-Oriented Hmm++

Alysha Behn

Jesse Vera

Page 2: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Project Overview: Hmm++

Revise existing BNF to facilitate inheritance and instanceof syntax

Near-complete revision of runtime stack usage Add an object-oriented feature: inheritance

Page 3: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

BNF: what existed previously

Program : {[ Declaration ]|retType Identifier Function | MyClass | MyObject}

Function : ( ) Block

MyClass: Class Idenitifier { {retType Identifier Function}Constructor {retType Identifier Function } }

MyObject: Identifier Identifier = create Identifier callArgs

Constructor: Identifier ([{ Parameter } ]) block

Declaration : Type Identifier [ [Literal] ]{ , Identifier [ [ Literal ] ] }

Page 4: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

BNF: what existed previously

Type : int|bool| float | list |tuple| object | string | void

Statements : { Statement }

Statement : ; | Declaration| Block |ForEach| Assignment |IfStatement|WhileStatement|CallStatement|ReturnStatement

Block : { Statements }

ForEach: for( Expression <- Expression ) Block

Assignment : Identifier [ [ Expression ] ]= Expression ;

Parameter : Type Identifier

Page 5: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

BNF: what existed previously

IfStatement: if ( Expression ) Block

Expression : Conjunction {|| Conjunction }

Conjunction : Equality {&&Equality }

Equality : Relation [EquOp Relation ]

EquOp: == | !=

Relation : Addition [RelOp Addition ]

RelOp: <|<= |>|>=

Addition : Term {AddOp Term }

Page 6: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

BNF: what existed previously

AddOp: + | -

Term : Factor {MulOp Factor }

MulOp: * | / | %

Factor : [UnaryOp]Primary

UnaryOp: - | !

Page 7: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Changes to BNF

RelOp: <|<= |>|>= → RelOp: <|<= |>|>= instanceof

MyClass: Class Identifier { {retType Identifier Function} Constructor {retType Identifier Function } }

→ MyClass: Class Identifier {extends Identifier}

{ {retType Identifier Function} Constructor {retType Identifier Function } }

ObjFunc : Class Dot MyObject Dot Function ( ) Block

→ ObjFunc : MyObject Dot Function ( ) Block

Page 8: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Revising stack

Old Stack

<main>

//Main calls foo

int foo(int num) {

Obj thing = create Obj(20, 4);

int result = 0;

thing.fun(num);

return result;

}

Page 9: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Revising stack

Old Stack

<main>

//Main calls foo

int foo(int num) {

Obj thing = create Obj(20, 4);

int result = 0;

thing.fun(num);

return result;

}

Page 10: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Revising stack

Old Stack

param num

foo::ret

<main>

//Main calls foo

int foo(int num) {

Obj thing = create Obj(20, 4);

int result = 0;

thing.fun(num);

return result;

}

Page 11: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Revising stack

Old Stack

myY

myX

param num

foo::ret

<main>

//Main calls foo

int foo(int num) {

Obj thing = create Obj(20, 4);

int result = 0;

thing.fun(num);

return result;

}

Page 12: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Revising stack

Old Stack

Y = 4

myY X = 20

myX

param num

foo::ret

<main>

//Main calls foo

int foo(int num) {

Obj thing = create Obj(20, 4);

int result = 0;

thing.fun(num);

return result;

}

Page 13: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Revising stack

Old Stack

var result

myY

myX

param num

foo::ret

<main>

//Main calls foo

int foo(int num) {

Obj thing = create Obj(20, 4);

int result = 0;

thing.fun(num);

return result;

}

Page 14: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Revising stack

Old Stack

var value

var result arg num

myY fun::ret

myX

param num

foo::ret

<main>

//Main calls foo

int foo(int num) {

Obj thing = create Obj(20, 4);

int result = 0;

result = thing.fun(num);

return result;

}

Page 15: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Revising stack

Old Stack

var value

var result arg num

myY fun::ret

myX

param num

foo::ret

<main>

//Main calls foo

int foo(int num) {

Obj thing = create Obj(20, 4);

int result = 0;

result = thing.fun(num);

return result;

}?

Page 16: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Revising stack

New Stack

<main>

//Main calls foo

int foo(int num) {

Obj thing = create Obj(20, 4);

int result = 0;

result = thing.fun(num);

return result;

}

Page 17: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Revising stack

New Stack

<main>

//Main calls foo

int foo(int num) {

Obj thing = create Obj(20, 4);

int result = 0;

result = thing.fun(num);

return result;

}

Page 18: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Revising stack

New Stack

param num

foo::ret

<main>

//Main calls foo

int foo(int num) {

Obj thing = create Obj(20, 4);

int result = 0;

result = thing.fun(num);

return result;

}

Page 19: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Revising stack

New Stack

myY

myX

thing

param num

foo::ret

<main>

//Main calls foo

int foo(int num) {

Obj thing = create Obj(20, 4);

int result = 0;

result = thing.fun(num);

return result;

}

Page 20: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Revising stack

New Stack

y = 4

x = 20

myY

myX

thing

param num

foo::ret

<main>

//Main calls foo

int foo(int num) {

Obj thing = create Obj(20, 4);

int result = 0;

result = thing.fun(num);

return result;

}

Page 21: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Revising stack

New Stack

var result

myY

myX

thing

param num

foo::ret

<main>

//Main calls foo

int foo(int num) {

Obj thing = create Obj(20, 4);

int result = 0;

result = thing.fun(num);

return result;

}

Page 22: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Revising stack

New Stack

arg num

fun::ret

var result

myY

myX

thing

param num

foo::ret

<main>

//Main calls foo

int foo(int num) {

Obj thing = create Obj(20, 4);

int result = 0;

result = thing.fun(num);

return result;

}

Page 23: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Changes for inheritance

MyClasses ”point” to their parent classes MyClass copy parent data in constructor MyObjects keep track of runtime type and

compileTime type MyObjects treated like a variable, placed on the

stack Instance Variables referenced by an offset of

their object

Page 24: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Demo continued

int main() {

int x = foo();

println( "It worked!" );

}

int foo()

int temp = 0;

Test objOne = create Test(20, 4);

temp = objOne.fun(2);

Test_2 objTwo = create Test_2(15);

temp = objTwo.fun_2();

temp = objOne.fun(7);

return temp;

}

Page 25: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Inheritance : Demo

Class Test{

int myX;

int myY;

Test(int x, int y){

myX = x;

myY = y;

}

int fun (int match){

println("The result from method fun is: ");

println(myX);

println(myY);

int value = myX + myY;

return value;

}

}

Class Test_2 extends Test{

Test(int x){

myY = 5;

myX = x;

}

int fun_2(){

println("the result from method fun_2 is: ");

println(myX);

println(myY);

return (myX + myY);

}

}

Page 26: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Demo abstract syntax treeProgram (abstract syntax):

Function = main; Return type = int

params =

Block:

int x =

Call: foo, stackOffset=2

args =

Call: println, stackOffset=0

args =

StringValue: It worked!

Function = foo; Return type = int

params =

Block:

int temp =

IntValue: 0

Object = objOne; Object type = Test

args =

IntValue: 20

IntValue: 4

Assignment:

Variable: temp, LOCAL addr=1

Object Function = objOne; Function Name = fun

args =

IntValue: 2

Object = objTwo; Object type = Test_2

args =

IntValue: 15

Assignment:

Variable: temp, LOCAL addr=1

Object Function = objTwo; Function Name = fun_2

args =

Page 27: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Abstract syntax Tree cont.

Assignment:

Variable: temp, LOCAL addr=1

Object Function = objOne; Function Name = fun

args =

IntValue: 7

Return:

Variable: return#foo, LOCAL addr=0

Variable: temp, LOCAL addr=1

Class: Test

int myX

int myY

Function = fun; Return type = int

params =

int match

Block:

Call: println, stackOffset=0

args =

StringValue: The result from method fun is:

Call: println, stackOffset=0

args =

Variable: myX, INSTANCE addr=1

Call: println, stackOffset=0

args =

Variable: myY, INSTANCE addr=2

int value =

Page 28: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Abstract syntax Tree cont.

args =

Variable: myX, INSTANCE addr=1

Call: println, stackOffset=0

args =

Variable: myY, INSTANCE addr=2

Return:

Variable: return#fun_2, LOCAL addr=8

Binary:

Operator: INT+

Variable: myX, INSTANCE addr=1

Variable: myY, INSTANCE addr=2

Constructor = Test

params =

int x

Block:

Assignment:

Variable: myY, INSTANCE addr=2

IntValue: 5

Assignment:

Variable: myX, INSTANCE addr=1

Variable: x, LOCAL addr=8

Page 29: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Demo output

The result from method fun is:

20

4

the result from method fun_2 is:

15

5

The result from method fun is:

20

4

It worked!

Page 30: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Changes to AbstractSyntax.java

Modified the class MyObject to extend Value instead of Statement to help treat MyObjects like variable

Each object gets individual copies of class data MyClass extends Value and implements Type to

compute instanceof Instanceof is added to the enum of Operators

Page 31: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

StaticTypeChecker/SymbolTable

Beyond checking types of assignments, these classes set the static offsets for referencing data within a scope

Calling a constructor now creates a new scope Instance variables no longer remain in the

scope indefinitely Calls to functions now account for local

variables when moving the stack pointer

Page 32: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Interpreter

Interface with the stack is brand newGlobal map of objects and their instancesSpecial interpretation of variables used for instanceof

Page 33: CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera

Questions