100 likes | 223 Views
Visitor-Based HMM. Israel Perez, Fayz Rahman , Chinedu Egboh. Changes to HMM. Edited AbstractSyntaxTree class All “node” classes now have accept methods. Added VisitorIF interface
E N D
Visitor-Based HMM Israel Perez, FayzRahman, ChineduEgboh
Changes to HMM • Edited AbstractSyntaxTree class • All “node” classes now have accept methods. • Added VisitorIF interface • Defines an interface for a Visitor. Has visit methods for all “node” classes in AST. Not all are used, but they are there if needed in the future. • Edited Hmm.java • Edited to utilize our new additions.
Changes to HMM • Added PrinterVisitor • A Visitor that prints out the Abstract Syntax Tree. • Added StaticTypeCheckVisitor • A Visitor that performs static-type checking on the Abstract Syntax Tree. • Added InterpreterVisitor • A Visitor that interprets the Abstract Syntax Tree and “runs” the program.
Changes to HMM • Added Souvenir class • Some AST nodes return different things in different contexts. This class holds returns of type boolean, Value, and Type. Used as a unified return type for each method in the visitor interface. • Added additional test programs
Why the Visitor Pattern? • We are separating Functionality from Structure. • We can add a new operation to an existing structure of objects without having to alter the classes of those objects. • In our project, we added PrintVisitor, StaticTypeCheckVisitor, and InterpreterVisitor. All use the same AST, but do very different things.
Interpretation in HMM Old HMM Visitor-Based HMM Evaluates global variables Finds main method, gets its body, creates new Visitor, calls runStatement(Visitor) Visitor finds correct visitor for specific node type, and does whatever it needs to for that node. Possibly recursive. • Evaluates global variables • Finds main method, gets its body, calls runStatement(body) • Goes through list of if/elseifinstanceof blocks, and do whatever it needs to for that node. Possibly recursive. • If node is an Expression, call runExpression • go through more if/elseifinstanceof blocks. Possibly recursive.
Refresher: Features of HMM • List Comprehension • Support for Tuples and Lists • Support for Strings • Built-in functions: • print • println int main() { list test = createTest(); list select = [(name1, name2) | (name1, name2) <- test, name1 == “Fayz” ]; println("Group Members: ”, test); println("Member: “, select); } list createTest() { return[("Fayz", "Rahman"), ("Nick", "Egboh"), ("Israel", "Perez")]; }
Refresher: Features ofHMM • Lambda Expressions • For-each loop int main() { list count = [1,2,3]; for (n <- count) { intdoubleFunc = application(n, (\x -> x*2) ); println("(\x -> (2*x)) applied to ", n, " = ", doubleFunc); } } int application(object x, (object, object) a) { intresult = a(x); return result; }
Refresher: Features of HMM int main() { list count = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]; intx = 10; list output = selector(count, lambdaFilter()); println("Count: ", count); println("Filtered: ", output); } (object, bool) lambdaFilter() { intx = 30; return (\y -> y < x); } list selector(list nums, (object, bool) filter) { return [ x | x <- nums, filter(x) ]; } • Static Scoping • Static Binding (variables in lambdas)