hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/eval.c
blob: 58415664886cce3718ee4951981c974adbace4fa (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
68
69
70
71
72
73
#include <assert.h>
#include <stdio.h>

#include "eval.h"

static int evaluate_value(const struct Node* node, long* out)
{
    switch (node->kind)
    {
        case VALUE_DIFFERENCE:
            *out = (long)node->first->character - (long)node->second->character;
            return 0;
        default: break;
    }

    assert(!"the parser handed a non-value where a value goes");
    return 1;
}

static int evaluate_type(const struct Node* node, struct Type* out)
{
    out->kind = KIND_VOID;
    out->number = 0;
    switch (node->kind)
    {
        case TYPE_DECLTYPE:
            if (evaluate_value(node->first, &out->number))
                return 1;
            out->kind = KIND_VALUE;
            return 0;
        default: break;
    }

    assert(!"the parser handed a non-type where a type goes");
    return 1;
}

static enum RunStatus emit(const struct Node* node)
{
    struct Type type;
    if (evaluate_type(node->first, &type))
        return RUN_FAILED;

    if (type.kind == KIND_VALUE)
        (void)printf("%ld\n", type.number);
    return RUN_DONE;
}

static enum RunStatus execute_one(const struct Node* node)
{
    switch (node->kind)
    {
        case STMT_EMIT: return emit(node);
        default: break;
    }

    assert(!"the parser handed a non-statement where a statement goes");
    return RUN_FAILED;
}

enum RunStatus execute(const struct Node* statement)
{
    while (statement != NULL)
    {
        enum RunStatus status = execute_one(statement);
        if (status != RUN_DONE)
            return status;

        statement = statement->next;
    }

    return RUN_DONE;
}