summaryrefslogtreecommitdiff
path: root/sources
diff options
context:
space:
mode:
authormoez <moez@epfl.ch>2005-10-25 10:57:51 +0000
committermoez <moez@epfl.ch>2005-10-25 10:57:51 +0000
commit6401f14a5cf1ddc82103da17528504a8d9b75601 (patch)
tree90dcf6f470375517f5cc2b673840ca06749398c7 /sources
parent5ffdc57de965d2f9e70e9128f5e726a50a7da656 (diff)
downloadscala-6401f14a5cf1ddc82103da17528504a8d9b75601.tar.gz
scala-6401f14a5cf1ddc82103da17528504a8d9b75601.tar.bz2
scala-6401f14a5cf1ddc82103da17528504a8d9b75601.zip
*** empty log message ***
Diffstat (limited to 'sources')
-rw-r--r--sources/scala/tools/nsc/Interpreter.scala55
-rwxr-xr-xsources/scala/tools/nsc/Main.scala10
2 files changed, 56 insertions, 9 deletions
diff --git a/sources/scala/tools/nsc/Interpreter.scala b/sources/scala/tools/nsc/Interpreter.scala
index a5ec788f09..a2dfe17b25 100644
--- a/sources/scala/tools/nsc/Interpreter.scala
+++ b/sources/scala/tools/nsc/Interpreter.scala
@@ -28,6 +28,9 @@ abstract class Interpreter {
// and parse it, using syntaxAnalyzer, to get input ASTs
val inASTs = syntaxAnalyzer.interpreterParse(
new compiler.CompilationUnit(
+ //if SourceFile is not modified, then fix a bug
+ //here by adding an EOF character to the end of
+ //the 'line'
new SourceFile("<console>",line.toCharArray())));
//todo: if (errors in parsing) after reporting them, exit method
@@ -47,18 +50,46 @@ abstract class Interpreter {
// first phase: compile auto-generated file
compiler.settings.outdir.value = getTempPath().getPath();
- (new compiler.Run) compile List(filename);
+ val cr = new compiler.Run;
+ cr compile List(filename);
-/*
//todo: if no errors in compilation then
// second phase: execute JVM, and print outcome
// else consider definition as if has not happened and exit method
+ //todo: use Scala's reflection API, which I designed, instead, for the following code
+ val cl = new java.net.URLClassLoader(Predef.Array(getTempPath().toURL()));
+ val interpreterResultObject: Class = Class.forName("InterpreterLine"+lineno+"Result",true,cl);
+ val resultValMethod: java.lang.reflect.Method = interpreterResultObject.getMethod("result",null);
+ var interpreterResultString: String = resultValMethod.invoke(interpreterResultObject,null).toString();
+
+ //var interpreterResultJavaTypeString: String = resultValMethod.getReturnType().getName();
+ //Console.println(compiler.definitions.EmptyPackage.info.members);
+ val interpreterResultSym: compiler.Symbol =
+ compiler.definitions.getMember(compiler.definitions.EmptyPackage,
+ compiler.newTermName("InterpreterLine"+lineno+"Result"));
+
+ def findSymbolWithName(ls: List[compiler.Symbol], name: compiler.Name): compiler.Symbol =
+ ls.find(s=>s.name == name) match {
+ case None => throw new IllegalStateException("Cannot find field '"+name+"' in InterpreterResult");
+ case Some(s) => s;
+ }
- //todo: uncomment
- //val futureJVMFile: File = getJVMInterpFile(interp_file);
- //futureJVMFile.deleteOnExit();
- //if (futureJVMFile.exists())
- // futureJVMFile.delete();
+ //var lastname: String = compiler.atPhase(cr.typerPhase.next){interpreterResultSym.info.decls.toList.last.name.toString()};
+ //reporter.info(null,lastname,true);
+ //todo: similar to what I should be doing for Scala's reflection??
+ var interpreterResultScalaTypeString: String =
+ compiler.atPhase(cr.typerPhase.next){
+ findSymbolWithName(interpreterResultSym.info.decls.toList,
+ compiler.nme.getterToLocal(compiler.newTermName("result")))
+ .tpe.toString()
+ };
+ reporter.info(null,interpreterResultString+": "+interpreterResultScalaTypeString/*+" ("+interpreterResultJavaTypeString+")"*/,true);
+
+/*
+ val scalaInterpFile: File = ScalaInterpFile(filename);
+ scalaInterpFile.deleteOnExit();
+ if(scalaInterpFile.exists())
+ scalaInterpFile.delete();
getvalue of line#.last_var_defined_in_line (from defined_vars)
(works for 'it' as it was added as last val to definedvars)
@@ -103,6 +134,8 @@ abstract class Interpreter {
import java.io.{File, PrintWriter, FileOutputStream};
val scalaFile = new File(filename);
scalaFile.deleteOnExit();
+ if(scalaFile.exists()) // to prevent old lingering files from having results from them reported!
+ scalaFile.delete();
val module = new PrintWriter(new FileOutputStream(scalaFile));
//todo:"import "+LoadedModules?.getName
@@ -119,6 +152,11 @@ abstract class Interpreter {
else fullLine = " " + line;
module.println(fullLine);
module.println("}");
+ module.println();
+ module.println("object InterpreterLine"+lineno+"Result ");
+ module.println("{ val result = (line"+lineno+"."+definedVars.toList.reverse.head+"); }");
+ // reflection is used later (see above) to get the result value above
+
module.flush();
module.close();
}
@@ -147,10 +185,11 @@ abstract class Interpreter {
//case AbsTypeDef(_,name,_,_) => throw new InterpIllegalDefException(name.toString()+": absract type definitions not allowed")
//case AliasTypeDef(_,name,_,_) => throw new InterpIllegalDefException(name.toString()+": alias type definitions not allowed")
//case LabelDef(name,_,_) => throw new InterpIllegalDefException(name.toString()+": label definitions not allowed")
- //case _ => assert(false) // throw new InterpIllegalDefException("Unsupported definition!")// ()
+ case _ => throw new InterpIllegalDefException("Unsupported interpreter definition. Contact Scala developers for adding interpreter support for it.")// ()
}
}
}
+ case class InterpIllegalDefException(msg: String) extends RuntimeException(msg);
// class ListTraverser extends Traverser {
// def traverse(trees: List[Tree]): Unit =
diff --git a/sources/scala/tools/nsc/Main.scala b/sources/scala/tools/nsc/Main.scala
index 02b758b662..76f43dea27 100755
--- a/sources/scala/tools/nsc/Main.scala
+++ b/sources/scala/tools/nsc/Main.scala
@@ -63,7 +63,15 @@ object Main {
val interpreter = new Interpreter {
val compiler: gCompiler.type = gCompiler
};
- loop(line => interpreter.interpret(line.trim(), reporter))
+ loop(line => try {
+ interpreter.interpret(line.trim(), reporter)
+ } catch {
+ case e: Exception => {
+ reporter.info(null,e.getMessage(),true);
+ //e.printStackTrace();
+ }
+ }
+ )
}
def process(args: Array[String]): unit = {