Jump to content

The Sway Reference Manual/Introduction

From Wikibooks, open books for an open world

Sway and the Sway Interpreter

[edit | edit source]

Consider a computer program designed to help people read an essay. The simplest task one might ask of this program is the meaning of a word. For example, one might ask the program the meaning of the word 'expeditious' to which the program might reply, 'adjective: characterized by quickness'. An online dictionary is an example of such a program. By analogy, one could imaging a similar program designed for people learning how to program. In this case, one would ask the meaning of a word or phrase in a programming language, rather than a human language. Such a program is known as an interpreter because it translates phrases from a 'foreign' language (the programming language) to their meanings (which presumably, we will understand). The Sway interpreter can answer those types of requests quite easily: To see that this is so, one starts up the Sway interpreter by issuing the command sway in response to the system prompt.

    % sway
    sway>

Here, the % sway represents a command to get the Sway interpreter going. The interpreter signals its readiness to handle requests by issuing the Sway prompt sway>. Consider someone typing the number 3 and then a semicolon at the Sway prompt:

    sway> 3;
    INTEGER: 3

In the remainder of the text, the person who started up the Sway interpreter (or is running a Sway program) is known as the user. The user has just asked the Sway interpreter to evaluate or give the meaning of 3. The semicolon after the 3 can be thought of as a signal to the interpreter that the request has been completed, much like the period at the end of a sentence. The interpreter reads in the request and responds with the phrase INTEGER: 3. In this text, requests to the interpreter will, in general, be preceded by the Sway prompt. Responses by the interpreter will, in general, follow requests. Sometimes, when the response by the interpreter is not of interest, the response will be omitted.

The response of the interpreter is two-fold; first the type of the result is displayed (in this case INTEGER) and then the meaning or value of the result (in this case 3). This is much like looking up a word in a dictionary. First the type of word (or part of speech) is given (e.g. noun, verb, adjective) followed by the meaning of the word. Unlike a dictionary, a word in Sway always has one meaning. Sway, as with most programming languages, has a small number of simple types: integers, real, numbers, strings, and symbols.

The Sway interpreter, in general, evaluates expressions. As a rule, one signals the end of an expression using the semicolon. In response to an expression, the interpreter determines its value. As will be seen, the Sway interpreter can evaluate much more complicated expressions than integers.


Starting Up