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:
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.
The RascalShell provides several built-in commands:
2.1. Clear Command
Clear all break points.
Removes all break points.
2.2. Edit Command
Open an editor for a Rascal module
>>>>>>>import IO; ^ Parse error hereok 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.
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.
Ctrl+d or Eof
:quit or Ctrl+d will end the execution of the shell.
5. Set Command
Set parameters that control options of RascalShell.
: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)
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
set command is completely unrelated to Rascal’s built-in
6. Test Command
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:
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).
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.
(After search text) Search backward in history.
(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.
When handling console input, RascalShell supports a subset of the shortcuts provided by GNU Readline
Some convenient shortcuts are:
Move cursor to begin of line.
EOF marker, closes the current RascalShell (equivalent to the command (
Move cursor to end of line.
Kill remainder of line after cursor.