
-----------------------------------
rizzix
Sun Mar 27, 2005 5:02 pm

Syntax Quickref: Turing &lt;--&gt; BeanShell
-----------------------------------
Following the similar style wtd implemented, (actually ripped off, but all credit to wtd :P) of intro tutorials for the Turing programmer:

BeanShell (the beginning steps to Java) for the Turing Programmer

Input-Output

Printing to the standard output.
Turing
put "Hello" ..
BeanShell 
print("Hello");

As for not skipping the new line:
Turing
put "Hello"
BeanShell
print("Hello\n");
   
Printing an integer.
Turing
put 42
BeanShell
print(42);

Printing a floating point number.
Turing
put 42.3
BeanShell
println(42.3);

Getting a line from the keyboard.
Turing
var line : string
get line : *
BeanShell
import *;
in = new BufferedReader(
    new InputStreamReader(System.in)
);
var string = in.readLine();

Getting a single character from the keyboard.
Turing
var ch : string
get ch : 1
BeanShell
var ch = (char) System.in.read();

Getting an integer from the keyboard.
Turing
var i : int
get i
BeanShell
import *;
in = new DataInputStream(
    new BufferedInputSream(System.in)
);
var i = in.readInt();

Getting a float from the keyboard.
Turing
var f : real
get f
BeanShell
import *;
var in = new DataInputStream(
    new BufferedInputSream(System.in)
);
var f = in.readFloat();

       
Math

Turing    | BeanShell   | Result
----------+-------------+---------
 4 + 3    |  4 + 3      |  7
 4 - 2    |  4 - 2      |  2
 2 * 3    |  2 * 3      |  6
 2 / 5    |  2 / 5      |  0.4
 6 div 4  |             |  1
 6 rem 4  |  6 % 4      |  2
 3 ** 2   |Math.pow(3,2)|  9.0
 sin      | Math.sin()  | 
 cos      | Math.cos()  |
          | Math.tan()  | 
          | Math.asin() |
          | Math.acos() |
          | Math.atan() |
 
Comparisons

Turing  | BeanShell     
--------+------------
 =      |  ==      
 not=   |  !=      
 =      
 not    |  !    
 and    |  && 
 or     |  ||
        |  &
        |  |


Conditional Statements/Expressions

Turing                      | BeanShell     
----------------------------+------------------------------
 if condition then          |  if (condition) {
   statement1               |     expression1;
   statement2               |     expression2;
 elsif otherCondition then  |  } else if (otherCondition) 
   statement3               |     expression2;
 else                       |  else defaultExpression;
   defaultStatement         |
 end if                     |
----------------------------+------------------------------
 case value of              |  switch (value) {
   label 1: statement1      |    case 1: expression1; break;
   label 2: statement2      |    case 2: expression2; break;
   label: default           |    default: default_expression;
 end case                   |  }

Function Definition

Turing                              | BeanShell                | Java
------------------------------------+--------------------------------------------------------
 function fooBar : string           | fooBar() {               | String fooBar() {
   result "foo, bar"                |    return "foo, bar";    |    return "foo, bar";
 end fooBar                         | }                        | }
------------------------------------+--------------------------------------------------------
 function square (n : int) : float  | square(n) {              | float square(int n) {
   result n ** 2                    |    return Math.pow(n,2); |    return Math.pow(n,2);
 end square                         | }                        | }
------------------------------------+--------------------------------------------------------
 procedure greet (name : string)    | greet(name) {            | String greet(String name) {
   put "Hello, " + name             |    return "Hello, "+name;|    return "Hello, " + name;
 end greet                          | }                        | }
 ------------------------------------+-------------------------------------------------------
 function fact (n : int) : int      | fact(n) {                | int fact(int n) {
   if n = 0 then                    |    if (n == 0)           |    if (n == 0)
     result 1                       |       return 1;          |       return 1;
   else                             |    else                  |    else
     result n * fact (n - 1)        |       return n*fact(n-1);|       return n*fact(n-1);
   end if                           | }                        | }
 end fact                           |


Types of Variables in BeanShell
int x;   // a type(d) variable, can only hold specified type (int) of data
var x;   // a var variable can hold any kind of data
x = value;  // gloabal variable, behaves like a var variable, but has global scope


Comments

Turing      | BeanShell     
------------+-------------
 %          |  //      
 /* ... */  |  /* ...  */
            |  /** ... */  

More to come.

-----------------------------------
Naveg
Sun Mar 27, 2005 7:39 pm


-----------------------------------
i think these posts should include where to get the language and how to edit/use it

-----------------------------------
Mazer
Sun Mar 27, 2005 7:44 pm


-----------------------------------
[url=http://www.google.ca/search?hl=en&q=beanshell&btnG=Google+Search&meta=]Because [url=http://www.google.ca/search?hl=en&q=beanshell&btnG=Google+Search&meta=]Google [url=http://www.google.ca/search?hl=en&q=beanshell&btnG=Google+Search&meta=]is [url=http://www.google.ca/search?hl=en&q=beanshell&btnG=Google+Search&meta=]too [url=http://www.google.ca/search?hl=en&q=beanshell&btnG=Google+Search&meta=]complicated[url=http://www.mnsi.net/~mdss/Evasive_Maneuvers.zip]?

-----------------------------------
rizzix
Sun Mar 27, 2005 9:23 pm


-----------------------------------
ok here where you get it: www.beanshell.org download the bsh-2.0b2.jar file, it includes everything.

BeanShell is just a framework that comes along with an Interpreter and a Windowed Shell (called Console). to run the shell double click the jar file. to run the interperter type the following in console: java -jar bsh-2.0b2.jar XYZ 

where XYZ is one of the following:
bsh.Console       // run the graphical desktop
bsh.Interpreter   // run as text-only on the command line
bsh.Interpreter filename  // run script file

to make things simpler is suggest you just drop the file in the $JAVA_HOME/lib/ext folder

then to run the interpreter you just type:java bsh.Interpreter in console.

now that its in the /ext folder you might ask how do i make in convenient so that when i double click it the shell pops up, or when i drag a file on it, bsh executes it? for that I created (and attached) this wrapper program that basically all it does is invokes the bsh.Interpreter class. It is necessary that you download the jar file and placed it in the folder stated above for this to work. 

And just a personal note: i find it convenient to keep the wrapper program on my desktop.

as for creating a new BeanShell script file, first open up a simple text editor and save a file as whatever.bsh. then you may code in it as you wish.

to run the file drag-n-drop it over the bsh.exe wrapper. if its a cui script i suggest you invoke java bsh.Interpreter whatever.bsh at console.

enjoy :wink:


PS: i know i haven't mentioned this, but BeanShell is 100% java compatible (except maybe for some J2SE 5.0 features).. but java is not 100% BeanShell compatible. This means that java code will run in beanshell, but not necessarily the other way around. You can think of BeanShell as the loosly typed java.

-----------------------------------
Naveg
Sun Mar 27, 2005 9:40 pm


-----------------------------------
very cool, thank you for that
