The following methods are recommended if you need to evaluate a
Scheme expression from a Java method.
(Some details (such as the ‘throws
’ lists) may change.)
Static method: void Scheme.registerEnvironment
()
Initializes the Scheme environment. Maybe needed if you try to load a module compiled from a Scheme source file.
Static method: Object Scheme.eval
(InPort
, Environment port
)env
Read expressions from
port
, and evaluate them in theenv
environment, until end-of-file is reached. Return the value of the last expression, orInterpreter.voidObject
if there is no expression.
Static method: Object Scheme.eval
(String
, Environment string
)env
Read expressions from
string
, and evaluate them in theenv
environment, until the end of the string is reached. Return the value of the last expression, orInterpreter.voidObject
if there is no expression.
Static method: Object Scheme.eval
(Object
, Environment sexpr
)env
The
sexpr
is an S-expression (as may be returned byread
). Evaluate it in theenv
environment, and return the result.
For the Environment
in most cases you could use
‘Environment.current()
’. Before you start, you
need to initialize the global environment,
which you can do with
Environment.setCurrent(new Scheme().getEnvironment());
Alternatively, rather than setting the global environment, you can use this style:
Scheme scm = new Scheme(); Object x = scm.eval("(+ 3 2)"); System.out.println(x);
Kawa also supports the standard
javax.script
API.
The main advantage of this API is if you want your users to be able to choose
between multiple scripting languages. That way you can support Kawa
without Kawa-specific programming.
For example the standard JDK tool jrunscript provides a
read-eval-print-loop for any language that implements the javax.script
API. It knows nothing about Kawa but can still use it:
$ jrunscript -cp kawa.jar -l scheme scheme> (cadr '(3 4 5)) 4
(Of course the jrunscript
REPL isn’t as nice as the one that
Kawa provides. For example the latter can handle multi-line inputs.)