Computer Science Canada

Syntax Quickref: Turing <--> BeanShell

Author:  rizzix [ Sun Mar 27, 2005 5:02 pm ]
Post subject:  Syntax Quickref: Turing <--> BeanShell

Following the similar style wtd implemented, (actually ripped off, but all credit to wtd Razz) 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
code:
put "Hello" ..

BeanShell
code:
print("Hello");


As for not skipping the new line:
Turing
code:
put "Hello"

BeanShell
code:
print("Hello\n");


Printing an integer.
Turing
code:
put 42

BeanShell
code:
print(42);


Printing a floating point number.
Turing
code:
put 42.3

BeanShell
code:
println(42.3);


Getting a line from the keyboard.
Turing
code:
var line : string
get line : *

BeanShell
code:
import *;
in = new BufferedReader(
    new InputStreamReader(System.in)
);
var string = in.readLine();


Getting a single character from the keyboard.
Turing
code:
var ch : string
get ch : 1

BeanShell
code:
var ch = (char) System.in.read();


Getting an integer from the keyboard.
Turing
code:
var i : int
get i

BeanShell
code:
import *;
in = new DataInputStream(
    new BufferedInputSream(System.in)
);
var i = in.readInt();


Getting a float from the keyboard.
Turing
code:
var f : real
get f

BeanShell
code:
import *;
var in = new DataInputStream(
    new BufferedInputSream(System.in)
);
var f = in.readFloat();



Math

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

code:
Turing  | BeanShell     
--------+------------
 =      |  ==     
 not=   |  !=     
 <, <=  |  <, <=     
 >, >=  |  >, >=     
 not    |  !   
 and    |  &&
 or     |  ||
        |  &
        |  |



Conditional Statements/Expressions

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

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

code:
Turing      | BeanShell     
------------+-------------
 %          |  //     
 /* ... */  |  /* ...  */
            |  /** ... */ 


More to come.

Author:  Naveg [ Sun Mar 27, 2005 7:39 pm ]
Post subject: 

i think these posts should include where to get the language and how to edit/use it

Author:  Mazer [ Sun Mar 27, 2005 7:44 pm ]
Post subject: 

Because Google is too complicated[url=http://www.mnsi.net/~mdss/Evasive_Maneuvers.zip]?[/url]

Author:  rizzix [ Sun Mar 27, 2005 9:23 pm ]
Post subject: 

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 [filename] [args]

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 [ args ] // 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:
code:
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.

Author:  Naveg [ Sun Mar 27, 2005 9:40 pm ]
Post subject: 

very cool, thank you for that


: