Swift - Programming Language

Introduction to REPL

REPL is abbreviation of Read Eval Print Loop which is interactive computer programming environment. This could be simply assumed that an interpreter program runs which takes the commands or instruction and immediately executes them and shows the output. All Unix/Linux shells are best example of REPL.

$ echo Hello World
Hello World

In python:

$ python

Python 2.7.10 (default, Oct 23 2015, 18:05:06)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print "Hello World"
Hello World

In Swift:

$ Swift:
Welcome to Apple Swift version 2.1.1 (swiftlang-700.1.101.15 clang-700.1.81). Type :help for assistance.
1> print("Hello World")
Hello World

How REPL works

  • Read: Read the code/instruction and parses it to internal data structure to understand what has to be done.
  • Eval: Evaluate the data structure in last step and process as per instruction to get result.
  • Print: Print the result after evaluation in last step.
  • Loop: Goes back to step 1 to read the next instruction
Note: Please read reference 1 to know more about REPL.

REPL and Swift

Apple has provided REPL environment for Swift so that we can do experiments with Swift code and learn the language easily with less effort. To use REPL on command line open your terminal shell and type swift on it. You would be able to see something like below image. A welcome message with version of swift.  

In the above image you can see the version of swift on my macbook is 2.1.1. Then you can see the command prompt with line number like 1>. On this prompt we can write single line instruction as well as multiline swift code and see how does it work. Lets do some experiments :)

1. Print 1 to 100 
1> for i in 1...100 {
2.            print(i, separator:"", terminator:" ")
3. }
4> print("")
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

2. Pass name as input in a function and print Hello with name
1>  func readAndPrintName(name: String) {
2.         print("Hello ! (name)")
3.   }
4>  readAndPrintName("Pawan")
Hello ! Pawan

we can use the command :quit to return to normal command prompt.

How to execute swift code saved in file using REPL

We can run the swift code written in a file or multiple files. In this section we will see how to do that.
Lets create a file named ex01.swift and write following code in and save it.


To execute this file on REPL command line, we can have two type of command.
First type:
$ swift ex01.swift

Second type:
Edit the file ex01.swift again and add the following line at the top of the file.
#!/usr/bin/env xcrun swift
So now the file looks like -
#!/usr/bin/env xcrun swift

Save this file and then run the following command.
$ chmod +x ex01.swift
$ ./ex01.swift

I will prefer first way to use REPL for swift code execution. I will return to multi-file source code compilation in latter tutorials.

Swift code compilation on command line

You can compile your code using swiftc command and output would be a platform dependent CLI based application. When swift code is compiled it provides an executable file and .dSYM file. You can run executable file using command on terminal shell. Lets see how to do that.

First we have to create a swift source code, so lets name it hello.swift and write a "hello world" program


Now we are to compile this code. So follow the steps

1. Open Terminal program. In MacOSX you will find it at /Applications/Utilities

2. Go to proper folder where your swift code file is saved.

     cd Desktop/swift     [In my case there is a swift folder on my desktop]

3. Compile the source code

      $ swiftc hello.swift

This command will compile the code and if everything is found correct in code then will produce the output executable file. Since we have not mentioned any output file name to it, so it will generate hello as output executable file.

4. Run the executable

We can mention the output file name while compiling and then we can run the output executable as follows:

Read arguments from command:

You might come up in a situation where you want to read some arguments to run your programme. For example you have written a code which will copy one file into another file. So you have to read input file path and output file path. To achieve this requirement you have the enum named Process.

Process has 3 property as follows -

  • argument - Reads the arguments and returns us as a string.
  • argc - Number of arguments given to the process.
  • unsafeArgv - Provides us the vector of arguments.

Sample code:
var inputArguments = Process.arguments
let scanner = NSScanner(string: inputArguments[1])
var index: Int = Int.min

Above 4 lines of code reads an integer from command line when a program is executed.

Example: ./example 100

Here example is an executable which takes 100 as input to execute.

REPL on Xcode LLDB console:

We can run the REPL on Xcode's lldb debugger console. To do this we need some project already written or we can create a simple hello world project and put the debug point at some place. When debug point reaches to the step (statement) then we can see the console is active to take commands. Here we type a command repl and our debugger is now into swift repl mode.

To quit from REPL mode you can use any of the following commands

:             [Only a colon and enter. It will come out of repl mode and continue on normal debug mode.]
:q          [colon q and enter. It will immediately stop debug mode and stop the program as well.]
:quit      [same as :q ]

      To run swift repl on debugger, you do not need swift project. It could work with any Objective-C project as well.

Practice and Revision-

  1. Read REPL wiki reference and try to understand about it.
  2. Write swift code and save them in a file and do experimentation with REPL.
  3. Try to compile the code using swiftc command and run the executable.