Changeset 1048

Show
Ignore:
Timestamp:
12/04/07 10:00:12 (1 year ago)
Author:
EricAllen
Message:

Modified type environments to return a new implicit type for a variable when none is explicitly provided.
Added a new AST node for this purpose.
Tweaked static_tests directory to include a lib subdirectory that includes .tfs files needed by the static tests.
Added FortressBuiltin? component/API source to test_library_native and test_library.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/ProjectFortress/astgen/Fortress.ast

    r1040 r1048  
    11851185             */ 
    11861186            UnitArg(UnitExpr unit); 
     1187        _RewriteImplicitType();     
    11871188    /** 
    11881189     * static expression 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/CompilerTopLevelJUTest.java

    r922 r1048  
    2020import junit.framework.TestCase; 
    2121import java.io.File; 
     22import java.io.IOException; 
    2223import java.util.Map; 
    2324import java.util.List; 
     
    3536import com.sun.fortress.compiler.index.ComponentIndex; 
    3637import com.sun.fortress.nodes.DottedName; 
     38import com.sun.fortress.shell.FileBasedRepository; 
    3739 
    3840import com.sun.fortress.interpreter.drivers.ProjectProperties; 
     
    4446    // relative to the top ProjectFortress directory 
    4547    private static String baseDir = ProjectProperties.BASEDIR; 
     48    private static String staticTests = baseDir + "static_tests/"; 
    4649 
    4750    private static final List<String> NOT_PASSING = Arrays.asList( 
    48         baseDir + "static_tests/SimpleProgram.fss", 
    49         baseDir + "static_tests/SimpleApi.fss", 
    50         baseDir + "static_tests/LocalFnRef.fss", 
    51         baseDir + "static_tests/LocalVarRef.fss", 
    52         baseDir + "static_tests/stub to eliminate comma trouble" 
     51        staticTests + "XXXMultipleRefErrors.fss", 
     52        staticTests + "XXXUndefinedArrayRef.fss",    
     53        staticTests + "XXXUndefinedInitializer.fss", 
     54        staticTests + "XXXUndefinedNestedRef.fss", 
     55        staticTests + "XXXUndefinedRefInLoop.fss", 
     56        staticTests + "XXXUndefinedVar.fss",                                                           
     57        staticTests + "stub to eliminate comma trouble" 
    5358    ); 
    5459     
     
    5863      })); 
    5964     
    60     public void testStaticTests()
     65    public void testStaticTests() throws IOException
    6166        boolean foundAFile = false; 
    6267        Predicate<File> filter = IOUtil.extensionFilePredicate("fss", IOUtil.IS_FILE); 
    63         for (File f : IOUtil.listFilesRecursively(new File(baseDir + "static_tests"), filter)) { 
     68        for (File f : IOUtil.listFilesRecursively(new File(staticTests), filter)) { 
    6469            foundAFile = true; 
    6570            if (SKIP_NOT_PASSING && NOT_PASSING_FILES.contains(f)) { continue; } 
     
    7075    } 
    7176     
    72     private void assertMalformedProgram(File f)
     77    private void assertMalformedProgram(File f) throws IOException
    7378        Iterable<? extends StaticError> errors = compile(f); 
    7479        assertFalse("Source " + f + " was compiled without error", 
     
    8085    } 
    8186     
    82     private void assertWellFormedProgram(File f)
     87    private void assertWellFormedProgram(File f) throws IOException
    8388        Iterable<? extends StaticError> errors = compile(f); 
    8489        String message = "Source " + f + " produces static errors:\n" + 
     
    8893    } 
    8994     
    90     private Iterable<? extends StaticError> compile(File f)
     95    private Iterable<? extends StaticError> compile(File f) throws IOException
    9196        final Map<DottedName, ApiIndex> apis = new HashMap<DottedName, ApiIndex>(); 
    92         Fortress fortress = new Fortress(new FortressRepository() { 
    93             public Map<DottedName, ApiIndex> apis() { 
    94                 return Collections.unmodifiableMap(apis); 
    95             } 
    96             public void addApi(DottedName name, ApiIndex def) { 
    97                 apis.put(name, def); 
    98             } 
    99             public void addComponent(DottedName name, ComponentIndex def) { 
    100                 /* ignore */ 
    101             } 
    102         }); 
     97        Fortress fortress = new Fortress(new FileBasedRepository(baseDir, staticTests + "lib")); 
     98//        new FortressRepository() { 
     99//            public Map<DottedName, ApiIndex> apis() { 
     100//                return Collections.unmodifiableMap(apis); 
     101//            } 
     102//            public void addApi(DottedName name, ApiIndex def) { 
     103//                apis.put(name, def); 
     104//            } 
     105//            public void addComponent(DottedName name, ComponentIndex def) { 
     106//                /* ignore */ 
     107//            } 
     108//        }); 
    103109        return fortress.compile(f); 
    104110    } 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/Fortress.java

    r1043 r1048  
    5050    public Iterable<? extends StaticError> compile(Iterable<File> files) { 
    5151        GlobalEnvironment env = new GlobalEnvironment(_repository.apis()); 
    52                  
     52   
    5353        FortressParser.Result pr = FortressParser.parse(files, env); 
    5454        // Parser.Result pr = Parser.parse(files, env); 
    5555        if (!pr.isSuccessful()) { return pr.errors(); } 
    56         System.out.println("parsing done."); 
     56        System.out.println("Parsing done."); 
    5757         
    5858        // Handle APIs first 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/typechecker/EmptyTypeEnv.java

    r1036 r1048  
    3232     
    3333    public Option<LValueBind> binding(IdName var) { return none(); } 
    34     public Option<Option<Type>> type(IdName var) { return none(); } 
     34    public Option<Type> type(IdName var) { return none(); } 
    3535    public Option<List<Modifier>> mods(IdName var) { return none(); } 
    3636    public Option<Boolean> mutable(IdName var) { return none(); } 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/typechecker/NonEmptyTypeEnv.java

    r1034 r1048  
    1919 
    2020import com.sun.fortress.nodes.*; 
     21import com.sun.fortress.nodes_util.NodeFactory; 
    2122import edu.rice.cs.plt.tuple.Option; 
    2223import java.util.*; 
     
    4243    } 
    4344     
    44     public Option<Option<Type>> type(IdName var) {  
    45         Option<LValueBind> binding = binding(var); 
     45    public Option<Type> type(IdName var) {  
     46        for (int i = 0; i < entries.length; i++) { 
     47            LValueBind entry = entries[i]; 
     48 
     49            if (var.equals(entry.getName())) { 
     50                Option<Type> type = entry.getType(); 
     51                if (type.isSome()) {  
     52                    return type;  
     53                } else {  
     54                    Type implicitType = new _RewriteImplicitType(); 
     55                    entries[i] =  
     56                        NodeFactory.makeLValue(entry, implicitType); 
     57                    return wrap(implicitType); 
     58                } 
     59            } 
     60        } 
     61        return Option.none(); 
     62    } 
    4663         
    47         if (binding.isSome()) { return wrap(unwrap(binding).getType()); } 
    48         else { return Option.none(); } 
    49     } 
    50  
    51  
    5264    public Option<List<Modifier>> mods(IdName var) {  
    5365        Option<LValueBind> binding = binding(var); 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/typechecker/TypeEnv.java

    r1036 r1048  
    2424import static com.sun.fortress.nodes_util.NodeFactory.makeIdName; 
    2525 
    26  
     26/**  
     27 * This class is used by the type checker to represent static type environments, 
     28 * mapping bound variables to their types.  
     29 */ 
    2730public abstract class TypeEnv { 
    2831    public static TypeEnv make(LValueBind... entries) { 
     
    3134     
    3235    public abstract Option<LValueBind> binding(IdName var); 
    33     public abstract Option<Option<Type>> type(IdName var); 
     36    public abstract Option<Type> type(IdName var); 
    3437    public abstract Option<List<Modifier>> mods(IdName var); 
    3538    public abstract Option<Boolean> mutable(IdName var); 
    3639     
    37     public Option<Option<Type>> type(String var) { return type(makeIdName(var)); } 
     40    public Option<Type> type(String var) { return type(makeIdName(var)); } 
    3841    public Option<List<Modifier>> mods(String var) { return mods(makeIdName(var)); } 
    3942    public Option<Boolean> mutable(String var) { return mutable(makeIdName(var)); } 
    4043         
     44    /** 
     45     * Produce a new type environment extending this with the given variable bindings. 
     46     */ 
    4147    public TypeEnv extend(LValueBind... entries) { 
    4248        if (entries.length == 0) { return EmptyTypeEnv.ONLY; } 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/typechecker/TypeEnvJUTest.java

    r1036 r1048  
    5454     
    5555    public void testLookupType() { 
    56         assertEquals(FOO, unwrap(unwrap(extended.type("x")))); 
    57         assertEquals(BAZ, unwrap(unwrap(extended.type("y")))); 
    58         assertEquals(BAR, unwrap(unwrap(extended.type("z")))); 
     56        assertEquals(FOO, unwrap(extended.type("x"))); 
     57        assertEquals(BAZ, unwrap(extended.type("y"))); 
     58        assertEquals(BAR, unwrap(extended.type("z"))); 
    5959         
    60         assert(! (BAR.equals(unwrap(unwrap(extended.type("x")))))); 
     60        assert(! (BAR.equals(unwrap(extended.type("x"))))); 
    6161    } 
    6262     
  • trunk/ProjectFortress/src/com/sun/fortress/shell/FileBasedRepository.java

    r1024 r1048  
    3737        new HashMap<DottedName, ComponentIndex>(); 
    3838    private final String pwd; 
    39     private final String path = Shell.fortressLocation()
     39    private final String path
    4040     
    4141    public FileBasedRepository(String _pwd) throws IOException { 
     42        this(_pwd, Shell.fortressLocation()); 
     43    } 
     44     
     45    public FileBasedRepository(String _pwd, String _path) throws IOException { 
    4246        pwd = _pwd; 
     47        path = _path; 
    4348        initialize(); 
    4449    } 
     
    4954         
    5055        for (File file: files) { 
    51             System.err.println("Loading " + file); 
    52              
    53             Option<CompilationUnit> candidate =  
    54                 Driver.readJavaAst(file.getCanonicalPath()); 
    55              
    56             if (candidate.isNone()) { 
    57                 throw new RepositoryError ("Compilation aborted. " + 
    58                                            "There were problems reading back the compiled file " +  
    59                                            file.getCanonicalPath()); 
     56            if (! file.isDirectory()) { 
     57                System.err.println("Loading " + file); 
     58                 
     59                Option<CompilationUnit> candidate =  
     60                    Driver.readJavaAst(file.getCanonicalPath()); 
     61                 
     62                if (candidate.isNone()) { 
     63                    throw new RepositoryError ("Compilation aborted. " + 
     64                                               "There were problems reading back the compiled file " +  
     65                                               file.getCanonicalPath()); 
     66                } 
     67                else { 
     68                    CompilationUnit _candidate = Option.unwrap(candidate); 
     69                     
     70                    if (_candidate instanceof Api) { 
     71                        ArrayList<Api> _candidates = new ArrayList<Api>(); 
     72                        _candidates.add((Api)_candidate); 
     73                        apis.putAll(IndexBuilder.buildApis(_candidates).apis()); 
     74                    } else if (_candidate instanceof Component) { 
     75                        ArrayList<Component> _candidates = new ArrayList<Component>(); 
     76                        _candidates.add((Component)_candidate); 
     77                         
     78                        components.putAll(IndexBuilder.buildComponents(_candidates).components()); 
     79                         
     80                        ArrayList<Id> _ids = new ArrayList<Id>(); 
     81                        for (Id id: _candidate.getName().getIds()) { _ids.add(new Id(new String(id.getText()))); } 
     82                         
     83                         
     84                    } else { 
     85                        throw new RuntimeException("The file " + file.getName() + " parsed to something other than a component or API!"); 
     86                    }    
     87                }                                  
    6088            } 
    61             else { 
    62                 CompilationUnit _candidate = Option.unwrap(candidate); 
    63                  
    64                 if (_candidate instanceof Api) { 
    65                     ArrayList<Api> _candidates = new ArrayList<Api>(); 
    66                     _candidates.add((Api)_candidate); 
    67                     apis.putAll(IndexBuilder.buildApis(_candidates).apis()); 
    68                 } else if (_candidate instanceof Component) { 
    69                     ArrayList<Component> _candidates = new ArrayList<Component>(); 
    70                     _candidates.add((Component)_candidate); 
    71                      
    72                     components.putAll(IndexBuilder.buildComponents(_candidates).components()); 
    73          
    74                     ArrayList<Id> _ids = new ArrayList<Id>(); 
    75                     for (Id id: _candidate.getName().getIds()) { _ids.add(new Id(new String(id.getText()))); } 
    76          
    77                      
    78                 } else { 
    79                     throw new RuntimeException("The file " + file.getName() + " parsed to something other than a component or API!"); 
    80                 }    
    81             }                                  
    8289        } 
    83     } 
    84               
     90    }  
    8591 
    8692    public Map<DottedName, ApiIndex> apis() { return apis; } 
     
    96102            if (ast instanceof Component) { 
    97103                Driver.writeJavaAst(ast, pwd + SEP + ast.getName() +  
    98                                    DOT + Driver.COMP_TREE_SUFFIX); 
     104        DOT + Driver.COMP_TREE_SUFFIX); 
    99105            } 
    100106            else { // ast instanceof Api 
    101107                Driver.writeJavaAst(ast, pwd + SEP + ast.getName() +  
    102                                    DOT + Driver.API_TREE_SUFFIX); 
     108        DOT + Driver.API_TREE_SUFFIX); 
    103109            } 
    104110        } catch (IOException e) { 
  • trunk/ProjectFortress/src/com/sun/fortress/syntaxabstractions/parser/FortressParser.java

    r1044 r1048  
    5656 
    5757        public Result(Iterable<? extends StaticError> errors) { 
    58                        super(errors); 
    59                        _apis = IterUtil.empty(); 
    60             _components = IterUtil.empty(); 
    61                
    62  
    63                public Result(Component component) { 
     58   super(errors); 
     59   _apis = IterUtil.empty(); 
     60            _components = IterUtil.empty(); 
     61 
     62 
     63  public Result(Component component) { 
    6464            _components = IterUtil.singleton(component); 
    6565            _apis = IterUtil.empty(); 
     
    144144            BufferedReader in = Useful.utf8BufferedFileReader(f); 
    145145            try { 
    146                 
    147                PreParser.Result ppr = PreParser.parse(f, env); 
    148                        if (!ppr.isSuccessful()) { return new Result(ppr.errors()); } 
    149  
    150                        xtc.parser.Result parseResult = null;  
    151                        ParserBase p = null; 
    152                         
    153                        System.out.print("Parsing file: "+f.getName()); 
    154                        if (!ppr.getGrammars().isEmpty()) { 
    155                                 
    156                                // Compile the macro declarations and create a temporary parser 
    157                                MacroCompiler macroCompiler = new FileBasedMacroCompiler(); 
    158                                MacroCompiler.Result tr = macroCompiler.compile(ppr.getGrammars()); 
    159                                if (!tr.isSuccessful()) { return new Result(tr.errors()); } 
    160                                Class<?> temporaryParserClass = tr.getParserClass(); 
    161                                 
    162                                try { 
    163                                                p = ParserMediator.getParser(temporaryParserClass, in, f.toString()); 
    164                                                parseResult = ParserMediator.parse(); 
    165                                } catch (Exception e) { 
    166                                        String desc = "Error occured while instantiating and executing a temporary parser: "+temporaryParserClass.getCanonicalName(); 
    167                            if (e.getMessage() != null) { desc += " (" + e.getMessage() + ")"; } 
    168                            return new Result(StaticError.make(desc, f.toString())); 
    169                                        }  
    170                                 
    171                        
    172                        else { 
     146             
     147            PreParser.Result ppr = PreParser.parse(f, env); 
     148       if (!ppr.isSuccessful()) { return new Result(ppr.errors()); } 
     149 
     150       xtc.parser.Result parseResult = null;  
     151       ParserBase p = null; 
     152        
     153       System.out.println("Parsing file: "+f.getName()); 
     154       if (!ppr.getGrammars().isEmpty()) { 
     155         
     156        // Compile the macro declarations and create a temporary parser 
     157        MacroCompiler macroCompiler = new FileBasedMacroCompiler(); 
     158        MacroCompiler.Result tr = macroCompiler.compile(ppr.getGrammars()); 
     159        if (!tr.isSuccessful()) { return new Result(tr.errors()); } 
     160        Class<?> temporaryParserClass = tr.getParserClass(); 
     161         
     162        try { 
     163      p = ParserMediator.getParser(temporaryParserClass, in, f.toString()); 
     164      parseResult = ParserMediator.parse(); 
     165        } catch (Exception e) { 
     166         String desc = "Error occured while instantiating and executing a temporary parser: "+temporaryParserClass.getCanonicalName(); 
     167                  if (e.getMessage() != null) { desc += " (" + e.getMessage() + ")"; } 
     168                  return new Result(StaticError.make(desc, f.toString())); 
     169     }  
     170         
     171       
     172       else { 
    173173                    p = new com.sun.fortress.parser.Fortress(in, f.toString()); 
    174174                    parseResult = ((com.sun.fortress.parser.Fortress) p).pFile(0); 
    175                        
    176                 
     175       
     176             
    177177 
    178178                if (parseResult.hasValue()) { 
  • trunk/ProjectFortress/static_tests/LocalVarRef.fss

    r727 r1048  
    2020   
    2121run(args:String...):() = do 
    22   x:ZZ = 23 
    23   y:ZZ = x 
     22  x:ZZ64 = 23 
     23  y:ZZ64 = x 
    2424  () 
    2525end 
  • trunk/ProjectFortress/static_tests/SimpleApi.fss

    r709 r1048  
    1818api SimpleApi 
    1919   
    20 x:ZZ 
    21 f(num:ZZ):ZZ 
     20x:ZZ64 
     21f(num:ZZ64): ZZ64 
    2222 
    2323end 
  • trunk/ProjectFortress/static_tests/SimpleProgram.fss

    r709 r1048  
    1919export Executable 
    2020   
    21 x:ZZ = 23 
    22 f(num:ZZ):ZZ = num + 1 
     21x:ZZ64 = 23 
     22f(num:ZZ64): ZZ64 = num + 1 
    2323 
    2424run(args:String...):() = do 
    25   z:ZZ = f(x) * f(10) 
     25  z:ZZ64 = f(x) * f(10) 
    2626  () 
    2727end