Changeset 2151

Show
Ignore:
Timestamp:
07/01/08 08:41:47 (3 months ago)
Author:
sukyoungryu
Message:

[refactoring] Cleaning up the repository directories.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/ProjectFortress/src/com/sun/fortress/Shell.java

    r2145 r2151  
    2424import java.util.*; 
    2525import edu.rice.cs.plt.tuple.Option; 
    26  
     26import edu.rice.cs.plt.iter.IterUtil; 
     27 
     28import com.sun.fortress.repository.GraphRepository; 
    2729import com.sun.fortress.compiler.*; 
    2830import com.sun.fortress.exceptions.shell.UserError; 
    2931import com.sun.fortress.exceptions.StaticError; 
     32import com.sun.fortress.exceptions.WrappedException; 
     33import com.sun.fortress.exceptions.ProgramError; 
    3034import com.sun.fortress.exceptions.FortressException; 
    3135import com.sun.fortress.exceptions.shell.RepositoryError; 
     
    3741import com.sun.fortress.nodes_util.NodeFactory; 
    3842import com.sun.fortress.nodes_util.ASTIO; 
     43import com.sun.fortress.interpreter.Driver; 
    3944import com.sun.fortress.useful.Path; 
    4045import com.sun.fortress.useful.Debug; 
     
    4449public final class Shell { 
    4550    static boolean test; 
     51 
     52    private final FortressRepository _repository; 
     53    public static FortressRepository CURRENT_INTERPRETER_REPOSITORY = null; 
     54 
     55    public FortressRepository getRepository() { 
     56        return _repository; 
     57    } 
     58 
     59    /** 
     60     * This is used to communicate, clumsily, with parsers generated by syntax expansion. 
     61     * The interface should be improved. 
     62     */ 
     63    public static void setCurrentInterpreterRepository( FortressRepository g ){ 
     64        CURRENT_INTERPRETER_REPOSITORY = g; 
     65    } 
     66 
     67    public static FortressRepository specificRepository(Path p, FortressRepository cache ){ 
     68        FortressRepository fr = new GraphRepository( p, cache ); 
     69        CURRENT_INTERPRETER_REPOSITORY = fr; 
     70        return fr; 
     71    } 
     72 
     73    public static FortressRepository specificRepository(Path p) { 
     74        return specificRepository( p, new CacheBasedRepository(ProjectProperties.ANALYZED_CACHE_DIR) ); 
     75    } 
     76 
     77    public Shell() { 
     78        _repository = new CacheBasedRepository(ProjectProperties.ANALYZED_CACHE_DIR); 
     79    } 
     80 
     81    public Shell(FortressRepository repository) { _repository = repository; } 
    4682 
    4783    /* Helper method to print usage message.*/ 
     
    192228    } 
    193229 
    194     public static boolean isApi(String file){ 
     230    private static boolean isApi(String file){ 
    195231        return file.endsWith(ProjectProperties.API_SOURCE_SUFFIX); 
    196232    } 
    197233 
    198     public static boolean isComponent(String file){ 
     234    private static boolean isComponent(String file){ 
    199235        return file.endsWith(ProjectProperties.COMP_SOURCE_SUFFIX); 
    200236    } 
    201237 
    202     public static APIName cuName( String file ){ 
     238    private static APIName cuName( String file ){ 
    203239        if ( file.endsWith( ProjectProperties.COMP_SOURCE_SUFFIX ) || 
    204240             file.endsWith( ProjectProperties.API_SOURCE_SUFFIX ) ){ 
     
    237273            compile(rest, out); 
    238274        } else { 
    239             compile(s, out ); 
     275            try { 
     276                Path path = ProjectProperties.SOURCE_PATH; 
     277 
     278                /* Questions 1) 
     279                   1) Not for parse 
     280                   2) What if there are multiple "/"s 
     281                */ 
     282                if (s.contains("/")) { 
     283                    String head = s.substring(0, s.lastIndexOf("/")); 
     284                    s = s.substring(s.lastIndexOf("/")+1, s.length()); 
     285                    path = path.prepend(head); 
     286                } 
     287                Iterable<? extends StaticError> errors = compile(path, s, out ); 
     288                if ( errors.iterator().hasNext() ){ 
     289                    for (StaticError error: errors) { 
     290                        System.err.println(error); 
     291                    } 
     292                } 
     293            } catch (RepositoryError error) { 
     294                System.err.println(error); 
     295            } 
    240296        } 
    241297    } 
    242298 
    243299    /** 
    244      * This compiler uses a different method for determining what to compile, 
    245      * and how to compile it. 
    246      * 
    247      * @param doLink 
    248      * @param s 
    249      * @throws UserError 
    250      * @throws InterruptedException 
    251      * @throws IOException 
     300     * Compile a file. 
    252301     */ 
    253     private static void compile(String s, Option<String> out) 
    254         throws UserError, InterruptedException { 
     302    public static Iterable<? extends StaticError> compile(Path path, String file) { 
     303        return compile(path, file, Option.<String>none()); 
     304    } 
     305 
     306    private static Iterable<? extends StaticError> compile(Path path, String file, Option<String> out) { 
     307        Shell shell = new Shell(); 
     308        FortressRepository bcr = specificRepository( path, shell.getRepository() ); 
     309 
     310        Debug.debug( 2, "Compiling file " + file ); 
     311        APIName name = cuName(file); 
    255312        try { 
    256             Fortress fortress = new Fortress(); 
    257             Path path = ProjectProperties.SOURCE_PATH; 
    258  
    259             /* Questions 1) 
    260                1) Not for parse 
    261                2) What if there are multiple "/"s 
    262              */ 
    263             if (s.contains("/")) { 
    264                 String head = s.substring(0, s.lastIndexOf("/")); 
    265                 s = s.substring(s.lastIndexOf("/")+1, s.length()); 
    266                 path = path.prepend(head); 
    267             } 
    268  
    269             Iterable<? extends StaticError> errors = fortress.compile(path, s); 
    270  
    271             if ( errors.iterator().hasNext() ){ 
    272                 for (StaticError error: errors) { 
    273                     System.err.println(error); 
    274                 } 
    275             // If there are no errors, all components will have been written to disk by the CacheBasedRepository. 
    276             } else if ( out.isSome() ){ 
    277                 try{ 
    278                     if ( isApi(s) ){ 
    279                         ASTIO.writeJavaAst(fortress.getRepository().getApi(cuName(s)).ast(), out.unwrap()); 
    280                     } else if ( isComponent(s) ){ 
    281                         ASTIO.writeJavaAst(fortress.getRepository().getComponent(cuName(s)).ast(), out.unwrap()); 
    282                     } else { 
    283                         System.out.println( "Don't know what kind of file " + s + " is. Append .fsi or .fss." ); 
    284                     } 
    285                 } catch ( IOException e ){ 
    286                     System.err.println( "Error while writing " + out.unwrap() ); 
    287                 } 
    288             } 
    289         } catch (RepositoryError error) { 
    290             System.err.println(error); 
    291         } 
     313            if ( isApi(file) ) { 
     314                Api a = (Api) bcr.getApi(name).ast(); 
     315                if ( out.isSome() ) 
     316                    ASTIO.writeJavaAst(shell.getRepository().getApi(name).ast(), out.unwrap()); 
     317            } else if (isComponent(file)) { 
     318                Component c = (Component) bcr.getComponent(name).ast(); 
     319                if ( out.isSome() ) 
     320                    ASTIO.writeJavaAst(shell.getRepository().getComponent(name).ast(), out.unwrap()); 
     321            } else { 
     322                System.out.println( "Don't know what kind of file " + file + 
     323                                    " is. Append .fsi or .fss." ); 
     324            } 
     325        } catch (ProgramError pe) { 
     326            Iterable<? extends StaticError> se = pe.getStaticErrors(); 
     327            if (se == null) { 
     328                return IterUtil.singleton(new WrappedException(pe, ProjectProperties.debug)); 
     329            } 
     330            else { 
     331                return se; 
     332            } 
     333        } catch (RepositoryError ex) { 
     334            throw ex; 
     335        } catch ( FileNotFoundException ex ){ 
     336            throw new WrappedException(ex); 
     337        } catch ( IOException e ){ 
     338            throw new WrappedException(e); 
     339        } catch (StaticError ex) { 
     340             return IterUtil.singleton(new WrappedException(ex, ProjectProperties.debug)); 
     341        } 
     342 
     343        if (bcr.verbose()) 
     344            System.err.println("Compiling done."); 
     345 
     346        return IterUtil.empty(); 
    292347    } 
    293348 
     
    324379        throws UserError, Throwable { 
    325380        try { 
    326             Fortress fortress = new Fortress(); 
    327  
     381            Shell shell = new Shell(); 
    328382            Path path = ProjectProperties.SOURCE_PATH; 
    329383 
     
    334388            } 
    335389 
    336             Iterable<? extends StaticError> errors = fortress.run(path, cuName(fileName), test, args); 
     390            APIName componentName = cuName(fileName); 
     391            FortressRepository bcr = specificRepository( path, shell.getRepository() ); 
     392            Iterable<? extends StaticError> errors = IterUtil.empty(); 
     393 
     394            try { 
     395                CompilationUnit cu = bcr.getLinkedComponent(componentName).ast(); 
     396                Driver.runProgram(bcr, cu, test, args); 
     397            } catch (Throwable th) { 
     398                // TODO FIXME what is the proper treatment of errors/exceptions etc.? 
     399                if (th instanceof FortressException) { 
     400                    FortressException pe = (FortressException) th; 
     401                    if (pe.getStaticErrors() != null) 
     402                        errors = pe.getStaticErrors(); 
     403                } 
     404                if (th instanceof RuntimeException) 
     405                    throw (RuntimeException) th; 
     406                if (th instanceof Error) 
     407                    throw (Error) th; 
     408                throw new WrappedException(th, ProjectProperties.debug); 
     409            } 
    337410 
    338411            for (StaticError error: errors) { 
     
    347420        } catch (RepositoryError e) { 
    348421            System.err.println(e.getMessage()); 
    349         } 
    350         catch (FortressException e) { 
     422        } catch (FortressException e) { 
    351423            System.err.println(e.getMessage()); 
    352424            e.printInterpreterStackTrace(System.err); 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/Fortress.java

    r2145 r2151  
    2626import com.sun.fortress.Shell; 
    2727import com.sun.fortress.repository.FortressRepository; 
    28 import com.sun.fortress.repository.GraphRepository; 
    2928import com.sun.fortress.repository.CacheBasedRepository; 
    3029import com.sun.fortress.compiler.environments.TopLevelEnvGen; 
     
    3736import com.sun.fortress.exceptions.shell.RepositoryError; 
    3837import com.sun.fortress.nodes_util.ASTIO; 
    39 import com.sun.fortress.interpreter.Driver; 
    4038import com.sun.fortress.repository.ProjectProperties; 
    4139import com.sun.fortress.nodes.APIName; 
     
    5351public class Fortress { 
    5452 
    55     private final FortressRepository _repository; 
    56     public static FortressRepository CURRENT_INTERPRETER_REPOSITORY = null; 
    57  
    58     public FortressRepository getRepository() { 
    59         return _repository; 
    60     } 
    61  
    62     /** 
    63      * This is used to communicate, clumsily, with parsers generated by syntax expansion. 
    64      * The interface should be improved. 
    65      */ 
    66     public static void setCurrentInterpreterRepository( FortressRepository g ){ 
    67         CURRENT_INTERPRETER_REPOSITORY = g; 
    68     } 
    69  
    70     public static FortressRepository specificRepository(Path p, FortressRepository cache ){ 
    71         FortressRepository fr = new GraphRepository( p, cache ); 
    72         CURRENT_INTERPRETER_REPOSITORY = fr; 
    73         return fr; 
    74     } 
    75  
    76     public static FortressRepository specificRepository(Path p) { 
    77         return specificRepository( p, new CacheBasedRepository(ProjectProperties.ANALYZED_CACHE_DIR) ); 
    78     } 
    79  
    80     public Fortress() { 
    81         _repository = new CacheBasedRepository(ProjectProperties.ANALYZED_CACHE_DIR); 
    82     } 
    83  
    84     public Fortress(FortressRepository repository) { _repository = repository; } 
    85  
    86     /** 
    87      * Compile all definitions in the given files, and any additional sources that 
    88      * they depend on, and add them to the fortress. 
    89      */ 
    90     public Iterable<? extends StaticError> compile(Path path, String file) { 
    91         FortressRepository bcr = specificRepository( path, _repository ); 
    92  
    93         Debug.debug( 2, "Compiling file " + file ); 
    94         Parser.Result result = new Parser.Result(); 
    95  
    96         APIName name = Shell.cuName(file); 
    97         try { 
    98             if (Shell.isApi(file)) { 
    99                 Api a = (Api) bcr.getApi(name).ast(); 
    100                 result = new Parser.Result(result, new Parser.Result(a, bcr.getModifiedDateForApi(name))); 
    101             } else if (Shell.isComponent(file)) { 
    102                 Component c = (Component) bcr.getComponent(name).ast(); 
    103                 result = new Parser.Result(result, new Parser.Result(c, bcr.getModifiedDateForComponent(name))); 
    104             } else { 
    105                 System.out.println( "Don't know what kind of file " + file + 
    106                                     " is. Append .fsi or .fss." ); 
    107             } 
    108         } catch (ProgramError pe) { 
    109             Iterable<? extends StaticError> se = pe.getStaticErrors(); 
    110             if (se == null) { 
    111                 result = new Parser.Result(result, new Parser.Result(new WrappedException(pe, ProjectProperties.debug))); 
    112             } 
    113             else { 
    114                 result = new Parser.Result(result, new Parser.Result(se)); 
    115             } 
    116         } catch (RepositoryError ex) { 
    117             throw ex; 
    118         } catch ( FileNotFoundException ex ){ 
    119             throw new WrappedException(ex); 
    120         } catch ( IOException e ){ 
    121             throw new WrappedException(e); 
    122         } catch (StaticError ex) { 
    123             result = new Parser.Result(result, new Parser.Result(new WrappedException(ex, ProjectProperties.debug))); 
    124         } 
    125  
    126         if (!result.isSuccessful()) { return result.errors(); } 
    127         if (bcr.verbose()) 
    128             System.err.println("Compiling done."); 
    129  
    130         GlobalEnvironment env = new GlobalEnvironment.FromMap(bcr.apis()); 
    131  
    132         return IterUtil.empty(); 
    133     } 
    134  
    135     public Iterable<? extends StaticError>  run(Path path, APIName componentName, boolean test, List<String> args) { 
    136         FortressRepository bcr = specificRepository( path, _repository ); 
    137  
    138         try { 
    139             CompilationUnit cu = bcr.getLinkedComponent(componentName).ast(); 
    140             Driver.runProgram(bcr, cu, test, args); 
    141         } catch (Throwable th) { 
    142             // TODO FIXME what is the proper treatment of errors/exceptions etc.? 
    143             if (th instanceof FortressException) { 
    144                 FortressException pe = (FortressException) th; 
    145                 if (pe.getStaticErrors() != null) 
    146                     return pe.getStaticErrors(); 
    147             } 
    148             if (th instanceof RuntimeException) 
    149                 throw (RuntimeException) th; 
    150             if (th instanceof Error) 
    151                 throw (Error) th; 
    152             throw new WrappedException(th, ProjectProperties.debug); 
    153         } 
    154  
    155         return IterUtil.empty(); 
    156     } 
    157  
    158     public Iterable<? extends StaticError> analyze(GlobalEnvironment env, 
    159                                                     Iterable<Api> apis, 
    160                                                     Iterable<Component> components, 
    161                                                     long lastModified) { 
     53    public static Iterable<? extends StaticError> analyze(FortressRepository _repository, 
     54                                                          GlobalEnvironment env, 
     55                                                          Iterable<Api> apis, 
     56                                                          Iterable<Component> components, 
     57                                                          long lastModified) { 
    16258        String phase = ""; 
    16359 
     
    270166    } 
    271167 
    272     private boolean compiledApi( APIName name, Iterable<Api> apis ){ 
     168    private static boolean compiledApi( APIName name, Iterable<Api> apis ){ 
    273169        for ( Api api : apis ){ 
    274170            if ( api.getName().equals(name) ){ 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/StaticTestSuite.java

    r2145 r2151  
    3131import edu.rice.cs.plt.lambda.Lambda; 
    3232 
     33import com.sun.fortress.Shell; 
    3334import com.sun.fortress.exceptions.StaticError; 
    3435import com.sun.fortress.exceptions.MultipleStaticError; 
     
    298299 
    299300        private Iterable<? extends StaticError> compile(File f) throws IOException { 
    300             Fortress fortress = new Fortress(); 
    301             return fortress.compile(ProjectProperties.SOURCE_PATH.prepend(f.getParent()), f.getName()); 
     301            return Shell.compile(ProjectProperties.SOURCE_PATH.prepend(f.getParent()), f.getName()); 
    302302        } 
    303303    } 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/environments/TopLevelEnvGenJUTest.java

    r2145 r2151  
    2424import junit.framework.TestCase; 
    2525 
    26 import com.sun.fortress.compiler.Fortress
     26import com.sun.fortress.Shell
    2727import com.sun.fortress.exceptions.StaticError; 
    2828import com.sun.fortress.repository.ProjectProperties; 
     
    166166 
    167167    private void compileTestProgram() { 
    168         Fortress fortress = new Fortress(); 
    169  
    170168        Path path = ProjectProperties.SOURCE_PATH; 
    171169        String s = ProjectProperties.BASEDIR + "tests" + 
     
    181179        } 
    182180 
    183         Iterable<? extends StaticError> errors = fortress.compile(path, s); 
     181        Iterable<? extends StaticError> errors = Shell.compile(path, s); 
    184182 
    185183        for (StaticError error: errors) { 
  • trunk/ProjectFortress/src/com/sun/fortress/interpreter/Driver.java

    r2145 r2151  
    105105 
    106106import static com.sun.fortress.interpreter.glue.WellKnownNames.*; 
    107 import static com.sun.fortress.compiler.Fortress.CURRENT_INTERPRETER_REPOSITORY; 
    108107 
    109108public class Driver { 
  • trunk/ProjectFortress/src/com/sun/fortress/repository/GraphRepository.java

    r2145 r2151  
    4444import com.sun.fortress.compiler.Parser.Result; 
    4545import com.sun.fortress.compiler.Parser; 
     46import com.sun.fortress.compiler.Fortress; 
    4647import com.sun.fortress.compiler.GlobalEnvironment; 
    47 import com.sun.fortress.compiler.Fortress
     48import com.sun.fortress.Shell
    4849import com.sun.fortress.exceptions.ParserError; 
    4950import com.sun.fortress.exceptions.ProgramError; 
     
    472473                GlobalEnvironment knownApis = new GlobalEnvironment.FromMap(parsedApis()); 
    473474                List<Component> components = new ArrayList<Component>(); 
    474                 Fortress fort = new Fortress(this); 
    475                 Iterable<? extends StaticError> errors = fort.analyze( knownApis, unparsed, components, System.currentTimeMillis() ); 
     475                Shell shell = new Shell(this); 
     476                Iterable<? extends StaticError> errors = Fortress.analyze(shell.getRepository(), 
     477                                                                          knownApis, unparsed, components, System.currentTimeMillis() ); 
    476478                if ( errors.iterator().hasNext() ){ 
    477479                    throw new MultipleStaticError(errors); 
     
    527529        Debug.debug( 1, "Parsing " + component + " at " + now ); 
    528530 
    529             Fortress fort = new Fortress(this); 
     531            Shell shell = new Shell(this); 
    530532            /* fort.analyze will call repository.add() with the parsed component, 
    531533                 * where that repository is 'this'. After that add the component node 
     
    533535                 */ 
    534536        /***/ 
    535             Iterable<? extends StaticError> errors = fort.analyze( knownApis, new ArrayList<Api>(), components, now ); 
     537            Iterable<? extends StaticError> errors = Fortress.analyze(shell.getRepository(), 
     538                                                                      knownApis, new ArrayList<Api>(), components, now ); 
    536539        if ( errors.iterator().hasNext() ){ 
    537540                    throw new MultipleStaticError(errors); 
     
    554557        GraphRepository g1 = new GraphRepository( this.path, this.cache ); 
    555558        /* FIXME: hack to prevent infinite recursion */ 
    556         Fortress.setCurrentInterpreterRepository( g1 ); 
     559        Shell.setCurrentInterpreterRepository( g1 ); 
    557560        Result result = FortressParser.parse(api_name, file, new GlobalEnvironment.FromRepository( g1 ), verbose()); 
    558561        // Result result = FortressParser.parse(file, new GlobalEnvironment.FromRepository(this), verbose()); 
    559562        /* FIXME: hack to prevent infinite recursion */ 
    560         Fortress.setCurrentInterpreterRepository( this ); 
     563        Shell.setCurrentInterpreterRepository( this ); 
    561564        if (result.isSuccessful()) { 
    562565            Debug.debug( 1, "Expanded component " + node ); 
  • trunk/ProjectFortress/src/com/sun/fortress/syntax_abstractions/rats/RatsParserGenerator.java

    r2119 r2151  
    1919 * Class given a set of Rats! modules it generates a new Fortress parser extended 
    2020 * with the modifications in the given modules. 
    21  *  
     21 * 
    2222 */ 
    2323 
     
    3535import xtc.tree.Attribute; 
    3636 
    37 import com.sun.fortress.compiler.Fortress; 
    3837import com.sun.fortress.compiler.StaticPhaseResult; 
    3938import com.sun.fortress.exceptions.WrappedException; 
  • trunk/ProjectFortress/src/com/sun/fortress/syntax_abstractions/util/InterpreterWrapper.java

    r2133 r2151  
    3030 
    3131import com.sun.fortress.repository.FortressRepository; 
    32 import com.sun.fortress.compiler.Fortress
     32import com.sun.fortress.Shell
    3333import com.sun.fortress.compiler.StaticPhaseResult; 
    3434import com.sun.fortress.exceptions.FortressException; 
     
    8383 
    8484    public  InterpreterWrapper() { 
    85         repository = Fortress.CURRENT_INTERPRETER_REPOSITORY; 
     85        repository = Shell.CURRENT_INTERPRETER_REPOSITORY; 
    8686    } 
    8787 
  • trunk/ProjectFortress/src/com/sun/fortress/unit_tests/FileTests.java

    r2145 r2151  
    3030import com.sun.fortress.repository.FortressRepository; 
    3131import com.sun.fortress.compiler.index.ComponentIndex; 
    32 import com.sun.fortress.compiler.Fortress
     32import com.sun.fortress.Shell
    3333import com.sun.fortress.interpreter.reader.Lex; 
    3434import com.sun.fortress.repository.ProjectProperties; 
     
    101101                    oldOut.print("  ") ; oldOut.print(f); oldOut.print(" "); oldOut.flush(); 
    102102                    APIName apiname = NodeFactory.makeAPIName(s); 
    103                     FortressRepository fr = Fortress.specificRepository( ProjectProperties.SOURCE_PATH.prepend(path), cache ); 
     103                    FortressRepository fr = Shell.specificRepository( ProjectProperties.SOURCE_PATH.prepend(path), cache ); 
    104104                    ComponentIndex ci = fr.getLinkedComponent(apiname); 
    105105