@abaplint/transpiler
Version:
87 lines • 4.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SwitchBodyTranspiler = void 0;
const core_1 = require("@abaplint/core");
const chunk_1 = require("../chunk");
const type_name_or_infer_1 = require("./type_name_or_infer");
const transpile_types_1 = require("../transpile_types");
const source_1 = require("./source");
class SwitchBodyTranspiler {
transpile(typ, body, traversal) {
if (!(typ.get() instanceof core_1.Expressions.TypeNameOrInfer)) {
throw new Error("SwitchBodyTranspiler, Expected TypeNameOrInfer");
}
else if (body.findDirectExpression(core_1.Expressions.Let)) {
throw new Error("SwitchBodyTranspiler, Let not supported, todo");
}
const source = traversal.traverse(body.findDirectExpression(core_1.Expressions.Source));
const whenThenOr = [];
const elseExpression = body.findExpressionAfterToken("ELSE");
{
let currentWhenThen = { whenOr: [], then: undefined };
let mode = "";
for (const c of body.getChildren()) {
if (c instanceof core_1.Nodes.TokenNode) {
if (c.concatTokens() === "WHEN") {
if (currentWhenThen.whenOr.length > 0) {
whenThenOr.push(currentWhenThen);
}
currentWhenThen = { whenOr: [], then: undefined };
mode = "WHEN";
}
else if (c.concatTokens() === "THEN") {
mode = "THEN";
}
}
else if (mode === "WHEN" && c instanceof core_1.Nodes.ExpressionNode) {
currentWhenThen.whenOr.push(c);
}
else if (mode === "THEN" && c instanceof core_1.Nodes.ExpressionNode) {
currentWhenThen.then = c;
}
}
if (currentWhenThen.whenOr.length > 0) {
whenThenOr.push(currentWhenThen);
}
}
const type = new type_name_or_infer_1.TypeNameOrInfer().findType(typ, traversal);
const target = transpile_types_1.TranspileTypes.toType(type);
const ret = new chunk_1.Chunk();
ret.appendString("(" + target + ".set(");
ret.appendString("await (async () => {\n");
for (const wto of whenThenOr) {
let thenValue = "";
if (wto.then?.get() instanceof core_1.Expressions.Source) {
thenValue = new source_1.SourceTranspiler().transpile(wto.then, traversal).getCode();
}
else {
throw new Error("SwitchBodyTranspiler, Expected Source1, todo, " + wto.then?.get().constructor.name);
}
// todo, async await?
for (const thenOr of wto.whenOr) {
let condition = "";
if (thenOr.get() instanceof core_1.Expressions.Source) {
condition = new source_1.SourceTranspiler().transpile(thenOr, traversal).getCode();
}
else {
throw new Error("SwitchBodyTranspiler, Expected Source2, todo, " + thenOr.get().constructor.name);
}
ret.appendString(`if (abap.compare.eq(${source.getCode()}, ${condition})) { return ${thenValue}; }\n`);
}
}
if (elseExpression) {
if (!(elseExpression.get() instanceof core_1.Expressions.Source)) {
throw new Error("SwitchBodyTranspiler, Expected Source3, todo, " + elseExpression.get().constructor.name);
}
const value = new source_1.SourceTranspiler().transpile(elseExpression, traversal).getCode();
ret.appendString(`return ${value};\n`);
}
else {
ret.appendString(`return ${target};\n`);
}
ret.appendString("})()))");
return ret;
}
}
exports.SwitchBodyTranspiler = SwitchBodyTranspiler;
//# sourceMappingURL=switch_body.js.map