330 likes | 414 Views
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.
E N D
CS 345 Project Presentation OOHA 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
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 ] ] }
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
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 }
BNF: what existed previously AddOp: + | - Term : Factor {MulOp Factor } MulOp: * | / | % Factor : [UnaryOp]Primary UnaryOp: - | !
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
Revising stack //Main calls foo int foo(int num) { Obj thing= create Obj(20, 4); int result = 0; thing.fun(num); return result; }
Revising stack //Main calls foo int foo(int num) { Obj thing= create Obj(20, 4); int result = 0; thing.fun(num); return result; }
Revising stack //Main calls foo int foo(int num) { Obj thing= create Obj(20, 4); int result = 0; thing.fun(num); return result; }
Revising stack //Main calls foo int foo(int num) { Obj thing= create Obj(20, 4); int result = 0; thing.fun(num); return result; }
Revising stack //Main calls foo int foo(int num) { Obj thing= create Obj(20, 4); int result = 0; thing.fun(num); return result; }
Revising stack //Main calls foo int foo(int num) { Obj thing= create Obj(20, 4); int result = 0; thing.fun(num); return result; }
Revising stack //Main calls foo int foo(int num) { Obj thing= create Obj(20, 4); int result = 0; result = thing.fun(num); return result; }
Revising stack //Main calls foo int foo(int num) { Obj thing= create Obj(20, 4); int result = 0; result = thing.fun(num); return result; } ?
Revising stack //Main calls foo int foo(int num) { Obj thing= create Obj(20, 4); int result = 0; result = thing.fun(num); return result; }
Revising stack //Main calls foo int foo(int num) { Obj thing= create Obj(20, 4); int result = 0; result = thing.fun(num); return result; }
Revising stack //Main calls foo int foo(int num) { Obj thing= create Obj(20, 4); int result = 0; result = thing.fun(num); return result; }
Revising stack //Main calls foo int foo(int num) { Obj thing= create Obj(20, 4); int result = 0; result = thing.fun(num); return result; }
Revising stack //Main calls foo int foo(int num) { Obj thing= create Obj(20, 4); int result = 0; result = thing.fun(num); return result; }
Revising stack //Main calls foo int foo(int num) { Obj thing= create Obj(20, 4); int result = 0; result = thing.fun(num); return result; }
Revising stack //Main calls foo int foo(int num) { Obj thing= create Obj(20, 4); int result = 0; result = thing.fun(num); return result; }
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
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; }
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); } }
Demo abstract syntax tree Program (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 =
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 =
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
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!
Changes to AbstractSyntax.java Modified the class MyObject to extend Value instead of Statementto 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
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
Interpreter Interface with the stack is brand new Global map of objects and their instances Special interpretation of variables used for instanceof