UNPKG

@fink/loxia

Version:

A fink to JavaScript compiler.

697 lines (612 loc) 18.9 kB
import { _in_ } from "@fink/js-interop/runtime.js"; import { reverse as reverse_0 } from "@fink/std-lib/iter.js"; import { update_value as update_value_0 } from "../ir/context.js"; import { get_value as get_value_0 } from "../ir/context.js"; const replace_arg_alias_0 = (...args_0) => { replace_arg_alias_0: do { const arg_0 = args_0[0]; const ctx_0 = args_0[1]; const drec_0 = get_value_0(arg_0, ctx_0); const hdm_0 = drec_0.alias; const new_arg_0 = undefined === hdm_0 ? arg_0 : hdm_0; /* istanbul ignore else */ if (new_arg_0 === arg_0) { return arg_0; } args_0 = [new_arg_0, ctx_0]; continue replace_arg_alias_0; } while (true); }; const replace_args_aliases_0 = (...args_1) => { replace_args_aliases_0: do { const dlst_0 = args_1[0]; const hdm_1 = dlst_0[0]; const arg_5 = undefined === hdm_1 ? false : hdm_1; const ctx_1 = args_1[1]; const hdm_2 = args_1[2]; const out_0 = undefined === hdm_2 ? [] : hdm_2; /* istanbul ignore else */ if (arg_5 === false) { return out_0; } const renamed_arg_0 = replace_arg_alias_0(arg_5, ctx_1); args_1 = [dlst_0.slice(1), ctx_1, [...out_0, { ...renamed_arg_0, loc: arg_5.loc }]]; continue replace_args_aliases_0; } while (true); }; const optimize_args_0 = (expr_0, ctx_2) => { const o_args_0 = replace_args_aliases_0(expr_0.args, ctx_2); return { ...expr_0, args: o_args_0 }; }; const add_alias_0 = (id_0, target_0, ctx_3) => { const result_11 = update_value_0(id_0, { alias: target_0 }, ctx_3); return result_11; }; const optimize_lst_0 = (dlst_1, ctx_4) => { const res_0 = dlst_1[1]; const next_ctx_0 = update_value_0(res_0[0], { type: `tpl`, items: [] }, ctx_4); return [[{ ...dlst_1[0], f: `tpl`, args: [] }, res_0], next_ctx_0]; }; const optimize_lst_a_0 = (dlst_3, ctx_5) => { const res_1 = dlst_3[1]; const res_id_1 = res_1[0]; const opt_expr_1 = optimize_args_0(dlst_3[0], ctx_5); const val_9 = opt_expr_1.args; const lst_id_0 = val_9[0]; const val_id_0 = val_9[1]; const info_0 = get_value_0(lst_id_0, ctx_5); /* istanbul ignore else */ if (null != info_0) { /* istanbul ignore else */ if (info_0.type === `tpl`) { /* istanbul ignore else */ if (undefined !== info_0.items) { const drec_3 = get_value_0(lst_id_0, ctx_5); const items_1 = drec_3.items; const next_ctx_2 = update_value_0(res_id_1, { type: `tpl`, items: [...items_1, val_id_0] }, ctx_5); return [[[{ ...opt_expr_1, f: `tpl`, args: [...items_1, val_id_0] }, res_1]], next_ctx_2]; } } } const next_ctx_1 = update_value_0(res_id_1, { type: `lst` }, ctx_5); return [[[opt_expr_1, res_1]], next_ctx_1]; }; const optimize_lst_h_0 = (dlst_5, ctx_6) => { const res_2 = dlst_5[1]; const opt_expr_2 = optimize_args_0(dlst_5[0], ctx_6); const lst_id_1 = opt_expr_2.args[0]; const info_1 = get_value_0(lst_id_1, ctx_6); /* istanbul ignore else */ if (null != info_1) { /* istanbul ignore else */ if (info_1.type === `tpl`) { const itm_6 = info_1.items; /* istanbul ignore else */ if (null != itm_6) { /* istanbul ignore else */ if (undefined !== itm_6[0]) { const next_ctx_3 = add_alias_0(res_2[0], info_1.items[0], ctx_6); return [[], next_ctx_3]; } } } } /* istanbul ignore else */ if (null != info_1) { /* istanbul ignore else */ if (info_1.type === `tpl`) { /* istanbul ignore else */ if (undefined !== info_1.slice_of) { const dlst_7 = info_1.slice_of; let ret_10; ret_10: { /* istanbul ignore else */ if (dlst_7[3] === 1) { ret_10 = dlst_7[1]; /* istanbul ignore next */ break ret_10; } ret_10 = dlst_7[2] - 1; /* istanbul ignore next */ break ret_10; } const idx_0 = ret_10; return [[[{ ...opt_expr_2, f: `tpl_i`, args: [dlst_7[0], idx_0] }, res_2]], ctx_6]; } } } /* istanbul ignore else */ if (null != info_1) { /* istanbul ignore else */ if (info_1.type === `tpl`) { return [[[{ ...opt_expr_2, f: `tpl_i`, args: [lst_id_1, 0] }, res_2]], ctx_6]; } } return [[[{ ...opt_expr_2, f: `tpl_i`, args: [lst_id_1, 0] }, res_2]], ctx_6]; }; const optimize_lst_t_0 = (dlst_8, ctx_7) => { const res_3 = dlst_8[1]; const res_id_3 = res_3[0]; const opt_expr_3 = optimize_args_0(dlst_8[0], ctx_7); const lst_id_3 = opt_expr_3.args[0]; const info_2 = get_value_0(lst_id_3, ctx_7); /* istanbul ignore else */ if (null != info_2) { /* istanbul ignore else */ if (info_2.type === `tpl`) { const itm_14 = info_2.items; /* istanbul ignore else */ if (null != itm_14) { /* istanbul ignore else */ if (undefined !== itm_14[0]) { const tail_46 = info_2.items.slice(1); const next_ctx_8 = update_value_0(res_id_3, { type: `tpl`, items: tail_46 }, ctx_7); return [[[{ ...opt_expr_3, f: `tpl`, args: tail_46 }, res_3]], next_ctx_8]; } } } } /* istanbul ignore else */ if (null != info_2) { /* istanbul ignore else */ if (info_2.type === `tpl`) { /* istanbul ignore else */ if (undefined !== info_2.items) { const next_ctx_7 = add_alias_0(res_id_3, lst_id_3, ctx_7); return [[], next_ctx_7]; } } } /* istanbul ignore else */ if (null != info_2) { /* istanbul ignore else */ if (info_2.type === `tpl`) { /* istanbul ignore else */ if (undefined !== info_2.slice_of) { const dlst_10 = info_2.slice_of; const lst_id_4 = dlst_10[0]; const start_1 = dlst_10[1]; const end_1 = dlst_10[2]; const inc_1 = dlst_10[3]; let ret_13; ret_13: { /* istanbul ignore else */ if (inc_1 === 1) { ret_13 = [lst_id_4, start_1 + 1, end_1]; /* istanbul ignore next */ break ret_13; } ret_13 = [lst_id_4, start_1, end_1 - 1]; /* istanbul ignore next */ break ret_13; } const args_10 = ret_13; const next_ctx_6 = update_value_0(res_id_3, { type: `tpl`, slice_of: [...args_10, inc_1] }, ctx_7); return [[[{ ...opt_expr_3, f: `tpl_s`, args: args_10 }, res_3]], next_ctx_6]; } } } /* istanbul ignore else */ if (null != info_2) { /* istanbul ignore else */ if (info_2.type === `tpl`) { const next_ctx_5 = update_value_0(res_id_3, { type: `tpl`, slice_of: [lst_id_3, 1, 0, 1] }, ctx_7); return [[[{ ...opt_expr_3, f: `tpl_s`, args: [lst_id_3, 1, 0] }, res_3]], next_ctx_5]; } } const next_ctx_4 = update_value_0(res_id_3, { type: `tpl`, slice_of: [lst_id_3, 1, 0, 1] }, ctx_7); return [[[{ ...opt_expr_3, f: `tpl_s`, args: [lst_id_3, 1, 0] }, res_3]], next_ctx_4]; }; const optimize_lst_c_0 = (dlst_11, ctx_8) => { const res_4 = dlst_11[1]; const res_id_4 = res_4[0]; const opt_expr_4 = optimize_args_0(dlst_11[0], ctx_8); const val_51 = opt_expr_4.args; const info1_0 = get_value_0(val_51[0], ctx_8); const info2_0 = get_value_0(val_51[1], ctx_8); /* istanbul ignore else */ if (null != [info1_0, info2_0]) { /* istanbul ignore else */ if (null != info1_0) { /* istanbul ignore else */ if (info1_0.type === `tpl`) { /* istanbul ignore else */ if (undefined !== info1_0.items) { /* istanbul ignore else */ if (null != info2_0) { /* istanbul ignore else */ if (info2_0.type === `tpl`) { /* istanbul ignore else */ if (undefined !== info2_0.items) { const items_6 = [...info1_0.items, ...info2_0.items]; const next_ctx_10 = update_value_0(res_id_4, { type: `tpl`, items: items_6 }, ctx_8); return [[[{ ...opt_expr_4, f: `tpl`, args: items_6 }, res_4]], next_ctx_10]; } } } } } } } const next_ctx_9 = update_value_0(res_id_4, { type: `tpl` }, ctx_8); return [[[opt_expr_4, res_4]], next_ctx_9]; }; const optimize_lst_r_0 = (dlst_13, ctx_9) => { const res_5 = dlst_13[1]; const res_id_5 = res_5[0]; const opt_expr_5 = optimize_args_0(dlst_13[0], ctx_9); const lst_id_5 = opt_expr_5.args[0]; const info_3 = get_value_0(lst_id_5, ctx_9); /* istanbul ignore else */ if (null != info_3) { /* istanbul ignore else */ if (undefined !== info_3.tpr) { const next_ctx_14 = add_alias_0(res_id_5, info_3.tpr, ctx_9); return [[], next_ctx_14]; } } /* istanbul ignore else */ if (null != info_3) { /* istanbul ignore else */ if (info_3.type === `tpl`) { /* istanbul ignore else */ if (undefined !== info_3.items) { const items_9 = reverse_0(info_3.items); const next_ctx_13 = update_value_0(res_id_5, { type: `tpl`, items: items_9, tpr: lst_id_5 }, ctx_9); return [[[{ ...opt_expr_5, f: `tpl`, args: items_9 }, res_5]], next_ctx_13]; } } } /* istanbul ignore else */ if (null != info_3) { /* istanbul ignore else */ if (info_3.type === `tpl`) { /* istanbul ignore else */ if (undefined !== info_3.slice_of) { const dlst_15 = info_3.slice_of; const tpl_id_0 = dlst_15[0]; const start_idx_0 = dlst_15[1]; const end_idx_0 = dlst_15[2]; const inc_2 = dlst_15[3]; let ret_18; ret_18: { /* istanbul ignore else */ if (inc_2 === 1) { ret_18 = [tpl_id_0, start_idx_0, end_idx_0, -inc_2]; /* istanbul ignore next */ break ret_18; } ret_18 = [tpl_id_0, start_idx_0, end_idx_0, -inc_2]; /* istanbul ignore next */ break ret_18; } const slice_of_1 = ret_18; const next_ctx_12 = update_value_0(res_id_5, { type: `tpl`, slice_of: slice_of_1, tpr: lst_id_5 }, ctx_9); /* istanbul ignore else */ if (inc_2 === 1) { return [[[opt_expr_5, res_5]], next_ctx_12]; } return [[[{ ...opt_expr_5, f: `tpl_s`, args: [tpl_id_0, start_idx_0, end_idx_0] }, res_5]], next_ctx_12]; } } } const next_ctx_11 = update_value_0(res_id_5, { type: `tpl`, slice_of: [lst_id_5, 0, 0, -1], tpr: lst_id_5 }, ctx_9); return [[[opt_expr_5, res_5]], next_ctx_11]; }; const optimize_body_0 = (dlst_16, ctx_10, optimize_refs_0) => { const expr_8 = dlst_16[0]; const val_79 = expr_8.args; const args_14 = val_79[0]; let ret_21; ret_21: { /* istanbul ignore else */ if (null != expr_8) { /* istanbul ignore else */ if (expr_8.f === `fn`) { const result_108 = update_value_0(args_14[0], { type: `tpl` }, ctx_10); ret_21 = result_108; /* istanbul ignore next */ break ret_21; } } ret_21 = ctx_10; /* istanbul ignore next */ break ret_21; } const body_ctx_0 = ret_21; const dlst_18 = optimize_refs_0(val_79[1], body_ctx_0, []); return [[[{ ...expr_8, args: [args_14, dlst_18[0]] }, dlst_16[1]]], dlst_18[1]]; }; const optimize_cn_0 = (dlst_19, exprs_0, ctx_11, optimize_refs_1, out_1) => { const expr_9 = dlst_19[0]; const res_7 = dlst_19[1]; const dlst_21 = expr_9.args; const dlst_24 = exprs_0[0]; const drec_14 = dlst_24[0]; const ac_op_0 = drec_14.f; const ac_id_0 = drec_14.args[0]; const res_id_6 = dlst_24[1][0]; const hdm_3 = exprs_0[1]; const drec_15 = (undefined === hdm_3 ? [{ args: [] }] : hdm_3)[0]; const cc_op_0 = drec_15.f; const val_85 = drec_15.args; /* istanbul ignore else */ if (null != [ac_op_0, ac_id_0, cc_op_0, res_id_6]) { /* istanbul ignore else */ if (ac_op_0 === `ac`) { /* istanbul ignore else */ if (ac_id_0 === res_7[0]) { /* istanbul ignore else */ if (cc_op_0 === `cc`) { /* istanbul ignore else */ if (res_id_6 === val_85[1]) { const b_ctx_0 = add_alias_0(dlst_21[0][0], val_85[0], ctx_11); const dlst_28 = optimize_refs_1(dlst_21[1], b_ctx_0, []); const result_120 = optimize_refs_1(exprs_0.slice(2), dlst_28[1], [...out_1, ...dlst_28[0]]); return result_120; } } } } } const dlst_27 = optimize_body_0([expr_9, res_7], ctx_11, optimize_refs_1); const result_116 = optimize_refs_1(exprs_0, dlst_27[1], [...out_1, ...dlst_27[0]]); return result_116; }; const optimize_refs_2 = (...args_16) => { optimize_refs_2: do { const dlst_30 = args_16[0]; const hdm_4 = dlst_30[0]; const expr_10 = undefined === hdm_4 ? false : hdm_4; const tail_107 = dlst_30.slice(1); const ctx_12 = args_16[1]; const hdm_5 = args_16[2]; const out_2 = undefined === hdm_5 ? [] : hdm_5; /* istanbul ignore else */ if (expr_10 === false) { return [out_2, ctx_12]; } /* istanbul ignore else */ if (null != expr_10) { const itm_52 = expr_10[0]; /* istanbul ignore else */ if (null != itm_52) { /* istanbul ignore else */ if (itm_52.f === `lst`) { const dlst_41 = optimize_lst_0(expr_10, ctx_12); args_16 = [tail_107, dlst_41[1], [...out_2, dlst_41[0]]]; continue optimize_refs_2; } } } /* istanbul ignore else */ if (null != expr_10) { const itm_50 = expr_10[0]; /* istanbul ignore else */ if (null != itm_50) { /* istanbul ignore else */ if (itm_50.f === `lst_a`) { const dlst_40 = optimize_lst_a_0(expr_10, ctx_12); args_16 = [tail_107, dlst_40[1], [...out_2, ...dlst_40[0]]]; continue optimize_refs_2; } } } /* istanbul ignore else */ if (null != expr_10) { const itm_48 = expr_10[0]; /* istanbul ignore else */ if (null != itm_48) { /* istanbul ignore else */ if (itm_48.f === `lst_h`) { const dlst_39 = optimize_lst_h_0(expr_10, ctx_12); args_16 = [tail_107, dlst_39[1], [...out_2, ...dlst_39[0]]]; continue optimize_refs_2; } } } /* istanbul ignore else */ if (null != expr_10) { const itm_46 = expr_10[0]; /* istanbul ignore else */ if (null != itm_46) { /* istanbul ignore else */ if (itm_46.f === `lst_t`) { const dlst_38 = optimize_lst_t_0(expr_10, ctx_12); args_16 = [tail_107, dlst_38[1], [...out_2, ...dlst_38[0]]]; continue optimize_refs_2; } } } /* istanbul ignore else */ if (null != expr_10) { const itm_44 = expr_10[0]; /* istanbul ignore else */ if (null != itm_44) { /* istanbul ignore else */ if (itm_44.f === `lst_c`) { const dlst_37 = optimize_lst_c_0(expr_10, ctx_12); args_16 = [tail_107, dlst_37[1], [...out_2, ...dlst_37[0]]]; continue optimize_refs_2; } } } /* istanbul ignore else */ if (null != expr_10) { const itm_42 = expr_10[0]; /* istanbul ignore else */ if (null != itm_42) { /* istanbul ignore else */ if (itm_42.f === `lst_r`) { const dlst_36 = optimize_lst_r_0(expr_10, ctx_12); args_16 = [tail_107, dlst_36[1], [...out_2, ...dlst_36[0]]]; continue optimize_refs_2; } } } /* istanbul ignore else */ if (null != expr_10) { const itm_40 = expr_10[0]; /* istanbul ignore else */ if (null != itm_40) { /* istanbul ignore else */ if (itm_40.f === `=`) { const next_ctx_19 = add_alias_0(expr_10[1][0], expr_10[0].args[0], ctx_12); args_16 = [tail_107, next_ctx_19, out_2]; continue optimize_refs_2; } } } /* istanbul ignore else */ if (null != expr_10) { const itm_38 = expr_10[0]; /* istanbul ignore else */ if (null != itm_38) { /* istanbul ignore else */ if (true === _in_(itm_38.f, [`int`, `float`, `str`, `bool`])) { args_16 = [tail_107, ctx_12, [...out_2, expr_10]]; continue optimize_refs_2; } } } /* istanbul ignore else */ if (null != expr_10) { const itm_36 = expr_10[0]; /* istanbul ignore else */ if (null != itm_36) { /* istanbul ignore else */ if (itm_36.f === `cn`) { const result_132 = optimize_cn_0(expr_10, tail_107, ctx_12, optimize_refs_2, out_2); return result_132; } } } /* istanbul ignore else */ if (null != expr_10) { const itm_34 = expr_10[0]; /* istanbul ignore else */ if (null != itm_34) { /* istanbul ignore else */ if (itm_34.f === `fn`) { const dlst_33 = optimize_body_0(expr_10, ctx_12, optimize_refs_2); args_16 = [tail_107, dlst_33[1], [...out_2, ...dlst_33[0]]]; continue optimize_refs_2; } } } /* istanbul ignore else */ if (null != expr_10) { const itm_32 = expr_10[0]; /* istanbul ignore else */ if (null != itm_32) { /* istanbul ignore else */ if (itm_32.f === `+`) { const o_expr_1 = optimize_args_0(expr_10[0], ctx_12); args_16 = [tail_107, ctx_12, [...out_2, [o_expr_1, expr_10[1]]]]; continue optimize_refs_2; } } } const o_expr_0 = optimize_args_0(expr_10[0], ctx_12); args_16 = [tail_107, ctx_12, [...out_2, [o_expr_0, expr_10[1]]]]; continue optimize_refs_2; } while (true); }; export const replace_arg_alias = replace_arg_alias_0, replace_args_aliases = replace_args_aliases_0, optimize_args = optimize_args_0, add_alias = add_alias_0, optimize_lst = optimize_lst_0, optimize_lst_a = optimize_lst_a_0, optimize_lst_h = optimize_lst_h_0, optimize_lst_t = optimize_lst_t_0, optimize_lst_c = optimize_lst_c_0, optimize_lst_r = optimize_lst_r_0, optimize_body = optimize_body_0, optimize_cn = optimize_cn_0, optimize_refs = optimize_refs_2;