Mercurial Hosting > reactionary
view src/beanshell.html.luan @ 88:f320d444face default tip
finish beanshell
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 22 Apr 2025 23:11:59 -0600 |
parents | 5f4cc9d3d3cb |
children |
line wrap: on
line source
local Luan = require "luan:Luan.luan" local error = Luan.error local pairs = Luan.pairs or error() local Io = require "luan:Io.luan" local Http = require "luan:http/Http.luan" local Shared = require "site:/lib/Shared.luan" local head = Shared.head or error() local header = Shared.header or error() local content = { intro = { title = [[Introduction]] content = function() %> <p><a href="https://beanshell.github.io/">BeanShell</a> is a <a href="https://en.wikipedia.org/wiki/Scripting_language">scripting language</a> based on Java which looks a lot like Java. It is much friendlier for beginners than Java is. If you learn BeanShell then learning Java will be much easier.</p> <% end } access = { title = [[Running BeanShell]] content = function() %> <p><a href="https://beanshell.github.io/download.html">Download bsh-xx.jar</a> and put it in your working directory.</p> <code block> ~/beanshell $ ls -F bsh-2.0b4.jar bsh_console.sh* ~/beanshell $ cat bsh_console.sh #!/bin/bash export CLASSPATH=bsh-2.0b4.jar java bsh.Console & ~/beanshell $ </code> <p>Copy my <code>bsh_console.sh</code> into your working directory. A <code>jar</code> file contains compiled Java code. The <code>CLASSPATH</code> tells the <code>java</code> command where to find compiled Java code.</p> <p>Now you can run BeanShell:</p> <code block> ~/beanshell $ ./bsh_console.sh ~/beanshell $ </code> <p>You should see a new window that lets you interact with BeanShell. From now on I will mostly refer interactions in that window.</p> <% end } start = { title = [[Getting Started]] content = function() %> <p>From the window:</p> <code block> bsh % pwd(); /Users/fschmidt/beanshell bsh % print("Hello world!"); Hello world! bsh % print(1+1); 2 bsh % </code> <p>The BeanShell <code>pwd</code> command is like the Bash <code>pwd</code> command, and the BeanShell <code>print</code> command is somewhat like the Bash <code>echo</code> command. But the syntax is different. BeanShell and Java have syntax like most programming languages. While Bash does <code>command arg1 arg2 arg3</code>, most programming languages do <code>command(arg1,arg2,arg3)</code>. In addition, Java requires a <code>;</code> at the end of a statement (command line).</p> <p>Also note that Java has the standard arithmetic operators.</p> <% end } vars = { title = [[Variables]] content = function() %> <code block> bsh % String message = "Hello world!"; bsh % print(message); Hello world! bsh % int i = 2; bsh % print(i); 2 bsh % print(i+1); 3 bsh % i = i + 5; bsh % print(i); 7 bsh % </code> <p>Variables hold values. In Java (but not BeanShell), variables must be declared with a type. The type <code>String</code> is for text, and <code>int</code> is for integers.</p> <p>The <code>=</code> command assigns a value to a variable.</p> <% end } objects = { title = [[Objects and Strings]] content = function() %> <code block> bsh % print( message.toUpperCase() ); HELLO WORLD! bsh % print( message.length() ); 12 bsh % print( message.getClass() ); class java.lang.String bsh % </code> <p>Java is an object-oriented language which means that it mostly deals with objects. I will explain with real-world examples. To show a dog or a toaster, I would do something like <code>show(dog)</code> or <code>show(toaster)</code>. Showing is something I do to these objects, it is not built into the objects. In contrast, to have a dog run I would do <code>dog.run()</code>, and to have a toaster toast bread I would do <code>toaster.toast(bread)</code>. Running is a function built into dogs, while toasting is built into toasters. Functions that are built into objects are called "member functions".</p> <p>But actually this goes deeper. Dogs are a type of canine and all canines can run. So the ability of dogs to run actually comes from them being canines. In Java, we would call dogs, canines, and toasters "classes", and we would say that dogs are a subclass of canines, and that canines are a superclass of dogs.</p> <p>Back to programming. The class of <code>message</code> (a String) is <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html">java.lang.String</a>. Click that link and see all the things that strings can do. For example, they can <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#toUpperCase--">toUpperCase</a> and <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#length--">length</a>. These functions return values. Strings can also <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--">getClass</a> but this functionality comes from the superclass <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html">java.lang.Object</a>. Objects are the base class of everything. Look again at the top of <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html">java.lang.String</a> and you will see the class hierarchy and see that String is a subclass of Object.</p> <% end } interfaces = { title = [[Interfaces and Lists]] content = function() %> <code block> bsh % List list = new ArrayList(); bsh % print( list ); [] bsh % print( list.size() ); 0 bsh % list.add("a"); bsh % print( list ); [a] bsh % print( list.size() ); 1 bsh % list.add("b"); bsh % list.add("c"); bsh % print( list ); [a, b, c] bsh % print( list.size() ); 3 bsh % print( list.get(0) ); a bsh % print( list.get(1) ); b bsh % print( list.getClass() ); class java.util.ArrayList bsh % </code> <p><a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html">java.util.ArrayList</a> is a class. To make a new object of a class, we use the <code>new</code> operator. If you look at the top of the ArrayList page, you will see "All Implemented Interfaces:" which includes "List" and clicking that takes you to <a href="https://docs.oracle.com/javase/8/docs/api/java/util/List.html">java.util.List</a>. List is an interface which you can think of as a purely abstract class. An interface only specifies what an object does, not how. A concrete class like ArrayList specifies how objects do what they do (the implementation) so that the object can actually do these actions. Note that an interface can be implemented/subclassed by multiple classes that implement the interface in different ways. You can see this in <a href="https://docs.oracle.com/javase/8/docs/api/java/util/List.html">java.util.List</a> under "All Known Implementing Classes:".</p> <p>Note that the first index of a list is 0 not 1. So the last index of a list is <code>list.size() - 1</code>.</p> <p>Java organizes classes into packages. The package of List and ArrayList is <a href="https://docs.oracle.com/javase/8/docs/api/java/util/package-summary.html">java.util</a>. It is full of useful classes. Java has a huge number of standard classes, and I am not going to go over any more classes than needed to convey the core concepts of Java. You can learn the classes you need on your own.</p> <% end } boolean = { title = [[Boolean Values and Equality]] content = function() %> <code block> bsh % print( 3 > 4 ); false bsh % print( 3 >= 4 ); false bsh % print( 3 < 4 ); true bsh % print( 3 <= 4 ); true bsh % print( 3 == 4 ); false bsh % print( 3 != 4 ); true bsh % print( 3 < 4 && 3 > 4 ); false bsh % print( 3 < 4 || 3 > 4 ); true bsh % boolean b = 3 < 4; bsh % print(b); true bsh % print( b == true ); true bsh % </code> <p><code>==</code> checks for equality, <code>!=</code> checks for inequality, <code>&&</code> is for "and", and <code>||</code> is for "or". These are boolean operators that return boolean values, unlike something like <code>+</code> which is an arithmetic operator. <code>boolean</code> is a type than can have the values <code>true</code> or <code>false</code>.</p> <code block> bsh % String s1 = new String("hi"); bsh % String s2 = new String("hi"); bsh % print( s1 == s2 ); false bsh % print( s1.equals(s2) ); true bsh % String s3 = s1; bsh % print( s1 == s3 ); true bsh % </code> <p>If you have two objects in the physical world that are identical, then they are not the same object but they are equivalent objects. In Java, <code>object1 == object2</code> checks whether object1 and object2 refer to the same object. Object variables refer to objects, they don't contain the object's value. <code>object1.equals(object2)</code> checks for object equivalence.</p> <p>The types <code>int</code> and <code>boolean</code> are primitive types. They are not objects. Primitive type variables contain primitive values, so they don't refer to anything. So in this case, <code>==</code> checks for equal values.</p> <% end } file = { title = [[Run File and Comment]] content = function() %> <p>Now I will return to the Bash command line.</p> <code block> ~/beanshell $ ls -F bsh-2.0b4.jar bsh.sh* bsh_console.sh* code.bsh ~/beanshell $ cat bsh.sh #!/bin/bash export CLASSPATH=bsh-2.0b4.jar java bsh.Interpreter $* ~/beanshell $ cat code.bsh /* This is a comment */ // This is also a comment print("Hi"); print("Bye"); ~/beanshell $ ./bsh.sh code.bsh Hi Bye ~/beanshell $ </code> <p><code>bsh.sh</code> runs a BeanShell file from the command line. I will keep modifying <code>code.bsh</code> to show my examples.</p> <p> In Java, comments are between <code>/*</code> and <code>*/</code> or between <code>//</code> and the end of the line. Comments are ignored.</p> <% end } ifs = { title = [[If Statements]] content = function() %> <code block> ~/beanshell $ cat code.bsh int x = 5; print("first if"); if( x == 5 ) { print("x == 5"); } print("second if"); if( x > 5 ) { print("x > 5"); } else { print("x <= 5"); } print("third if"); if( x > 5 ) { print("x > 5"); } else if( x < 5 ) { print("x < 5"); } else { print("x == 5"); } ~/beanshell $ ./bsh.sh code.bsh first if x == 5 second if x <= 5 third if x == 5 ~/beanshell $ </code> <p><code>if</code> statements take a boolean argument.</p> <% end } loops = { title = [[Loops]] content = function() %> <code block> ~/beanshell $ cat code.bsh ~/beanshell $ cat code.bsh print("first loop"); int i = 1; while( i <= 3 ) { print(i); i = i + 1; } print("second loop"); for( int i = 1; i <= 3; i = i + 1 ) { print(i); } List list = new ArrayList(); list.add("a"); list.add("b"); list.add("c"); print("third loop"); for( int i = 0; i < list.size(); i = i + 1 ) { String s = list.get(i); print(s); } print("fourth loop"); Iterator iter = list.iterator(); while( iter.hasNext() ) { String s = iter.next(); print(s); } print("fifth loop"); for( String s : list ) { print(s); } ~/beanshell $ ./bsh.sh code.bsh first loop 1 2 3 second loop 1 2 3 third loop a b c fourth loop a b c fifth loop a b c ~/beanshell $ </code> <p>The first form of the <code>for</code> loop has three elements: an initialization statement, a loop condition, and a statement that is run after each iteration of the loop. The second form of the <code>for</code> loop iterates through anything that is iterable, returning values in sequence. For the fourth loop, see <a href="https://docs.oracle.com/javase/8/docs/api/java/util/List.html#iterator--">List.iterator()</a>.</p> <% end } blocks = { title = [[Blocks]] content = function() %> <code block> ~/beanshell $ cat code.bsh { String s = "ok"; print( "first " + s ); } print( "second " + s ); ~/beanshell $ ./bsh.sh code.bsh first ok second void ~/beanshell $ </code> <p>Code between the braces <code>{</code> and <code>}</code> is a block. When braces are part of a statement like a <code>for</code> statement, the block extends to include the whole statement. Variables declared inside a block only exist within that block.</p> <p>In BeanShell an undefined variable is <code>void</code> but in Java you would get a compile error.</p> <p>Note that besides being an arithmetic operator, <code>+</code> also concatenates strings.</p> <% end } arrays = { title = [[Arrays and null]] content = function() %> <code block> ~/beanshell $ cat code.bsh String[] array = { "a", "b", "c", "d", "e" }; print( "array = " + Arrays.asList(array) ); print( "length = " + array.length ); print( "array[1] = " + array[1] ); array[1] = "bb"; print( "modified array = " + Arrays.asList(array) ); print("loop"); for( String s : array ) { print(s); } array = new String[3]; print( "new array = " + Arrays.asList(array) ); array[2] = "two"; print( "modified new array = " + Arrays.asList(array) ); ~/beanshell $ ./bsh.sh code.bsh array = [a, b, c, d, e] length = 5 array[1] = b modified array = [a, bb, c, d, e] loop a bb c d e new array = [null, null, null] modified new array = [null, null, two] ~/beanshell $ </code> <p>Arrays are an anomaly - neither primitive type nor object. Arrays don't do anything that lists can't do. The reason that Java has arrays is because arrays are a fundamental low-level programming concept, and Java is a low-level language.</p> <p>Arrays are declared with the type that they will hold, and then they can only hold that type. Arrays cannot change size.</p> <p>Since arrays don't know how become strings, I used <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList-T...-">Arrays.asList()</a> which converts an array into a list so that it can be displayed properly. Note that this is a <code>static</code> function which means that it isn't part of an object, but rather is a general function that is associated with a class.</p> <p>In the second part of the code, I assign <code>array</code> to a new array of length 3 with no values. Java uses the special value <code>null</code> to mean no value.</p> <% end } fns = { title = [[Functions]] content = function() %> <code block> ~/beanshell $ cat code.bsh int add(int x,int y) { return x + y; } void addAndShow(int x,int y) { int sum = x + y; print( x + " + " + y + " = " + sum ); } void sayBye() { print("Bye"); } print( "add(2,3) = " + add(2,3) ); addAndShow(2,3); sayBye(); ~/beanshell $ ./bsh.sh code.bsh add(2,3) = 5 2 + 3 = 5 Bye ~/beanshell $ </code> <p>Functions make possible reusable code, and are the core concept of programming. In Java, function definitions start with the return type, then the function name, then the function parameters with types, and then a block that implements the function. A return type of <code>void</code> means nothing is returned.</p> <% end } impl = { title = [[Implementing Interfaces]] content = function() %> <code block> ~/beanshell $ cat code.bsh Iterator iter = new Iterator() { int i = 0; boolean hasNext() { return i < 10; } int next() { i = i + 1; return i; } }; while( iter.hasNext() ) { print( iter.next() ); } ~/beanshell $ ./bsh.sh code.bsh 1 2 3 4 5 6 7 8 9 10 ~/beanshell $ </code> <p>BeanShell doesn't let you implement new classes, you must use Java for that. But BeanShell does let you implement interfaces which is similar to implementing a new class. In this case, I implement <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html">Iterator</a> which defines two member functions which must be implemented. Typically an implementation will include both member functions and member data/variables. The member data describes the state of the object, and the member functions often modify that state. This iterator counts from 1 to 10, and the member variable <code>i</code> keeps track of the current count.</p> <p>This example is just to give a taste of object-oriented programming. When you learn Java, you will learn much more about this.</p> <% end } conclusion = { title = [[Conclusion]] content = function() %> <p>The purpose of this tutorial is to introduce basic Java concepts that will make learning Java easier. I avoided details and involved examples.</p> <p>When you are learning Java, I still think BeanShell is a useful tool for experimentation. You can quickly test things in BeanShell that would take longer to test in Java.</p> <% end } } local function show_toc(content) %> <ul> <% for id, info in pairs(content) do %> <li><a id="c_<%=id%>" href="#<%=id%>"><%=info.title%></a></li> <% end %> </ul> <% end local function show_content(content,h) for id, info in pairs(content) do %> <div heading> <h<%=h%>><a id="<%=id%>" href="#<%=id%>"><%=info.title%></a></h<%=h%>> <a href="#c_<%=id%>">contents</a> </div> <% info.content() end end return function() Io.stdout = Http.response.text_writer() %> <!doctype html> <html> <head> <% head() %> <title>Reactionary BeanShell Tutorial</title> </head> <body> <% header() %> <div content> <h1><a href="beanshell.html">Reactionary BeanShell Tutorial</a></h1> <hr> <h2>Contents</h2> <div toc> <% show_toc(content) %> </div> <hr> <% show_content(content,2) %> </div> </body> </html> <% end