atom feed1 message in read-eval-print-loop
FromSent OnAttachments
Per BothnerFeb 5, 2009 10:31 pm 
Subject:JavaFX read-eval-print-loop
From:Per Bothner (Per.@Sun.COM)
Date:Feb 5, 2009 10:31:00 pm

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 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

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( at sun.reflect.DelegatingMethodAccessorImpl.invoke( at java.lang.reflect.Method.invoke( at at at at at$ at 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.