RascalTutor
Synopsis

The interactive Rascal commandline shell

Description

The Rascal shell is an interactive console — or if you prefer a Command Line Interface (CLI) or Read Eval Print Loop (REPL) — for entering and executing Rascal expressions, statements and declarations.

Next to Rascal language expressions, the shell features a number of specific commands available only in the shell:

1. Read Eval Print Loop (REPL)

Synopsis

The Rascal Read Eval Print Loop (REPL)

Description

The Rascal shell is an interactive console to experiment with Rascal code. You can write your own expressions, statements and declarations right there. Or, you can import library modules and try out their functionality.

Examples

Rascal code can be a trivial expression

rascal>1+2
int: 3

Or a more complex list comprehension:

rascal>[ n * n | int n <- [0..10] ]
list[int]: [0,1,4,9,16,25,36,49,64,81]

Or importing a module and using a function declared in it:

rascal>import List;
ok
rascal>size([ n * n | int n <- [0..10] ])
int: 10

Another use is to declare variables

rascal>int x = 2;
int: 2
rascal>int y = 3;
int: 3

and use them later on:

rascal>x * y
int: 6
Pitfalls
  • Rascal is quite demanding as far as the proper placement of semicolons (;) is concerned.

  • Rascal is a statically typed language but we have not released the type checker yet. Sometimes this is confusing because the interpreter will try to run otherwise broken code and produce an error message.

2. Commands

Synopsis

RascalShell commands

Description

The RascalShell provides several built-in commands:

2.1. Clear Command

Synopsis

Clear all break points.

Syntax

clear

Description

Removes all break points.

2.2. Edit Command

Synopsis

Open an editor for a Rascal module

Syntax
  • :edit ModuleName

Description
>>>>>>>import IO;
              ^ Parse error here
ok
rascal>:edit IO
ok

WARNING: unexpected errors in the above SHELL example. Documentation author please fix! :leveloffset: +1

3. Help Command

Synopsis

Get a reminder of the shell commands.

Syntax
  • :help

Description
rascal>:help
Welcome to the Rascal command shell.

Shell commands:
:help                      Prints this message
:quit or EOF               Quits the shell
:set <option> <expression> Sets an option
e.g. profiling    true/false
     tracing      true/false
     errors       true/false
:edit <modulename>         Opens an editor for that module
:test                      Runs all unit tests currently loaded

Example rascal statements and declarations:
1 + 1;                     Expressions simply print their output and (static) type
int a;                     Declarations allocate a name in the current scope
a = 1;                     Assignments store a value in a (optionally previously declared) variable
int a = 1;                 Declaration with initialization
import IO;                 Importing a module makes its public members available
println("Hello World")     Function calling

Please read the manual for further information
ok

4. Quit Command

Synopsis

Quit the RascalShell.

Syntax
  • :quit

  • Ctrl+d or Eof

Description

Typing :quit or Ctrl+d will end the execution of the shell.

5. Set Command

Synopsis

Set parameters that control options of RascalShell.

Syntax
  • :set

  • :set Option TrueOrFalse

Description

The shell provides a number of options to control its behaviour. The set command manages their value.

In the first form, the list of current settings is printed.

In the second form a specific option is set to true or false.

The options are:

  • profiling: record execution times while executing subsequent Rascal code and print the results after each RascalShell command.

  • tracing: while executing Rascal code, print a trace of all function calls.

  • errors: print more diagnostic stack traces if available (of internal functionality)

Examples

Turn tracing on and execute a function:

rascal>import demo::basic::Factorial;
ok
rascal>:set tracing true
ok
rascal>fac(5)
call  >Factorial::fac(5)
call  >>Factorial::fac(4)
call  >>>Factorial::fac(3)
call  >>>>Factorial::fac(2)
call  >>>>>Factorial::fac(1)
call  >>>>>>Factorial::fac(0)
return>>>>>>>Factorial::fac:1
return>>>>>>Factorial::fac:1
return>>>>>Factorial::fac:2
return>>>>Factorial::fac:6
return>>>Factorial::fac:24
return>>Factorial::fac:120
int: 120

Turn trace off and execute the same function:

rascal>:set tracing false
ok
rascal>fac(5)
int: 120
Pitfalls

The set command is completely unrelated to Rascal’s built-in set type.

6. Test Command

Synopsis

Run tests.

Syntax
  • :test

Description

Run Rascal tests. The tests in all currently imported modules are executed and the results are reported in the terminal.

Examples

Execute the tests in an imported module:

rascal>import demo::basic::Factorial;
ok
rascal>test

Execute the tests in the Integers module in the Rascal test suite:

>>>>>>>test lang::rascal::tests::basic::Integers

Command Completion

Synopsis

Context-dependent command completion.

Description

RascalShell provides context-dependent command completion. Typing Tab autocompletes from the current cursor position and will show all possible completions (or will directly add the completion when this is unique).

Command History

Synopsis

Use the command history.

Description

RascalShell provides a history of previously entered commands. This can be accessed as follows:

Show previous command in history; Type Return to execute it.

Next command in history; Type Return to execute it.

Ctrl+r

(After search text) Search backward in history.

Ctrl+s

(After search text) Forward search in history

Examples
rascal>a = 1;
int: 1
rascal>b = 2;
int: 2
rascal>c = 3;
int: 3

Typing will then show the text (not followed by Return!):

rascal>c = 3;

Typing Return will reexecute c = 3.

Typing the letter a (the search text in this example) followed by Ctrl+r will show the text:

(reverse-i-search)`a': a = 3;

And again, typing Return will reexecute a = 3.

Keyboard Shortcuts

Synopsis

Keybord shortcuts.

Description

When handling console input, RascalShell supports a subset of the shortcuts provided by GNU Readline

Some convenient shortcuts are:

Ctrl+a

Move cursor to begin of line.

Ctrl+d

EOF marker, closes the current RascalShell (equivalent to the command (quit).

Ctrl+e

Move cursor to end of line.

Ctrl+k

Kill remainder of line after cursor.

Ctrl+l

Clear screen.