Changeset 807


Ignore:
Timestamp:
Jul 9, 2012 4:03:20 AM (12 years ago)
Author:
biz002
Message:

Rascal repl and run module support using Magnolia console machinery

fix swapped out and err writer passed to Evaluator

add .mg file parse tree dumper for test data generation

'rreload' clojure function for reloading specified rascal modules

support for interactive Rascal repl with Magnolia loaded, from console

Uses classpath settings and such already specified for leiningen.

This requires the jline library, also used by rascal-shell project.
Putting the JAR under 'lib'.

support for specifying (extra) rascal search path components for console

support default component in MAGNOLIA_JAVA_CLASSPATH

variant of runModule, hooked up with rascal-shell main function

core -> clojure.core

getTf and getVf to compiler api

support dynamic config of Rascal Evaluator to use from Clojure

Also support reloading of Rascal modules.

Location:
trunk/magnolia-eclipse
Files:
6 added
10 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/magnolia-eclipse/.classpath

    r640 r807  
    55        <classpathentry kind="lib" path="lib/annotations.jar"/>
    66        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
     7        <classpathentry kind="lib" path="lib/jline-0.9.94.jar"/>
    78        <classpathentry kind="output" path="bin"/>
    89</classpath>
  • trunk/magnolia-eclipse/.gitignore

    r789 r807  
    99magnolia-tests/
    1010.lein-deps-sum
     11clj-bin/
    1112clj-lib/
    1213.lein-plugins/
  • trunk/magnolia-eclipse/project.clj

    r806 r807  
    1111the command 'lein repl'. To launch a REPL under Emacs you may simply
    1212use M-x clojure-jack-in, provided you have a bleeding edge setup of
    13 Emacs with SLIME.
     13Emacs with clojure-mode.
    1414
    1515It is assumed that the Magnolia code base Java code is built
     
    4040  than the one installed as an Eclipse plugin - e.g.
    4141  'bin:lib/*:'$MAGNOLIA_ECLIPSE_HOME'/workspace/rascal/src:ext-lib/*'
     42  - any '_DEFAULT_' component is replaced by the default
     43  MAGNOLIA_JAVA_CLASSPATH - e.g.
     44  $MAGNOLIA_ECLIPSE_HOME/workspace/rascal/src:_DEFAULT_
    4245
    4346  MAGNOLIA_MODULE_PATH - in PATH env var style manner specifies (for
    4447  the Magnolia compiler) where to look for '.mg' files - e.g.
    4548  magnolia-tests/src
     49
     50  MAGNOLIA_RASCAL_MODULE_PATH - optional search path for additional
     51  '.rsc' files, which allows for making use of Magnolia compiler
     52  functionality in other Rascal projects - e.g.
     53  /tmp/rascal-tests/src:/tmp/rascal-tools/src
    4654
    4755After you have set the above env vars, execute 'make update-ext-lib'
     
    5967
    6068(defn get-extra-classpath-dirs []
    61   (let [ev (System/getenv "MAGNOLIA_JAVA_CLASSPATH")]
    62     (if (not ev)
    63       [(dir-path "bin")
    64        (jar-path "lib")
    65        (jar-path "ext-lib")]
    66       (map dir-path (seq (.split #":" ev))))))
     69  (let [ev (System/getenv "MAGNOLIA_JAVA_CLASSPATH")
     70        de [(dir-path "bin")
     71            (jar-path "lib")
     72            (jar-path "ext-lib")]]
     73    (if-not ev
     74      de
     75      (let [x (seq (.split #":" ev))
     76            y (mapcat #(if (= % "_DEFAULT_") de [(dir-path %)]) x)]
     77        y))))
    6778
    6879(defproject org.magnolialang "0.2-SNAPSHOT"
     
    7182  :compile-path "clj-bin"
    7283  :library-path "clj-lib"
    73   :repl-init org.magnolialang.core
     84  :repl-init org.magnolialang.clojure.core
     85  :aot [org.magnolialang.clojure.core
     86        org.magnolialang.clojure.rascal-shell]
    7487  ;; Time-consuming, but shows what classes loaded from where.
    7588  ;;:jvm-opts ["-verbose:class"]
  • trunk/magnolia-eclipse/src/org/magnolialang/clojure/core.clj

    r806 r807  
    1 (ns org.magnolialang.core
     1;; E.g. (use 'org.magnolialang.clojure.core :reload-all)
     2(ns org.magnolialang.clojure.core
    23  (:import
    34   (org.magnolialang.testutil RascalEval TestUtil)
     
    1213(def Type_ModuleResource
    1314  org.magnolialang.resources.IResourceManager/Type_ModuleResource)
    14 
    15 (def Type_DefInfo MagnoliaFacts/Type_DefInfo)
    1615
    1716;; (.getId (magnolia-language)) => "Magnolia"
     
    2423  (org.magnolialang.infra.ConsoleInfra/setInfra))
    2524
     25(defn get-infra []
     26  (org.magnolialang.infra.ConsoleInfra/getInfra))
     27
     28(defn get-ri []
     29  (RascalEval.))
     30
     31(defn path-env-var-to-list [n]
     32  (java.util.ArrayList.
     33   (map
     34    #(-> % (java.io.File.) (.getAbsolutePath))
     35    (let [ev (System/getenv n)]
     36      (if ev (seq (.split #":" ev)) [])))))
     37
     38(def MAGNOLIA_RASCAL_MODULE_PATH
     39  (path-env-var-to-list "MAGNOLIA_RASCAL_MODULE_PATH"))
     40
    2641(def compiler-op
    2742  (doto (new java.util.HashMap)
    2843    (.put "MAGNOLIA_MODULE_PATH"
    29           (java.util.ArrayList.
    30            (map
    31             #(-> % (java.io.File.) (.getAbsolutePath))
    32             (let [ev (System/getenv "MAGNOLIA_MODULE_PATH")]
    33               (if ev (seq (.split #":" ev)) [])))))
    34     (.put "a" "A")
    35     (.put "b" "B")))
    36 
    37 (defn init-compiler []
    38   (TestUtil/setInstance compiler-op)
    39   (let [state (TestUtil/getInstance)
    40         lang (magnolia-language)]
    41     (.addFile state
    42               "org.magnolialang.magnolia.templates.MagicMain"
    43               (java.io.File. "src/org/magnolialang/magnolia/templates/MagicMain.cxx"))
    44     (org.magnolialang.resources.LanguageRegistry/registerLanguage lang)
    45     (let [parser (.getParser lang)]
    46       (.initialize parser))))
    47 
    48 (defn get-infra []
    49   (org.magnolialang.infra.Infra/get))
    50 
    51 (defn get-parser [s]
    52   (.getParser (get-infra) s))
    53 
    54 (defn get-magnolia-parser []
    55   (get-parser (.getModuleName (.getParser (magnolia-language)))))
    56 
    57 (defn new-file-fact [l s]
    58   (org.magnolialang.testutil.UnmanagedFileFact. l s))
    59 
    60 (defn new-mg-file-fact [s]
    61   (new-file-fact (magnolia-language) (java.io.File. s)))
    62 
    63 ;; for testing only, fixed module
    64 (defn add-mg-module-fact []
    65   (let [state (TestUtil/getInstance)
    66         lang (magnolia-language)]
    67     (.addModuleResource state lang "basic.Basic" (java.io.File. "magnolia-tests/src/basic/Basic.mg"))))
    68 
    69 ;; (find-mg-module-fact "basic.Basic")
    70 (defn find-mg-module-fact [s]
    71   (.findModuleFact (TestUtil/getInstance) (magnolia-language) s))
    72 
    73 (defn get-mg-module-fact [s]
    74   (.getModuleFact (TestUtil/getInstance) (magnolia-language) s))
    75 
    76 (defn get-name-ast [s]
    77   (.getNameAST (magnolia-language) s))
    78 
    79 (defn get-parse-tree [s]
    80   (let [state (TestUtil/getInstance)
    81         lang (magnolia-language)
    82         modNameAst (.getNameAST lang s)]
    83     (.getParseTree state modNameAst)))
    84 
    85 (defn get-imploded-tree [s]
    86   (let [state (TestUtil/getInstance)
    87         lang (magnolia-language)
    88         modNameAst (.getNameAST lang s)]
    89     (.getImplodedTree state modNameAst)))
    90 
    91 (defn rstring [s]
    92   (.string org.magnolialang.terms.TermFactory/vf s))
    93 
    94 (defn get-option-string-fact [s]
    95   (let [state (TestUtil/getInstance)]
    96     (.getOptionStringFact state (rstring s))))
     44          (path-env-var-to-list "MAGNOLIA_MODULE_PATH"))
     45    ))
     46
     47(org.magnolialang.infra.ConsoleInfra/setRascalSearchPath
     48 MAGNOLIA_RASCAL_MODULE_PATH)
     49(set-infra)
     50
     51(def infra (get-infra))
     52(def rascal-monitor (.getMonitor infra))
     53
     54(TestUtil/setOptions compiler-op)
     55(def tu-state)
     56(def mg-lang)
     57
     58;; The Rascal evaluator instance to use. By replacing this with a
     59;; fresh copy you can "reload" Rascal modules. Clojure modules can
     60;; also be reloaded, naturally. However, unless you're running in an
     61;; controlled environment (such as Eclipse) with custom class loaders,
     62;; reloading Java classes is not possible, as Java itself does not
     63;; allow it.
     64(def rsc-evaluator (get-ri))
    9765
    9866(defn get-ric []
    99   (let [state (TestUtil/getInstance)
     67  (let [state tu-state
    10068        compiler (.getCompiler state)
    10169        evaluator (.getEvaluator compiler)
     
    10371    ri))
    10472
     73;; Use this to replace the compiler Rascal evaluator with a fresh
     74;; instance. Note that 'init-compiler' does not do this.
     75(defn refresh-evaluator []
     76  (org.magnolialang.testutil.MagnoliaCompiler/refreshEvaluator)
     77  (def rsc-evaluator (get-ric)))
     78       
     79;; You can do things like "pure" Rascal use without this, but
     80;; this (time-consuming) initialization is required (once) to use
     81;; the Magnolia toolchain proper.
     82(defn init-compiler []
     83  (def tu-state (new TestUtil))
     84  (def mg-lang (magnolia-language))
     85  (.addFile tu-state
     86            "org.magnolialang.magnolia.templates.MagicMain"
     87            (java.io.File.
     88             "src/org/magnolialang/magnolia/templates/MagicMain.cxx"))
     89  (org.magnolialang.resources.LanguageRegistry/registerLanguage mg-lang)
     90  (let [parser (.getParser mg-lang)]
     91    (.initialize parser))
     92  (def rsc-evaluator (get-ric)))
     93
     94(defn get-parser [s]
     95  (.getParser (get-infra) s))
     96
     97(defn get-magnolia-parser []
     98  (get-parser (.getModuleName (.getParser mg-lang))))
     99
     100(defn new-file-fact [l s]
     101  (org.magnolialang.testutil.UnmanagedFileFact. l s))
     102
     103(defn new-mg-file-fact [s]
     104  (new-file-fact mg-lang (java.io.File. s)))
     105
     106;; for testing only, fixed module
     107(defn add-mg-module-fact []
     108  (let [state tu-state
     109        lang mg-lang]
     110    (.addModuleResource state lang "basic.Basic" (java.io.File. "magnolia-tests/src/basic/Basic.mg"))))
     111
     112;; (find-mg-module-fact "basic.Basic")
     113(defn find-mg-module-fact [s]
     114  (.findModuleFact tu-state mg-lang s))
     115
     116(defn get-mg-module-fact [s]
     117  (.getModuleFact tu-state mg-lang s))
     118
     119(defn get-name-ast [s]
     120  (.getNameAST mg-lang s))
     121
     122(defn get-parse-tree [s]
     123  (let [state tu-state
     124        lang mg-lang
     125        modNameAst (.getNameAST lang s)]
     126    (.getParseTree state modNameAst)))
     127
     128(defn get-imploded-tree [s]
     129  (let [state tu-state
     130        lang mg-lang
     131        modNameAst (.getNameAST lang s)]
     132    (.getImplodedTree state modNameAst)))
     133
     134(defn rstring [s]
     135  (.string org.magnolialang.terms.TermFactory/vf s))
     136
     137(defn get-option-string-fact [s]
     138  (let [state tu-state]
     139    (.getOptionStringFact state (rstring s))))
     140
    105141(defn rtostring [r]
    106142  (RascalEval/resultToString r))
    107143
    108 ;; Evaluate in compiler Evaluator.
     144;; Evaluate Rascal expression in an Evaluator.
    109145;; E.g. (reval "registerCompiler")
    110146(defn reval [s]
    111   (.eval (get-ric) s))
     147  (.eval rsc-evaluator s))
    112148
    113149(defn reval-str [s]
     
    116152;; E.g. (reval-more "import util::tasks::Manager; getFact; 5;")
    117153(defn reval-more [s]
    118   (.evalMore (get-ric) s))
     154  (.evalMore rsc-evaluator s))
    119155
    120156;; E.g. (rimport "util::tasks::Manager")
    121157(defn rimport [s]
    122   (.doImport (get-ric) s))
     158  (.doImport rsc-evaluator s))
    123159
    124160(defn rcall
    125161  ;; Clojure does not (yet) appear to support optional positional
    126162  ;; arguments with default values, so we do it like this
    127   ([s as] (.call (get-ric) s as))
     163  ([s as] (.call rsc-evaluator s as))
    128164  ([s] (rcall s [])))
    129165
     
    131167  (rcall s as))
    132168
     169;; For reloading Rascal modules individually.
     170;; E.g. (rreload "org::magnolialang::pgf::Token")
     171(defn rreload [& s]
     172  (let [evaluator (.getEvaluator rsc-evaluator)
     173        errorLoc (java.net.URI/create "clojure:///")
     174        mods (java.util.HashSet. s)]
     175    (.reloadModules evaluator rascal-monitor mods errorLoc)))
     176
    133177(defn get-fact [type name]
    134   (.getFact (TestUtil/getInstance)
     178  (.getFact tu-state
    135179            type name))
    136180
    137181(defn get-def-info [s]
    138   (get-fact Type_DefInfo
     182  (get-fact MagnoliaFacts/Type_DefInfo
    139183            (get-name-ast s)))
    140184
     
    158202
    159203(defn ctor-get-field [v s]
    160   (.ctorGetField (TestUtil/getInstance) v s))
     204  (.ctorGetField tu-state v s))
    161205
    162206(defn ctor-get-val [v]
     
    169213;; E.g. (println (get-cxx-code "basic.Basic"))
    170214(defn get-cxx-code [s]
    171   (.getCxxCode (TestUtil/getInstance) (magnolia-language) s))
    172 
     215  (.getCxxCode tu-state mg-lang s))
     216
     217;; E.g. (view-flattened-module "basic.Basic")
    173218(defn view-flattened-module [s]
    174219  (println (.getValue
     
    176221                    (ctor-get-val (get-type-checked s))))))
    177222
    178 (defn get-ri []
    179   (RascalEval.))
    180 
    181 (defn peval [s]
    182   (.eval (get-ri) s))
    183 
    184 (defn peval-str [s]
    185   (RascalEval/resultToString (peval s)))
    186 
    187 (defn pcall [s as]
    188   (.call (get-ri) s as))
  • trunk/magnolia-eclipse/src/org/magnolialang/infra/ConsoleInfra.java

    r804 r807  
    88import java.net.URI;
    99import java.util.Arrays;
     10import java.util.Collections;
    1011import java.util.HashMap;
    1112import java.util.List;
     
    2223import org.magnolialang.terms.TermFactory;
    2324import org.magnolialang.testutil.MagnoliaCompiler;
    24 import org.rascalmpl.interpreter.Configuration;
    2525import org.rascalmpl.interpreter.Evaluator;
    2626import org.rascalmpl.interpreter.IRascalMonitor;
     
    4040 */
    4141public final class ConsoleInfra implements IInfra {
     42
     43        public static List<String> getRascalSearchPath() {
     44                return rascalSearchPath;
     45        }
     46
     47
     48        /**
     49         * @param rascalSearchPath
     50         *            list of absolute paths from which to look for Rascal modules
     51         */
     52        public static void setRascalSearchPath(List<String> rascalSearchPath) {
     53                ConsoleInfra.rascalSearchPath = rascalSearchPath;
     54        }
     55
     56        private static List<String>     rascalSearchPath        = Collections.emptyList();
     57
    4258
    4359        /**
     
    7692                //ClassResourceInputOutput eclipseResolver = new ClassResourceInputOutput(registry, "eclipse-std", RascalScriptInterpreter.class, "/org/rascalmpl/eclipse/library");
    7793                //registry.registerInput(eclipseResolver);
    78                 Evaluator eval = new Evaluator(TermFactory.vf, out, err, root, heap, loaders, resolver); // URIResolverRegistry
     94                Evaluator eval = new Evaluator(TermFactory.vf, err, out, root, heap, loaders, resolver); // URIResolverRegistry
    7995                // specifies one possible mapping to a rascal:// URI
    8096                eval.addRascalSearchPath((new File(".", "src")).toURI());
     97                for(String s : rascalSearchPath)
     98                        eval.addRascalSearchPath((new File(s)).toURI());
    8199
    82                 System.out.println("Rascal Classpath: " + Configuration.getRascalJavaClassPathProperty());
    83                 System.out.println("Magnolia Classpath: " + System.getProperty("magnolia.java.classpath"));
     100                //System.out.println("Rascal Classpath: " + Configuration.getRascalJavaClassPathProperty());
     101                //System.out.println("Magnolia Classpath: " + System.getProperty("magnolia.java.classpath"));
    84102                //Configuration.setRascalJavaClassPathProperty(System.getProperty("magnolia.java.classpath"));
    85103
     
    110128                if(parser != null)
    111129                        return parser;
    112                 System.out.println("generating parser for " + moduleName);
     130                //System.out.println("generating parser for " + moduleName);
    113131                /*
    114132                String moduleBaseName;
  • trunk/magnolia-eclipse/src/org/magnolialang/infra/IInfra.java

    r804 r807  
    2626
    2727
     28        /**
     29         * Note that the arguments are reversed compared to Evaluator constructor.
     30         *
     31         * @param out
     32         *            writer to use as Rascal stdout
     33         * @param err
     34         *            writer to use as Rascel stderr
     35         * @return Rascal evaluator
     36         */
    2837        public Evaluator newEvaluator(PrintWriter out, PrintWriter err);
    2938
  • trunk/magnolia-eclipse/src/org/magnolialang/pgf/tests/Makefile

    r713 r807  
    1 dump :
     1mg :
     2        rascal-src-with-magnolia DumpMgData.rsc > MgTestData.rsc
     3
     4expr :
    25        rascal-src DumpExprData.rsc > ExprTestData.rsc
  • trunk/magnolia-eclipse/src/org/magnolialang/testutil/MagnoliaCompiler.java

    r803 r807  
    77import org.eclipse.imp.pdb.facts.IConstructor;
    88import org.eclipse.imp.pdb.facts.IValue;
     9import org.eclipse.imp.pdb.facts.IValueFactory;
    910import org.eclipse.imp.pdb.facts.type.Type;
     11import org.eclipse.imp.pdb.facts.type.TypeFactory;
    1012import org.magnolialang.compiler.ICompiler;
    1113import org.magnolialang.compiler.IXRefResolver;
     
    128130
    129131
    130         private synchronized void reinitialize() {
    131                 try {
    132                         for(IIValueTask producer : producers) {
    133                                 registry.unregisterProducer(producer);
    134                         }
    135                 }
    136                 catch(Throwable t) { // NOPMD by anya on 1/5/12 5:39 AM
    137                 }
    138                 for(IIValueTask producer : producers) {
    139                         registry.registerProducer(producer);
    140                 }
     132        public TypeFactory getTf() {
     133                return Evaluator.__getTf();
     134        }
     135
     136
     137        public IValueFactory getVf() {
     138                return getEvaluator().__getVf();
    141139        }
    142140
  • trunk/magnolia-eclipse/src/org/magnolialang/testutil/RascalEval.java

    r798 r807  
    1616package org.magnolialang.testutil;
    1717
     18import java.io.File;
    1819import java.io.PrintWriter;
    1920import java.net.URI;
     
    2425import org.eclipse.imp.pdb.facts.IValueFactory;
    2526import org.eclipse.imp.pdb.facts.type.Type;
     27import org.magnolialang.infra.ConsoleInfra;
    2628import org.rascalmpl.interpreter.Evaluator;
    2729import org.rascalmpl.interpreter.env.GlobalEnvironment;
     
    3638        private static final String     EVAL_MODULE     = "$eval$";
    3739
    38         private final Evaluator         evaluator;
     40
     41        public Evaluator getEvaluator() {
     42                return evaluator;
     43        }
     44
     45        private final Evaluator evaluator;
    3946
    4047
     
    4653                IValueFactory vf = ValueFactoryFactory.getValueFactory();
    4754                evaluator = new Evaluator(vf, stderr, stdout, root, heap);
     55
     56                evaluator.addRascalSearchPath((new File(".", "src")).toURI());
     57                for(String s : ConsoleInfra.getRascalSearchPath())
     58                        evaluator.addRascalSearchPath((new File(s)).toURI());
    4859        }
    4960
     
    7687
    7788
     89        /**
     90         * @param statement
     91         * @return
     92         * @throws NullPointerException
     93         *             if there's no actual result produced
     94         */
    7895        public Result<IValue> evalMore(String statement) {
    7996                return evaluator.evalMore(null, statement, URI.create("prompt:///"));
  • trunk/magnolia-eclipse/src/org/magnolialang/testutil/TestUtil.java

    r804 r807  
    2121public final class TestUtil {
    2222
    23         public static TestUtil getInstance() {
    24                 return instance;
    25         }
    26 
    27         /**
    28          * The latest set of options passed to setInstance,
    29          * whether it succeeded or not.
    30          */
    31         private static Map<String, Object>      options = null;
     23        private static Map<String, Object>      options = Collections.emptyMap();
    3224
    3325
     
    3729
    3830
    39         /**
    40          * Clears any existing instance, even on error.
    41          *
    42          * @param op
    43          *            options
    44          */
    45         public static void setInstance(Map<String, Object> op) {
    46                 TestUtil.options = op;
    47                 TestUtil.instance = null;
    48                 TestUtil.instance = new TestUtil();
     31        public static void setOptions(Map<String, Object> op) {
     32                options = op;
    4933        }
    50 
    51 
    52         public static void setInstance() {
    53                 Map<String, Object> op = Collections.emptyMap();
    54                 setInstance(op);
    55         }
    56 
    57         private static TestUtil                 instance        = null;
    5834
    5935        private final MagnoliaCompiler  magnoliaCompiler;
     
    6137
    6238
    63         private TestUtil() {
    64                 ConsoleInfra.setInfra();
     39        public TestUtil() {
     40                //ConsoleInfra.setInfra();
    6541                magnoliaCompiler = new MagnoliaCompiler();
    6642                tr = magnoliaCompiler.getTransaction();
     
    140116
    141117
     118        public IRascalMonitor getMonitor() {
     119                IRascalMonitor rm = getInfra().getMonitor();
     120                return rm;
     121        }
     122
     123
    142124        public String getCxxCode(ILanguage lang, String modName) {
    143125                IRascalMonitor rm = getInfra().getMonitor();
  • trunk/magnolia-eclipse/test/org/magnolialang/test/core.clj

    r795 r807  
    11(ns org.magnolialang.test.core
    2   (:use [org.magnolialang.core])
     2  (:use [org.magnolialang.clojure.core])
    33  (:use [clojure.test]))
    44
     
    2121  (get-parse-tree "basic.Basic")
    2222  (get-imploded-tree "basic.Basic"))
     23
     24(deftest test-view-flattened-module
     25  (view-flattened-module "basic.Basic"))
Note: See TracChangeset for help on using the changeset viewer.