JastAdd & JastAddJ クリックチュートリアル

26
/26 JastAdd & JastAddJ クイックチュートリアル 武 信 CSG 勉強会 2012731 2012/07/31 1 JastAdd & JastAddJ クイックチュートリアル

description

JastAdd の概念を中心に紹介します。

Transcript of JastAdd & JastAddJ クリックチュートリアル

  • 1. JastAdd & JastAddJ CSG 20127312012/07/31 JastAdd & JastAddJ 1 /26

2. JastAdd JastAdd , BNF Java Composite AspectJ 2! 2012/07/31 JastAdd & JastAddJ 2 /26 3. JastAdd AOP Feature-oriented programming / soFware product lines : hKp://jastadd.org/ OOPSLA The jastadd extensible java compiler Grel Hedin (Lund University, Sweden) 2012/07/31 JastAdd & JastAddJ 3 /26 4. JastAddJ JastAdd Java 1.7 Java Java : GluonJ, Aspect bench compiler (AspectJ) 2012/07/31 JastAdd & JastAddJ 4 /26 5. 1/2 AST ex yacc 3 Programenv x = 3, y = 1; x * y + 10; x = 3 +x * x * y; y = 113 * 109xy2012/07/31 JastAdd & JastAddJ 5 /26 6. 2/2 Programclass Program extends ASTNode { void run() { getEnv() getExprs() System.out.println( getExprs(0).eval()); x = 3 +}} y = 1class Plus extends Expr { int eval() { * 10 return getLeft.eval() + getRight.eval(); }}xy2012/07/31 JastAdd & JastAddJ 6 /26 7. JastAdd JastAdd Java AST yacc, ex Beaver JFlex C Java JavaCC 2012/07/31JastAdd & JastAddJ 7 /26 8. AST AST class_name: super ::= child_name:class ; child_name: *.ast ASTNode; abstract Expr: ASTNode; Env: ASTNode; Program: ASTNode ::= [Env] Expr*; ASTNodePlus: Expr ::= Left:Expr Right:Expr; Mult: Expr ::= Left:Expr Right:Expr; Var: Expr ::= ; Literal: Expr ::= ;Program Expr Env Plus MultVar Literal2012/07/31JastAdd & JastAddJ 8 /26 9. : Plus geKer, seKer Plus: Expr ::= Left:Expr Right:Expr; class Plus extends Expr { Expr(Expr p0, Expr p1) { setChild(p0, 0); setChild(p1, 1); } Expr getLeft() { return (Expr)getChild(p0, 0); } void setLeft() { } Expr getRight() { } void setRight() { } ASTNode getParent() { } }2012/07/31 JastAdd & JastAddJ 9 /26 10. : Program ([X]), Y* hasX() getNumY(), getNumY(), getYs() Program: ASTNode ::= [Env] Expr*; class Program extends ASTNode { boolean hasEnv() { } Env getEnv() { } int getNumExpr() { } Expr getExprs(int i) { } // setter }2012/07/31JastAdd & JastAddJ 10 /26 11. AST JastAdd eval() syn inh 2 eval() Eval print() Print 2012/07/31 JastAdd & JastAddJ 11 /26 12. syn 1/2 super AspectJ aspect Eval { syn int Expr.eval(); // Expr eval() // Expr eq Plus.eval() = getLeft().eval() + getRight().eval(); // eq Mult.eval() = { return getLeft().eval() * getRight().eval(); } eq Literal.eval() = getValue(); eq Var.eval() = !; }2012/07/31JastAdd & JastAddJ 12 /26 13. syn 2/2syn Program13 x = 3 + y = 1 eval()3* 10eval()3 1x y2012/07/31 JastAdd & JastAddJ 13 /26 14. Java class Var extends Expr { Program int eval() { // ID return lookup(getID()); } x = 3 + y = 1 int lookUp(String id) { *10 } xy }2012/07/31 JastAdd & JastAddJ 14 /26 15. Java class Var extends Expr { Program int eval() { // ID return lookup(getID()); } x = 3 + y = 1 int lookUp(String id) { n = this; for (;;) { *10 n = n.getParent(); if (n instanceof Program) { Program p = (Program)n; return p.getEnv().value(id); xy }}}}2012/07/31 JastAdd & JastAddJ 15 /26 16. inh 1 aspect LookUp { // lookUp Program inh int Var.lookUp(String id); getExprs() // Program Expr lookUp x = 3 // + y = 1 eq Program.getExpr().lookUp(String id) = // this Program this.getEnv().value(id); } lookUp("x") = 3 * 10lookUp("y") = 1 aspect Eval { : : eq Var.eval() = lookUp(getID()); x y}2012/07/31 JastAdd & JastAddJ 16 /26 17. lazy aspect LookUp { // id lazy inh int Var.lookUp(String id); eq Program.getExpr().lookUp(String id) = this.getEnv().value(id); }2012/07/31JastAdd & JastAddJ 17 /26 18. : 2syn AST inh Program AST x = 3 +y = 1ASTNode *10 Program Expr Env xy PlusMult VarLiteral2012/07/31JastAdd & JastAddJ 18 /26 19. JastAdd AST geKer, seKer inh: 2012/07/31JastAdd & JastAddJ 19 /26 20. AST JastAdd Java loop loop { l = System.in.readln(); if (l == null) break; System.out.println(l); } 2012/07/31JastAdd & JastAddJ 20 /26 21. JastAddJ NullCheck ant, Java 1: AST JastAddJ *.ast ant AST LoopStmt: Stmt ::= Body:Stmt;2012/07/31 JastAdd & JastAddJ 21 /26 22. JastAddJ (*.ex ) { loop" { return sym(Terminals.LOOP); } }2012/07/31JastAdd & JastAddJ 22 /26 23. JastAddJ (*.parser ) statement.s statement s while_stetement Stmt statement = loop_statement.l {: return l; :} ; Stmt statement_no_short_if = loop_statement_no_short_if.l {: return l; :} ; LoopStmt loop_statement = LOOP statement.s {: return new LoopStmt(s); :} ; LoopStmt loop_statement_no_short_if = LOOP statement_no_short_if.s {: return new LoopStmt(s); :} ; 2012/07/31JastAdd & JastAddJ 23 /26 24. Rewriter loop while while when JastAddJ Loop aspect LoopToWhile { rewrite LoopStmt { Block to WhileStmt { Expr cond = new BooleanLiteral(true); Stmt body = getBody(); return new WhileStmt(cond, body); }}}While true Block2012/07/31 JastAdd & JastAddJ 24 /26 25. Rewrite inh NullPointerExcepkon rewrite beforeaFerparent aKrBlockBlock WhileLoop WhileBlock trueBlock true Block2012/07/31JastAdd & JastAddJ 25 /26 26. AST Java AST loop 2012/07/31 JastAdd & JastAddJ 26 /26