/* functions */ u3_noun u3qb_scag( u3_atom a, u3_noun b) { if ( !_(u3a_is_cat(a)) ) { return u3m_bail(c3__fail); } else { u3_noun acc; c3_w i_w = a; if ( !i_w ) return u3_nul; while ( i_w ) { if ( c3n == u3du(b) ) { return u3_nul; } acc = u3i_cell( u3h(b), acc ); b = u3t(b); i_w--; } return u3kb_flop(acc); } }
/* _n_nock_on(): produce .*(bus fol). Do not virtualize. */ static u3_noun _n_nock_on(u3_noun bus, u3_noun fol) { u3_noun hib, gal; while ( 1 ) { hib = u3h(fol); gal = u3t(fol); u3R->pro.nox_d += 1; if ( c3y == u3r_du(hib) ) { u3_noun poz, riv; poz = _n_nock_on(u3k(bus), u3k(hib)); riv = _n_nock_on(bus, u3k(gal)); u3a_lose(fol); return u3i_cell(poz, riv); } else switch ( hib ) { default: return u3m_bail(c3__exit); case 0: { if ( c3n == u3r_ud(gal) ) { return u3m_bail(c3__exit); } else { u3_noun pro = u3k(u3at(gal, bus)); u3a_lose(bus); u3a_lose(fol); return pro; } } c3_assert(!"not reached"); case 1: { u3_noun pro = u3k(gal); u3a_lose(bus); u3a_lose(fol); return pro; } c3_assert(!"not reached"); case 2: { u3_noun nex = _n_nock_on(u3k(bus), u3k(u3t(gal))); u3_noun seb = _n_nock_on(bus, u3k(u3h(gal))); u3a_lose(fol); bus = seb; fol = nex; continue; } c3_assert(!"not reached"); case 3: { u3_noun gof, pro; gof = _n_nock_on(bus, u3k(gal)); pro = u3r_du(gof); u3a_lose(gof); u3a_lose(fol); return pro; } c3_assert(!"not reached"); case 4: { u3_noun gof, pro; gof = _n_nock_on(bus, u3k(gal)); pro = u3i_vint(gof); u3a_lose(fol); return pro; } c3_assert(!"not reached"); case 5: { u3_noun wim = _n_nock_on(bus, u3k(gal)); u3_noun pro = u3r_sing(u3h(wim), u3t(wim)); u3a_lose(wim); u3a_lose(fol); return pro; } c3_assert(!"not reached"); case 6: { u3_noun b_gal, c_gal, d_gal; u3x_trel(gal, &b_gal, &c_gal, &d_gal); { u3_noun tys = _n_nock_on(u3k(bus), u3k(b_gal)); u3_noun nex; if ( 0 == tys ) { nex = u3k(c_gal); } else if ( 1 == tys ) { nex = u3k(d_gal); } else return u3m_bail(c3__exit); u3a_lose(fol); fol = nex; continue; } } c3_assert(!"not reached"); case 7: { u3_noun b_gal, c_gal; u3x_cell(gal, &b_gal, &c_gal); { u3_noun bod = _n_nock_on(bus, u3k(b_gal)); u3_noun nex = u3k(c_gal); u3a_lose(fol); bus = bod; fol = nex; continue; } } c3_assert(!"not reached"); case 8: { u3_noun b_gal, c_gal; u3x_cell(gal, &b_gal, &c_gal); { u3_noun heb = _n_nock_on(u3k(bus), u3k(b_gal)); u3_noun bod = u3nc(heb, bus); u3_noun nex = u3k(c_gal); u3a_lose(fol); bus = bod; fol = nex; continue; } } c3_assert(!"not reached"); case 9: { u3_noun b_gal, c_gal; u3x_cell(gal, &b_gal, &c_gal); { u3_noun seb = _n_nock_on(bus, u3k(c_gal)); u3_noun pro; u3t_off(noc_o); pro = u3j_kick(seb, b_gal); u3t_on(noc_o); if ( u3_none != pro ) { u3a_lose(fol); return pro; } else { if ( c3n == u3r_ud(b_gal) ) { return u3m_bail(c3__exit); } else { u3_noun nex = u3k(u3at(b_gal, seb)); u3a_lose(fol); bus = seb; fol = nex; continue; } } } } c3_assert(!"not reached"); case 10: { u3_noun p_gal, q_gal; u3x_cell(gal, &p_gal, &q_gal); { u3_noun zep, hod, nex; if ( c3y == u3r_du(p_gal) ) { u3_noun b_gal = u3h(p_gal); u3_noun c_gal = u3t(p_gal); u3_noun d_gal = q_gal; zep = u3k(b_gal); hod = _n_nock_on(u3k(bus), u3k(c_gal)); nex = u3k(d_gal); } else { u3_noun b_gal = p_gal; u3_noun c_gal = q_gal; zep = u3k(b_gal); hod = u3_nul; nex = u3k(c_gal); } u3a_lose(fol); return _n_hint(zep, hod, bus, nex); } } case 11: { u3_noun ref = _n_nock_on(u3k(bus), u3k(u3h(gal))); u3_noun gof = _n_nock_on(bus, u3k(u3t(gal))); u3_noun val; u3t_off(noc_o); val = u3m_soft_esc(ref, u3k(gof)); u3t_on(noc_o); if ( !_(u3du(val)) ) { u3m_bail(u3nt(1, gof, 0)); } if ( !_(u3du(u3t(val))) ) { // // replace with proper error stack push // u3t_push(u3nc(c3__hunk, _n_mush(gof))); return u3m_bail(c3__exit); } else { u3_noun pro; u3z(gof); u3z(fol); pro = u3k(u3t(u3t(val))); u3z(val); return pro; } } c3_assert(!"not reached"); } } }
u3_noun u3qe_rexp(u3_noun lub, u3_noun rad) { c3_y* lub_y = u3r_tape(lub); c3_y* rad_y = u3r_tape(rad); u3k(lub); int lub_l = u3kb_lent(lub); if (lub_l != strlen((char *)lub_y)) { free(lub_y); free(rad_y); return u3_nul; } char* rec = (char*)lub_y; char* end; while(*rec != 0) { if(*rec > 127) { free(lub_y); free(rad_y); return u3_nul; } else if(*rec == '\\') { rec++; switch (*rec) { case 'P': case 'p': free(lub_y); free(rad_y); return u3_nul; case 'Q': end = strstr(rec, "\\E"); if(end == NULL) rec += strlen(rec) - 1; else rec = end; } } else if(*rec == '(') { rec++; if(*rec == '?') { rec++; if(*rec != ':') { free(lub_y); free(rad_y); return u3_nul; } rec++; } } else rec++; } cre2_regexp_t * rex; cre2_options_t * opt; opt = cre2_opt_new(); if (opt) { cre2_opt_set_log_errors(opt, 0); cre2_opt_set_encoding(opt, CRE2_UTF8); cre2_opt_set_perl_classes(opt, 1); cre2_opt_set_one_line(opt, 1); cre2_opt_set_longest_match(opt, 1); rex = cre2_new((const char *)lub_y, strlen((char *)lub_y), opt); if (rex) { if (!cre2_error_code(rex)) { int text_len = strlen((char *)rad_y); int captures = cre2_num_capturing_groups(rex); cre2_string_t matches[captures+1]; int match = cre2_match(rex, (const char*)rad_y, text_len, 0, text_len, CRE2_UNANCHORED, matches, captures+1); if (!match) { // No matches cre2_opt_delete(opt); cre2_delete(rex); free(lub_y); free(rad_y); return u3i_cell(u3_nul, u3_nul); } u3_noun map = u3_nul; int i; for (i = 0; i < captures+1; i++) { char * buf = malloc(matches[i].length + 1); memcpy(buf, matches[i].data, matches[i].length); buf[matches[i].length] = 0; map = u3kdb_put(map, i, u3i_tape(buf)); free(buf); } cre2_opt_delete(opt); cre2_delete(rex); free(lub_y); free(rad_y); return u3i_cell(u3_nul, u3i_cell(u3_nul, map)); } else { // Compiling the regular expression failed cre2_opt_delete(opt); cre2_delete(rex); free(lub_y); free(rad_y); return u3_nul; } cre2_delete(rex); } cre2_opt_delete(opt); } free(lub_y); free(rad_y); u3m_bail(c3__exit); return u3_nul; }