apfl/src/main.c

83 lines
1.7 KiB
C
Raw Normal View History

2021-12-10 20:22:16 +00:00
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include "apfl.h"
static bool
repl_source_reader(void *context, char *buf, size_t *len, bool need)
{
(void)context;
printf(need ? "... " : "> ");
fflush(stdout);
size_t maxlen = *len;
if (fgets(buf, maxlen, stdin) == NULL) {
if (feof(stdin)) {
*len = 0;
return true;
} else {
return false;
}
}
*len = strlen(buf);
return true;
}
int
main(int argc, const char **argv)
{
(void)argc;
(void)argv;
int rv = 0;
apfl_tokenizer_ptr tokenizer = NULL;
apfl_parser_ptr parser = NULL;
if ((tokenizer = apfl_tokenizer_new(repl_source_reader, NULL)) == NULL) {
2021-12-10 20:22:16 +00:00
fprintf(stderr, "Failed initializing tokenizer\n");
goto exit;
}
if ((parser = apfl_parser_new(apfl_tokenizer_as_token_source(tokenizer))) == NULL) {
fprintf(stderr, "Failed initializing parser\n");
goto exit;
}
2021-12-10 20:22:16 +00:00
while (true) {
struct apfl_error err;
struct apfl_expr expr;
2021-12-10 20:22:16 +00:00
switch (apfl_parser_next(parser)) {
2021-12-10 20:22:16 +00:00
case APFL_PARSE_OK:
expr = apfl_parser_get_expr(parser);
apfl_expr_print(expr, stdout);
apfl_expr_deinit(&expr);
2021-12-10 20:22:16 +00:00
break;
case APFL_PARSE_EOF:
goto exit;
case APFL_PARSE_ERROR:
err = apfl_parser_get_error(parser);
2021-12-10 20:22:16 +00:00
apfl_error_print(err, stderr);
if (APFL_ERROR_IS_FATAL(err)) {
rv = 1;
goto exit;
}
break;
}
}
exit:
apfl_tokenizer_destroy(tokenizer);
apfl_parser_destroy(parser);
2021-12-10 20:22:16 +00:00
return rv;
}