
The interactive Rascal commandline shell
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:
-
Commands: RascalShell commands.
-
Command Completion: Context-dependent command completion.
-
Command History: Use the command history.
-
Keyboard Shortcuts: Keybord shortcuts.
1. Read Eval Print Loop (REPL)
The Rascal Read Eval Print Loop (REPL)
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.
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
-
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
RascalShell commands
The RascalShell provides several built-in commands:
-
Clear Command: Clear all break points.
-
Edit Command: Open an editor for a Rascal module.
-
Help Command: Get a reminder of the shell commands.
-
Quit Command: Quit the RascalShell.
-
Set Command: Set parameters that control options of RascalShell.
-
Test Command: Run tests.
2.1. Clear Command
Clear all break points.
clear
Removes all break points.
2.2. Edit Command
Open an editor for a Rascal module
-
:edit ModuleName
>>>>>>>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
Get a reminder of the shell commands.
-
:help
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
Quit the RascalShell.
-
:quit
-
Ctrl+d or Eof
Typing :quit
or Ctrl+d will end the execution of the shell.
5. Set Command
Set parameters that control options of RascalShell.
-
:set
-
:set Option TrueOrFalse
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)
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
The set
command is completely unrelated to Rascal’s built-in set
type.
6. Test Command
Run tests.
-
:test
Run Rascal tests. The tests in all currently imported modules are executed and the results are reported in the terminal.
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
Context-dependent command completion.
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
Use the command history.
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 |
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
Keybord shortcuts.
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 ( |
Ctrl+e |
Move cursor to end of line. |
Ctrl+k |
Kill remainder of line after cursor. |
Ctrl+l |
Clear screen. |