- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
rlwrap with scala interpreter
Wed, 2009-04-29, 07:03
Hi,
I want to use readline with scala, doing this,
> rlwrap scala
but weirdly that does nothing. The rlwrap manual says that
"When the standard input is not a terminal, rlwrap will check,
and then ignore, all options and simply execute command.
When stdout (or stderr) is not a terminal, rlwrap will re-open it
to /dev/tty (the users terminal) after it has started command"
Is stdin redirected to something else? I remember
rlwrap working with an older version of the interpreter.
Slowly learning Scala, lots of fun so far : )
Thanks,
Howard
Wed, 2009-04-29, 07:37
#2
Re: rlwrap with scala interpreter
> Hello Howard try this
> rlwrap scala -Xnojline
great, it works! Thank you so much, Tony!
Wed, 2010-09-01, 04:37
#3
scala Interpreter
Hi,
I've been working on embedding the scala interpreter in an app and loading a file into it but I'm stuck, In the source it says you can compile a complete source file:
quote:
* The compile()
method loads a
* complete Scala file.
but I can't get it to work.
This is what I've been working with:
var settings = new GenericRunnerSettings(str => println(str))
settings.loadfiles.appendToValue("script/project.scala")
settings.usejavacp.value = true
// interpreter.compile
var interpreter = new Interpreter(settings)
// interpreter.interpret(":load script/project.scala")
interpreter.interpret("val project = Project")
interpreter.interpret("project.sayHello")
the commented out lines either don't compile or fail at runtime. How do I load a file into the interpreter?
thx in advance,
Wed, 2010-09-01, 04:57
#4
Re: scala Interpreter
On Tue, Aug 31, 2010 at 11:30:36PM -0400, Michael Fortin wrote:
> I've been working on embedding the scala interpreter in an app and
> loading a file into it but I'm stuck, In the source it says you can
> compile a complete source file: quote:
> * The compile()
method loads a
> * complete Scala file.
Those comments are pretty old. I can't remember ever reading them in
the course of rewriting most of the interpreter, so that doesn't bode
well for the accuracy.
> var settings = new GenericRunnerSettings(str => println(str))
> settings.loadfiles.appendToValue("script/project.scala")
> settings.usejavacp.value = true
> // interpreter.compile
> var interpreter = new Interpreter(settings)
> // interpreter.interpret(":load script/project.scala")
> interpreter.interpret("val project = Project")
> interpreter.interpret("project.sayHello")
The commands like :load are understood in InterpreterLoop, which you
might want to look at. Once you strip away all the plumbing it doesn't
do anything besides call the interpret method a bunch of times, so for
instance if you had one line to a file, this would work:
import scala.tools.nsc._
object Test {
def main(args: Array[String]): Unit = {
var settings = new GenericRunnerSettings(str => println(str))
settings.loadfiles.appendToValue("script/project.scala")
settings.usejavacp.value = true
var interpreter = new Interpreter(settings)
// old school
io.File("/tmp/project.scala").lines() foreach (interpreter interpret _)
interpreter.interpret("val project = Project")
interpreter.interpret("project.sayHello")
}
}
// /tmp/project.scala
def f(x: Int) = x*x
println("Bippy boo: " + f(10))
object Project { def sayHello = scala.tools.nsc.io.Process("/usr/bin/say \"Hello Dr. Falcon\"") }
...and there it goes. If you need real parsing you should probably
create an InterpreterLoop, something vaguely like
val intLoop = new InterpreterLoop
intLoop.settings = new Settings(Console.println)
// XXX come back to the dot handling
intLoop.settings.classpath.value = "."
intLoop.createInterpreter
intLoop.in = InteractiveReader.createDefault(intLoop.interpreter)
Wed, 2010-09-01, 14:47
#5
Re: scala Interpreter
> io.File("/tmp/project.scala").lines() foreach (interpreter interpret _)
Are there any issues if interpreter.interpret() is called with multiple lines of
code?
I have had pretty good luck running multiple lines of code with the interpret()
method. In this particular case, the following code also works fine:
import scala.tools.nsc._
import scala.io.Source
object InterpretMultiLines {
def main(args: Array[String]): Unit = {
var settings = new GenericRunnerSettings(str => println(str))
settings.usejavacp.value = true
var interpreter = new Interpreter(settings)
interpreter.interpret(Source.fromFile("/tmp/project.scala").mkString)
}
}
Regards,
- Lalit
Wed, 2010-09-01, 17:27
#6
Re: scala Interpreter
Thanks for the suggestions, Using Source was the quick & easy way to get my proof of concept working. For now, my preference is to use it instead of the higher level Loop, but it's good to know its there.
Thanks again.
Mike
> io.File("/tmp/project.scala").lines() foreach (interpreter interpret _)
Are there any issues if interpreter.interpret() is called with multiple lines of
code?
I have had pretty good luck running multiple lines of code with the interpret()
method. In this particular case, the following code also works fine:
import scala.tools.nsc._
import scala.io.Source
object InterpretMultiLines {
def main(args: Array[String]): Unit = {
var settings = new GenericRunnerSettings(str => println(str))
settings.usejavacp.value = true
var interpreter = new Interpreter(settings)
interpreter.interpret(Source.fromFile("/tmp/project.scala").mkString)
}
}
Regards,
- Lalit
Fri, 2010-09-03, 03:57
#7
Re: scala Interpreter
Hi,
I have another interpreter question. I can only get my code to run if i set
settings.usejavacp.value = true
otherwise I get:
Failed to initialize compiler: object scala not found.
** Note that as of 2.8 scala does not assume use of the java classpath.
** For the old behavior pass -usejavacp to scala, or if using a Settings
** object programatically, settings.usejavacp.value = true.
debug| setup env
Exception in thread "main" java.lang.NullPointerException
at scala.tools.nsc.Global$Run.(Global.scala:582)
at scala.tools.nsc.Interpreter.compileSources(Interpreter.scala:498)
at scala.tools.nsc.Interpreter.compileString(Interpreter.scala:506)
at scala.tools.nsc.Interpreter.bind(Interpreter.scala:615)
at org.brzy.fab.Main$.loadEnv(Main.scala:86)
at org.brzy.fab.Main$.main(Main.scala:48)
at org.brzy.fab.Main.main(Main.scala)
I've tried setting the libs like:
settings.classpath.append("project/boot/scala-2.8.0/lib/scala-compiler.jar")
settings.classpath.append("project/boot/scala-2.8.0/lib/scala-library.jar")
settings.classpath.append("target/scala_2.8.0/classes")
… other libs
but that didn't help. What default libraries would I need to get the interpreter to run in it's own classloader? Would I need to modify the bootclasspath or javabootclasspath options?
thanks,
Mike
On Sep 1, 2010, at 12:24 PM, Michael Fortin wrote:
> Thanks for the suggestions, Using Source was the quick & easy way to get my proof of concept working. For now, my preference is to use it instead of the higher level Loop, but it's good to know its there.
>
> Thanks again.
> Mike
>
>> io.File("/tmp/project.scala").lines() foreach (interpreter interpret _)
>
> Are there any issues if interpreter.interpret() is called with multiple lines of
> code?
>
>
> I have had pretty good luck running multiple lines of code with the interpret()
> method. In this particular case, the following code also works fine:
>
> import scala.tools.nsc._
> import scala.io.Source
>
> object InterpretMultiLines {
> def main(args: Array[String]): Unit = {
> var settings = new GenericRunnerSettings(str => println(str))
> settings.usejavacp.value = true
> var interpreter = new Interpreter(settings)
> interpreter.interpret(Source.fromFile("/tmp/project.scala").mkString)
> }
> }
>
> Regards,
> - Lalit
>
>
>
>
> ----- Original Message ----
> From: Paul Phillips
> To: Michael Fortin
> Cc: scala-user@listes.epfl.ch
> Sent: Wed, September 1, 2010 9:26:00 AM
> Subject: Re: [scala-user] scala Interpreter
>
> On Tue, Aug 31, 2010 at 11:30:36PM -0400, Michael Fortin wrote:
>> I've been working on embedding the scala interpreter in an app and
>> loading a file into it but I'm stuck, In the source it says you can
>> compile a complete source file: quote:
>> * The compile()
method loads a
>> * complete Scala file.
>
> Those comments are pretty old. I can't remember ever reading them in
> the course of rewriting most of the interpreter, so that doesn't bode
> well for the accuracy.
>
>> var settings = new GenericRunnerSettings(str => println(str))
>> settings.loadfiles.appendToValue("script/project.scala")
>> settings.usejavacp.value = true
>> // interpreter.compile
>> var interpreter = new Interpreter(settings)
>> // interpreter.interpret(":load script/project.scala")
>> interpreter.interpret("val project = Project")
>> interpreter.interpret("project.sayHello")
>
> The commands like :load are understood in InterpreterLoop, which you
> might want to look at. Once you strip away all the plumbing it doesn't
> do anything besides call the interpret method a bunch of times, so for
> instance if you had one line to a file, this would work:
>
> import scala.tools.nsc._
>
> object Test {
> def main(args: Array[String]): Unit = {
> var settings = new GenericRunnerSettings(str => println(str))
> settings.loadfiles.appendToValue("script/project.scala")
> settings.usejavacp.value = true
> var interpreter = new Interpreter(settings)
> // old school
> io.File("/tmp/project.scala").lines() foreach (interpreter interpret _)
> interpreter.interpret("val project = Project")
> interpreter.interpret("project.sayHello")
> }
> }
>
> // /tmp/project.scala
> def f(x: Int) = x*x
> println("Bippy boo: " + f(10))
> object Project { def sayHello = scala.tools.nsc.io.Process("/usr/bin/say \"Hello
> Dr. Falcon\"") }
>
> ...and there it goes. If you need real parsing you should probably
> create an InterpreterLoop, something vaguely like
>
> val intLoop = new InterpreterLoop
> intLoop.settings = new Settings(Console.println)
> // XXX come back to the dot handling
> intLoop.settings.classpath.value = "."
> intLoop.createInterpreter
> intLoop.in = InteractiveReader.createDefault(intLoop.interpreter)
>
Fri, 2010-09-03, 06:07
#8
Re: scala Interpreter
On Thu, Sep 02, 2010 at 10:56:45PM -0400, Michael Fortin wrote:
> I've tried setting the libs like:
> settings.classpath.append("project/boot/scala-2.8.0/lib/scala-compiler.jar")
> settings.classpath.append("project/boot/scala-2.8.0/lib/scala-library.jar")
> settings.classpath.append("target/scala_2.8.0/classes")
> … other libs
>
> but that didn't help. What default libraries would I need to get the
> interpreter to run in it's own classloader? Would I need to modify
> the bootclasspath or javabootclasspath options?
Note that you don't actually need jline, but power mode wouldn't stand
for its absence and I like to use it. It's obvious from the classpaths,
but they're relative to the scala distribution.
% java -classpath lib/scala-compiler.jar:lib/scala-library.jar
scala.tools.nsc.MainGenericRunner -classpath
lib/scala-compiler.jar:lib/scala-library.jar:lib/jline.jar
Welcome to Scala version 2.8.0.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_20).
Type in expressions to have them evaluated.
Type :help for more information.
scala> :power
** Power User mode enabled - BEEP BOOP **
** scala.tools.nsc._ has been imported **
** New vals! Try repl, global, power **
** New cmds! :help to discover them **
** New defs! Type power. to reveal **
scala> repl.compilerClasspath foreach println
file:/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Classes/classes.jar
file:/System/Library/Frameworks/JavaVM.framework/Frameworks/JavaRuntimeSupport.framework/Resources/Java/JavaRuntimeSupport.jar
file:/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Classes/ui.jar
file:/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Classes/jsse.jar
file:/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Classes/jce.jar
file:/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Classes/charsets.jar
file:/System/Library/Java/Extensions/AppleScriptEngine.jar
file:/System/Library/Java/Extensions/dns_sd.jar
file:/System/Library/Java/Extensions/j3daudio.jar
file:/System/Library/Java/Extensions/j3dcore.jar
file:/System/Library/Java/Extensions/j3dutils.jar
file:/System/Library/Java/Extensions/jai_codec.jar
file:/System/Library/Java/Extensions/jai_core.jar
file:/System/Library/Java/Extensions/mlibwrapper_jai.jar
file:/System/Library/Java/Extensions/MRJToolkit.jar
file:/System/Library/Java/Extensions/QTJava.zip
file:/System/Library/Java/Extensions/vecmath.jar
file:/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/lib/ext/apple_provider.jar
file:/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/lib/ext/dnsns.jar
file:/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/lib/ext/localedata.jar
file:/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/lib/ext/sunjce_provider.jar
file:/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/lib/ext/sunpkcs11.jar
file:/scala/inst/scala-2.8.0.final/lib/scala-compiler.jar
file:/scala/inst/scala-2.8.0.final/lib/scala-library.jar
file:/scala/inst/scala-2.8.0.final/lib/jline.jar
Sat, 2010-09-04, 18:17
#9
Re: scala Interpreter
Thanks again Paul, that was a big help.
On Sep 3, 2010, at 12:59 AM, Paul Phillips wrote:
> On Thu, Sep 02, 2010 at 10:56:45PM -0400, Michael Fortin wrote:
>> I've tried setting the libs like:
>> settings.classpath.append("project/boot/scala-2.8.0/lib/scala-compiler.jar")
>> settings.classpath.append("project/boot/scala-2.8.0/lib/scala-library.jar")
>> settings.classpath.append("target/scala_2.8.0/classes")
>> … other libs
>>
>> but that didn't help. What default libraries would I need to get the
>> interpreter to run in it's own classloader? Would I need to modify
>> the bootclasspath or javabootclasspath options?
>
> Note that you don't actually need jline, but power mode wouldn't stand
> for its absence and I like to use it. It's obvious from the classpaths,
> but they're relative to the scala distribution.
>
> % java -classpath lib/scala-compiler.jar:lib/scala-library.jar
> scala.tools.nsc.MainGenericRunner -classpath
> lib/scala-compiler.jar:lib/scala-library.jar:lib/jline.jar
>
> Welcome to Scala version 2.8.0.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_20).
> Type in expressions to have them evaluated.
> Type :help for more information.
>
> scala> :power
> ** Power User mode enabled - BEEP BOOP **
> ** scala.tools.nsc._ has been imported **
> ** New vals! Try repl, global, power **
> ** New cmds! :help to discover them **
> ** New defs! Type power. to reveal **
>
> scala> repl.compilerClasspath foreach println
> file:/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Classes/classes.jar
> file:/System/Library/Frameworks/JavaVM.framework/Frameworks/JavaRuntimeSupport.framework/Resources/Java/JavaRuntimeSupport.jar
> file:/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Classes/ui.jar
> file:/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Classes/jsse.jar
> file:/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Classes/jce.jar
> file:/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Classes/charsets.jar
> file:/System/Library/Java/Extensions/AppleScriptEngine.jar
> file:/System/Library/Java/Extensions/dns_sd.jar
> file:/System/Library/Java/Extensions/j3daudio.jar
> file:/System/Library/Java/Extensions/j3dcore.jar
> file:/System/Library/Java/Extensions/j3dutils.jar
> file:/System/Library/Java/Extensions/jai_codec.jar
> file:/System/Library/Java/Extensions/jai_core.jar
> file:/System/Library/Java/Extensions/mlibwrapper_jai.jar
> file:/System/Library/Java/Extensions/MRJToolkit.jar
> file:/System/Library/Java/Extensions/QTJava.zip
> file:/System/Library/Java/Extensions/vecmath.jar
> file:/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/lib/ext/apple_provider.jar
> file:/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/lib/ext/dnsns.jar
> file:/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/lib/ext/localedata.jar
> file:/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/lib/ext/sunjce_provider.jar
> file:/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/lib/ext/sunpkcs11.jar
> file:/scala/inst/scala-2.8.0.final/lib/scala-compiler.jar
> file:/scala/inst/scala-2.8.0.final/lib/scala-library.jar
> file:/scala/inst/scala-2.8.0.final/lib/jline.jar
>
Howard Yeh wrote:
> Hi,
>
> I want to use readline with scala, doing this,
>
>
>> rlwrap scala
>>
>
> but weirdly that does nothing. The rlwrap manual says that
>
> "When the standard input is not a terminal, rlwrap will check,
> and then ignore, all options and simply execute command.
> When stdout (or stderr) is not a terminal, rlwrap will re-open it
> to /dev/tty (the users terminal) after it has started command"
>
> Is stdin redirected to something else? I remember
> rlwrap working with an older version of the interpreter.
>
> Slowly learning Scala, lots of fun so far : )
>
> Thanks,
> Howard
>
>
Hello Howard try this
rlwrap scala -Xnojline