开发者

Scripting from Scala

开发者 https://www.devze.com 2023-04-11 01:01 出处:网络
I have a little project with an editor, some special need I had and which I hacked together in an afternoon. A co-worker looked over my shoulder (don\'t you hate that?) and said \"Oh neat, but can it

I have a little project with an editor, some special need I had and which I hacked together in an afternoon. A co-worker looked over my shoulder (don't you hate that?) and said "Oh neat, but can it do X?"

So, to combine my ever-lasting need to learn new things and to enable some extensions on that little editor, I want to put some simple manipulation functions into there. And thus, I want to enable some scripting.

I found some tutorials about the ScriptEngine, that is not the problem. The problem is that it seems only to support JavaScript via Rhino. I have them all installed via apt-get: JRuby, Jython, Groovy, but the ScriptEngine doesn't know them.

What I would prefer is dropping a dependency in my build.sbt. Maybe someone knows a way to do that properly?

And if I re开发者_开发知识库sume to JavaScript, can I safely assume Rhino is installed on every machine with a JDK?


Here is an article about embedding a Scala interpreter. This might be overkill for what you are doing, however.

http://suereth.blogspot.com/2009/04/embedding-scala-interpreter.html

You could also look at the scaladocs for an interpreter directly.

http://www.scala-lang.org/archives/downloads/distrib/files/nightly/docs/compiler/scala/tools/nsc/interpreter/package.html

If they are really simple manipulation functions, you could write your own language using parser combinators.

http://www.codecommit.com/blog/scala/the-magic-behind-parser-combinators


Stir some Groovy into your Java apps is a developerWorks tutorial which doesn't depend on ScriptEngine but instead uses groovy.lang.GroovyClassLoader - I had been meaning to try adapting this to Scala but hadn't got round to it yet (let me know how it goes).

When adapting it to Scala, remember that:

ClassLoader parent = CLEmbedGroovy.class.getClassLoader();

is written:

val parent: ClassLoader = classOf[CLEmbedGroovy].getClassLoader() 

Further down that article there are other interesting options such as GroovyScriptEngine. SBT-wise I think (source) you just need to drop in:

val groovyAll1.0Jsr = "groovy" % "groovy-all-1.0-jsr" % "05"

Good luck!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号