<commit_before>#include <stdio.h>
#include "memory.h"
#include "error.h"
#include "execpath.h"
#include "global.h"
#include "scanner.h"
#include <string.h>
#include "debugger.h"
#include "ast.h"
#include "vm.h"
#include "codegen.h"
#include "source.h"
#include "optimizer.h"

#ifdef _WIN32
char* readline(char* prompt) {
    fputs(prompt, stdout);
    char* cpy = safe_malloc(OP_INPUT_BUFFER_SIZE);
    fgets(cpy, OP_INPUT_BUFFER_SIZE, stdin);
    return cpy;
}

void add_history(char* prompt) {
    return;
}

void clear_console() {
    system("cls");
}
#else
#include <readline/readline.h>
#include <readline/history.h>

void clear_console() {
    system("clear");
}
#endif

// WendyScript Interpreter in C
// By: <NAME>
// 
// main.c: used to handle REPL and calling the interpreter on a file.

void invalid_usage() {
    printf("usage: wendy [file] [-help] [-nogc] [-c] [-ast] [-disassemble] "/*[-d file b1 b2 ...]*/"\n\n");
    printf("    file            : is either a compiled WendyScript file, or a raw source file.\n");
    printf("    -h, --help      : shows this message.\n");
    printf("    --nogc          : disables garbage-collection.\n");
    printf("    --noop          : disables optimization algorithm.\n");
    printf("    -c, --compile   : compiles the given file but does not run.\n");
    printf("    --ast           : prints out the constructed AST.\n");
    printf("    --toklst        : prints out the parsed tokens.\n");
    printf("    --disassemble   : prints out the disassembled bytecode.\n");
    printf("\nWendy will enter REPL mode if no parameters are supplied.\n");
//  printf("\n-d file b1 b2 ...\n   Enables debugging mode, with an output file and an initial set of breakpoints.");

    safe_exit(1);
}

void process_options(char** options, int len) {
    for (int i = 0; i < len; i++) {
        if (strcmp("-c", options[i]) == 0 
        ||  strcmp("--compile", options[i]) == 0) {
            set_settings_flag(SETTINGS_COMPILE);
        }
        /*else if (strcmp("-d", options[i]) == 0) {
            if (i == len - 1) {
                invalid_usage();
            }
            debug_output_path = options[i + 1];
            i++;
            for (; i < len; i++) {
                add_breakpoint(atoi(options[i]));
            }
        }*/ 
        else if (strcmp("--nogc", options[i]) == 0) {
            set_settings_flag(SETTINGS_NOGC);
        }
        else if (strcmp("--noop", options[i]) == 0) {
            set_settings_flag(SETTINGS_NOOP);
        }
        else if (strcmp("--ast", options[i]) == 0) {
            set_settings_flag(SETTINGS_ASTPRINT);
        }
        else if (strcmp("--toklst", options[i]) == 0) {
            set_settings_flag(SETTINGS_TOKEN_LIST_PRINT);
        }
        else if (strcmp("--disassemble", options[i]) == 0) {
            set_settings_flag(SETTINGS_DISASSEMBLE);
        }
        else {
            invalid_usage();
        }
    }
}

void run(char* input_string) {
    size_t alloc_size = 0;
    token* tokens;
    size_t tokens_count;

    // Scanning and Tokenizing
    tokens_count = scan_tokens(input_string, &tokens, &alloc_size);

    // Build AST
    statement_list* ast = generate_ast(tokens, tokens_count);
    if (!get_settings_flag(SETTINGS_NOOP)) {
        ast = optimize_ast(ast);
    }
    //print_ast(ast);   
    // Generate Bytecode
    //printf("%d\n", ast_error_flag());
    if(!ast_error_flag()) { 
        size_t size;
        uint8_t* bytecode = generate_code(ast, &size);
        vm_run(bytecode, size);
        safe_free(bytecode);
    }
    free_ast(ast);
    safe_free(tokens);
}

bool bracket_check(char* source) {
    if (!source[0]) return false;
    // Parentheses, Square, Curly, Single Quote, Double Quote
    int p = 0, s = 0, c = 0, sq = 0, dq = 0;
    for (int i = 0; source[i]; i++) {
        switch(source[i]) {
            case '(': p++; break;
            case ')': p--; break;
            case '[': s++; break;
            case ']': s--; break;
            case '{': c++; break;
            case '}': c--; break;
            case '"': dq++; break;
            case '\'': sq++; break;
        }
    }
    return !p && !s && !c && !(dq % 2) && !(sq % 2);
}

int main(int argc, char** argv) {
    init_memory();
    determine_endianness();
    if (argc == 1) {
        init_source(0, "", 0, false);
        clear_console();
        printf("Welcome to %s created by: <NAME>\n", WENDY_VERSION);
        printf("Run `wendy -help` to get help. \n");
        printf("Press Ctrl+D (EOF) to exit REPL.\n");
        char* path = get_path();
        printf("Path: %s\n", path);
        safe_free(path);
        char* input_buffer;
        char* source_to_run = safe_malloc(1 * sizeof(char));
        // ENTER REPL MODE
        set_settings_flag(SETTINGS_NOOP);
        set_settings_flag(SETTINGS_REPL);
        set_settings_flag(SETTINGS_STRICT_ERROR);
        push_frame("main", 0, 0);

        while (1) {
            size_t source_size = 1;
            source_to_run[0] = 0;
            bool first = true;
            // Perform bracket check to determine whether or not to execute:<commit_msg>disabled strict error on REPL<commit_after>#include <stdio.h>
#include "memory.h"
#include "error.h"
#include "execpath.h"
#include "global.h"
#include "scanner.h"
#include <string.h>
#include "debugger.h"
#include "ast.h"
#include "vm.h"
#include "codegen.h"
#include "source.h"
#include "optimizer.h"

#ifdef _WIN32
char* readline(char* prompt) {
    fputs(prompt, stdout);
    char* cpy = safe_malloc(OP_INPUT_BUFFER_SIZE);
    fgets(cpy, OP_INPUT_BUFFER_SIZE, stdin);
    return cpy;
}

void add_history(char* prompt) {
    return;
}

void clear_console() {
    system("cls");
}
#else
#include <readline/readline.h>
#include <readline/history.h>

void clear_console() {
    system("clear");
}
#endif

// WendyScript Interpreter in C
// By: <NAME>
// 
// main.c: used to handle REPL and calling the interpreter on a file.

void invalid_usage() {
    printf("usage: wendy [file] [-help] [-nogc] [-c] [-ast] [-disassemble] "/*[-d file b1 b2 ...]*/"\n\n");
    printf("    file            : is either a compiled WendyScript file, or a raw source file.\n");
    printf("    -h, --help      : shows this message.\n");
    printf("    --nogc          : disables garbage-collection.\n");
    printf("    --noop          : disables optimization algorithm.\n");
    printf("    -c, --compile   : compiles the given file but does not run.\n");
    printf("    --ast           : prints out the constructed AST.\n");
    printf("    --toklst        : prints out the parsed tokens.\n");
    printf("    --disassemble   : prints out the disassembled bytecode.\n");
    printf("\nWendy will enter REPL mode if no parameters are supplied.\n");
//  printf("\n-d file b1 b2 ...\n   Enables debugging mode, with an output file and an initial set of breakpoints.");

    safe_exit(1);
}

void process_options(char** options, int len) {
    for (int i = 0; i < len; i++) {
        if (strcmp("-c", options[i]) == 0 
        ||  strcmp("--compile", options[i]) == 0) {
            set_settings_flag(SETTINGS_COMPILE);
        }
        /*else if (strcmp("-d", options[i]) == 0) {
            if (i == len - 1) {
                invalid_usage();
            }
            debug_output_path = options[i + 1];
            i++;
            for (; i < len; i++) {
                add_breakpoint(atoi(options[i]));
            }
        }*/ 
        else if (strcmp("--nogc", options[i]) == 0) {
            set_settings_flag(SETTINGS_NOGC);
        }
        else if (strcmp("--noop", options[i]) == 0) {
            set_settings_flag(SETTINGS_NOOP);
        }
        else if (strcmp("--ast", options[i]) == 0) {
            set_settings_flag(SETTINGS_ASTPRINT);
        }
        else if (strcmp("--toklst", options[i]) == 0) {
            set_settings_flag(SETTINGS_TOKEN_LIST_PRINT);
        }
        else if (strcmp("--disassemble", options[i]) == 0) {
            set_settings_flag(SETTINGS_DISASSEMBLE);
        }
        else {
            invalid_usage();
        }
    }
}

void run(char* input_string) {
    size_t alloc_size = 0;
    token* tokens;
    size_t tokens_count;

    // Scanning and Tokenizing
    tokens_count = scan_tokens(input_string, &tokens, &alloc_size);

    // Build AST
    statement_list* ast = generate_ast(tokens, tokens_count);
    if (!get_settings_flag(SETTINGS_NOOP)) {
        ast = optimize_ast(ast);
    }
    //print_ast(ast);   
    // Generate Bytecode
    //printf("%d\n", ast_error_flag());
    if(!ast_error_flag()) { 
        size_t size;
        uint8_t* bytecode = generate_code(ast, &size);
        vm_run(bytecode, size);
        safe_free(bytecode);
    }
    free_ast(ast);
    safe_free(tokens);
}

bool bracket_check(char* source) {
    if (!source[0]) return false;
    // Parentheses, Square, Curly, Single Quote, Double Quote
    int p = 0, s = 0, c = 0, sq = 0, dq = 0;
    for (int i = 0; source[i]; i++) {
        switch(source[i]) {
            case '(': p++; break;
            case ')': p--; break;
            case '[': s++; break;
            case ']': s--; break;
            case '{': c++; break;
            case '}': c--; break;
            case '"': dq++; break;
            case '\'': sq++; break;
        }
    }
    return !p && !s && !c && !(dq % 2) && !(sq % 2);
}

int main(int argc, char** argv) {
    init_memory();
    determine_endianness();
    if (argc == 1) {
        init_source(0, "", 0, false);
        clear_console();
        printf("Welcome to %s created by: <NAME>\n", WENDY_VERSION);
        printf("Run `wendy -help` to get help. \n");
        printf("Press Ctrl+D (EOF) to exit REPL.\n");
        char* path = get_path();
        printf("Path: %s\n", path);
        safe_free(path);
        char* input_buffer;
        char* source_to_run = safe_malloc(1 * sizeof(char));
        // ENTER REPL MODE
        set_settings_flag(SETTINGS_NOOP);
        set_settings_flag(SETTINGS_REPL);        
        push_frame("main", 0, 0);

        while (1) {
            size_t source_size = 1;
            source_to_run[0] = 0;
            bool first = true;
            // Perform bracket check to determine whether or not to execute:
{'fraction_non_alphanumeric': 0.15720993934650754, 'fraction_numerical': 0.007630600665231853, 'mean_word_length': 2.149414664202095, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, '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': '2910042', 'n_tokens_mistral': 3445, 'n_tokens_neox': 3393, 'n_words': 870}