Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 AspectJ Assignment. 2 The simple language Tiny program y=2; x=2+y*3; z=x+y*2; –A program is a sequence of assignments; –Expressions on the right hand.

Similar presentations


Presentation on theme: "1 AspectJ Assignment. 2 The simple language Tiny program y=2; x=2+y*3; z=x+y*2; –A program is a sequence of assignments; –Expressions on the right hand."— Presentation transcript:

1 1 AspectJ Assignment

2 2 The simple language Tiny program y=2; x=2+y*3; z=x+y*2; –A program is a sequence of assignments; –Expressions on the right hand side of assignment statements are arithmetic expressions; –Assume variables are always of integer types; –Arithmatic operators include +, -, *, and /. There are no other operators. –Assume input is always correct.

3 3 Parsing The parser creates an abstract syntax tree represented in Program class public class UseParser { public static void main(String[] args) { try { File inputFile = new File("calc.tiny"); Calc2Parser parser = new Calc2Parser(new Calc2Scanner( new FileReader(inputFile))); Program pm = (Program) parser.parse().value; System.out.println(pm.toString()); } catch (Exception e) { e.printStackTrace(); }

4 4 Parsing result for z=x+y*2 - z - + - x - * - y - 2 … Program.java Assignment.java Expr.java

5 5 Program class consists of a vector of statements public class Program { private Vector statements; public static Hashtable varTable=new Hashtable (); public Program(Statement s) { statements = new Vector (); statements.add(s); } public Program(Statement s, Program p) { statements = p.getStatements(); statements.add(s); } public String toString(){ … }

6 6 Assignment class public class Assignment extends Statement{ private String lhs; private Expr rhs; public Assignment(String l, Expr r){ lhs=l; rhs=r; Program.varTable.put(l, r); } public String getLHS(){ return lhs;} public Expr getRHS() {return rhs; } }

7 7 Your output Assignment 0 is: y=2 2==> ? 2==>2 Assignment 1 is: x=(2+(y*3)) (2+(y*3))==> ? 2==> ? 2==>2 (y*3)==> ? y==> ? 2==> ? 2==>2 3==> ? 3==>3 (2*3)==>6 (2+(2*3))==>8 Assignment 2 is: z=(x+(y*2)) (x+(y*2))==> ? x==> ? (2+(2*3))==> ? 2==> ? 2==>2 (2*3)==> ? 2==> ? 2==>2 3==> ? 3==>3 (2*3)==>6 (2+(2*3))==>8 8==>8 (y*2)==> ? y==> ? 2==> ? 2==>2 2==> ? 2==>2 (2*2)==>4 (8+(2*2))==>12 Number of Expressions:11

8 8 Print out the program Print out the parsed program y = 2; x = (2+(y*3)); z = (x+(y*2)); Suppress printout in the parser by skipping calls to System.out.println(..) in UseParser void around() : call(* *..print*(..)) && !within(A2) { } Note that call(* System.out.println(..)) Won’t work public class UseParser { public static void main(String[] args) throws Exception{ File inputFile = new File("calc.tiny"); Calc2Parser parser = new Calc2Parser (new Calc2Scanner( new FileReader(inputFile))); Program pm = (Program) parser.parse().value; System.out.println(pm.toString()); }

9 9 Print out the parsed program Define your own print method in classes such as Expr, Assignment. Note that the method will be added inside the Expr class. public String Expr.print(){ String result=""; if (this.isPrimitive() && this.getValue() != null) { return this.getValue().toString(); } else if (this.isPrimitive() && this.getID() != null) { result=this.getID(); } else { String op = this.getOp(); Expr left = this.getLeft(); Expr right = this.getRight(); result= "("+left.print()+op+right.print()+")"; } return result; } public class Expr { private Integer value; private boolean primitive = false; private String id; private Expr left; private Expr right; private String op; public Expr() { super(); } public Expr(Expr l, Expr r, String o) { left = l; right = r; op = o; } public Expr(Integer i) { value = i; primitive = true; } … }

10 10 Print out the total number of expressions in this program count calls to constructors static int Expr.exprCount=0; after(Expr e): execution( Expr.new(..)) && this(e) { e.exprCount++; }

11 11 Evaluate the program y===>2 x===>8 z===>12 add evaluation method into Expr class public Integer Expr.evaluate() { Integer val; if (this.isPrimitive() && this.getValue() != null) { return this.getValue(); } else if (this.isPrimitive() && this.getID() != null) { val = Program.varTable.get(this.getID()).evaluate(); } else { String op = this.getOp(); Expr left = this.getLeft(); Expr right = this.getRight(); if (op.equals("+")) { val = left.evaluate() + right.evaluate(); } else if (op.equals("*")) { val = left.evaluate() * right.evaluate(); … public class Expr { private Integer value; private boolean primitive = false; private String id; private Expr left; private Expr right; private String op; public Expr() { super(); } public Expr(Expr l, Expr r, String o) { left = l; right = r; op = o; } public Expr(Integer i) { value = i; primitive = true; } … }

12 12 Print indented trace of the evaluation process –check JoinPointTraceAspect in account example –Count the nested level before(): call(* *.evaluate()){ indentLevel++; Expr exp = (Expr) thisJoinPoint.getTarget(); for (int i = 0, sp = indentLevel * 3; i < sp; i++) System.out.print(" "); System.out.println(exp.print() + "==> ?"); }


Download ppt "1 AspectJ Assignment. 2 The simple language Tiny program y=2; x=2+y*3; z=x+y*2; –A program is a sequence of assignments; –Expressions on the right hand."

Similar presentations


Ads by Google