Changeset 3138

Show
Ignore:
Timestamp:
12/02/08 17:29:50 (12 months ago)
Author:
sukyoungryu
Message:

[ast refactoring] Merged GeneratedExpr? and For.

Location:
trunk/ProjectFortress
Files:
12 modified

Legend:

Unmodified
Added
Removed
  • trunk/ProjectFortress/astgen/Fortress.ast

    r3137 r3138  
    723723                         Option<Block> elseClause = Option.<Block>none()); 
    724724                /** 
    725                  * for expression 
    726                  * DelimitedExpr ::= for GeneratorClauseList DoFront end 
    727                  * e.g.) for i <- sequential(1:5) do 
    728                  *         print (i " ") 
    729                  *       end 
    730                  */ 
    731                 For(List<GeneratorClause> gens, Block body); 
    732                 /** 
    733725                 * if expression 
    734726                 * DelimitedExpr ::= if CondExpr then BlockElems Elifs? Else? end 
     
    834826                 */ 
    835827                While(GeneratorClause testExpr, Do body); 
     828                /** 
     829                 * for expression 
     830                 * DelimitedExpr ::= for GeneratorClauseList DoFront end 
     831                 * e.g.) for i <- sequential(1:5) do 
     832                 *         print (i " ") 
     833                 *       end 
     834                 * 
     835                 * generated expression 
     836                 * BlockElem ::= Expr(, GeneratorClauseList)? 
     837                 * e.g.) print (i " "), i <- sequential(1:5) 
     838                 */ 
     839                For(List<GeneratorClause> gens, Block body); 
    836840                /** 
    837841                 * comprehension or accumulator 
     
    940944                    LocalVarDecl(List<LValue> lhs, 
    941945                                 Option<Expr> rhs = Option.<Expr>none()); 
    942                 /** 
    943                  * generated expression 
    944                  * BlockElem ::= Expr(, GeneratorClauseList)? 
    945                  * e.g.) print (i " "), i <- sequential(1:5) 
    946                  */ 
    947                 GeneratedExpr(Expr expr, List<GeneratorClause> gens); 
    948946                /** 
    949947                 * expression that is simple or using operators 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/desugarer/FreeNameCollector.java

    r3132 r3138  
    4545    // A stack keeping track of all nodes that can create new scope 
    4646    // TraitDecl, ObjectDecl, FnDecl, FnExpr, IfClause, For, LetFn, LocalVarDecl, 
    47     // Label, Catch, Typecase, GeneratedExpr, While, and ObjectExpr 
     47    // Label, Catch, Typecase, While, and ObjectExpr 
    4848    private Stack<Node> scopeStack; 
    4949    // A stack keeping track of (potentially nested) object exprs 
     
    212212 
    213213    @Override 
    214     public void forGeneratedExpr(GeneratedExpr that) { 
    215         scopeStack.push(that); 
    216         super.forGeneratedExpr(that); 
    217         scopeStack.pop(); 
    218     } 
    219  
    220     @Override 
    221214    public void forWhile(While that) { 
    222215        scopeStack.push(that); 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/desugarer/ObjectExpressionVisitor.java

    r3123 r3138  
    355355        scopeStack.push(that); 
    356356        Node returnValue = super.forTypecase(that); 
    357         scopeStack.pop(); 
    358         return returnValue; 
    359     } 
    360  
    361     @Override 
    362         public Node forGeneratedExpr(GeneratedExpr that) { 
    363         scopeStack.push(that); 
    364         Node returnValue = super.forGeneratedExpr(that); 
    365357        scopeStack.pop(); 
    366358        return returnValue; 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/disambiguator/ExprDisambiguator.java

    r3137 r3138  
    4848import com.sun.fortress.nodes.FnRef; 
    4949import com.sun.fortress.nodes.For; 
    50 import com.sun.fortress.nodes.GeneratedExpr; 
    5150import com.sun.fortress.nodes.GeneratorClause; 
    5251import com.sun.fortress.nodes.GrammarDef; 
     
    831830 
    832831    @Override 
    833         public Node forGeneratedExpr(GeneratedExpr that) { 
    834         Pair<List<GeneratorClause>,Set<Id>> pair = bindInListGenClauses(this, that.getGens()); 
    835         ExprDisambiguator extended_d = this.extendWithVars(pair.second()); 
    836         Option<Type> type_result = recurOnOptionOfType(that.getExprType()); 
    837         return forGeneratedExprOnly(that, type_result, 
    838                                     (Expr)that.getExpr().accept(extended_d), 
    839                                     pair.first()); 
    840     } 
    841  
    842  
    843     @Override 
    844832        public Node forAccumulator(Accumulator that) { 
    845833        // Accumulator can bind variables 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/typechecker/TypeChecker.java

    r3137 r3138  
    22852285                                returnType = wrap(bodyType); 
    22862286                        } 
    2287                          
     2287 
    22882288//                         System.err.println("Location: " + that.getSpan()); 
    22892289//                         System.err.println("body type: " + bodyType.getClass() + ":" + bodyType); 
    2290 //                         if (bodyType instanceof TraitType) {  
     2290//                         if (bodyType instanceof TraitType) { 
    22912291//                             System.err.println("name: " + ((TraitType)bodyType).getName()); 
    22922292//                             System.err.println("args:" + ((TraitType)bodyType).getArgs()); 
    22932293//                         } 
    22942294//                         System.err.println("return type: " + returnType.unwrap().getClass() + ":" + returnType.unwrap()); 
    2295 //                         if (returnType.unwrap() instanceof TraitType) {  
     2295//                         if (returnType.unwrap() instanceof TraitType) { 
    22962296//                             System.err.println("name: " + ((TraitType)returnType.unwrap()).getName()); 
    22972297//                             System.err.println("args:" + ((TraitType)returnType.unwrap()).getArgs()); 
     
    25742574        } 
    25752575 
    2576         @Override 
    2577         public TypeCheckerResult forGeneratedExpr(GeneratedExpr that) { 
    2578                 Pair<List<TypeCheckerResult>,List<LValue>> pair = recurOnListsOfGeneratorClauseBindings(that.getGens()); 
    2579                 TypeChecker extend = this.extend(pair.second()); 
    2580                 TypeCheckerResult body_result = that.getExpr().accept(extend); 
    2581                 List<TypeCheckerResult> res = pair.first(); 
    2582  
    2583                 // make sure body type-checked 
    2584                 if( !body_result.isSuccessful() ) 
    2585                         return TypeCheckerResult.compose(that, subtypeChecker, body_result); 
    2586  
    2587                 //make sure body has type void? 
    2588                 String err = "Body of generated expression must have type VOID but had type " + body_result.type().unwrap(); 
    2589                 TypeCheckerResult void_body = checkSubtype(body_result.type().unwrap(), Types.VOID, that.getExpr(), err); 
    2590  
    2591                 GeneratedExpr new_node = new GeneratedExpr(that.getSpan(), 
    2592                                 that.isParenthesized(), 
    2593                                 Option.<Type>some(Types.VOID), 
    2594                                 (Expr)body_result.ast(), 
    2595                                 (List<GeneratorClause>)TypeCheckerResult.astFromResults(res)); 
    2596  
    2597                 TypeCheckerResult result = TypeCheckerResult.compose(new_node, subtypeChecker, body_result, void_body, 
    2598                                 TypeCheckerResult.compose(new_node, subtypeChecker, res)).addNodeTypeEnvEntry(new_node, typeEnv); 
    2599                 return TypeCheckerResult.compose(new_node, Types.VOID, subtypeChecker, result); 
    2600         } 
    2601  
    26022576        private Pair<TypeCheckerResult, List<LValue>> forGeneratorClauseGetBindings(GeneratorClause that, 
    26032577                        boolean mustBeCondition) { 
     
    26332607                                        (Expr)init_result.ast()); 
    26342608 
    2635                          
     2609 
    26362610                        // If bindings are empty, then init_result must be of type boolean, a filter, 13.14 
    26372611                        TypeCheckerResult bool_result; 
    26382612 
    2639                         if (init_result.type().isNone()) {  
     2613                        if (init_result.type().isNone()) { 
    26402614                            String err = "Filter expressions in generator clauses must have type boolean, but " + 
    26412615                                that.getInit() + " was not well typed."; 
    26422616                            bool_result = new TypeCheckerResult(that.getInit(), TypeError.make(err, that.getInit())); 
    2643                         } else {  
     2617                        } else { 
    26442618                            bool_result = 
    26452619                                this.checkSubtype(init_result.type().unwrap(), Types.BOOLEAN, that.getInit(), 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/typechecker/TypeCheckerOutput.java

    r3102 r3138  
    3434import com.sun.fortress.nodes.FnExpr; 
    3535import com.sun.fortress.nodes.For; 
    36 import com.sun.fortress.nodes.GeneratedExpr; 
    3736import com.sun.fortress.nodes.IfClause; 
    3837import com.sun.fortress.nodes.Label; 
     
    130129 
    131130                        @Override 
    132                         public void forGeneratedExpr(GeneratedExpr that) { 
    133                                 // TODO Auto-generated method stub 
    134                                 removed.remove(Pair.make(that,that.getSpan())); 
    135                                 super.forGeneratedExpr(that); 
    136                         } 
    137  
    138                         @Override 
    139131                        public void forIfClause(IfClause that) { 
    140132                                // TODO Auto-generated method stub 
  • trunk/ProjectFortress/src/com/sun/fortress/interpreter/rewrite/DesugarerVisitor.java

    r3137 r3138  
    6868import com.sun.fortress.nodes.FnExpr; 
    6969import com.sun.fortress.nodes.For; 
    70 import com.sun.fortress.nodes.GeneratedExpr; 
    7170import com.sun.fortress.nodes.GeneratorClause; 
    7271import com.sun.fortress.nodes.GrammarDef; 
     
    10971096 
    10981097    @Override 
    1099     public Node forGeneratedExpr(GeneratedExpr ge) { 
    1100         return visitLoop(ge.getSpan(), ge.getGens(), ge.getExpr()); 
    1101     } 
    1102  
    1103     @Override 
    11041098    public Node forSpawn(Spawn s) { 
    11051099        Expr body = s.getBody(); 
  • trunk/ProjectFortress/src/com/sun/fortress/nodes_util/ExprFactory.java

    r3137 r3138  
    798798                              e.getReturnType(), e.getWhereClause(), 
    799799                              e.getThrowsClause(), e.getBody()); 
    800         } 
    801         public Expr forGeneratedExpr(GeneratedExpr e) { 
    802             return new GeneratedExpr(e.getSpan(), true, 
    803                     e.getExpr(), e.getGens()); 
    804800        } 
    805801        public Expr forLetFn(LetFn e) { 
  • trunk/ProjectFortress/src/com/sun/fortress/nodes_util/NodeUtil.java

    r3133 r3138  
    458458            public IterableOnce<String> forFnDecl(FnDecl d) { 
    459459                return new UnitIterable<String>(nameString(d.getName())); 
    460             } 
    461             public IterableOnce<String> forGeneratedExpr(GeneratedExpr d) { 
    462                 return new UnitIterable<String>("GeneratedExpr"); 
    463460            } 
    464461            public IterableOnce<String> forLetFn(LetFn d) { 
  • trunk/ProjectFortress/src/com/sun/fortress/parser/LocalDecl.rats

    r3082 r3138  
    6565   / a1:NoNewlineExpr a2:(s comma w NoNewlineGeneratorClauseList)? 
    6666     { if (a2 == null) yyValue = a1; 
    67        else yyValue = new GeneratedExpr(createSpan(yyStart,yyCount), false, 
    68                                         a1, a2); 
     67       else yyValue = new For(createSpan(yyStart,yyCount), false, 
     68                              a2, ExprFactory.makeBlock(a1)); 
    6969     }; 
    7070 
  • trunk/ProjectFortress/src/com/sun/fortress/syntax_abstractions/phases/Transform.java

    r3137 r3138  
    137137     *    While - done 
    138138     *    Accumulator - 
    139      *    GeneratedExpr 
    140139     *    ArrayComprehensionClause 
    141140     *    IfClause - done 
  • trunk/ProjectFortress/src/com/sun/fortress/tools/FortressAstToConcrete.java

    r3137 r3138  
    15771577    } 
    15781578 
    1579     @Override public String forGeneratedExprOnly(GeneratedExpr that, Option<String> exprType_result, 
    1580                                                  String expr_result, 
    1581                                                  List<String> gens_result) { 
    1582         StringBuilder s = new StringBuilder(); 
    1583  
    1584         s.append( expr_result ); 
    1585         if ( ! gens_result.isEmpty() ) { 
    1586             s.append( ", " ); 
    1587             s.append( join(gens_result, ", ") ); 
    1588         } 
    1589  
    1590         return handleParen( s.toString(), 
    1591                             that.isParenthesized() ); 
    1592     } 
    1593  
    15941579    @Override public String forSubscriptExprOnly(SubscriptExpr that, Option<String> exprType_result, 
    15951580                                                 String obj_result,