Luban Interpreter Tutorial

 

To run Luban code, we use a simple Luban interpreter named, surprise, luban”. This luban interpreter takes Luban code in the files and runs them through.

 

The same Luban interpreter can also run in interactive mode. In this mode you can explore the content of Luban name space, and do interactive scripting. The things the interpreter can do are actually quite simple. Though I believe it serves its purpose.

 

1.1         Load and Run Luban Source Code Files

It is very simple to run Luban source code in the form of files. All you need to do is to put them on the command line when you start Luban interpreter. For example:

 

In file “saygood.lbn” type in below Luban code:

 

            std::println(obj=”World is good”);

 

In file “saybad.lbn” type in below Luban code:

 

            std::println(obj=”World is bad”);

 

At the command prompt of your operating system, type below:

 

                Mycomputer > luban saygood.lbn saybad.lbn

      World is good

      World is bad

 

You can see that Luban interpreter takes the Luban script file and runs them in the same order of the command line.

 

There are different Luban source code file type than Luban script file. They are Luban component definition module files that only define Luban component structure without invoking any. You can freely mix Luban component definition code files with script files and Luban interpreter will handle them automatically. For example:

 

In file “saygood.lbn” type in below Luban code:

           

            namespace demo;

 

            struct SayGood()

            as process

            {

                        std::println(obj=”World is good”);

            }

 

In file “saybad.lbn”, type in below Luban code:

 

            namespace demo;

 

            struct SayBad()

            as process

            {

                        std::println(obj=”World is bad”);

            }

 

In file “start.lbn” type in below Luban code:

           

            demo::SayGood(=);

            demo::SayBad(=);

 

At the command prompt of your operating system, type below:

 

                Mycomputer > luban saybad.lbn start.lbn saygood.lbn

      World is good

      World is bad

 

You get the same result as our first example. Though this time we used a different code structure. This time, saygood.lbn and saybad.lbn define two Luban component structure instead of two directly executable scripts. And the two component structures defined by saygood.lbn and saybad.lbn are invoked by the script inside start.lbn.

Luban always processes the component definition before running any script file, that’s why the order between script file and component definition file does not matter to Luban interpreter.

One more thing we want to mention is that the order of Luban component definition files doesn’t matter either. Luban read all component files into memory and handles the inter-dependency automatically so user don’t have to worry about as far as all components are presented at the command line.

 

1.2         Interactive Mode of Luban Interpreter

Below command will start Luban interpreter in interactive mode

 

                Mycomputer > lubani

6 imported types

script/s - to start scripting

edit/e  - to edit script using $EDITOR or vi

list/l <namespace name> - to browse name space

quit/q - to quit

Luban> _

 

Flag –i lets Luban interpreter get into interactive mode. In interactive mode, user can type in command to check the content of Luban namespace, enter Luban script and run it. For Luban Beta1.2 or higher, user can invoke external editor to edit Luban script. The following examples show how these command work.

 

                Mycomputer > lubani

> list std

std::console    std::console

std::deserializer       std::deserializer

std::file       std::file

std::print      std::print(input readwrite obj;)

std::des        std::des(input readwrite string stream;output readonly obj;)

std::printline  std::printline(input readwrite obj;)

std::println    std::printline(input readwrite obj;)

 

Above example uses “list” command to list the content in Luban namespace “std”. Using “list” without specify namespace name will list all the content at the root of Luban namespace.

 

                Mycomputer > lubani

> script

std::console().writeline(“Hello, world”);

ESC\ENTER

Hello, world

 

The above example users “script” command to start entering Luban script directly at command console. Pressing ESC key followed by ENTER will stop the entering of script and Luban will take the code that you have entered and run it through. In above example user entered a line of code and the running result is to print out “Hello, world” on screen.

 

                Mycomputer > lubani

> edit

 

Above command will invoke an external editor for user to edit Luban program. Luban will take what users edited in the external editor and run it through. It is probably more convenient than directly input at command console. User can specify external editor to use by defining “EDITOR” environment variable, like below example

 

                Mycomputer > export EDITOR=xemacs

 

 

If the editor is not specified, Luban will try to use traditional UNIX editor “vi”. If failed to invoke, Luban will report editor error.

The external editor feature is only available for Linux version and Cygwin+Windows. The binary package for windows does not have this feature, unless user already has a working Cygwin installed.

 

1.3         Other Command Line Arguments

Below are more command line arguments that can be used to control the behavior of interpreter.

·        -t <filename>

This flag is to specify the name of the file that lists all the data types imported from other languages like C++ in the form of shared library. If this argument is not specified, Luban uses a default imported type list file, which is either /usr/lib/luban/imports or environemtn variable $LUBAN_HOME/imports. More details of data type importing can be found in chapter 10.

 

·        -s <Luban code>

Use this flag you can put a short line of Luban code in the command line when starting Luban interpreter. And Luban interpreter will run the code, like below example:

           

            Mycomputer > luban –s “std::console().writeline(100);”

      100

            Mycomputer >

    

·        All other arguments other than above flags will be taken as Luban source code file names. And all the non-component Luban script files will be run in the order of their command line position.