170 likes | 276 Views
CS345 Project Presentation. Language: Hmm ++. Tanmaya Godbole , Melissa Olson, Sriratana Sutasirisap . Project Overview: Hmm++. Revise and correct existing BNF Implement First Class Functions Add an object oriented feature: Classes. BNF: What actually exists in Hmm.
E N D
CS345 Project Presentation Language: Hmm++ TanmayaGodbole, Melissa Olson, Sriratana Sutasirisap
ProjectOverview: Hmm++ • Revise and correct existing BNF • Implement First Class Functions • Add an object oriented feature: Classes
BNF: What actually exists in Hmm • Program : {[ Declaration ]|retType Identifier Function} • Function : ( ) Block • Declaration : Type Identifier [ [Literal] ]{ , Identifier [ [ Literal ] ] } • 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 • IfStatement: if ( Expression ) Block [elseifStatement| Block ] • WhileStatement: while ( Expression ) Block
BNF: What actually exists in Hmm • Expression : Conjunction {|| Conjunction } • Conjunction : Equality {&& Equality } • Equality : Relation [EquOp Relation ] • EquOp: == | != • Relation : Addition [RelOp Addition ] • RelOp: < | <= | > | >= • Addition : Term {AddOp Term } • AddOp: + | - • Term : Factor {MulOp Factor } • MulOp: * | / | % • Factor : [UnaryOp]Primary • UnaryOp: - | !
BNF: What actually exists in Hmm • Primary : callOrLambda|IdentifierOrArrayRef| Literal |subExpressionOrTuple|ListOrListComprehension • callOrLambda : Identifier callArgs|LambdaDef • callArgs : ([Expression |passFunc { ,Expression |passFunc}] ) • passFunc : Identifier (Type Identifier { Type Identifier } ) • LambdaDef : (\\ Identifier { ,Identifier } -> Expression) • IdentifierOrArrayRef : Identifier [ [Expression] ] • subExpressionOrTuple : ([ Expression [,[ Expression { , Expression } ] ] ] ) • ListOrListComprehension: [ Expression {, Expression } ] | | Expression[<- Expression ] {, Expression[<- Expression ] } ]
BNF: What actually exists in Hmm • Identifier : (a |b|…|z| A | B |…| Z){ (a |b|…|z| A | B |…| Z )|(0 | 1 |…| 9)} • Literal : Integer | True | False | ClFloat | ClString • Integer : Digit { Digit } • ClFloat: 0 | 1 |…| 9 {0 | 1 |…| 9}.{0 | 1 |…| 9} • ClString: ” {~[“] }”
BNF: Revised and Corrected • Update the concrete syntax, it matches the existing code. • Change ClFloat , after the dot, should be a ()+ not a ()* old: ClFloat: (0 | 1 |…| 9) {0 | 1 |…| 9}.{0 | 1 |…| 9} new: ClFloat: (0 | 1 |…| 9) {0 | 1 |…| 9}. (0 | 1 |…| 9) {0 | 1 |…| 9}) • In ifStatement : (in the else for ifStatement) old: IfStatement: if ( Expression ) Block [elseifStatement| Block ] new: IfStatement: if ( Expression ) Block [ Block ]
First Class Function: Changes in BNF Old • Primary : callOrLambda|IdentifierOrArrayRef| Literal |subExpressionOrTuple|ListOrListComprehension New • Primary : callOrLambda|IdentifierOrArrayRef|FuncArg| Literal |subExpressionOrTuple|ListOrListComprehension • FuncArg : Identifier ({Parameter})
First Class Function: OldImplementation int main() { list emp = createEmp(); int x = 6000; println( selectDept20(emp, getSelector()) ); } (object, bool) getSelector(){ int x = 1000; return (\ y -> y < x); } list selectDept20(list emp,(object, bool) selector) { int x = 20; return [ (name, sal) | (_, name, _, _, _, sal, dept) <- emp, selector(sal), dept == x]; } list createEmp() { return [ (7839, "KING", "PRESIDENT", 0, "17-NOV-81", 5000, 10), (7369, "SMITH", "CLERK", 7902, "17-DEC-80", 800, 20)]; }
Program (abstract syntax): Function = main; Return type = int params = Block: list emp = Call: createEmp, stackOffset=2 args = int x = IntValue: 6000 Call: println, stackOffset=0 args = Call: selectDept20, stackOffset=3 args = Variable: emp, LOCAL addr=1 Call: getSelector, stackOffset=3 args = Function = getSelector; Return type = (object, bool) params = Block: int x = IntValue: 1000 Return: Variable: return#getSelector, LOCAL addr=0 Lambda: [y] Binary: Operator: < Variable: y, LAMBDA addr=0 Variable: x, LAMBDA addr=1 Function = selectDept20; Return type = list params = list emp (object, bool) selector Block: int x = IntValue: 20 Return: Variable: return#selectDept20, LOCAL addr=0 ListComprehension: ListTupleExpression: Tuple of: Variable: name, LOCAL addr=4 Variable: sal, LOCAL addr=5 TupleGenerator: (null, name, null, null, null, sal, dept) Variable: emp, LOCAL addr=1 Call: selector, stackOffset=0 args = Variable: sal, LOCAL addr=5 Binary: Operator: == Variable: dept, LOCAL addr=6 Variable: x, LOCAL addr=3 Function = createEmp; Return type = list params = Block: Return: Variable: return#createEmp, LOCAL addr=0 ListTupleExpression: List of: ListTupleExpression: Tuple of: IntValue: 7839 StringValue: KING StringValue: PRESIDENT IntValue: 0 StringValue: 17-NOV-81 IntValue: 5000 IntValue: 10 ListTupleExpression: Tuple of: IntValue: 7369 StringValue: SMITH StringValue: CLERK IntValue: 7902 StringValue: 17-DEC-80 IntValue: 800 IntValue: 20 [ (SMITH, 800) ]
First Class Function: NewImplementation int main() { list emp = createEmp(); int x = 6000; println( selectDept20(emp, getSelector(int y)) ); } boolgetSelector(int y) { int x = 1000; return y < x; } list selectDept20(list emp, (int -> bool) selector) { int x = 20; return [ (name, sal) | (_, name, _, _, _, sal, dept) <- emp, selector(sal), dept == x]; } list createEmp() { return [ (7839, "KING", "PRESIDENT", 0, "17-NOV-81", 5000, 10), (7369, "SMITH", "CLERK", 7902, "17-DEC-80", 800, 20)]; }
Program (abstract syntax): Function = main; Return type = int params = Block: list emp = Call: createEmp, stackOffset=2 args = int x = IntValue: 6000 Call: println, stackOffset=0 args = Call: selectDept20, stackOffset=3 args = Variable: emp, LOCAL addr=1 FuncArg: getSelector args = int y Function = getSelector; Return type = bool params = int y Block: int x = IntValue: 1000 Return: Variable: return#getSelector, LOCAL addr=0 Binary: Operator: INT< Variable: y, LOCAL addr=1 Variable: x, LOCAL addr=2 Function = selectDept20; Return type = list params = list emp (int -> bool) selector Block: int x = IntValue: 20 Return: Variable: return#selectDept20, LOCAL addr=0 ListComprehension: ListTupleExpression: Tuple of: Variable: name, LOCAL addr=4 Variable: sal, LOCAL addr=5 TupleGenerator: (null, name, null, null, null, sal, dept) Variable: emp, LOCAL addr=1 Call: selector, stackOffset=0 args = Variable: sal, LOCAL addr=5 Binary: Operator: == Variable: dept, LOCAL addr=6 Variable: x, LOCAL addr=3 Function = createEmp; Return type = list params = Block: Return: Variable: return#createEmp, LOCAL addr=0 ListTupleExpression: List of: ListTupleExpression: Tuple of: IntValue: 7839 StringValue: KING StringValue: PRESIDENT IntValue: 0 StringValue: 17-NOV-81 IntValue: 5000 IntValue: 10 ListTupleExpression: Tuple of: IntValue: 7369 StringValue: SMITH StringValue: CLERK IntValue: 7902 StringValue: 17-DEC-80 IntValue: 800 IntValue: 20
First Class Function: Changes to the Code Changes to Parser.jj • Changed Primary to also include funcArg • funcArg method - identifies the arguments of the first class function that is passed as a parameter. - It creates a FuncArg object which is added to the AST Changes to AbstractSyntax.java • FuncArg class - stores information about parameters and name of the method being passed
First Class Function: Parser.jj Expression primary() : { Expression e; Token t;} { LOOKAHEAD(3) e = callOrLambda() { return e; } | LOOKAHEAD(3) e = funcArg() { return e; } //added later | LOOKAHEAD(2) e = identifierOrArrayRef() { return e; } | e = literal() { return e; } | LOOKAHEAD(2) t = <LBRACE> <RBRACE> { return ListTupleExpression.emptyList(t.beginLine); } | e = subExpressionOrTuple() { return e; } | e = listOrListComprehension() { return e; } /* TODO: Figure out the cast: | type() <LPAREN> e = expression() <RPAREN> { return e; } */ } //function added later Expression funcArg() : {Token id; Declaration dec = null; List<Declaration> args= new ArrayList<Declaration>(); } { id = <IDENTIFIER> <LPAREN> (dec = parameter() {args.add(dec);})* <RPAREN> {return new FuncArg(id, args);} }
First Class Function: AbstractSyntax.java public static class FuncArg extends Expression implements LValue{ private String name; private List<Declaration> args; private intlineNum; public FuncArg(Token t, List<Declaration> a) { name = t.image; lineNum = t.beginLine; args = a; } public void display(int level){ super.display(level); Indenter indent = new Indenter(level); System.out.print(name); indent.display(" args = "); for( Declaration d: args){ d.display(level + 1); } } public String getName(){ return name; } @Override public intgetLineNum(){ return lineNum; } }
First Class Function: Future Changes • Update Interpreter to carry out correct evaluation of first class function • Be able to store the function in a variable • Be able to return function inside function
Classes: Implementation Plan • not modifying files with structs • Syntax class Identifier { Constructor (Identifier)* (Function)* } • Be able to instantiate the class ClassType Identifier = create ClassType(parameters); • Be able to access internal methods with an instance of a class