aboutsummaryrefslogtreecommitdiffstats
path: root/amend/execute.c
blob: 9162ad647c414d04c40a6bf026ea30dc973c7158 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#undef NDEBUG
#include <assert.h>
#include "ast.h"
#include "execute.h"

typedef struct {
    int c;
    const char **v;
} StringList;

static int execBooleanValue(ExecContext *ctx,
        const AmBooleanValue *booleanValue, bool *result);
static int execStringValue(ExecContext *ctx, const AmStringValue *stringValue,
        const char **result);

static int
execBooleanExpression(ExecContext *ctx,
        const AmBooleanExpression *booleanExpression, bool *result)
{
    int ret;
    bool arg1, arg2;
    bool unary;

    assert(ctx != NULL);
    assert(booleanExpression != NULL);
    assert(result != NULL);
    if (ctx == NULL || booleanExpression == NULL || result == NULL) {
        return -__LINE__;
    }

    if (booleanExpression->op == AM_BOP_NOT) {
        unary = true;
    } else {
        unary = false;
    }

    ret = execBooleanValue(ctx, booleanExpression->arg1, &arg1);
    if (ret != 0) return ret;

    if (!unary) {
        ret = execBooleanValue(ctx, booleanExpression->arg2, &arg2);
        if (ret != 0) return ret;
    } else {
        arg2 = false;
    }

    switch (booleanExpression->op) {
    case AM_BOP_NOT:
        *result = !arg1;
        break;
    case AM_BOP_EQ:
        *result = (arg1 == arg2);
        break;
    case AM_BOP_NE:
        *result = (arg1 != arg2);
        break;
    case AM_BOP_AND:
        *result = (arg1 && arg2);
        break;
    case AM_BOP_OR:
        *result = (arg1 || arg2);
        break;
    default:
        return -__LINE__;
    }

    return 0;
}

static int
execFunctionArguments(ExecContext *ctx,
        const AmFunctionArguments *functionArguments, StringList *result)
{
    int ret;

    assert(ctx != NULL);
    assert(functionArguments != NULL);
    assert(result != NULL);
    if (ctx == NULL || functionArguments == NULL || result == NULL) {
        return -__LINE__;
    }

    result->c = functionArguments->argc;
    result->v = (const char **)malloc(result->c * sizeof(const char *));
    if (result->v == NULL) {
        result->c = 0;
        return -__LINE__;
    }

    int i;
    for (i = 0; i < functionArguments->argc; i++) {
        ret = execStringValue(ctx, &functionArguments->argv[i], &result->v[i]);
        if (ret != 0) {
            result->c = 0;
            free(result->v);
            //TODO: free the individual args, if we're responsible for them.
            result->v = NULL;
            return ret;
        }
    }

    return 0;
}

static int
execFunctionCall(ExecContext *ctx, const AmFunctionCall *functionCall,
        const char **result)
{
    int ret;

    assert(ctx != NULL);
    assert(functionCall != NULL);
    assert(result != NULL);
    if (ctx == NULL || functionCall == NULL || result == NULL) {
        return -__LINE__;
    }

    StringList args;
    ret = execFunctionArguments(ctx, functionCall->args, &args);
    if (ret != 0) {
        return ret;
    }

    ret = callFunction(functionCall->fn, args.c, args.v, (char **)result, NULL);
    if (ret != 0) {
        return ret;
    }

    //TODO: clean up args

    return 0;
}

static int
execStringValue(ExecContext *ctx, const AmStringValue *stringValue,
        const char **result)
{
    int ret;

    assert(ctx != NULL);
    assert(stringValue != NULL);
    assert(result != NULL);
    if (ctx == NULL || stringValue == NULL || result == NULL) {
        return -__LINE__;
    }

    switch (stringValue->type) {
    case AM_SVAL_LITERAL:
        *result = strdup(stringValue->u.literal);
        break;
    case AM_SVAL_FUNCTION:
        ret = execFunctionCall(ctx, stringValue->u.function, result);
        if (ret != 0) {
            return ret;
        }
        break;
    default:
        return -__LINE__;
    }

    return 0;
}

static int
execStringComparisonExpression(ExecContext *ctx,
        const AmStringComparisonExpression *stringComparisonExpression,
        bool *result)
{
    int ret;

    assert(ctx != NULL);
    assert(stringComparisonExpression != NULL);
    assert(result != NULL);
    if (ctx == NULL || stringComparisonExpression == NULL || result == NULL) {
        return -__LINE__;
    }

    const char *arg1, *arg2;
    ret = execStringValue(ctx, stringComparisonExpression->arg1, &arg1);
    if (ret != 0) {
        return ret;
    }
    ret = execStringValue(ctx, stringComparisonExpression->arg2, &arg2);
    if (ret != 0) {
        return ret;
    }

    int cmp = strcmp(arg1, arg2);

    switch (stringComparisonExpression->op) {
    case AM_SOP_LT:
        *result = (cmp < 0);
        break;
    case AM_SOP_LE:
        *result = (cmp <= 0);
        break;
    case AM_SOP_GT:
        *result = (cmp > 0);
        break;
    case AM_SOP_GE:
        *result = (cmp >= 0);
        break;
    case AM_SOP_EQ:
        *result = (cmp == 0);
        break;
    case AM_SOP_NE:
        *result = (cmp != 0);
        break;
    default:
        return -__LINE__;
        break;
    }

    return 0;
}

static int
execBooleanValue(ExecContext *ctx, const AmBooleanValue *booleanValue,
        bool *result)
{
    int ret;

    assert(ctx != NULL);
    assert(booleanValue != NULL);
    assert(result != NULL);
    if (ctx == NULL || booleanValue == NULL || result == NULL) {
        return -__LINE__;
    }

    switch (booleanValue->type) {
    case AM_BVAL_EXPRESSION:
        ret = execBooleanExpression(ctx, &booleanValue->u.expression, result);
        break;
    case AM_BVAL_STRING_COMPARISON:
        ret = execStringComparisonExpression(ctx,
                &booleanValue->u.stringComparison, result);
        break;
    default:
        ret = -__LINE__;
        break;
    }

    return ret;
}

static int
execCommand(ExecContext *ctx, const AmCommand *command)
{
    int ret;

    assert(ctx != NULL);
    assert(command != NULL);
    if (ctx == NULL || command == NULL) {
        return -__LINE__;
    }

    CommandArgumentType argType;
    argType = getCommandArgumentType(command->cmd);
    switch (argType) {
    case CMD_ARGS_BOOLEAN:
        {
            bool bVal;
            ret = execBooleanValue(ctx, command->args->u.b, &bVal);
            if (ret == 0) {
                ret = callBooleanCommand(command->cmd, bVal);
            }
        }
        break;
    case CMD_ARGS_WORDS:
        {
            AmWordList *words = command->args->u.w;
            ret = callCommand(command->cmd, words->argc, words->argv);
        }
        break;
    default:
        ret = -__LINE__;
        break;
    }

    return ret;
}

int
execCommandList(ExecContext *ctx, const AmCommandList *commandList)
{
    int i;
    for (i = 0; i < commandList->commandCount; i++) {
        int ret = execCommand(ctx, commandList->commands[i]);
        if (ret != 0) {
            int line = commandList->commands[i]->line;
            return line > 0 ? line : ret;
        }
    }

    return 0;
}