2

Getting Started

2.1 Installation

Slogan is distributed in source code form. Download the latest release package and extract it. Releases are available as .zip and .tar.gz archives.1 In the extracted folder, run the install command:2


$ sudo ./install
      

The installation process should run smoothly on all modern operating systems in the UNIX family, like OS X, GNU Linux and the various BSDs.

The best way to experiment with Slogan is through the REPL (the read-eval-print loop). The REPL is the primary interface to the Slogan interpreter. You can launch the REPL by typing the command,


$ slogan
      

This should land you in the REPL with the prompt shown below:

slogan>

Now the Slogan interpreter3 is waiting for some input. Let us try typing 1 + 2; followed by the enter key. Slogan will respond back with the result: 4


slogan> 1 + 2;
// 3
      

2.2 Saying Hello

Next we will write a small program to read the name of the user and print back a greeting. You may want to type this program into a file and load it into the interpreter. I decided to type this program into a file named hello.sn. (By convention, Slogan source files have the .sn extension). The following code listing shows the complete program:5


showln("Hello ", read_line())
      

The program consists of calls to two built-in functions - showln and read_line. Showln accepts an arbitrary number of arguments and prints them to the standard output. Read_line reads one line of characters from the standard input. To test the program, let us load the script into the REPL:


slogan> load("hello");
      

You should see the REPL waiting for you to input something. Type a name, like "Nemo", and press enter. You should see the following output:

Hello Nemo

Slogan is a compiled language. You can translate the "hello" program into instructions of the underlying hardware. This compilation can be done from the REPL itself, by calling the compile function as shown below:


compile("hello", exe = true);
      

A binary file named "hello" will be produced. This is a stand-alone executable and can be distributed to run on other similar systems. You may want to test the binary once by invoking it directly from the command line:


$ ./hello
Vijay
//> Hello Vijay
      

2.2 A Network Time Server and Client

In this section we examine how Slogan programs can communicate with each other over a network.

The first program we write is a server, a program that responds to requests that it receive over the network. When the program runs, it will start listening on port number 2121 for incoming requests. A valid request is the message GET TIME. The server will return the current time as response. All other requests are considered invalid and an error message is returned.

The following code listing shows the first version of our "time server". A description of what each line in the program does is given as comments in the code itself. (A line that starts with two slash characters (//) is treated as a comment. Multi-line comments are enclosed in a /* and a */. Comments are ignored by the interpreter and the compiler).


// file: time_server.sn

// Start the server and listen for connections on port 2121
let server = tcp_server_stream(2121)
            
// Accept the first client connection
let client = read(server)

/* Read request from client, if it is valid
  return the current time on server,
  otherwise return an error message */  
let request = read_line(client)    
if (request == "GET TIME")
  showln(stream = client, time_to_string(now()))
else
  showln(stream = client, "error: invalid request")

// Release the system resources used by both streams
close_stream(client)
close_stream(server)
      

To test the server, we need a client to talk in our time protocol. Let us write the client next:


// file: time_client.sn

// Connect to the time server, running on the same machine
let client = tcp_client_stream("localhost", port_number = 2121)
showln(stream = client, "GET TIME")
flush_writer(client)
showln(read_line(client))
close_stream(client)
        

Now we are all set to test the server-client pair. Make sure you have two instances of the Slogan REPL running. Load7 the server into the first REPL:


slogan> load("time_server");
      

Next, load the client into the second REPL. You should see the client printing the current time it received from the server.


slogan> load("time_client");
//> 2017-04-24T22:46:47
      

The time server, though it works, clearly has some limitations. First of all, it can handle only one client. It has to be restarted to handle the next one! We need to change the code in such a way that the server once invoked will go on to process all clients that wish to talk to it. It should also respond to each client as fast as possible. That means handling one client should not block or delay the response to other clients. The server should also become more robust. For instance, what will happen if the client tries to connect to a server that is not up and running? Right now, the client will display an error message and crash. In the following chapters we will learn about features offered by Slogan to help us fix all these problems. So, stay along!


1You can also clone the development branch from github to have access to the latest bug fixes and features.

2The README file in the source folder contains detailed information on compiling and installing Slogan.

3Having an interpreter greatly influences the way programs are designed and developed. You can quickly experiment with ideas in the REPL and get immediate feedback. Once you have ironed-out an idea, it can make into the final program which is compiled to machine code.

4In the code examples, the value of an expression will be shown as a simple comment right after the expression. The commenting convention //> is used to mark output produced by a side-effecting operation, like printing a value to the standard output stream.

5Any text editor can be used to compose Slogan programs. An emacs-mode is available in the Slogan source distribution to provide basic syntax highlighting for the Emacs editor.

6Another way to produce an executable is to use the -x option of the slogan command: $ slogan -x hello

7If you make changes to a script, you should call the reload function on that script to make the changes available in the REPL.


Next | Previous | Contents