-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagate-cli.c
More file actions
82 lines (65 loc) · 2 KB
/
agate-cli.c
File metadata and controls
82 lines (65 loc) · 2 KB
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
// SPDX-License-Identifier: MIT
// Copyright (c) 2022 Julien Bernard
#include <stdio.h>
#include <stdlib.h>
#include "agate.h"
#include "agate-support.h"
#include "agate-std.h"
#include "config.h"
static void print(AgateVM *vm, const char* text) {
fputs(text, stdout);
}
static void write(AgateVM *vm, uint8_t byte) {
fputc(byte, stdout);
}
static void error(AgateVM *vm, AgateErrorKind kind, const char *unit_name, int line, const char *message) {
switch (kind) {
case AGATE_ERROR_COMPILE:
printf("[%s:%d]: error: %s\n", unit_name, line, message);
break;
case AGATE_ERROR_RUNTIME:
printf("Error: %s\n", message);
break;
case AGATE_ERROR_STACKTRACE:
printf("from [%s:%d]: %s\n", unit_name, line, message);
break;
}
}
static bool input(AgateVM *vm, char *buffer, size_t size) {
return fgets(buffer, size, stdin) != NULL;
}
int main(int argc, const char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: agate <script> [...]\n");
return EXIT_FAILURE;
}
AgateConfig config;
agateConfigInitialize(&config);
config.unit_handler = agateExUnitHandler;
config.foreign_class_handler = agateExForeignClassHandler;
config.foreign_method_handler = agateExForeignMethodHandler;
config.print = print;
config.write = write;
config.error = error;
config.input = input;
AgateVM *vm = agateExNewVM(&config);
agateExUnitAddIncludePath(vm, AGATE_UNIT_DIRECTORY);
agateExUnitAddIncludePath(vm, ".");
agateStdConfigureClassHandlers(vm);
agateStdConfigureMethodHandlers(vm);
const char *source = agateExUnitLoad(vm, argv[1]);
int return_code = EXIT_SUCCESS;
if (source != NULL) {
agateSetArgs(vm, argc - 1, argv + 1);
AgateStatus status = agateCallString(vm, argv[1], source);
agateExUnitRelease(vm, source);
if (status != AGATE_STATUS_OK) {
return_code = EXIT_FAILURE;
}
} else {
fprintf(stderr, "Unknown unit: '%s'\n", argv[1]);
return_code = EXIT_FAILURE;
}
agateExDeleteVM(vm);
return return_code;
}