-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.c
160 lines (124 loc) · 3.77 KB
/
interpreter.c
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
/** !
* Eddy interpreter
*
* @file interpreter.c
*
* @author Jacob Smith
*/
// Standard library
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
// array
#include <array/array.h>
// dict
#include <dict/dict.h>
// json
#include <json/json.h>
// list processor
#include <list_processor/list_processor.h>
/** !
* Parse command line arguments
*
* @param argc the argc parameter of the entry point
* @param argv the argv parameter of the entry point
*
* @return void on success, program abort on failure
*/
void parse_command_line_arguments ( int argc, const char *argv[] );
// Entry point
int main ( int argc, const char *argv[] )
{
// Initialized data
list_processor *p_list_processor = (void *) 0;
json_value *p_value = (void *) 0;
bool running = true;
char _program[1024] = { 0 };
// Parse command line arguments
parse_command_line_arguments(argc, argv);
// Construct a list constructor
lp_constructor(&p_list_processor, 4096);
// Read Eval Write loop
while ( running )
{
// Clear input buffer
memset(_program, 0, sizeof(_program));
static int y = 0;
fprintf(stderr, " >>> ", y++);
// Read line from standard in
if ( -1 == scanf(" %[^\n]", &_program) )
{
// EOF
running = false;
// Done
continue;
}
getchar();
// Parse the input
json_value_parse(_program, 0, &p_value);
// Evaluate the input
lp_eval(p_list_processor, p_value, &p_value);
// Release the input
//json_value_free(p_value);
switch (p_value->type)
{
case JSON_VALUE_STRING:
fprintf(stderr, "\033[96m\033[1m\033[4mstr\033[0m \033[03m\"%.15s\"\033[0m", p_value->string);
fprintf(stderr, "%s", p_value->string);
break;
case JSON_VALUE_BOOLEAN:
fprintf(stderr, "\033[96m\033[1m\033[4mbool\033[0m \033[03m%s\033[0m", p_value->boolean ? "true" : "false");
break;
case JSON_VALUE_INTEGER:
fprintf(stderr, "\033[96m\033[1m\033[4mint\033[0m \033[03m%d\033[0m", p_value->integer);
break;
case JSON_VALUE_NUMBER:
fprintf(stderr, "\033[96m\033[1m\033[4mnum\033[0m \033[03m%g\033[0m", p_value->number);
break;
case JSON_VALUE_OBJECT:
json_value_fprint(p_value, stderr);
break;
case JSON_VALUE_ARRAY:
{
for (size_t i = 0; i < array_size(p_value->list) - 1; i++)
{
json_value *p_xvalue = (void *) 0;
array_index(p_xvalue->list, i, (void **) &p_xvalue);
fprintf(stderr, "arr @%d : ", i);
json_value_fprint(p_xvalue, stderr);
}
}
break;
default:
break;
}
};
// End
fprintf(stderr, "\033[44m%s \033[0m", argv[0]);
// EOF?
if ( feof(stdin) ) fprintf(stderr, "\033[44m\033[0m\n");
// Success
return EXIT_SUCCESS;
}
void parse_command_line_arguments ( int argc, const char *argv[] )
{
// If no command line arguments are supplied, run all the examples
if ( argc == 1 ) return;
size_t mem = 0;
size_t scale = 1;
char unit = 0;
// Success
return;
// Error handling
{
// Argument errors
{
invalid_arguments:
// Print a usage message to standard out
//print_usage(argv[0]);
// Abort
exit(EXIT_FAILURE);
}
}
}