<commit_before>#include "blaze.h"

static int var_counter = 0;

Var* var_new(Decl* owner, Instr* ir, Type* type, String* name) {
    Var* res = new(Var);
    res->id = var_counter++;
    res->uses = 0;
    res->owner = owner;
    res->ir = ir;
    res->type = type;
    if (name) res->name = string_clone(name);
    list_append(owner->vars, res);
    return res;
}

void var_dump(Var* v) {
    assert(v);
    printf("Var %d", v->id);
    if (v->name) printf(" (%s)", v->name->str);
}

void var_free(Var* v) {
    if (v->name) string_free(v->name);
    free(v);
}

void instr_dump(Instr* ir) {
    int i;
    assert(ir);
    switch (ir->kind) {
    case Iret: printf("Iret"); break;
    case Inew: printf("Inew"); break;
    case Iset: printf("Iset"); break;
    case Ivar: printf("Ivar"); break;
    case Ideref: printf("Ideref"); break;
    case Iaddr: printf("Iaddr"); break;
    case Icall: printf("Icall"); break;
    case Iint: printf("Iint (i:%s)", ir->s->str); break;
    }

    if (ir->v) {
        printf(" of (");
        for (i=0; i<list_len(ir->v); ++i) {
            if (i) printf(", ");
            var_dump(ir->v[i]);
        }
        putchar(')');
    }

    if (ir->dst) {
        printf(" -> ");
        var_dump(ir->dst);
        printf(" used %d time", ir->dst->uses);
        if (ir->dst->uses > 1) putchar('s');
    }

    putchar('\n');
}

void instr_free(Instr* ir) {
    switch (ir->kind) {
    case Iint: string_free(ir->s); break;
    default: break;
    }
    free(ir);
}

void decl_dump(Decl* f) {
    int i;
    printf("Decltion %d %s:\n", f->id, f->name->str);
    for (i=0; i<list_len(f->sons); ++i) {
        printf("  ");
        instr_dump(f->sons[i]);
    }
}

void decl_free(Decl* f) {
    int i;
    for (i=0; i<list_len(f->sons); ++i) {
        list_free(f->sons[i]->v);
        instr_free(f->sons[i]);
    }
    for (i=0; i<list_len(f->vars); ++i)
        if (f->vars[i]->owner == f && f->vars[i]->ir) var_free(f->vars[i]);
    for (i=0; i<list_len(f->args); ++i) var_free(f->args[i]);
    list_free(f->sons);
    list_free(f->vars);
    list_free(f->args);<commit_msg>How did I not notice this before...<commit_after>#include "blaze.h"

static int var_counter = 0;

Var* var_new(Decl* owner, Instr* ir, Type* type, String* name) {
    Var* res = new(Var);
    res->id = var_counter++;
    res->uses = 0;
    res->owner = owner;
    res->ir = ir;
    res->type = type;
    if (name) res->name = string_clone(name);
    list_append(owner->vars, res);
    return res;
}

void var_dump(Var* v) {
    assert(v);
    printf("Var %d", v->id);
    if (v->name) printf(" (%s)", v->name->str);
}

void var_free(Var* v) {
    if (v->name) string_free(v->name);
    free(v);
}

void instr_dump(Instr* ir) {
    int i;
    assert(ir);
    switch (ir->kind) {
    case Iret: printf("Iret"); break;
    case Inew: printf("Inew"); break;
    case Iset: printf("Iset"); break;
    case Ivar: printf("Ivar"); break;
    case Ideref: printf("Ideref"); break;
    case Iaddr: printf("Iaddr"); break;
    case Icall: printf("Icall"); break;
    case Iint: printf("Iint (i:%s)", ir->s->str); break;
    }

    if (ir->v) {
        printf(" of (");
        for (i=0; i<list_len(ir->v); ++i) {
            if (i) printf(", ");
            var_dump(ir->v[i]);
        }
        putchar(')');
    }

    if (ir->dst) {
        printf(" -> ");
        var_dump(ir->dst);
        printf(" used %d time", ir->dst->uses);
        if (ir->dst->uses != 1) putchar('s');
    }

    putchar('\n');
}

void instr_free(Instr* ir) {
    switch (ir->kind) {
    case Iint: string_free(ir->s); break;
    default: break;
    }
    free(ir);
}

void decl_dump(Decl* f) {
    int i;
    printf("Decl %d %s:\n", f->id, f->name->str);
    for (i=0; i<list_len(f->sons); ++i) {
        printf("  ");
        instr_dump(f->sons[i]);
    }
}

void decl_free(Decl* f) {
    int i;
    for (i=0; i<list_len(f->sons); ++i) {
        list_free(f->sons[i]->v);
        instr_free(f->sons[i]);
    }
    for (i=0; i<list_len(f->vars); ++i)
        if (f->vars[i]->owner == f && f->vars[i]->ir) var_free(f->vars[i]);
    for (i=0; i<list_len(f->args); ++i) var_free(f->args[i]);
    list_free(f->sons);
    list_free(f->vars);
    list_free(f->args);
{'fraction_non_alphanumeric': 0.2208067940552017, 'fraction_numerical': 0.00377447511205473, 'mean_word_length': 2.4754098360655736, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 100, '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': '5950116', 'n_tokens_mistral': 1714, 'n_tokens_neox': 1694, 'n_words': 365}