comparison src/beanshell.html.luan @ 87:5f4cc9d3d3cb

start beanshell
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 21 Apr 2025 21:47:15 -0600
parents src/learn_bash.html.luan@88f46d75d28e
children f320d444face
comparison
equal deleted inserted replaced
86:9b700f8d9610 87:5f4cc9d3d3cb
1 local Luan = require "luan:Luan.luan"
2 local error = Luan.error
3 local pairs = Luan.pairs or error()
4 local Io = require "luan:Io.luan"
5 local Http = require "luan:http/Http.luan"
6 local Shared = require "site:/lib/Shared.luan"
7 local head = Shared.head or error()
8 local header = Shared.header or error()
9
10
11 local content = {
12 intro = {
13 title = [[Introduction]]
14 content = function()
15 %>
16 <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>
17 <%
18 end
19 }
20 access = {
21 title = [[Running BeanShell]]
22 content = function()
23 %>
24 <p><a href="https://beanshell.github.io/download.html">Download bsh-xx.jar</a> and put it in your working directory.</p>
25
26 <code block>
27 ~/beanshell $ ls -F
28 bsh-2.0b4.jar bsh.sh*
29 ~/beanshell $ cat bsh.sh
30 #!/bin/bash
31
32 export CLASSPATH=bsh-2.0b4.jar
33 java bsh.Console &
34 ~/beanshell $
35 </code>
36
37 <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>
38
39 <p>Now you can run BeanShell:</p>
40
41 <code block>
42 ~/beanshell $ ./bsh.sh
43 ~/beanshell $
44 </code>
45
46 <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>
47
48 <%
49 end
50 }
51 start = {
52 title = [[Getting Started]]
53 content = function()
54 %>
55 <p>From the window:</p>
56
57 <code block>
58 bsh % pwd();
59 /Users/fschmidt/beanshell
60 bsh % print("Hello world!");
61 Hello world!
62 bsh % print(1+1);
63 2
64 bsh %
65 </code>
66
67 <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>
68
69 <p>Also note that Java has the standard arithmetic operators.</p>
70
71 <%
72 end
73 }
74 vars = {
75 title = [[Variables]]
76 content = function()
77 %>
78
79 <code block>
80 bsh % String message = "Hello world!";
81 bsh % print(message);
82 Hello world!
83 bsh % int i = 2;
84 bsh % print(i);
85 2
86 bsh % print(i+1);
87 3
88 bsh % i = i + 5;
89 bsh % print(i);
90 7
91 bsh %
92 </code>
93
94 <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>
95
96 <p>The <code>=</code> command assigns a value to a variable.</p>
97
98 <%
99 end
100 }
101 objects = {
102 title = [[Objects and Strings]]
103 content = function()
104 %>
105
106 <code block>
107 bsh % print( message.toUpperCase() );
108 HELLO WORLD!
109 bsh % print( message.length() );
110 12
111 bsh % print( message.getClass() );
112 class java.lang.String
113 bsh %
114 </code>
115
116 <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>
117
118 <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>
119
120 <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>
121
122 <%
123 end
124 }
125 interfaces = {
126 title = [[Interfaces and Lists]]
127 content = function()
128 %>
129
130 <code block>
131 bsh % List list = new ArrayList();
132 bsh % print( list );
133 []
134 bsh % print( list.size() );
135 0
136 bsh % list.add("a");
137 bsh % print( list );
138 [a]
139 bsh % print( list.size() );
140 1
141 bsh % list.add("b");
142 bsh % list.add("c");
143 bsh % print( list );
144 [a, b, c]
145 bsh % print( list.size() );
146 3
147 bsh % print( list.get(0) );
148 a
149 bsh % print( list.get(1) );
150 b
151 bsh % print( list.getClass() );
152 class java.util.ArrayList
153 bsh %
154 </code>
155
156 <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>
157
158 <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>
159
160 <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>
161
162
163 <%
164 end
165 }
166 }
167
168
169 local function show_toc(content)
170 %>
171 <ul>
172 <%
173 for id, info in pairs(content) do
174 %>
175 <li><a id="c_<%=id%>" href="#<%=id%>"><%=info.title%></a></li>
176 <%
177 end
178 %>
179 </ul>
180 <%
181 end
182
183 local function show_content(content,h)
184 for id, info in pairs(content) do
185 %>
186 <div heading>
187 <h<%=h%>><a id="<%=id%>" href="#<%=id%>"><%=info.title%></a></h<%=h%>>
188 <a href="#c_<%=id%>">contents</a>
189 </div>
190 <%
191 info.content()
192 end
193 end
194
195 return function()
196 Io.stdout = Http.response.text_writer()
197 %>
198 <!doctype html>
199 <html>
200 <head>
201 <% head() %>
202 <title>Reactionary BeanShell Tutorial</title>
203 </head>
204 <body>
205 <% header() %>
206 <div content>
207 <h1><a href="beanshell.html">Reactionary BeanShell Tutorial</a></h1>
208 <hr>
209 <h2>Contents</h2>
210 <div toc>
211 <% show_toc(content) %>
212 </div>
213 <hr>
214 <% show_content(content,2) %>
215 </div>
216 </body>
217 </html>
218 <%
219 end