-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse_test.c
80 lines (66 loc) · 1.78 KB
/
parse_test.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
#include <assert.h>
#include "lex.h"
#include "parse_protos.h"
#include "str.h"
#include "variable.h"
static void pretty_print_mfa(struct function_call *call)
{
if (call->module) {
pretty_print_atom(stdout, call->module);
fputc(':', stdout);
}
pretty_print_atom(stdout, call->function);
pretty_print_argument_list(stdout, &call->args);
}
static void print(struct statement *st)
{
switch (st->type) {
default:
case AST_ST_NOP:
printf("nop.\n");
break;
case AST_ST_V_OF_TERM:
str_print(stdout, symbol_name(st->variable));
fputs(" = ", stdout);
term head;
enif_get_list_cell(NULL, st->call.args, &head, NULL);
pretty_print_term(stdout, &head);
puts(".");
assert(variable_assign(st->variable, head));
break;
case AST_ST_V_OF_MFA:
str_print(stdout, symbol_name(st->variable));
fputs(" = ", stdout);
pretty_print_mfa(&st->call);
puts(".");
break;
case AST_ST_MFA:
pretty_print_mfa(&st->call);
puts(".");
break;
case AST_ST_VAR:
str_print(stdout, symbol_name(st->variable));
puts(".");
break;
}
}
int main()
{
FILE *in = stdin;
char *line = NULL;
size_t line_len = 0;
struct lexer lexer;
lex_init(&lexer);
void *pParser;
pParser = ParseAlloc(malloc);
ssize_t nread = 0;
while (-1 != (nread = getline(&line, &line_len, in))) {
lex_setup_next_line(&lexer, line, nread, feof(in));
struct token token;
while (lex(&lexer, &token))
Parse(pParser, token.type, token, print);
}
Parse(pParser, 0, (struct token){.type = 0, .location = lexer.location}, print);
free(line);
ParseFree(pParser, free);
}