Skip to content

Commit

Permalink
Fix and revert to Eclipse compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
mmhelloworld committed Oct 2, 2013
1 parent 8c12ea9 commit ce3141f
Show file tree
Hide file tree
Showing 10 changed files with 376 additions and 457 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ compile src = do
javaSource <- liftIO $ javaSourceStringWriter.toString
if (Global.errors compilerState == 0) then do --No compilation errors
javaSource <- liftIO $ javaSourceStringWriter.toString
newclasses <- liftIO $ javac.classes
newclasses <- liftIO $ javac.lastCompilation >>= JavaCompilationInfo.classes
Interpreter.put $ s.{classes=newclasses}
return . CompilationSuccess $ CompilationInfo{
javaSource=javaSource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,15 @@ derive Exceptional ScriptException

data MemoryJavaCompiler = native frege.memoryjavac.MemoryJavaCompiler where
native new :: ClassLoader -> MutableIO (JMap String ByteArr) -> IOMutable MemoryJavaCompiler
native compile :: MutableIO MemoryJavaCompiler -> String -> String -> IO Bool
native classLoader :: MutableIO MemoryJavaCompiler -> IO ClassLoader
native error :: MutableIO MemoryJavaCompiler -> IO String
native classes :: MutableIO MemoryJavaCompiler -> IOMutable (JMap String ByteArr)
native compile :: MutableIO MemoryJavaCompiler -> String -> String -> IOMutable JavaCompilationInfo
native lastCompilation :: MutableIO MemoryJavaCompiler -> IOMutable JavaCompilationInfo
native classLoader :: MutableIO MemoryJavaCompiler -> IO ClassLoader

data JavaCompilationInfo = native frege.memoryjavac.CompilationInfo where
native isSuccess :: MutableIO JavaCompilationInfo -> IO Bool
native errorsAsString :: MutableIO JavaCompilationInfo -> IO String
native classLoader :: MutableIO JavaCompilationInfo -> IO ClassLoader
native classes :: MutableIO JavaCompilationInfo -> IOMutable (JMap String ByteArr)

data ByteArr = mutable native "byte[]"

Expand All @@ -243,10 +248,11 @@ javacPass compiler src = do
let !packName = g.sub.thisPack.unpack g
!loader = g.sub.loader
!jsrc <- doio $ src.toString
isSuccess <- doio $ compiler.compile jsrc packName
res <- doio $ compiler.compile jsrc packName
isSuccess <- doio $ res.isSuccess
if !isSuccess
then do
msg <- doio $ compiler.error
msg <- doio $ res.errorsAsString
U.error Position.null (TEXT msg)
return ("javac", 1)
else
Expand All @@ -256,6 +262,7 @@ javacPass compiler src = do
compile :: String -> [String] -> [String] -> PrintWriter ->
StringWriter -> MutableIO MemoryJavaCompiler -> String -> String -> String -> IO (SourceInfo, Global)
compile src paths libpaths !ow !jw !compiler !predefs !moduleName !modulePrelude = do
--println $ "compiling:\n" ++ src
let flags = Flags.fromList [IDE]
let steps = passes jw moduleName compiler
loader <- compiler.classLoader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ data ScriptContext = mutable native javax.script.ScriptContext where
pure native globalScope javax.script.ScriptContext.GLOBAL_SCOPE :: Int
native getBindings :: ScriptContext -> Int -> IO Bindings

data SimpleScriptContext = mutable native javax.script.SimpleScriptContext where
native new :: () -> IO SimpleScriptContext

data Bindings = mutable native javax.script.Bindings where
native asMap "(java.util.Map)" :: Bindings -> IOMutable (JMap String Object)

initScript = "import scripting.Setting"

native curThread Thread.currentThread :: () -> IO Thread
native curClassLoader getContextClassLoader :: Thread -> IO ClassLoader

initInterpreterState :: ScriptContext -> IO InterpreterState
initInterpreterState session = do
urlarr <- URLArray.new 0
loader <- ClassLoader.current >>= URLClassLoader.new urlarr
loader <- curThread () >>= curClassLoader >>= URLClassLoader.new urlarr
classesMaybe <- session.getAttribute "classes" ScriptContext.engineScope
classes <- maybe (HashMap.new () :: IOMutable (JMap String ByteArr)) asClassesMap classesMaybe
strMaybe <- session.getAttribute "script" ScriptContext.engineScope
Expand Down Expand Up @@ -49,7 +55,7 @@ eval script context = do
load :: String -> ScriptContext -> IO ([String] | ClassLoader)
load script context = do
state <- initInterpreterState context
evalInterpreter (loadIntp script) state
evalInterpreter (loadIntp script context) state

compiledScript :: String -> ScriptContext -> IO ([String] | CompilationResult)
compiledScript script context = do
Expand All @@ -70,12 +76,14 @@ evalCompiledScript compilation script context = do
putContext context "script" state.currentScript ScriptContext.engineScope
return res

loadIntp :: String -> Interpreter ([String] | ClassLoader )
loadIntp script = do
loadIntp :: String -> ScriptContext -> Interpreter ([String] | ClassLoader)
loadIntp script context = do
fregecRes <- compile script
case fregecRes of
CompilationSuccess (c@CompilationInfo{sourceInfo=sourceInfo,state=g}) ->
return $ Right g.sub.loader
CompilationSuccess (c@CompilationInfo{sourceInfo=sourceInfo,state=g}) -> do
s <- Interpreter.get
liftIO $ putContext context "classes" s.classes ScriptContext.engineScope
return (Right g.sub.loader)
CompilationFailure err -> return $ Left err

interpret :: String -> ScriptContext -> Interpreter ([String] | Maybe Object)
Expand Down Expand Up @@ -161,4 +169,12 @@ putContext context name value scope =
native asClassesMap "(java.util.Map)" :: Object -> IOMutable (JMap String ByteArr)
native asString "(java.lang.String)" :: Object -> IO String
native asURLClassLoader "(java.net.URLClassLoader)" :: Object -> IO URLClassLoader
native asBindingsMap "(java.util.Map)" :: Object -> IOMutable (JMap String Object)
native asBindingsMap "(java.util.Map)" :: Object -> IOMutable (JMap String Object)

main _ = do
context <- SimpleScriptContext.new ()
res <- load "module t.Hello where {x = 4;}" context
case res of
Left errs -> println errs
_ -> println "successful"

6 changes: 6 additions & 0 deletions jfrege-script-engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.theoryinpractise.frege</groupId>
<artifactId>frege-maven-plugin</artifactId>
<classifier>${frege.version}</classifier>
<version>${frege.plugin.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>frege-script-engine</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;

import javax.script.AbstractScriptEngine;
import javax.script.Bindings;
Expand All @@ -15,23 +13,18 @@
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleBindings;

import frege.interpreter.FregeInterpreter.TCompilationResult;
import frege.prelude.PreludeBase.TEither;
import frege.prelude.PreludeBase.TList;
import frege.prelude.PreludeBase.TList.DCons;
import frege.prelude.PreludeBase.TMaybe;
import frege.runtime.Func1;
import frege.runtime.Lambda;
import frege.runtime.Lazy;
import frege.runtime.Delayed;

import frege.memoryjavac.MemoryClassLoader;
import frege.scriptengine.FregeScriptEngine;

import static frege.interpreter.FregeInterpreter.TCompilationResult;

public class JFregeScriptEngine extends AbstractScriptEngine implements
Compilable {
private final ScriptEngineFactory factory;
Expand All @@ -48,26 +41,22 @@ public class JFregeScriptEngine extends AbstractScriptEngine implements

public JFregeScriptEngine(final ScriptEngineFactory factory) {
this.factory = factory;
final Func1 io = (Func1) FregeScriptEngine.load(initScript,
final Lambda io = FregeScriptEngine.load(initScript,
context);
final TEither intpRes = io.apply(1).result().<TEither>forced();
final int cons = intpRes._constructor();
if (cons == 0) {
final TList errs = getLeft(intpRes);
final List<String> errMsgs = toJavaList(errs);
throw new RuntimeException(errMsgs.toString());
} else {
final Object right = getRight(intpRes);
final MemoryClassLoader loader = (MemoryClassLoader) right;
context.setAttribute("classes", loader.getClasses(), ScriptContext.ENGINE_SCOPE);
}
}

@Override
public Object eval(final String script, final ScriptContext context)
throws ScriptException {
final Func1 res = (Func1) FregeScriptEngine.eval(script, context);
final TEither intpRes = res.apply(1).<TEither> forced();
final Lambda res = FregeScriptEngine.eval(script, context);
final TEither intpRes = res.apply(1).result().<TEither>forced();
final int cons = intpRes._constructor();
if (cons == 0) {
final TList errs = getLeft(intpRes);
Expand Down Expand Up @@ -173,7 +162,7 @@ public ScriptEngineFactory getFactory() {

@Override
public CompiledScript compile(final String script) throws ScriptException {
final Func1 io = (Func1) FregeScriptEngine.compiledScript(script, getContext());
final Lambda io = FregeScriptEngine.compiledScript(script, getContext());
final TEither intpRes = io.apply(1).result().<TEither>forced();
final int cons = intpRes._constructor();
final TCompilationResult res;
Expand All @@ -189,7 +178,7 @@ public CompiledScript compile(final String script) throws ScriptException {

@Override
public Object eval(final ScriptContext context) throws ScriptException {
final Func1 io = (Func1) FregeScriptEngine.evalCompiledScript(res, script, context);
final Lambda io = FregeScriptEngine.evalCompiledScript(res, script, context);
final TEither intpRes = io.apply(1).result().<TEither>forced();
final int cons = intpRes._constructor();
if (cons == 0) {
Expand Down
60 changes: 60 additions & 0 deletions memory-javac/src/main/java/frege/memoryjavac/CompilationInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package frege.memoryjavac;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.eclipse.jdt.core.compiler.CategorizedProblem;
import org.eclipse.jdt.internal.compiler.CompilationResult;

public class CompilationInfo {

private final List<CompilationResult> results;
private final Map<String, byte[]> classes;
private final ClassLoader classLoader;

public CompilationInfo(final List<CompilationResult> results,
final Map<String, byte[]> classes,
final ClassLoader classLoader) {
this.results = Collections.unmodifiableList(results);
this.classes = Collections.unmodifiableMap(classes);
this.classLoader = classLoader;
}

public boolean isSuccess() {
for (final CompilationResult res : results) {
if (res.getProblems() != null) {
for (final CategorizedProblem problem : res.getProblems()) {
if (problem.isError()) {
return false;
}
}
}
}
return true;
}

public String errorsAsString() {
final StringBuilder msgBuilder = new StringBuilder();
for (final CompilationResult res : results) {
if (res.getProblems() != null) {
for (final CategorizedProblem problem : res.getProblems()) {
if (problem.isError()) {
msgBuilder.append(problem.getMessage());
msgBuilder.append("\n");
}
}
}
}
return msgBuilder.toString();
}

public ClassLoader classLoader() {
return classLoader;
}

public Map<String, byte[]> classes() {
return classes;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package frege.memoryjavac;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

Expand Down
Loading

0 comments on commit ce3141f

Please sign in to comment.