@fink/loxia
Version:
A fink to JavaScript compiler.
271 lines (232 loc) • 7.39 kB
JavaScript
const {
_in_
} = require("@fink/js-interop/runtime.js");
const types_0 = require("@babel/types");
const variableDeclaration_0 = types_0.variableDeclaration;
const variableDeclarator_0 = types_0.variableDeclarator;
const {
get_transformer: get_transformer_0
} = require("./context.js");
const {
get_js: get_js_0
} = require("./context.js");
const {
get_js_literal: get_js_literal_0
} = require("./context.js");
const {
set_js2: set_js2_0
} = require("./context.js");
const {
with_loc: with_loc_0
} = require("./context.js");
const {
get_value: get_value_0
} = require("../ir/context.js");
const {
ident: ident_0
} = require("./identifier/init.js");
const ops_to_js_0 = {
"^": `**`,
"==": `===`,
"!=": `!==`,
not: `!`
};
const transform_binary_0 = (expr_0, ctx_0) => {
const drec_1 = expr_0[0];
const op_0 = drec_1.f;
const val_4 = drec_1.args;
const left_0 = get_js_0(val_4[0], ctx_0);
const right_0 = get_js_0(val_4[1], ctx_0);
const hdm_0 = ops_to_js_0[op_0];
const js_0 = types_0.binaryExpression(undefined === hdm_0 ? op_0 : hdm_0, left_0, right_0);
const result_5 = set_js2_0(expr_0, js_0, ctx_0);
return result_5;
};
const transform_unary_0 = (expr_1, ctx_1) => {
const drec_3 = expr_1[0];
const op_1 = drec_3.f;
const right_1 = get_js_0(drec_3.args[0], ctx_1);
const hdm_1 = ops_to_js_0[op_1];
const js_1 = types_0.unaryExpression(undefined === hdm_1 ? op_1 : hdm_1, right_1);
const result_10 = set_js2_0(expr_1, js_1, ctx_1);
return result_10;
};
const transform_binary_or_unary_0 = (expr_2, ctx_2) => {
/* istanbul ignore else */
if (null != expr_2) {
const itm_0 = expr_2[0];
/* istanbul ignore else */
if (null != itm_0) {
const itm_1 = itm_0.args;
/* istanbul ignore else */
if (null != itm_1) {
/* istanbul ignore else */
if (undefined !== itm_1[1]) {
const result_13 = transform_binary_0(expr_2, ctx_2);
return result_13;
}
}
}
}
const result_12 = transform_unary_0(expr_2, ctx_2);
return result_12;
};
const transform_0 = (expr_3, ctx_3) => {
const transform_expr_0 = get_transformer_0(expr_3, ctx_3);
const result_15 = transform_expr_0(expr_3, ctx_3);
return result_15;
};
const transform_body_expr_0 = (expr_4, ctx_4) => {
const op_2 = expr_4[0].f;
const res_id_0 = expr_4[1][0];
const next_ctx_0 = transform_0(expr_4, ctx_4);
const res_val_0 = get_value_0(res_id_0, next_ctx_0);
const value_1 = [op_2, res_val_0];
/* istanbul ignore else */
if (null != value_1) {
/* istanbul ignore else */
if (null != res_val_0) {
/* istanbul ignore else */
if (res_val_0.inline === false) {
/* istanbul ignore else */
if (true === res_val_0.refs > 0) {
const id_1 = ident_0(res_id_0);
const arg_53 = variableDeclarator_0(id_1, res_val_0.js);
const item_11 = with_loc_0(res_id_0, arg_53);
const arg_49 = variableDeclaration_0(`const`, [item_11]);
const js_4 = with_loc_0(res_id_0, arg_49);
return [[js_4], next_ctx_0];
}
}
}
}
/* istanbul ignore else */
if (null != value_1) {
/* istanbul ignore else */
if (null != res_val_0) {
/* istanbul ignore else */
if (true === (res_val_0.inline !== true)) {
/* istanbul ignore else */
if (true === res_val_0.refs > 1) {
const val_11 = get_js_literal_0(res_id_0, next_ctx_0);
let ret_6;
ret_6: {
/* istanbul ignore else */
if (null != val_11) {
/* istanbul ignore else */
if (val_11.type === `VariableDeclaration`) {
const result_28 = with_loc_0(res_id_0, val_11);
ret_6 = result_28;
/* istanbul ignore next */
break ret_6;
}
}
const id_0 = ident_0(res_id_0);
const arg_42 = variableDeclarator_0(id_0, val_11);
const item_7 = with_loc_0(res_id_0, arg_42);
const arg_38 = variableDeclaration_0(`const`, [item_7]);
const result_27 = with_loc_0(res_id_0, arg_38);
ret_6 = result_27;
/* istanbul ignore next */
break ret_6;
}
const js_3 = ret_6;
return [[js_3], next_ctx_0];
}
}
}
}
/* istanbul ignore else */
if (null != value_1) {
/* istanbul ignore else */
if (op_2 === `af`) {
/* istanbul ignore else */
if (null != res_val_0) {
/* istanbul ignore else */
if (true === (res_val_0.refs === 0)) {
const val_8 = get_js_literal_0(res_id_0, next_ctx_0);
const arg_32 = types_0.expressionStatement(val_8);
const js_2 = with_loc_0(res_id_0, arg_32);
return [[js_2], next_ctx_0];
}
}
}
}
return [[], next_ctx_0];
};
const transform_exprs_0 = (...args_5) => {
transform_exprs_0: do {
const dlst_4 = args_5[0];
const hdm_2 = dlst_4[0];
const expr_5 = undefined === hdm_2 ? false : hdm_2;
const tail_28 = dlst_4.slice(1);
const ctx_5 = args_5[1];
const hdm_3 = args_5[2];
const body_0 = undefined === hdm_3 ? [] : hdm_3;
/* istanbul ignore else */
if (expr_5 === false) {
return [body_0, ctx_5];
}
/* istanbul ignore else */
if (null != expr_5) {
const itm_19 = expr_5[0];
/* istanbul ignore else */
if (null != itm_19) {
/* istanbul ignore else */
if (true === _in_(itm_19.f, [`cf`, `cc`, `cif`, `ac`, `imp`])) {
const dlst_7 = transform_0(expr_5, ctx_5);
args_5 = [tail_28, dlst_7[1], [...body_0, ...dlst_7[0]]];
continue transform_exprs_0;
}
}
}
/* istanbul ignore else */
if (null != expr_5) {
const itm_17 = expr_5[0];
/* istanbul ignore else */
if (null != itm_17) {
/* istanbul ignore else */
if (itm_17.f === `cn`) {
const next_ctx_3 = transform_0(expr_5, ctx_5);
args_5 = [tail_28, next_ctx_3, body_0];
continue transform_exprs_0;
}
}
}
/* istanbul ignore else */
if (null != expr_5) {
const itm_15 = expr_5[0];
/* istanbul ignore else */
if (null != itm_15) {
/* istanbul ignore else */
if (itm_15.f === `mod`) {
const dlst_6 = transform_0(expr_5, ctx_5);
args_5 = [tail_28, dlst_6[1], [...body_0, ...dlst_6[0]]];
continue transform_exprs_0;
}
}
}
/* istanbul ignore else */
if (null != expr_5) {
const itm_13 = expr_5[1];
/* istanbul ignore else */
if (null != itm_13) {
/* istanbul ignore else */
if (undefined !== itm_13[0]) {
const dlst_5 = transform_body_expr_0(expr_5, ctx_5);
args_5 = [tail_28, dlst_5[1], [...body_0, ...dlst_5[0]]];
continue transform_exprs_0;
}
}
}
/* istanbul ignore next */
return;
} while (true);
};
exports.ops_to_js = ops_to_js_0;
exports.transform_binary = transform_binary_0;
exports.transform_unary = transform_unary_0;
exports.transform_binary_or_unary = transform_binary_or_unary_0;
exports.transform = transform_0;
exports.transform_body_expr = transform_body_expr_0;
exports.transform_exprs = transform_exprs_0;