kgabis / ape

Ape Programming Language
262 stars 20 forks source link

Collision / compile error on Windows #12

Closed blakepell closed 1 year ago

blakepell commented 1 year ago

I was trying to get this to run on both Windows and Linux, and in Visual Studio I was getting a compile error on the enum error_type where there was a collision on ERROR_TIMEOUT with something in Windows.h. Just to test I changed all copies of that to be ERROR_TIME_OUT and it ran compiled/ran perfectly after.

I've looked at a lot of C based scripting languages over the last few weeks and I really like how well put together this is and how easily it's called via an interop scenario with C.

Since you asked someone else I thought I'd share, for fun, I work on MUDs (multi user dimensions, text based D&D type games online, were very popular in the 90's, the one I play still has hundreds of players). One of the most popular code bases are "Diku Muds" which have spawned thousands of forks/games. Most DikuMuds or their decedents have a VERY bare bones scripting language called mud progs or mob progs to allow the NPCs to crudely interact with players (they have basic control flow and access to some in game features, but no variable storage, etc.). Some muds have swapped that out for Lua (which is a big task and one so big that most people who do it don't share it which is a shame.. Lua is great but it also requires an insane amount of glue code and a lot of diligent managing the interop / using the stack correctly).

I was considering trying to swap this in either side by side or in replacement of mob progs in one of the open source code bases (probably ROM 2.4 but it's the one I know the best and plays the most like I expect a mud to play out of the box: https://github.com/avinson/rom24-quickmud). If I can do it successfully, I'll let you know.

blakepell commented 1 year ago

Also, thank you for sharing your source. I really like how easy it is to extend the language, added my first function in a few minutes. :)

static object_t indexof_fn(vm_t *vm, void *data, int argc, object_t *args) {
    int start_index = argc == 3 && object_get_type(args[2]) == OBJECT_NUMBER ? object_get_number(args[2]) : 0;

    if ((argc == 2 || argc == 3)
        && object_get_type(args[0]) == OBJECT_STRING
        && object_get_type(args[1]) == OBJECT_STRING)
    {
        const char *search_str = object_get_string(args[0]);
        const char *search_for = object_get_string(args[1]);
        char *result = strstr(search_str + start_index, search_for);

        if (result == NULL)
        {
            return object_make_number(-1);
        }

        return object_make_number(result - search_str);
    }

    return object_make_number(-1);
}
kgabis commented 1 year ago

Hi, it should be fixed now. In hindsight including windows.h maybe wasn't the best idea but I didn't want to burden users with setting up their own time functions.

I'm happy that you're finding ape easy to use, thanks for sharing your use case! Please do let me know how it went.