Mercurial Hosting > reactionary
changeset 87:5f4cc9d3d3cb default tip
start beanshell
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 21 Apr 2025 21:47:15 -0600 |
parents | 9b700f8d9610 |
children | |
files | src/beanshell.html.luan |
diffstat | 1 files changed, 219 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/beanshell.html.luan Mon Apr 21 21:47:15 2025 -0600 @@ -0,0 +1,219 @@ +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.sh* +~/beanshell $ cat bsh.sh +#!/bin/bash + +export CLASSPATH=bsh-2.0b4.jar +java bsh.Console & +~/beanshell $ +</code> + +<p>Copy my <code>bsh.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.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.</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> + +<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 + } +} + + +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