<commit_before>#include "lily_impl.h"
#include "lily_symtab.h"
#include "lily_opcode.h"

static char *typename(lily_sym *sym)
{
    char *ret;

    if (sym->flags & VAR_SYM)
        ret = "var";
    else if (sym->flags & LITERAL_SYM)
        ret = "literal";
    else if (sym->flags & STORAGE_SYM)
        ret = "storage";
    else
        ret = NULL;

    return ret;
}

static void print_two(int *code, char *str, int i)
{
    lily_sym *left, *right;

    lily_impl_debugf(str, i);
    left = ((lily_sym *)code[i+1]);
    right = ((lily_sym *)code[i+2]);
    lily_impl_debugf("[%s #%d] [%s #%d]\n",
                     typename((lily_sym *)left), left->id,
                     typename((lily_sym *)right), right->id);
}

static void print_three(int *code, char *str, int i)
{
    lily_sym *left, *right, *result;

    lily_impl_debugf(str, i);
    left = ((lily_sym *)code[i+1]);
    right = ((lily_sym *)code[i+2]);
    result = ((lily_sym *)code[i+3]);
    lily_impl_debugf("[%s #%d] [%s #%d] [%s #%d]\n",
                     typename((lily_sym *)left), left->id,
                     typename((lily_sym *)right), right->id,
                     typename((lily_sym *)result), result->id);
}

static void show_code(lily_var *var)
{
    int i, len;
    int *code;
    lily_method_val *m;
    lily_sym *left;

    m = (lily_method_val *)var->value.ptr;
    i = 0;
    code = m->code;
    len = m->pos;

    while (i < len) {
        switch (code[i]) {
            case o_assign:
                print_two(code, "    [%d] assign        ", i);
                i += 3;
                break;
            case o_obj_assign:
                print_two(code, "    [%d] obj_assign    ", i);
                i += 3;
                break;
            case o_integer_add:
                print_three(code, "    [%d] integer_add   ", i);
                i += 4;
                break;
            case o_integer_minus:
                print_three(code, "    [%d] integer_minus ", i);
                i += 4;
                break;
            case o_number_add:
                print_three(code, "    [%d] number_add    ", i);
                i += 4;
                break;
            case o_number_minus:
                print_three(code, "    [%d] number_minus  ", i);
                i += 4;
                break;
            case o_less:
                print_three(code, "    [%d] less          ", i);
                i += 4;
                break;
            case o_less_eq:
                print_three(code, "    [%d] less_equal    ", i);
                i += 4;
                break;
            case o_is_equal:
                print_three(code, "    [%d] is_equal      ", i);
                i += 4;
                break;
            case o_greater:
                print_three(code, "    [%d] greater       ", i);
                i += 4;
                break;
            case o_greater_eq:
                print_three(code, "    [%d] greater_equal ", i);
                i += 4;
                break;
            case o_jump:
                lily_impl_debugf("    [%d] jump          ", i);
                lily_impl_debugf("[%d].\n", code[i+1]);
                i += 2;
                break;
            case o_jump_if_false:
                lily_impl_debugf("    [%d] jump_if_false ", i);
                left = ((lily_sym *)code[i+1]);
                lily_impl_debugf("%s #%d -> [%d].\n",
                                 typename((lily_sym *)left), left->id,
                                 code[i+2]);
                i += 3;
                break;
            case o_func_call:
                {
                int j;
                lily_var *var;

                /* var, func, #args, args... */
                var = (lily_var *)code[i+1];
                lily_impl_debugf("    [%d] func_call  \n        ", i);

                if (var->line_num == 0)
                    lily_impl_debugf("name: (builtin) %s.\n        args:\n",
                                     var->name, code[i+3]);
                else
                    lily_impl_debugf("name: %s (from line %d).\n        args:\n",
                                     var->name, var->line_num, code[i+3]);

                for (j = 0;j < code[i+3];j++) {
                    lily_sym *sym = (lily_sym *)code[i+4+j];
                    lily_impl_debugf("            #%d: %s #%d\n", j+1,
                                     typename(sym), sym->id);
                }

                i += 4 + j;
                }
                break;
            case o_method_call:
                {
                int j;
                lily_var *var;

                /* var, func, #args, args... */
                var = (lily_var *)code[i+1];
                lily_impl_debugf("    [%d] func_call  \n        ", i);

                if (var->line_num == 0)
                    lily_impl_debugf("name: (builtin) %s.\n        args:\n",
                                     var->name, code[i+3]);
                else
                    lily_impl_debugf("name: %s (from line %d).\n        args:\n",
                                     var->name, var->line_num, code[i+3]);

                for (j = 0;j < code[i+3];j++) {
                    lily_sym *sym = (lily_sym *)code[i+4+j];
                    lily_impl_debugf("            #%d: %s #%d\n", j+1,
                                     typename(sym), sym->id);
                }

                i += 4 + j;
                }
                break;
            case o_vm_return:
                lily_impl_debugf("    [%d] vm_return\n", i);
                return;
            default:
                lily_impl_debugf("    [%d] bad opcode %d.\n", i, code[i]);
                return;
        }
    }
}

static void show_sym(lily_sym *sym)
{
    if (!(sym->flags & S_IS_NIL)) {
        int cls_id;
        if (sym->sig->cls->id != SYM_CLASS_OBJECT)
            cls_id = sym->sig->cls->id;
        else
            cls_id = sym->sig->node.value_sig->cls->id;

        if (cls_id == SYM_CLASS_STR) {
            lily_impl_debugf("str(%-0.50s)\n",
                            ((lily_strval *)sym->value.ptr)->str);
        }
        else if (cls_id == SYM_CLASS_INTEGER)
            lily_impl_debugf("integer(%d)\n", sym->value.integer);
        else if (cls_id == SYM_CLASS_NUMBER)
            lily_impl_debugf("number(%f)\n", sym->value.number);
    }
    else<commit_msg>debug: Add print_call, to print debug for functions and methods.<commit_after>#include "lily_impl.h"
#include "lily_symtab.h"
#include "lily_opcode.h"

static char *typename(lily_sym *sym)
{
    char *ret;

    if (sym->flags & VAR_SYM)
        ret = "var";
    else if (sym->flags & LITERAL_SYM)
        ret = "literal";
    else if (sym->flags & STORAGE_SYM)
        ret = "storage";
    else
        ret = NULL;

    return ret;
}

static void print_two(int *code, char *str, int i)
{
    lily_sym *left, *right;

    lily_impl_debugf(str, i);
    left = ((lily_sym *)code[i+1]);
    right = ((lily_sym *)code[i+2]);
    lily_impl_debugf("[%s #%d] [%s #%d]\n",
                     typename((lily_sym *)left), left->id,
                     typename((lily_sym *)right), right->id);
}

static void print_three(int *code, char *str, int i)
{
    lily_sym *left, *right, *result;

    lily_impl_debugf(str, i);
    left = ((lily_sym *)code[i+1]);
    right = ((lily_sym *)code[i+2]);
    result = ((lily_sym *)code[i+3]);
    lily_impl_debugf("[%s #%d] [%s #%d] [%s #%d]\n",
                     typename((lily_sym *)left), left->id,
                     typename((lily_sym *)right), right->id,
                     typename((lily_sym *)result), result->id);
}

static void print_call(int *code, char *str, int i)
{
    int j;
    lily_var *var;

    /* For methods, this is:   var, method_val, #args, args
       For functions, this is: var, func_ptr,   #args, args */

    var = (lily_var *)code[i+1];
    lily_impl_debugf(str, i);

    if (var->line_num == 0)
        lily_impl_debugf("name: (builtin) %s.\n        args:\n",
                         var->name, code[i+3]);
    else
        lily_impl_debugf("name: %s (from line %d).\n        args:\n",
                         var->name, var->line_num, code[i+3]);

    for (j = 0;j < code[i+3];j++) {
        lily_sym *sym = (lily_sym *)code[i+4+j];
        lily_impl_debugf("            #%d: %s #%d\n", j+1,
                         typename(sym), sym->id);
    }
}

static void show_code(lily_var *var)
{
    int i, len;
    int *code;
    lily_method_val *m;
    lily_sym *left;

    m = (lily_method_val *)var->value.ptr;
    i = 0;
    code = m->code;
    len = m->pos;

    while (i < len) {
        switch (code[i]) {
            case o_assign:
                print_two(code, "    [%d] assign        ", i);
                i += 3;
                break;
            case o_obj_assign:
                print_two(code, "    [%d] obj_assign    ", i);
                i += 3;
                break;
            case o_integer_add:
                print_three(code, "    [%d] integer_add   ", i);
                i += 4;
                break;
            case o_integer_minus:
                print_three(code, "    [%d] integer_minus ", i);
                i += 4;
                break;
            case o_number_add:
                print_three(code, "    [%d] number_add    ", i);
                i += 4;
                break;
            case o_number_minus:
                print_three(code, "    [%d] number_minus  ", i);
                i += 4;
                break;
            case o_less:
                print_three(code, "    [%d] less          ", i);
                i += 4;
                break;
            case o_less_eq:
                print_three(code, "    [%d] less_equal    ", i);
                i += 4;
                break;
            case o_is_equal:
                print_three(code, "    [%d] is_equal      ", i);
                i += 4;
                break;
            case o_greater:
                print_three(code, "    [%d] greater       ", i);
                i += 4;
                break;
            case o_greater_eq:
                print_three(code, "    [%d] greater_equal ", i);
                i += 4;
                break;
            case o_jump:
                lily_impl_debugf("    [%d] jump          ", i);
                lily_impl_debugf("[%d].\n", code[i+1]);
                i += 2;
                break;
            case o_jump_if_false:
                lily_impl_debugf("    [%d] jump_if_false ", i);
                left = ((lily_sym *)code[i+1]);
                lily_impl_debugf("%s #%d -> [%d].\n",
                                 typename((lily_sym *)left), left->id,
                                 code[i+2]);
                i += 3;
                break;
            case o_func_call:
                /* For both functions and methods, [i+3] is the # of args. */
                print_call(code, "    [%d] func_call  \n        ", i);
                i += 4 + code[i+3];
                break;
            case o_method_call:
                print_call(code, "    [%d] method_call\n        ", i);
                i += 4 + code[i+3];
                break;
            case o_vm_return:
                lily_impl_debugf("    [%d] vm_return\n", i);
                return;
            default:
                lily_impl_debugf("    [%d] bad opcode %d.\n", i, code[i]);
                return;
        }
    }
}

static void show_sym(lily_sym *sym)
{
    if (!(sym->flags & S_IS_NIL)) {
        int cls_id;
        if (sym->sig->cls->id != SYM_CLASS_OBJECT)
            cls_id = sym->sig->cls->id;
        else
            cls_id = sym->sig->node.value_sig->cls->id;

        if (cls_id == SYM_CLASS_STR) {
            lily_impl_debugf("str(%-0.50s)\n",
                            ((lily_strval *)sym->value.ptr)->str);
        }
        else if (cls_id == SYM_CLASS_INTEGER)
            lily_impl_debugf("integer(%d)\n", sym->value.integer);
        else if (cls_id == SYM_CLASS_NUMBER)
            lily_impl_debugf("number(%f)\n", sym->value.number);
    }
    else
{'fraction_non_alphanumeric': 0.1432349780973634, 'fraction_numerical': 0.006694768162658071, 'mean_word_length': 1.2189620392444527, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 74, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4839549', 'n_tokens_mistral': 4160, 'n_tokens_neox': 3924, 'n_words': 836}