Download Game! Currently 73 players and visitors. Last logged in:CordobaDefaultCorrelPazi

Blitzer's Blog >> 72048

Back to blogs index
Posted: 05 Sep 2026 13:26 [ permalink ]
/ tests/repl.c
void create() {
    printf("Type 'help' for a list of commands.
");
}

void process_repl_command_line(string input) {
    string cmd, arg;
    
    // Parse command vs argument
    if (sscanf(input, "%s %s", cmd, arg) != 2) {
        cmd = input;
    }

    if (cmd == "help") {
        printf("Available commands:
");
        printf("  help          - Show this message
");
        printf("  time          - Display system time
");
        printf("  md5 <string>  - Hash a string
");
        printf("  eval <lpc>    - Evaluate LPC expression dynamically
");
        printf("  clear         - Clear the screen (handled by frontend)
");
    } 
    else if (cmd == "time") {
        printf("System uptime: %d seconds
", uptime());
        printf("Current time: %s
", ctime(time()));
    }
    else if (cmd == "md5" && arg) {
        printf("MD5('%s') = %s
", arg, md5(arg));
    }
    else if (cmd == "eval" && arg) {
        // Use a random filename to bypass the JIT blueprint cache
        string filename = "tmp_eval_" + random(9999999) + ".c";
        
        // Wrap the user's expression inside a valid LPC function
        string src = "mixed do_eval() { return (" + arg + "); }
";
        
        if (write_file(filename, src)) {
            object ob = clone_object(filename);
            if (ob) {
                mixed res = ob->do_eval();
                printf("Result: %s
", res);
                destruct(ob);
            } else {
                printf("Error: Failed to compile expression.
");
            }
            rm(filename); // Clean up the VFS
        } else {
            printf("Error: Failed to write temp file to VFS.
");
        }
    }
    else if (cmd == "") {
        // Do nothing on empty enter
    }
    else {
        printf("Unrecognized command: %s
", cmd);
    }
}

==