Changeset 419


Ignore:
Timestamp:
Jun 11, 2010 9:39:28 AM (14 years ago)
Author:
Anya Helene Bagge
Message:
  • Module loader
  • Tighter integration with Rascal code
Location:
trunk/MagnoliaPlugin/src/org/magnolialang
Files:
2 added
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagnoliaPlugin/src/org/magnolialang/eclipse/commands/ViewFlattenedModule.java

    r410 r419  
    11package org.magnolialang.eclipse.commands;
    22
     3import java.io.IOException;
    34import java.io.StringBufferInputStream;
    45import java.net.URI;
     
    1314import org.eclipse.imp.model.ISourceProject;
    1415import org.eclipse.imp.parser.IParseController;
     16import org.eclipse.imp.pdb.facts.IList;
    1517import org.eclipse.imp.pdb.facts.IValue;
    1618import org.eclipse.imp.pdb.facts.IString;
     
    1820import org.eclipse.ui.IEditorPart;
    1921import org.eclipse.ui.handlers.HandlerUtil;
     22import org.magnolialang.eclipse.editor.ImplementationError;
    2023import org.magnolialang.eclipse.rascal.RascalInterpreter;
     24import org.magnolialang.env.MagnoliaEnvironment;
    2125import org.magnolialang.load.ProjectURIResolver;
    22 
     26import org.magnolialang.load.ModuleLoader;
    2327public class ViewFlattenedModule extends AbstractHandler {
    2428        @SuppressWarnings("deprecation")
     
    3034                        URI uri = ProjectURIResolver.constructProjectURI(parseCtrl.getProject(), parseCtrl.getPath());
    3135
     36                        ModuleLoader load = new ModuleLoader(parseCtrl.getProject());
     37                        IList mods;
     38                        IValue result = null;
     39                        try {
     40                                MagnoliaEnvironment env = new MagnoliaEnvironment();
     41                                IValue tree = load.loadModule(uri, env);
     42                                mods = env.getImports();
     43                               
     44                                RascalInterpreter eval = RascalInterpreter.getInstance();
     45                                IValue mEnv = eval.call("loadTrees", "import MagnoliaCompile;", mods);
     46                                result = eval.call("flattenModule", "import MagnoliaCompile;", tree, mEnv);
     47                        } catch (Throwable e1) {
     48                                e1.printStackTrace();
     49                                throw new ImplementationError("Flattening failed:", e1);
     50                        }
     51                       
    3252                        //RascalInterpreter.getInstance().eval("import MagnoliaCompile;");
    33                         String cmd = "flattenModule(|" + uri.toString() + "|, \"sophus-src\")";
    34                         IValue result = RascalInterpreter.getInstance().eval(cmd, "import MagnoliaCompile;");
     53                        //String cmd = "flattenModule(|" + uri.toString() + "|, \"sophus-src\")";
    3554                       
    3655                        if(result != null && result.getType().isStringType()) {
  • trunk/MagnoliaPlugin/src/org/magnolialang/eclipse/editor/ParseController.java

    r405 r419  
    1919import org.eclipse.jface.text.IRegion;
    2020import org.magnolialang.eclipse.MagnoliaPlugin;
     21import org.magnolialang.env.MagnoliaEnvironment;
    2122import org.magnolialang.load.ModuleLoader;
    2223import org.magnolialang.load.ProjectURIResolver;
     
    8990                       
    9091                        URI uri = ProjectURIResolver.constructProjectURI(project, path);
    91                         IConstructor result = loader.parseModule(uri, input, new ModuleEnvironment("***editor***"));
     92                        IConstructor result = loader.parseModule(uri, input, new MagnoliaEnvironment());
    9293                       
    9394                        if (result.getConstructorType() == Factory.ParseTree_Summary) {
  • trunk/MagnoliaPlugin/src/org/magnolialang/eclipse/rascal/RascalInterpreter.java

    r414 r419  
    6666                });
    6767               
    68                 if(!prelude.equals(""))
    69                         eval(prelude, eval);
    70                
     68                if(!prelude.equals("")) {
     69                        String[] cmds = prelude.split(";");
     70                        for(String cmd : cmds)
     71                                eval(cmd + ";", eval);
     72                }
    7173                evals.put(prelude, eval);
    7274                return eval;
  • trunk/MagnoliaPlugin/src/org/magnolialang/load/ModuleLoader.java

    r397 r419  
    77import java.util.ArrayList;
    88import java.util.List;
     9import org.eclipse.imp.model.ISourceProject;
    910
    1011import org.magnolialang.eclipse.editor.ImplementationError;
    11 import org.eclipse.imp.pdb.facts.IConstructor;
    12 import org.eclipse.imp.pdb.facts.ISourceLocation;
    13 import org.eclipse.imp.pdb.facts.IValueFactory;
    14 import org.eclipse.imp.pdb.facts.exceptions.FactTypeUseException;
    15 import org.eclipse.imp.pdb.facts.io.PBFReader;
    16 import org.rascalmpl.ast.ASTFactory;
    17 import org.rascalmpl.ast.AbstractAST;
    18 import org.rascalmpl.ast.Module;
    19 import org.rascalmpl.interpreter.env.ModuleEnvironment;
    20 import org.rascalmpl.interpreter.staticErrors.ModuleLoadError;
     12import org.magnolialang.env.MagnoliaEnvironment;
     13import org.magnolialang.parsetree.TreeImploder;
     14import static org.magnolialang.parsetree.XaTreeAdapter.*;
    2115import org.rascalmpl.interpreter.staticErrors.SyntaxError;
    22 import org.rascalmpl.interpreter.utils.Names;
    23 import org.rascalmpl.parser.ASTBuilder;
    2416import org.rascalmpl.uri.URIResolverRegistry;
    2517import org.rascalmpl.values.ValueFactoryFactory;
     
    2719import org.rascalmpl.values.errors.SummaryAdapter;
    2820import org.rascalmpl.values.uptr.Factory;
    29 
     21import org.rascalmpl.values.uptr.ParsetreeAdapter;
     22import org.eclipse.core.runtime.Path;
     23import org.eclipse.imp.pdb.facts.IConstructor;
     24import org.eclipse.imp.pdb.facts.IList;
     25import org.eclipse.imp.pdb.facts.IMap;
     26import org.eclipse.imp.pdb.facts.ISourceLocation;
     27import org.eclipse.imp.pdb.facts.IValue;
     28import org.eclipse.imp.pdb.facts.IValueFactory;
     29import org.eclipse.imp.pdb.facts.exceptions.FactTypeUseException;
     30import org.eclipse.imp.pdb.facts.io.PBFReader;
     31import static org.magnolialang.parsetree.XaTreeFactory.*;
    3032public class ModuleLoader{
    3133        //private List<ISdfSearchPathContributor> contributors = new ArrayList<ISdfSearchPathContributor>();
    3234        private ModuleParser parser;
    3335        private final boolean saveParsedModules = (System.getProperty("rascal.options.saveBinaries") != null ? System.getProperty("rascal.options.saveBinaries").equals("true") : false);
     36        private ISourceProject project;
    3437       
    3538        public ModuleLoader(){
    36                 this(new ModuleParser());
     39                this(new ModuleParser(), null);
    3740        }
    3841       
    39         public ModuleLoader(ModuleParser parser){
     42        public ModuleLoader(ISourceProject project){
     43                this(new ModuleParser(), project);
     44        }
     45       
     46        public ModuleLoader(ModuleParser parser, ISourceProject project){
    4047                this.parser = parser;
     48                this.project = project;
    4149        }
    4250
     
    8088
    8189
    82         public IConstructor parseModule(URI location, String moduleString, ModuleEnvironment env) throws IOException{
     90        public IConstructor parseModule(URI location, String moduleString, MagnoliaEnvironment env) throws IOException{
    8391                byte[] data = moduleString.getBytes();
    8492               
    85                 IConstructor tree = parser.parseModule(location, data, env);
     93                IConstructor tree = parser.parseModule(location, data);
    8694
    8795                if(tree.getConstructorType() == Factory.ParseTree_Summary){
     
    92100        }
    93101
    94         public IConstructor parseModule(URI location, ModuleEnvironment env) throws IOException{
     102        public IConstructor parseModule(URI location, MagnoliaEnvironment env) throws IOException{
    95103                byte[] data;
    96104               
     
    105113                }
    106114               
    107                 IConstructor tree = parser.parseModule(location, data, env);
     115                IConstructor tree = parser.parseModule(location, data);
    108116               
    109117                if(tree.getConstructorType() == Factory.ParseTree_Summary){
     
    113121                return tree;
    114122        }
    115 
     123       
     124        public IConstructor loadModule(URI location, MagnoliaEnvironment env) throws IOException {
     125                IConstructor tree = ParsetreeAdapter.getTop(parseModule(location, null));
     126                tree = (IConstructor) TreeImploder.implodeTree(tree);
     127                return loadModule(tree, env);
     128        }
     129        public IConstructor loadModule(IConstructor tree, MagnoliaEnvironment env) throws IOException {
     130                if(isCons(tree, "MagnoliaTree", 2)) {
     131                        IValue head = getArg(tree, 0);
     132                        if(isCons(head, "ModuleHead", 2)) {
     133                                env.addImport(yield(getArg(head, 0)), tree);
     134                                for(IValue clause : getArgs(getArg(head, 1))) {
     135                                        if(isCons(clause, "Imports", 1)) {
     136                                                for(IValue imp : getArgs(getArg(clause, 0))) {
     137                                                        if(isCons(imp, "ImportAll", 1)) {
     138                                                                String name = yield(getArg(imp, 0));
     139                                                                URI uri = ProjectURIResolver.constructProjectURI(project, new Path(name).addFileExtension("mg"));
     140                                                                loadModule(uri, env);
     141                                                                System.out.println("import " + name);
     142                                                        }
     143                                                }
     144                                        }
     145                                }       
     146                        }
     147                }
     148                return tree;
     149        }
    116150        public void setParser(ModuleParser parser) {
    117151                this.parser = parser;
  • trunk/MagnoliaPlugin/src/org/magnolialang/load/ModuleParser.java

    r369 r419  
    9999        }
    100100
    101         public IConstructor parseModule(URI location, InputStream source, ModuleEnvironment env) throws IOException {
     101        public IConstructor parseModule(URI location, InputStream source) throws IOException {
    102102                try {
    103103                        return parseFromStream(getTable(), location, source);
     
    107107        }
    108108       
    109         public IConstructor parseModule(URI location, byte[] data, ModuleEnvironment env) throws IOException {
     109        public IConstructor parseModule(URI location, byte[] data) throws IOException {
    110110                try {
    111111                        return parseFromData(getTable(), location, data);
  • trunk/MagnoliaPlugin/src/org/magnolialang/parsetree/TreeImplodeVisitor.java

    r414 r419  
    1616import org.rascalmpl.values.ValueFactoryFactory;
    1717import org.rascalmpl.values.uptr.Factory;
     18import org.rascalmpl.values.uptr.ParsetreeAdapter;
    1819import org.rascalmpl.values.uptr.ProductionAdapter;
    1920import org.rascalmpl.values.uptr.SymbolAdapter;
     
    6667                        }
    6768                }
    68 
     69                if(cons == null)
     70                        cons = ProductionAdapter.getConstructorName(prod);
     71               
    6972                IConstructor result = null;
    7073                IList concrete = null;
     
    7679                        concrete = null; // vf.list(child(0));
    7780                }
     81                else if(sort.equals("<START>")) {
     82                        ITuple t = visitChildren(TreeAdapter.getArgs(tree));
     83                        concrete = (IList) t.get(1);
     84                        cons = cons != null ? cons : sort;
     85                        result = (IConstructor) ((IList) t.get(0)).get(0);
     86                        concrete = ((IList) result.getAnnotation("concrete")).insert(concrete.get(0)).append(concrete.get(2));
     87                }
    7888                else if(SymbolAdapter.isCf(rhs)) {
    7989                        // the nested symbol within the cf(...)
     
    113123                                ITuple t = visitChildren(TreeAdapter.getArgs(tree));
    114124                                concrete = (IList) t.get(1);
    115                                 result = cons(cons == null ? cons : ProductionAdapter.getConstructorName(prod),
     125                                result = cons(cons != null ? cons : sort,
    116126                                                sort,
    117127                                                (IList) t.get(0));
     
    126136                                result = result.setAnnotation("concrete", concrete);
    127137                }
     138                else
     139                        return result;
    128140
    129141                return result;
  • trunk/MagnoliaPlugin/src/org/magnolialang/parsetree/XaTreeAdapter.java

    r417 r419  
    9090                && ((IConstructor)tree).getConstructorType() == Type_Cons;
    9191        }
     92       
     93        public static boolean isCons(IValue tree, String name) {
     94                return tree instanceof IConstructor
     95                && ((IConstructor)tree).getConstructorType() == Type_Cons
     96                && ((IString)((IConstructor)tree).get("name")).getValue().equals(name);
     97        }
     98       
     99        public static boolean isCons(IValue tree, String name, int arity) {
     100                return tree instanceof IConstructor
     101                && ((IConstructor)tree).getConstructorType() == Type_Cons
     102                && ((IString)((IConstructor)tree).get("name")).getValue().equals(name)
     103                && ((IList)((IConstructor)tree).get("args")).length() == arity;
     104        }
    92105
    93106        public static boolean isSeq(IValue tree) {
     
    101114        }
    102115
     116        public static boolean isLeaf(IValue tree, String chars) {
     117                return tree instanceof IConstructor
     118                && ((IConstructor)tree).getConstructorType() == Type_Leaf
     119                && ((IString)((IConstructor)tree).get("strVal")).getValue().equals(chars);
     120        }
     121
    103122        public static boolean isVar(IValue tree) {
    104123                return tree instanceof IConstructor
     
    106125        }
    107126       
     127        public static boolean isVar(IValue tree, String name) {
     128                return tree instanceof IConstructor
     129                && ((IConstructor)tree).getConstructorType() == Type_TermVar
     130                && ((IString)((IConstructor)tree).get("name")).getValue().equals(name);
     131        }
     132       
     133        public static IList getArgs(IValue tree) {
     134                assert isSeq(tree) || isCons(tree);
     135                return (IList) ((IConstructor)tree).get("args");
     136        }
     137       
     138        public static IValue getArg(IValue tree, int arg) {
     139                assert isSeq(tree) || isCons(tree);
     140                return ((IList) ((IConstructor)tree).get("args")).get(arg);
     141        }
     142
     143        public static String getString(IValue tree) {
     144                if(tree instanceof IString)
     145                        return ((IString)tree).getValue();
     146                else
     147                        return null;
     148        }
    108149        public static boolean isGround(IValue tree) {
    109150                try {
     
    144185                return lw.done();
    145186        }
     187       
     188        public static String yield(IValue tree) {
     189                try {
     190                        return tree.accept(new NullVisitor<String>() {
     191                                public String visitConstructor(IConstructor c) throws VisitorException {
     192                                        IList concrete = (IList) c.getAnnotation("concrete");
     193                                        if(concrete == null || concrete.length() == 0) {
     194                                                if(isLeaf(c))
     195                                                        return ((IString)c.get("strVal")).getValue();
     196                                                else if(isVar(c))
     197                                                        return "<" + ((IString)c.get("name")).getValue() + ">";
     198                                                else {
     199                                                        StringBuffer result = new StringBuffer();
     200                                                        for(IValue child : getArgs(c))
     201                                                                result.append(child.accept(this));
     202                                                        return result.toString();
     203                                                }
     204                                        }
     205                                        IList args;
     206                                        if(isLeaf(c) || isVar(c))
     207                                                args = vf.list(c.removeAnnotation("concrete"));
     208                                        else
     209                                                args = getArgs(c);
     210                                       
     211                                        StringBuffer result = new StringBuffer();
     212                                        for(IValue token : concrete) {
     213                                                Type type = ((IConstructor)token).getConstructorType();
     214                                                if(type == Type_Token || type == Type_Layout || type == Type_Comment) {
     215                                                        result.append(((IConstructor)token).get("chars"));
     216                                                }
     217                                                else {
     218                                                        int index = ((IInteger)((IConstructor)token).get("index")).intValue();
     219                                                        result.append(args.get(index).accept(this));
     220                                                }
     221                                        }                                       
     222                                        return result.toString();
     223                                }});
     224                } catch (VisitorException e) {
     225                        return null;
     226                }
     227        }
    146228}
  • trunk/MagnoliaPlugin/src/org/magnolialang/parsetree/XaTreeFactory.java

    r418 r419  
    8383        public static IConstructor termvar(String name, String sort) {
    8484                return vf.constructor(Type_TermVar, vf.string(name), vf.string(sort));
     85        }
     86       
     87        public static IConstructor termvar(String name) {
     88                return vf.constructor(Type_TermVar, vf.string(name), vf.string("_"));
    8589        }
    8690       
Note: See TracChangeset for help on using the changeset viewer.