summaryrefslogtreecommitdiff
path: root/src/wren_inter/wren_inter.c
blob: 69df112a196185aede5cd10df0205c40ec6b18d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "wren_inter.h"
#include "../utils/stringBuilder.h"
#include <string.h> 
#include <raylib.h>
#include <stdio.h>
#include <wren.h>

StringBuilder stdout_sb = {0};

void writeFn(WrenVM* vm, const char* text) {
    AppendStringBuilder(&stdout_sb, text, '\n');
}

void errorFn(WrenVM* vm, WrenErrorType errorType,
        const char* module, const int line,
        const char* msg)
{
    switch (errorType)
    {
        case WREN_ERROR_COMPILE:
            {
                // TraceLog(LOG_ERROR, "[%s line %d] [Error] %s", module, line, msg);
                AppendStringBuilder(&stdout_sb, "Comptime error: ", '\n');
                AppendStringBuilder(&stdout_sb, msg, ' ');
            } break;
        case WREN_ERROR_STACK_TRACE:
            {
                AppendStringBuilder(&stdout_sb, "Stack trace: ", '\n');
                AppendStringBuilder(&stdout_sb, msg, ' ');
                // TraceLog(LOG_ERROR, "[%s line %d] in %s", module, line, msg);
            } break;
        case WREN_ERROR_RUNTIME:
            {
                AppendStringBuilder(&stdout_sb, "Runtime error: ", '\n');
                AppendStringBuilder(&stdout_sb, msg, ' ');
                // TraceLog(LOG_ERROR, "[Runtime Error] %s", msg);
            } break;
    }
}

char *EvaluateScript(char *script) {
    TraceLog(LOG_INFO, "Evaluating %s...", script); 
    WrenConfiguration config;
    wrenInitConfiguration(&config);
    config.writeFn = writeFn;
    config.errorFn = errorFn;

    WrenVM* vm = wrenNewVM(&config);
    if (vm == NULL) {
        TraceLog(LOG_ERROR, "Failed to initialize a wren vm");
        return NULL;
    }

    WrenInterpretResult result = wrenInterpret(vm, "main", script);
    switch (result) {
        case WREN_RESULT_SUCCESS: break;
        case WREN_RESULT_COMPILE_ERROR: break;
        case WREN_RESULT_RUNTIME_ERROR: break;
    }
    printf("Wren outp: %s", stdout_sb.val);
    wrenFreeVM(vm);

    if (stdout_sb.val == NULL) return NULL;
    char *outp = strdup(stdout_sb.val);
    FreeStringBuilder(&stdout_sb);
    return outp;
}