2020-05-23 21:15:20 +02:00
|
|
|
import {
|
|
|
|
BoltFunctionBodyElement,
|
|
|
|
BoltReturnStatement,
|
|
|
|
SyntaxKind,
|
2020-05-24 17:47:04 +02:00
|
|
|
BoltExpression,
|
|
|
|
BoltSourceFile,
|
|
|
|
JSSourceFile
|
2020-05-23 21:15:20 +02:00
|
|
|
} from "./ast";
|
|
|
|
|
2020-05-24 17:47:04 +02:00
|
|
|
export type SourceFile
|
|
|
|
= BoltSourceFile
|
|
|
|
| JSSourceFile
|
|
|
|
|
2020-05-23 21:15:20 +02:00
|
|
|
export type BoltFunctionBody = BoltFunctionBodyElement[];
|
|
|
|
|
2020-05-24 11:17:56 +02:00
|
|
|
export function getReturnStatementsInFunctionBody(body: BoltFunctionBody): BoltReturnStatement[] {
|
2020-05-23 21:15:20 +02:00
|
|
|
|
|
|
|
const results: BoltReturnStatement[] = [];
|
|
|
|
|
|
|
|
for (const element of body) {
|
|
|
|
visit(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
|
|
|
function visit(node: BoltFunctionBodyElement) {
|
|
|
|
switch (node.kind) {
|
|
|
|
case SyntaxKind.BoltReturnStatement:
|
|
|
|
results.push(node);
|
|
|
|
break;
|
|
|
|
case SyntaxKind.BoltExpressionStatement:
|
|
|
|
visitExpression(node.expression);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function visitExpression(node: BoltExpression) {
|
|
|
|
switch (node.kind) {
|
|
|
|
case SyntaxKind.BoltBlockExpression:
|
|
|
|
for (const element of node.statements) {
|
|
|
|
visit(element);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SyntaxKind.BoltMatchExpression:
|
|
|
|
for (const arm of node.arms) {
|
|
|
|
visitExpression(arm.body);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SyntaxKind.BoltCallExpression:
|
|
|
|
visitExpression(node.operator);
|
|
|
|
for (const operand of node.operands) {
|
|
|
|
visitExpression(operand);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|