refactor code to prepare for multiple files

verbose compiler output is now behind --verbose flag
This commit is contained in:
Andrew Kelley
2015-11-30 19:58:53 -07:00
parent ef482ece7c
commit 55b8472374
24 changed files with 474 additions and 287 deletions

View File

@@ -100,6 +100,8 @@ const char *node_type_str(NodeType node_type) {
return "Symbol";
case NodeTypePrefixOpExpr:
return "PrefixOpExpr";
case NodeTypeUse:
return "Use";
}
zig_unreachable();
}
@@ -241,6 +243,9 @@ void ast_print(AstNode *node, int indent) {
fprintf(stderr, "PrimaryExpr Symbol %s\n",
buf_ptr(&node->data.symbol));
break;
case NodeTypeUse:
fprintf(stderr, "%s '%s'\n", node_type_str(node->type), buf_ptr(&node->data.use.path));
break;
}
}
@@ -1231,7 +1236,36 @@ static AstNode *ast_parse_root_export_decl(ParseContext *pc, int *token_index, b
}
/*
TopLevelDecl : FnDef | ExternBlock | RootExportDecl
Use : many(Directive) token(Use) token(String) token(Semicolon)
*/
static AstNode *ast_parse_use(ParseContext *pc, int *token_index, bool mandatory) {
assert(mandatory == false);
Token *use_kw = &pc->tokens->at(*token_index);
if (use_kw->id != TokenIdKeywordUse)
return nullptr;
*token_index += 1;
Token *use_name = &pc->tokens->at(*token_index);
*token_index += 1;
ast_expect_token(pc, use_name, TokenIdStringLiteral);
Token *semicolon = &pc->tokens->at(*token_index);
*token_index += 1;
ast_expect_token(pc, semicolon, TokenIdSemicolon);
AstNode *node = ast_create_node(NodeTypeUse, use_kw);
parse_string_literal(pc, use_name, &node->data.use.path);
node->data.use.directives = pc->directive_list;
pc->directive_list = nullptr;
return node;
}
/*
TopLevelDecl : FnDef | ExternBlock | RootExportDecl | Use
*/
static void ast_parse_top_level_decls(ParseContext *pc, int *token_index, ZigList<AstNode *> *top_level_decls) {
for (;;) {
@@ -1258,6 +1292,12 @@ static void ast_parse_top_level_decls(ParseContext *pc, int *token_index, ZigLis
continue;
}
AstNode *use_node = ast_parse_use(pc, token_index, false);
if (use_node) {
top_level_decls->append(use_node);
continue;
}
if (pc->directive_list->length > 0) {
ast_error(directive_token, "invalid directive");
}