summaryrefslogtreecommitdiff
path: root/src/wren_inter/wren_inter.c
blob: a1b48e24186143ef5d2032a7910250c50e57cf71 (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
#include "wren_inter.h"
#include "../utils/stringBuilder.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);
            } break;
        case WREN_ERROR_STACK_TRACE:
            {
                TraceLog(LOG_ERROR, "[%s line %d] in %s", module, line, msg);
            } break;
        case WREN_ERROR_RUNTIME:
            {
                TraceLog(LOG_ERROR, "[Runtime Error] %s", msg);
            } break;
    }
}

void EvaluateScript(char *script) {
    TraceLog(LOG_INFO, "Evaluating %s...", script); 
    FreeStringBuilder(&stdout_sb);
    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;
    }

    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);
}