| From | Sent On | Attachments |
|---|---|---|
| Per Bothner | Feb 5, 2009 10:30 pm |
| Subject: | JavaFX read-eval-print-loop | |
|---|---|---|
| From: | Per Bothner (Per....@Sun.COM) | |
| Date: | Feb 5, 2009 10:30:37 pm | |
| List: | net.java.dev.openjfx-compiler.dev | |
I just checked in a new read-eval-print-loop. (I.e. it's only available in the Mercurial repository so far.)
A read-eval-print-loop is a well-known feature of many language implementations but isn't that common with compiled languages, especially ones that have static typing and name lookup. That caused some difficulties: Before we had JavaFX support for the JSR-223 javax.script API, unfortunately you could declare a variable in one "eval" and make it visible in future commands, which made it have limited usefulness.
(The repl doesn't actually depend on javax.script, but there is a wrapper on top that implements javax.script to the extent we can.)
The class com.sun.tools.javafx.script.ScriptShell implements the actual repl. It an application (with a main), but you can also subclass it if you want to customize it.
So how do you run the repl? For now this is no script from-end, so you'll have to start it directly, for example:
$ CLASSPATH=/tmp:dist/lib/shared/javafxc.jar \ java com.sun.tools.javafx.script.ScriptShell
Then you type commands at it:
/*fx1*/ var xy = 12 12 /*fx2*/ xy+10 12
The prompt has the form of a comment, to make it easier to cut-and-paste. The name inside the comments is used as the name of a "scriptlet" which show up in error messages and exceptions.
You can define functions, modify variables, and call functions:
/*fx3*/ function xy_plus (n:Integer):Integer { xy + n } /*fx4*/ ++xy 13 /*fx5*/ xy_plus(100) 113
You get warnings:
/*fx6*/ xy=2.5 fx6:1: possible loss of precision found : Number required: Integer 2
and errors:
/*fx7*/ xy="str" fx7:1: incompatible types found : String required: Integer
and exceptions:
/*fx8*/ xy/0 java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:616) at com.sun.tools.javafx.script.JavaFXCompiledScript.eval(JavaFXCompiledScript.java:59) at com.sun.tools.javafx.script.ScriptShell.evaluate(ScriptShell.java:350) at com.sun.tools.javafx.script.ScriptShell.evaluateString(ScriptShell.java:311) at com.sun.tools.javafx.script.ScriptShell.processSource(ScriptShell.java:244) at com.sun.tools.javafx.script.ScriptShell$1.run(ScriptShell.java:177) at com.sun.tools.javafx.script.ScriptShell.main(ScriptShell.java:51) Caused by: java.lang.ArithmeticException: / by zero at fx8.javafx$run$(fx8]:1) ... 10 more
You can redefine variables, but previously-compiled references refer to the earlier definition:
/*fx9*/ var xy:String="str" str /*fx10*/ xy str /*fx11*/ xy_plus(100) 102
Feedback welcome.
-- --Per Bothner per....@sun.com pe...@bothner.com http://per.bothner.com/





