JudoScript.COM Design principles of Judo the sport and the language
HomeJudo LanguageJuSP PlatformJamaica Language 
Judo ReferenceJuSP ReferenceWiki/WeblogTutorials/PresentationsDownloadsGoodiesFeedback  



 By FreeFind.com

For New Users

There is a lot of information about JudoScript. The reference holds all the details about everything in JudoScript. It is good to browse the lists of functional areas and system functions. If you love to read grammars in BNF, it is presented in the non-terminals listing. To find out facts and ideas about JudoScript, the white paper is the best source.

To learn how to use JudoScript, these technical articles describe major features and functional areas. The example library has examples for almost every features in the language. It would be easier if you know some of the very basics before diving into these articles and examples, which is what this article is trying to do. It is hoped that the above goal can be achieved if you spend one to two nights studying this article along with related topics and play with examples.

People learn by emulating. With enough knowledge, our brain strangely connects the dots and magically produces a "hologram". We are going to run through simple examples, and help you connect the dots; in the end, you should be able to write useful scripts on your own, and ready for the articles.

JudoScript language has three facets:

  1. a general-purpose scripting language,
  2. Java scripting abilities, and
  3. application features.
To get a wholesome picture of what is in JudoScript, see the Functional Areas. This tutorial helps you on the basics, that is, the first two.

To run JudoScript, simply put judo.jar into your Java classpath, and run:
 % java judo your_script.judo
There are more options to run, see the documentation. It also describes the command-line paramters and user options.

Our first program is:

println 'Hello, World!';
. "Hello, World!";
The dot (.) is a shortcut for the command println. You will see a lot of it. Note how strings are quoted. The println can take multiple values, and have formating options. The following program prints the file "Foo.java" with line numbers, which can be up to 3 digits.
lineCnt = 0;
do 'Foo.java' as lines {
  . ++lineCnt :>3, ' ', ;
Variables (like lineCnt) can be used without being declared first. The :>3 is a formating directive that have the value printed right-aligned to 3 letters. If the text is longer than 3, it is printed as-is. To truncate any long text to 3 letters, use :>3!. The do .. as lines { } is a convenience to read lines from a file or a URL; the special variable in this context represents the line just read. ( is used in many other occasions where it carries different meanings). This is a possible output:
  1: import java.io.*;
  2: public class Foo
  3: {
  4:   public static void main(String[] args) {
  5:     try {
  6:       int lineCnt = 0;
  7:       BufferedReader br = new BufferedReader(new FileReader(args[0]));
  8:       while (true) {
  9:         String line = br.readLine();
 10:         if (line == null) break;
 11:         System.out.println(++lineCnt + "  " + line);
 12:       }
 13:     } catch(Exception e) { e.printStackTrace(); }
 14:   }
 15: }
Without the convenience of do as lines {}, you would do it more conventionally:
lineCnt = 0;
f = openTextFile('Foo.java');
while true {
  if (line = f.readLine()) == eof { break; }
  . ++lineCnt :>3, ' ', line;
In JudoScript, the parentheses in while/if/... statements can be omitted, but the curly braces { } can never be omitted. Built-in functions like openTextFile() is one of the many system functions.

To print to a file, use the same print-family statements:

function convertToHtml fileName
  outFile = openTextFile(fileName @ '.html', 'w'); // for write.
  . <outFile> '<html><body><pre>';
  do fileName as lines {
    . <outFile> .replace('&', '&amp;')
                  .replace('<', '&lt;');
  . <outFile> '</pre></body></html>';
This is a function that creates an HTML file for any text file, typically source code. To print into a text file, first open it for write, then use that open file variable in the print statements. We used the string value's replace() method to escape '&' and '<'. String and numbers are simple values. They can be used interchangeably.

For a more advanced use of println, take a look at bincat.judo, which prints the byte values of binary files.

Simple values like numbers also have methods.

a = 1234;
hex_a = '0x' @ a.fmtHex();
. hex_a;  // result: 0x4D2
The operator @ is string concatenation. (It is not + because that is used for numeric addition. Since JudoScript is a weakly-typed language, this is necessary.)

[Review] Programming in JudoScript is familiar to anyone with Java experience. These points should be noted:
  • A variable does not have to be declared first, and can be assigned any type of value.
  • All values, including simple ones like strings and numbers, have methods.
  • Its flow-control statements and expressions are similar to Java. Parantheses can be omitted in function declarations and while/if/for/switch statements. Curly braces {}, however, must be used to quote the bodies of these statements.
  • The println (and its shortcut form, a dot) can print values with alignment. It can print to stdout, text files, or even string variables.
  • The convenience statement do ... as lines {} for reading lines in files.

Data Structures and Objects

JudoScript intimately supports many data structures. The most used, of course, are arrays and structs. Struct is also the root of any user-defined classes. Let us start with arrays and the various for loops available.

zodiacs = [ 'mouse', 'ox', 'tiger', 'rabbit', 'dragon', 'snake',
            'horse', 'sheep', 'monkey', 'rooster', 'dog', 'pig'
for i=0; i<zodiacs.size()-1; ++i { . zodiacs[i]; }
for i from 0 to zodiacs.lastIndex() { . zodiacs[i]; }
for x in arr { . x; }
The 3 for loops do exactly the same thing: print out the array elements line-by-line. For the fun of it, this program prints out the zodiac for a particular year:
function getZodiac year { return zodiacs[ (year - 1900) % 12 ]; }
Arrays, like other objects, have methods.
names = new Array;
names.add('Gustavo [M]');
names.add('Paradorn [M]');
names.add('Conchita [F]');
names.add('Venus [F]');
names.add('Marat [M]');

for x in names.sort() { . x; }
The sort() sorts the array locally and returns itself. It can take a "function variable" for customized sorting. The next example uses the same array and put female names ahead:
for x in names.sort( &myComparator ) { . x; }
function myComparator lhs, rhs { // comparator functions return -1, 0 or 1.
  if lhs.endsWith('[M]') { return rhs.endsWith('[F]') ? 1 :  0; }
  else                   { return rhs.endsWith('[F]') ? 0 : -1; }
// Result:
//  Conchita [F]
//  Venus [F]
//  Gustavo [M]
//  Paradorn [M]
//  Marat [M]
The & operator takes a reference to a function. A lambda function is an anonymous function used exclusively through references:
myComp = lambda lhs, rhs {
  if lhs.endsWith('[M]') { return rhs.endsWith('[F]') ? 1 :  0; }
  else                   { return rhs.endsWith('[F]') ? 0 : -1; }
for x in names.sort( myComp ) { . x; }
A struct is nothing but a map.
names = new struct( Gustavo = 'Kuerton',
                    Paradorn = 'Srichaphan',
                    Conchita = 'Martinez' );
names.Venus = 'Williams');
names.('Marat') = 'Safin';

for firstName in names.keys() { . names.(firstName), ', ', firstName; }
// Result:
//  Martinez, Conchita
//  Safin, Marat
//  Williams, Venus
//  Kuerton, Gustavo
//  Srichaphan, Paradorn
The values can be set through initialization or the member setting operation. If the name has no white-space characters, can access it like a data member in Java. Using its keys() method, we printed out all the elements. The returned keys can sorted. What is cool is, they can be sorted by their values as well:
for firstName in names.keysByValue() { . names.(firstName), ', ', firstName; }
// Result:
//  Kuerton, Gustavo
//  Martinez, Conchita
//  Safin, Marat
//  Srichaphan, Paradorn
//  Williams, Venus
An orderedMap is exactly the same as a struct except the order of the added elements is retained as the order of the keys.

  • Arrays can be created with the new array operator, or with the { } initializer.
  • Arrays have methods to sort and filter its elements. These methods can take specific function references as comparator or filter.
  • Functions can be referenced and stored in variables or passed as call parameters. Lambda functions are anonymous functions.
  • Struct are simply maps. OrderedMap are maps that retains the order for the keys. They are created with the new operator, optionally with named initializers.
  • Struct data members are accessed with the dot operator. If the key is a single name, it can be used directly for accessing its value; if the name contains white spaces, use the .() access operator, where the key can be any string or variable.
  • Struct's keys can be obtained by keys() methods, which can be sorted and/or filterd either by the keys or by their corresponding values.

Scripting Java

To create a Java object or array, use javanew:
longObj = javanew java.lang.Long(9);
hashtable = javanew java.util.Hashtable();

byteArr = javanew byte[9];
ByteArr = javanew java.lang.Byte[] { 1, 3, 5 };
for x in ByteArr { . x; }
As you see, Java arrays are processed the same way as native arrays.

To access a static member in a Java class, use :::

java.lang.System::gc();        // static method call
. java.util.Calendar::SUNDAY;  // static member access
Static Java methods can be aliased to look like JudoScript functions; some of the system functions are indeed Java static methods.
function rt for java.lang.Runtime::getRuntime();
To get a Java class, use javaclass. A Java class object can be used to create instances and access/invoke static members/methods. Static members are accessed like normal members.
Cal = javaclass java.util.Calendar;

This is it. For more advanced Java topics, read the article on using Java.

  • Operator javanew is to create Java instances or arrays.
  • Operator javaclass is to obtain a Java class object.
  • Static members of Java classes can be directly accessed with ::.
  • Static methods of Java classes can be aliased to become JudoScript functions.

Other Topics

You should also learn about these: Congratulations! You are well equipped to start the adventure. The following is a list of the most useful practises to start with; browse the article list for a relevant article; the links here all point to the reference:

Copyright © 2001-2005 JudoScript.COM. All Rights Reserved.