authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-11-23 21:45:10-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-11-23 21:45:10-07:00
loga22bc8d20a1ad9158a28f29f6f866b0008b11e3e
tree65bfb1c6a0beeec39062d80613921d4a637f85d7
parent1b24f4c73c7f61d123eb8f0c4e90b267d77ec596

more detailed AST inspection


1 files changed, 49 insertions(+), 0 deletions(-)

src/parser.cpp+49
...@@ -73,6 +73,54 @@ void ast_print(AstNode *node, int indent) {...@@ -73,6 +73,54 @@ void ast_print(AstNode *node, int indent) {
7373
74 break;74 break;
75 }75 }
76 case NodeTypeBlock:
77 {
78 fprintf(stderr, "%s\n", node_type_str(node->type));
79 for (int i = 0; i < node->data.block.statements.length; i += 1) {
80 AstNode *child = node->data.block.statements.at(i);
81 ast_print(child, indent + 2);
82 }
83 break;
84 }
85 case NodeTypeParamDecl:
86 {
87 Buf *name_buf = &node->data.param_decl.name;
88 fprintf(stderr, "%s '%s'\n", node_type_str(node->type), buf_ptr(name_buf));
89
90 ast_print(node->data.param_decl.type, indent + 2);
91
92 break;
93 }
94 case NodeTypeType:
95 switch (node->data.type.type) {
96 case AstNodeTypeTypePrimitive:
97 {
98 Buf *name_buf = &node->data.type.primitive_name;
99 fprintf(stderr, "%s '%s'\n", node_type_str(node->type), buf_ptr(name_buf));
100 break;
101 }
102 case AstNodeTypeTypePointer:
103 {
104 const char *const_or_mut_str = node->data.type.is_const ? "const" : "mut";
105 fprintf(stderr, "'%s' PointerType\n", const_or_mut_str);
106
107 ast_print(node->data.type.child_type, indent + 2);
108 break;
109 }
110 }
111 break;
112 case NodeTypeStatement:
113 switch (node->data.statement.type) {
114 case AstNodeStatementTypeReturn:
115 fprintf(stderr, "ReturnStatement\n");
116 ast_print(node->data.statement.data.retrn.expression, indent + 2);
117 break;
118 case AstNodeStatementTypeExpression:
119 fprintf(stderr, "ExpressionStatement\n");
120 ast_print(node->data.statement.data.expr.expression, indent + 2);
121 break;
122 }
123 break;
76 default:124 default:
77 fprintf(stderr, "%s\n", node_type_str(node->type));125 fprintf(stderr, "%s\n", node_type_str(node->type));
78 break;126 break;
...@@ -124,6 +172,7 @@ static AstNode *ast_parse_type(ParseContext *pc, int token_index, int *new_token...@@ -124,6 +172,7 @@ static AstNode *ast_parse_type(ParseContext *pc, int token_index, int *new_token
124 node->data.type.type = AstNodeTypeTypePrimitive;172 node->data.type.type = AstNodeTypeTypePrimitive;
125 ast_buf_from_token(pc, token, &node->data.type.primitive_name);173 ast_buf_from_token(pc, token, &node->data.type.primitive_name);
126 } else if (token->id == TokenIdStar) {174 } else if (token->id == TokenIdStar) {
175 node->data.type.type = AstNodeTypeTypePointer;
127 Token *const_or_mut = &pc->tokens->at(token_index);176 Token *const_or_mut = &pc->tokens->at(token_index);
128 token_index += 1;177 token_index += 1;
129 if (const_or_mut->id == TokenIdKeywordMut) {178 if (const_or_mut->id == TokenIdKeywordMut) {