Changeset 2151
- Timestamp:
- 07/01/08 08:41:47 (3 months ago)
- Files:
-
- trunk/ProjectFortress/src/com/sun/fortress/Shell.java (modified) (8 diffs)
- trunk/ProjectFortress/src/com/sun/fortress/compiler/Fortress.java (modified) (4 diffs)
- trunk/ProjectFortress/src/com/sun/fortress/compiler/StaticTestSuite.java (modified) (2 diffs)
- trunk/ProjectFortress/src/com/sun/fortress/compiler/environments/TopLevelEnvGenJUTest.java (modified) (3 diffs)
- trunk/ProjectFortress/src/com/sun/fortress/interpreter/Driver.java (modified) (1 diff)
- trunk/ProjectFortress/src/com/sun/fortress/repository/GraphRepository.java (modified) (5 diffs)
- trunk/ProjectFortress/src/com/sun/fortress/syntax_abstractions/rats/RatsParserGenerator.java (modified) (2 diffs)
- trunk/ProjectFortress/src/com/sun/fortress/syntax_abstractions/util/InterpreterWrapper.java (modified) (2 diffs)
- trunk/ProjectFortress/src/com/sun/fortress/unit_tests/FileTests.java (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/ProjectFortress/src/com/sun/fortress/Shell.java
r2145 r2151 24 24 import java.util.*; 25 25 import edu.rice.cs.plt.tuple.Option; 26 26 import edu.rice.cs.plt.iter.IterUtil; 27 28 import com.sun.fortress.repository.GraphRepository; 27 29 import com.sun.fortress.compiler.*; 28 30 import com.sun.fortress.exceptions.shell.UserError; 29 31 import com.sun.fortress.exceptions.StaticError; 32 import com.sun.fortress.exceptions.WrappedException; 33 import com.sun.fortress.exceptions.ProgramError; 30 34 import com.sun.fortress.exceptions.FortressException; 31 35 import com.sun.fortress.exceptions.shell.RepositoryError; … … 37 41 import com.sun.fortress.nodes_util.NodeFactory; 38 42 import com.sun.fortress.nodes_util.ASTIO; 43 import com.sun.fortress.interpreter.Driver; 39 44 import com.sun.fortress.useful.Path; 40 45 import com.sun.fortress.useful.Debug; … … 44 49 public final class Shell { 45 50 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; } 46 82 47 83 /* Helper method to print usage message.*/ … … 192 228 } 193 229 194 p ublicstatic boolean isApi(String file){230 private static boolean isApi(String file){ 195 231 return file.endsWith(ProjectProperties.API_SOURCE_SUFFIX); 196 232 } 197 233 198 p ublicstatic boolean isComponent(String file){234 private static boolean isComponent(String file){ 199 235 return file.endsWith(ProjectProperties.COMP_SOURCE_SUFFIX); 200 236 } 201 237 202 p ublicstatic APIName cuName( String file ){238 private static APIName cuName( String file ){ 203 239 if ( file.endsWith( ProjectProperties.COMP_SOURCE_SUFFIX ) || 204 240 file.endsWith( ProjectProperties.API_SOURCE_SUFFIX ) ){ … … 237 273 compile(rest, out); 238 274 } 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 } 240 296 } 241 297 } 242 298 243 299 /** 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. 252 301 */ 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); 255 312 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(); 292 347 } 293 348 … … 324 379 throws UserError, Throwable { 325 380 try { 326 Fortress fortress = new Fortress(); 327 381 Shell shell = new Shell(); 328 382 Path path = ProjectProperties.SOURCE_PATH; 329 383 … … 334 388 } 335 389 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 } 337 410 338 411 for (StaticError error: errors) { … … 347 420 } catch (RepositoryError e) { 348 421 System.err.println(e.getMessage()); 349 } 350 catch (FortressException e) { 422 } catch (FortressException e) { 351 423 System.err.println(e.getMessage()); 352 424 e.printInterpreterStackTrace(System.err); trunk/ProjectFortress/src/com/sun/fortress/compiler/Fortress.java
r2145 r2151 26 26 import com.sun.fortress.Shell; 27 27 import com.sun.fortress.repository.FortressRepository; 28 import com.sun.fortress.repository.GraphRepository;29 28 import com.sun.fortress.repository.CacheBasedRepository; 30 29 import com.sun.fortress.compiler.environments.TopLevelEnvGen; … … 37 36 import com.sun.fortress.exceptions.shell.RepositoryError; 38 37 import com.sun.fortress.nodes_util.ASTIO; 39 import com.sun.fortress.interpreter.Driver;40 38 import com.sun.fortress.repository.ProjectProperties; 41 39 import com.sun.fortress.nodes.APIName; … … 53 51 public class Fortress { 54 52 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) { 162 58 String phase = ""; 163 59 … … 270 166 } 271 167 272 private boolean compiledApi( APIName name, Iterable<Api> apis ){168 private static boolean compiledApi( APIName name, Iterable<Api> apis ){ 273 169 for ( Api api : apis ){ 274 170 if ( api.getName().equals(name) ){ trunk/ProjectFortress/src/com/sun/fortress/compiler/StaticTestSuite.java
r2145 r2151 31 31 import edu.rice.cs.plt.lambda.Lambda; 32 32 33 import com.sun.fortress.Shell; 33 34 import com.sun.fortress.exceptions.StaticError; 34 35 import com.sun.fortress.exceptions.MultipleStaticError; … … 298 299 299 300 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()); 302 302 } 303 303 } trunk/ProjectFortress/src/com/sun/fortress/compiler/environments/TopLevelEnvGenJUTest.java
r2145 r2151 24 24 import junit.framework.TestCase; 25 25 26 import com.sun.fortress. compiler.Fortress;26 import com.sun.fortress.Shell; 27 27 import com.sun.fortress.exceptions.StaticError; 28 28 import com.sun.fortress.repository.ProjectProperties; … … 166 166 167 167 private void compileTestProgram() { 168 Fortress fortress = new Fortress();169 170 168 Path path = ProjectProperties.SOURCE_PATH; 171 169 String s = ProjectProperties.BASEDIR + "tests" + … … 181 179 } 182 180 183 Iterable<? extends StaticError> errors = fortress.compile(path, s);181 Iterable<? extends StaticError> errors = Shell.compile(path, s); 184 182 185 183 for (StaticError error: errors) { trunk/ProjectFortress/src/com/sun/fortress/interpreter/Driver.java
r2145 r2151 105 105 106 106 import static com.sun.fortress.interpreter.glue.WellKnownNames.*; 107 import static com.sun.fortress.compiler.Fortress.CURRENT_INTERPRETER_REPOSITORY;108 107 109 108 public class Driver { trunk/ProjectFortress/src/com/sun/fortress/repository/GraphRepository.java
r2145 r2151 44 44 import com.sun.fortress.compiler.Parser.Result; 45 45 import com.sun.fortress.compiler.Parser; 46 import com.sun.fortress.compiler.Fortress; 46 47 import com.sun.fortress.compiler.GlobalEnvironment; 47 import com.sun.fortress. compiler.Fortress;48 import com.sun.fortress.Shell; 48 49 import com.sun.fortress.exceptions.ParserError; 49 50 import com.sun.fortress.exceptions.ProgramError; … … 472 473 GlobalEnvironment knownApis = new GlobalEnvironment.FromMap(parsedApis()); 473 474 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() ); 476 478 if ( errors.iterator().hasNext() ){ 477 479 throw new MultipleStaticError(errors); … … 527 529 Debug.debug( 1, "Parsing " + component + " at " + now ); 528 530 529 Fortress fort = new Fortress(this);531 Shell shell = new Shell(this); 530 532 /* fort.analyze will call repository.add() with the parsed component, 531 533 * where that repository is 'this'. After that add the component node … … 533 535 */ 534 536 /***/ 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 ); 536 539 if ( errors.iterator().hasNext() ){ 537 540 throw new MultipleStaticError(errors); … … 554 557 GraphRepository g1 = new GraphRepository( this.path, this.cache ); 555 558 /* FIXME: hack to prevent infinite recursion */ 556 Fortress.setCurrentInterpreterRepository( g1 );559 Shell.setCurrentInterpreterRepository( g1 ); 557 560 Result result = FortressParser.parse(api_name, file, new GlobalEnvironment.FromRepository( g1 ), verbose()); 558 561 // Result result = FortressParser.parse(file, new GlobalEnvironment.FromRepository(this), verbose()); 559 562 /* FIXME: hack to prevent infinite recursion */ 560 Fortress.setCurrentInterpreterRepository( this );563 Shell.setCurrentInterpreterRepository( this ); 561 564 if (result.isSuccessful()) { 562 565 Debug.debug( 1, "Expanded component " + node ); trunk/ProjectFortress/src/com/sun/fortress/syntax_abstractions/rats/RatsParserGenerator.java
r2119 r2151 19 19 * Class given a set of Rats! modules it generates a new Fortress parser extended 20 20 * with the modifications in the given modules. 21 * 21 * 22 22 */ 23 23 … … 35 35 import xtc.tree.Attribute; 36 36 37 import com.sun.fortress.compiler.Fortress;38 37 import com.sun.fortress.compiler.StaticPhaseResult; 39 38 import com.sun.fortress.exceptions.WrappedException; trunk/ProjectFortress/src/com/sun/fortress/syntax_abstractions/util/InterpreterWrapper.java
r2133 r2151 30 30 31 31 import com.sun.fortress.repository.FortressRepository; 32 import com.sun.fortress. compiler.Fortress;32 import com.sun.fortress.Shell; 33 33 import com.sun.fortress.compiler.StaticPhaseResult; 34 34 import com.sun.fortress.exceptions.FortressException; … … 83 83 84 84 public InterpreterWrapper() { 85 repository = Fortress.CURRENT_INTERPRETER_REPOSITORY;85 repository = Shell.CURRENT_INTERPRETER_REPOSITORY; 86 86 } 87 87 trunk/ProjectFortress/src/com/sun/fortress/unit_tests/FileTests.java
r2145 r2151 30 30 import com.sun.fortress.repository.FortressRepository; 31 31 import com.sun.fortress.compiler.index.ComponentIndex; 32 import com.sun.fortress. compiler.Fortress;32 import com.sun.fortress.Shell; 33 33 import com.sun.fortress.interpreter.reader.Lex; 34 34 import com.sun.fortress.repository.ProjectProperties; … … 101 101 oldOut.print(" ") ; oldOut.print(f); oldOut.print(" "); oldOut.flush(); 102 102 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 ); 104 104 ComponentIndex ci = fr.getLinkedComponent(apiname); 105 105
