UNPKG

@abaplint/transpiler

Version:
71 lines 3.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CondBodyTranspiler = 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 cond_1 = require("./cond"); const source_1 = require("./source"); class CondBodyTranspiler { transpile(typ, body, traversal) { if (!(typ.get() instanceof core_1.Expressions.TypeNameOrInfer)) { throw new Error("CondBodyTranspiler, Expected TypeNameOrInfer"); } else if (body.findDirectExpression(core_1.Expressions.Let)) { throw new Error("CondBodyTranspiler, Let not supported, todo"); } const whenThen = []; const expressions = []; for (const c of body.getChildren()) { if (c instanceof core_1.Nodes.TokenNode) { if (c.concatTokens().toUpperCase() === "ELSE") { break; } } else { expressions.push(c); } } for (let i = 0; i < expressions.length; i = i + 2) { whenThen.push({ when: expressions[i], then: expressions[i + 1] }); } 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 { when, then } of whenThen) { let condition = ""; if (when.get() instanceof core_1.Expressions.Cond) { condition = new cond_1.CondTranspiler().transpile(when, traversal).getCode(); } else { throw new Error("CondBodyTranspiler, Expected Cond, todo, " + when.get().constructor.name); } let value = ""; if (then.get() instanceof core_1.Expressions.Source) { value = new source_1.SourceTranspiler().transpile(then, traversal).getCode(); } else { throw new Error("CondBodyTranspiler, Expected Source, todo, " + then.get().constructor.name); } ret.appendString(`if (${condition}) { return ${value}; }\n`); } const els = body.findExpressionAfterToken("ELSE"); if (els) { if (!(els.get() instanceof core_1.Expressions.Source)) { throw new Error("CondBodyTranspiler, Expected Source, todo, " + els.get().constructor.name); } const value = new source_1.SourceTranspiler().transpile(els, traversal).getCode(); ret.appendString(`return ${value};\n`); } else { ret.appendString(`return ${target};\n`); } ret.appendString("})()))"); return ret; } } exports.CondBodyTranspiler = CondBodyTranspiler; //# sourceMappingURL=cond_body.js.map