// package edu.neu.ccs.demeterf.examples; /** Functional Transformation Test Class. This includes structural duplication, * Num2Str, and a more complicated type unifying traversal, * Eval, demonstrating transformations and Builders with * Traversal Arguments. */ // Import the Transformation Package import edu.neu.ccs.demeterf.*; // Simple Arithmetic Expressions class Exp{} // The usual operators for Binary Expressions... abstract class Op extends Exp{ abstract String op(); Exp left, right; Op(Exp l, Exp r){ left = l; right = r; } public String toString(){ return "("+op()+" "+left+" "+right+")"; } } class Plus extends Op{ String op(){ return "+"; } Plus(Exp l, Exp r){ super(l,r); } } class Minus extends Op{ String op(){ return "-"; } Minus(Exp l, Exp r){ super(l,r); } } class Times extends Op{ String op(){ return "*"; } Times(Exp l, Exp r){ super(l,r); } } class Div extends Op{ String op(){ return "/"; } Div(Exp l, Exp r){ super(l,r); } } // The Leafs... Numbers or Strings class Num extends Exp{ Integer num; Num(Integer i){ num = i; } public String toString(){ return ""+num; } } // Adds one to each leaf, and reconstruct the Tree class AddOne extends IDf{ public Integer apply(Integer i){ return i+1; } } // Evaluates the Tree, 'Unifies' to an Integer class Eval extends IDb{ public Integer combine(Num n, Integer i){ return i; } public Integer combine(Plus p, Integer l, Integer r){ return l+r; } public Integer combine(Minus p, Integer l, Integer r){ return l-r; } public Integer combine(Times p, Integer l, Integer r){ return l*r; } public Integer combine(Div p, Integer l, Integer r){ return l/r; } } // Calculated the Max depth of the Tree, using an Argument // this close to an implementation of my earlier Functional Visitors class Depth extends ID{ public Integer apply(Exp o, Integer a){ return a; } public Integer combine(Exp n, Integer i, Integer a){ return a; } public Integer combine(Exp o, Integer l, Integer r, Integer a){ return Math.max(l,r); } } // Depth Augmentor, updates the traversal argument to keep track // of the current depth in the tree class Da extends IDa{ public Integer update(Op o, Integer i){ return i+1; } } // A form of String Expressions with Tree Depth class Str extends Exp{ String str; int depth; Str(String s, int d){ str = s; depth = d; } public String toString(){ return "\""+str+"\":"+depth; } } //Converts a Number Expression into a String + Depth class Num2Str extends IDf{ public Str apply(Num n, Integer d){ return new Str(""+n,d); } } // Main Test Class public class EvalTest{ private EvalTest(){} static void print(String s){ System.out.println(s); } static public void main(String[] args){ //util.Util.setDebug(true); Exp p = new Div(new Plus(new Plus(new Times(new Num(9), new Num(6)), new Num(8)), new Minus(new Num(5), new Num(7))), new Num(4)); print(" Before: "+p); print(" AddOne: "+new Traversal(new AddOne()).traverse(p)); print(" Eval: "+new Traversal(new Eval()).traverse(p)); Da da = new Da(); print(" Num2Str: "+new Traversal(new Num2Str(), da).traverse(p, 0)); Depth d = new Depth(); print(" Depth: "+new Traversal(d,d, da).traverse(p, 0)); } }