<commit_before>#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <stdarg.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include "parse.h"
#include "opt.h"

typedef struct Dtree Dtree;
struct Dtree {
    /* If the values are equal, go to 'sub'. If 'val' is null, anything matches. */
    Node **val;
    size_t nval;
    Dtree **sub;
    size_t nsub;
    Dtree *any;

    /* captured variables and action */
    Node **cap;
    size_t ncap;
    Node *act;

};

static Dtree *addpat(Dtree *t, Node *pat, Node ***cap, size_t *ncap);

static Dtree *mkdtree()
{
    return zalloc(sizeof(Dtree));
}

static int uconeq(Node *a, Node *b)
{
    return !strcmp(namestr(a), namestr(b));
}

static Dtree *addunion(Dtree *t, Node *pat, Node ***cap, size_t *ncap)
{
    Dtree *sub;
    size_t i;

    /* if we have the value already... */
    sub = NULL;
    for (i = 0; i < t->nval; i++) {
        if (!t->val[i])
            fatal(pat, "constructor already matched by earlier variable");
        if (uconeq(t->val[i], pat->expr.args[0])) {
            return addpat(t->sub[i], pat->expr.args[1], cap, ncap);
        }
    }

    sub = mkdtree();
    lappend(&t->val, &t->nval, pat->expr.args[0]);
    lappend(&t->sub, &t->nsub, sub);
    if (pat->expr.nargs == 2)
        sub = addpat(sub, pat->expr.args[1], cap, ncap);
    return sub;
}

static Dtree *addwild(Dtree *t, Node *var, Node ***cap, size_t *ncap)
{
    if (t->any)
        return t->any;
    t->any = mkdtree();
    lappend(cap, ncap, var);
    return t->any;
}

static Dtree *addpat(Dtree *t, Node *pat, Node ***cap, size_t *ncap)
{
    Type *ty;

    if (pat == NULL)
        return t;
    ty = tybase(exprtype(pat));
    if (exprop(pat) == Ovar)
        return addwild(t, pat, cap, ncap);

    switch (ty->type) {
        case Tyunion:
            t = addunion(t, pat, cap, ncap);
            break;
            /*
        case Tyslice:
            t = addslice(t, pat);
            break;
        case Tytuple:
            t = addtuple(t, pat);
            break;
        case Tyarray:
            t = addtuple(t, pat);
            break;
        case Tystruct:
            t = addstruct(t, pat);

        case Tybool: case Tychar: case Tybyte:
        case Tyint8: case Tyint16: case Tyint32: case Tyint:
        case Tyuint8: case Tyuint16: case Tyuint32: case Tyuint:
        case Tyint64: case Tyuint64: case Tylong:  case Tyulong:
        case Tyflt32: case Tyflt64:
        case Typtr: case Tyfunc:
            t = addlit(t, pat);
            break;

            */
        default:
            /* Right now, we just use this code for warning. */
            /*
            fatal(pat, "unsupported match type %s", tystr(ty));
            */
            return NULL;
            break;
    }

    return t;
}

static void checkcomprehensive(Dtree *dt)
{
}

static Node *genmatch(Dtree *dt)
{
    return NULL;
}

Node *gensimpmatch(Node *m)
{
    Dtree *t, *leaf;
    Node **pat, **cap;
    size_t npat, ncap;
    size_t i;

    t = mkdtree();
    pat = m->matchstmt.matches;
    npat = m->matchstmt.nmatches;
    cap = NULL;<commit_msg>Clean up match.c a bit.<commit_after>#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <stdarg.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include "parse.h"
#include "opt.h"

typedef struct Dtree Dtree;
struct Dtree {
    /* If the values are equal, go to 'sub'. If 'val' is null, anything matches. */
    Node **val;
    size_t nval;
    Dtree **sub;
    size_t nsub;
    Dtree *any;

    /* captured variables and action */
    Node **cap;
    size_t ncap;
    Node *act;

};

static Dtree *addpat(Dtree *t, Node *pat, Node ***cap, size_t *ncap);

static Dtree *mkdtree()
{
    return zalloc(sizeof(Dtree));
}

static int uconeq(Node *a, Node *b)
{
    return !strcmp(namestr(a), namestr(b));
}

static Dtree *addunion(Dtree *t, Node *pat, Node ***cap, size_t *ncap)
{
    Dtree *sub;
    size_t i;

    /* if we have the value already... */
    sub = NULL;
    for (i = 0; i < t->nval; i++) {
        if (!t->val[i])
            fatal(pat, "constructor already matched by earlier variable");
        if (uconeq(t->val[i], pat->expr.args[0])) {
            return addpat(t->sub[i], pat->expr.args[1], cap, ncap);
        }
    }

    sub = mkdtree();
    lappend(&t->val, &t->nval, pat->expr.args[0]);
    lappend(&t->sub, &t->nsub, sub);
    if (pat->expr.nargs == 2)
        sub = addpat(sub, pat->expr.args[1], cap, ncap);
    return sub;
}

static Dtree *addwild(Dtree *t, Node *var, Node ***cap, size_t *ncap)
{
    Node *dcl;

    dcl = decls[var->expr.did];
    if (dcl->decl.isconst)
        return NULL;
    if (t->any)
        return t->any;
    t->any = mkdtree();
    lappend(cap, ncap, var);
    return t->any;
}

static Dtree *addpat(Dtree *t, Node *pat, Node ***cap, size_t *ncap)
{
    Dtree *ret;

    if (pat == NULL)
        return t;
    switch (exprop(pat)) {
        case Ovar:
            ret = addwild(t, pat, cap, ncap);
            break;
        case Oucon:
            ret = addunion(t, pat, cap, ncap);
            break;
            /*
        case Olit:
            ret = addlit(t, pat, cap, ncap);
            break;
        case Otup:
            ret = addtup(t, pat, cap, ncap);
            break;
        case Oarr:
            ret = addarr(t, pat, cap, ncap);
            break;
            */
        default:
            /* Right now, we just use this code for warning.
             *
             * We shoudl fatal(unsupported match) here*/
            return NULL;
    }

    return ret;
}

static void checkcomprehensive(Dtree *dt)
{
}

static Node *genmatch(Dtree *dt)
{
    return NULL;
}

Node *gensimpmatch(Node *m)
{
    Dtree *t, *leaf;
    Node **pat, **cap;
    size_t npat, ncap;
    size_t i;

    t = mkdtree();
    pat = m->matchstmt.matches;
    npat = m->matchstmt.nmatches;
    cap = NULL;
{'fraction_non_alphanumeric': 0.1530880420499343, 'fraction_numerical': 0.00492772667542707, 'mean_word_length': 2.0924327069578466, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '167015', 'n_tokens_mistral': 2282, 'n_tokens_neox': 2241, 'n_words': 624}