My favorites | Sign in
Project Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* Interpreter.scala
*
* (c) 2009, Dave Ray <daveray@gmail.com>
*/

package org.darevay.rooscaloo

import java.io.{ StringWriter, PrintWriter }

import javax.script.ScriptException;

import scala.tools.nsc.InterpreterResults._
import scala.tools.nsc.{Interpreter => ScalaInterpreter, Settings}

class ResultHolder(var value : Any)

class Interpreter {
// http://lampsvn.epfl.ch/trac/scala/ticket/874
// http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk/src/compiler/scala/tools/nsc/Interpreter.scala

private val writer = new java.io.StringWriter()
private val interpreter = new ScalaInterpreter(new Settings(), new PrintWriter(writer));

/**
* Bind the given value to the given variable name in the interpreter
*
* @param name the variable name
* @param value the value
*/
def bind(name : String, value : AnyRef) {
interpreter.bind(name, value.getClass.getName, value)
}

/**
* Execute a string of Scala code and ignore its result.
*
* @param code the code to execute
* @throws ScriptException if the code fails to parse
*/
def exec(code : String) {
// Clear the previous output buffer
writer.getBuffer.setLength(0)

// Execute the code and catch the result
val ir = interpreter.interpret(code);

// Return value or throw an exception based on result
ir match {
case Success => ()
case Error => throw new ScriptException("error in: '" + code + "'\n" + writer toString)
case Incomplete => throw new ScriptException("incomplete in :'" + code + "'\n" + writer toString)
}
}

/**
* Evaluate the given string of Scala code and return the result. The code
* must evaluate to a value.
*
* @param code the code to evaluate
* @return the result
* @throws ScriptException if the code fails to parse
*/
def eval(code : String) : Any = {
//println("-------\n" + code)
// Clear the previous output buffer
writer.getBuffer.setLength(0)

// Create an object to hold the result and bind in the interpreter
val holder = new ResultHolder(null)
bind("$result__", holder);

// Execute the code and catch the result
val ir = interpreter.interpret("$result__.value = " + code);

// Return value or throw an exception based on result
ir match {
case Success => holder.value
case Error => throw new ScriptException("error in: '" + code + "'\n" + writer toString)
case Incomplete => throw new ScriptException("incomplete in :'" + code + "'\n" + writer toString)
}
}
}
Show details Hide details

Change log

r14 by daveray on Jan 26, 2009   Diff
Built the real language parser with parser
combinators. Added version of demo that
uses this format rather than xml.
Go to: 
Project members, sign in to write a code review

Older revisions

r13 by daveray on Jan 25, 2009   Diff
Clean up.
r12 by daveray on Jan 24, 2009   Diff
(1) Added support for imports
(2) some doc and cleanup
r11 by daveray on Jan 24, 2009   Diff
(1) Correctly compiling parsed tests
and actions from Scala code and
populating engine with compiled rules
(2) Added a very simple demo that
loads rules from an XML file and
...
All revisions of this file

File info

Size: 2626 bytes, 83 lines
Hosted by Google Code