Rascal Cheat Sheet
by rubene via [Link]/19109/cs/2146/
Default datatypes Location For loop (array)
String str File |file:///home/rascal/ras[Link]| list[int] numbers = [1, 2, 3, 4,
Boolean bool Project |project://android-project/| 5];
for (number <- numbers) {
Array list[T ] URL |http://www.goo[Link]|
iprintln(number);
Location loc Folder |file:///home/rascal/|
}
Map / set map[T , T ]
Data manipulation 1
Integer int For loop (map)
String - append str : "str" + "str"
Set (unique array) set[T ] map[str, int] numbers = ["one" :
String - compare bool : "str == "str"
T = type. For example: list[str] for a list of 1, "two" : 2,
strings String - interpolation "value: <str>" "three" : 3, "four" : 4, "‐
List - assignment list[int] : [3,2,1]; five" : 5];
Imports List - append list[int] : [3,2] + 1; for (numberText <- numbers) {
String import String Set - assignment set[int]: {3,2,1} int numberValue =
Boolean import Boolean numbers[numberText];
Set - append set[int]: {3,2} + 1;
iprintln(num‐
List import List All values are immutable
berValue);
Location import Location
}
Map import Map Data manipulation 2
Integer import util::Math // data Game = game(list[Player] File manipulation
Set import Set players);
Read file readFile(location)
// Game game = getGame();
M3 AST import analysis::m3::AST Write file writeFile(location, value)
// Access players:
IO import IO
game.players
Method definition
// Overwrite players:
Read java project public str getString() {
game.players = []
createAstsFromDirectory(loc // Add player: return "String";
project, true); game.players = game.players + }
This method is located in the M3 AST library player The method test() is reserved
return: set[Declaration]
If else Visit pattern - Matches
Debugging
if("str" == "str") { wildcard _
Print to console (one line) println(value) iprintln("true"); Value check "exact value"
Print to console (formatted) iprintln(value) } else {
Assignment method
iprintln("false");
This methods are located in the IO library Block assignment block:
}
Visit pattern - Example 1
visit (ast) {
case \import(importN‐
ame): {
iprintln(‐
importName);
}
}
By rubene Published 28th May, 2014. Sponsored by [Link]
[Link]/rubene/ Last updated 11th May, 2016. Learn to solve cryptic crosswords!
Page 1 of 2. [Link]
Rascal Cheat Sheet
by rubene via [Link]/19109/cs/2146/
Visit pattern - Example 2
visit (ast) {
case \import(_): {
iprintln‐
("Found an import");
}
}
Visit pattern - Example 3
visit (ast) {
case \import("Im‐
portedClass"): {
iprintln‐
("found ImportedClas‐
s");
}
}
Visit pattern - Example 4
visit (ast) {
case \field(simpleTy‐
pe(simpleName(name)), _): {
iprintln‐
("found a field: <name>");
}
}
Syntax keywords
layout The layout of the syntax. Is the
syntax separated by spaces or
new lines.
lexical Definition for a block of the input,
which is separated by the layout.
start The global syntax definition of the
syntax input
syntax The global syntax can be
separated into smaller pieces,
which can be defined with a
syntax.
By rubene Published 28th May, 2014. Sponsored by [Link]
[Link]/rubene/ Last updated 11th May, 2016. Learn to solve cryptic crosswords!
Page 2 of 2. [Link]