-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug.c
More file actions
104 lines (88 loc) · 2.4 KB
/
debug.c
File metadata and controls
104 lines (88 loc) · 2.4 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* Copyright (C) 2021 by Alexandru-Sergiu Marton
This file is part of shell243.
shell243 is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
shell243 is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with shell243. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include "debug.h"
const char *token_names[] = {
[TOK_GT] = "TOK_GT",
[TOK_DGT] = "TOK_DGT",
[TOK_LT] = "TOK_LT",
[TOK_DLT] = "TOK_DLT",
[TOK_PIPE] = "TOK_PIPE",
[TOK_AMP] = "TOK_AMP",
[TOK_OR] = "TOK_OR",
[TOK_AND] = "TOK_AND",
[TOK_IONUM] = "TOK_IONUM",
[TOK_SEMI] = "TOK_SEMI",
[TOK_WORD] = "TOK_WORD",
[TOK_ERR] = "TOK_ERR",
[TOK_EOF] = "TOK_EOF"
};
const char *ast_names[] = {
[AST_PROGRAM] = "AST_PROGRAM",
[AST_AMP] = "AST_AMP",
[AST_SEMI] = "AST_SEMI",
[AST_AND] = "AST_AND",
[AST_OR] = "AST_OR",
[AST_PIPE_SEQ] = "AST_PIPE_SEQ",
[AST_COMMAND] = "AST_COMMAND",
[AST_WORD] = "AST_WORD",
[AST_REDIRECT] = "AST_REDIRECT",
[AST_REDIR_OP] = "AST_REDIR_OP",
[AST_NUMBER] = "AST_NUMBER",
[AST_ERROR] = "AST_ERROR"
};
const char *
token_type_to_string (token_type type)
{
return token_names[type];
}
void
print_token (token tok)
{
printf ("token: { type: %s, content: %.*s }\n", token_names[tok.type],
tok.length, tok.start);
}
void
print_tokens ()
{
for (;;)
{
token tok = next_token ();
print_token (tok);
if (tok.type == TOK_EOF)
break;
}
}
const char *
ast_type_to_string (ast_node_type type)
{
return ast_names[type];
}
void
print_ast (ast_node *node, int indent_level)
{
for (int i = 0; i < indent_level; i++)
putchar ('\t');
printf ("%s, %d children, content: ", ast_names[node->type], node->len);
if (node->string != NULL)
printf ("%s", node->string);
else if (node->type == AST_NUMBER)
printf ("%d", node->number);
puts ("");
if (node->len > 0)
{
for (int i = 0; i < node->len; i++)
print_ast (node->children[i], indent_level + 1);
}
}