-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
77 lines (60 loc) · 1.29 KB
/
parse.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
/*
* A brain-dead parser
*/
#import <stdio.h>
#import <stdlib.h>
#import "parse.h"
#import "tokenise.h"
#import "ast.h"
void
show_tokens(token **tokens, int token_count)
{
printf("Number of tokens found: %d\n", token_count);
int i;
for (i = 0; i < token_count; i++) {
token *t = tokens[i];
printf("[%d]: ", i);
int token_length = t->end_pos - t->start_pos;
printf("%d: ", token_length);
int j;
for (j = 0; j <= token_length; j++) {
printf("%c", t->data[j]);
}
printf("\n");
}
}
void
pretty_print_ast(ast *tree) {
printf("AST:\n");
}
int
parse(char *file)
{
token *tokens[MAX_TOKEN_NUM];
ast *syntax_tree;
int token_count = tokenise(file, tokens);
if (tokens == NULL) {
fprintf(stderr, "Couldn't read file: %s\n", file);
return -1;
}
show_tokens(tokens, token_count);
build_ast(tokens, syntax_tree);
if (syntax_tree == NULL) {
fprintf(stderr, "Couldn't parse file: %s\n", file);
return -1;
}
pretty_print_ast(syntax_tree);
return 0;
}
int
main(int argc, char **argv)
{
int retcode = 0;
if (argv[1] == NULL) {
printf("Need a source file\n");
}
else {
retcode = parse(argv[1]);
}
return retcode;
}