module demo::lang::Pico::ControlFlow
rascal-0.40.17
Usage
import demo::lang::Pico::ControlFlow;
Dependencies
import analysis::graphs::Graph;
import demo::lang::Pico::Abstract;
import demo::lang::Pico::Load;
import List;
data CFNode
data CFNode
= entry(loc location)
| exit()
| choice(loc location, EXP exp)
| statement(loc location, STATEMENT stat)
;
alias CFGraph
tuple[set[CFNode] entry, Graph[CFNode] graph, set[CFNode] exit]
function cflowStat
CFGraph cflowStat(s:asgStat(PicoId Id, EXP Exp)) { ❸
S = statement(s.src, s);
return <{S}, {}, {S}>;
}
CFGraph cflowStat(ifElseStat(EXP Exp, ❹
list[STATEMENT] Stats1,
list[STATEMENT] Stats2)) {
CF1 = cflowStats(Stats1);
CF2 = cflowStats(Stats2);
E = {choice(Exp.src, Exp)};
return < E, (E * CF1.entry) + (E * CF2.entry) + CF1.graph + CF2.graph, CF1.exit + CF2.exit >;
}
CFGraph cflowStat(whileStat(EXP Exp, list[STATEMENT] Stats)) { ❺
CF = cflowStats(Stats);
E = {choice(Exp.src, Exp)};
return < E, (E * CF.entry) + CF.graph + (CF.exit * E), E >;
}
function cflowStats
CFGraph cflowStats(list[STATEMENT] Stats) { ❻
if(size(Stats) == 1) {
return cflowStat(Stats[0]);
}
CF1 = cflowStat(Stats[0]);
CF2 = cflowStats(tail(Stats));
return < CF1.entry, CF1.graph + CF2.graph + (CF1.exit * CF2.entry), CF2.exit >;
}
function cflowProgram
CFGraph cflowProgram(PROGRAM P:program(list[DECL] _, list[STATEMENT] Series)) { ❼
CF = cflowStats(Series);
Entry = entry(P.src);
Exit = exit();
return <{Entry}, ({Entry} * CF.entry) + CF.graph + (CF.exit * {Exit}), {Exit}>;
}
CFGraph cflowProgram(str txt) = cflowProgram(load(txt));