diff options
| author | Josh Wolfe <thejoshwolfe@gmail.com> | 2015-12-07 08:29:19 -0700 |
|---|---|---|
| committer | Josh Wolfe <thejoshwolfe@gmail.com> | 2015-12-07 08:29:19 -0700 |
| commit | f6eecfe5f486b250de33a8129b1c85198be43280 (patch) | |
| tree | 2cef7045ade894407c6b6d99e6daa5c86a9a496b /src/parser.cpp | |
| parent | 94e61287e708f61d2a9d814bfe7910d13e067ada (diff) | |
| download | zig-f6eecfe5f486b250de33a8129b1c85198be43280.tar.gz zig-f6eecfe5f486b250de33a8129b1c85198be43280.zip | |
getting started on array types
Diffstat (limited to 'src/parser.cpp')
| -rw-r--r-- | src/parser.cpp | 35 |
1 files changed, 25 insertions, 10 deletions
diff --git a/src/parser.cpp b/src/parser.cpp index fc47c7ad64..d6f9b96a8d 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -180,6 +180,13 @@ void ast_print(AstNode *node, int indent) { ast_print(node->data.type.child_type, indent + 2); break; } + case AstNodeTypeTypeArray: + { + fprintf(stderr, "ArrayType\n"); + ast_print(node->data.type.child_type, indent + 2); + ast_print(node->data.type.array_size, indent + 2); + break; + } } break; case NodeTypeReturnExpr: @@ -448,8 +455,9 @@ static void ast_parse_directives(ParseContext *pc, int *token_index, /* -Type : token(Symbol) | PointerType | token(Unreachable) -PointerType : token(Star) token(Const) Type | token(Star) token(Mut) Type; +Type : token(Symbol) | token(Unreachable) | token(Void) | PointerType | ArrayType +PointerType : token(Star) (token(Const) | token(Mut)) Type +ArrayType : token(LBracket) Type token(Semicolon) token(Number) token(RBracket) */ static AstNode *ast_parse_type(ParseContext *pc, int token_index, int *new_token_index) { Token *token = &pc->tokens->at(token_index); @@ -463,12 +471,6 @@ static AstNode *ast_parse_type(ParseContext *pc, int token_index, int *new_token } else if (token->id == TokenIdKeywordVoid) { node->data.type.type = AstNodeTypeTypePrimitive; buf_init_from_str(&node->data.type.primitive_name, "void"); - } else if (token->id == TokenIdKeywordTrue) { - node->data.type.type = AstNodeTypeTypePrimitive; - buf_init_from_str(&node->data.type.primitive_name, "true"); - } else if (token->id == TokenIdKeywordFalse) { - node->data.type.type = AstNodeTypeTypePrimitive; - buf_init_from_str(&node->data.type.primitive_name, "false"); } else if (token->id == TokenIdSymbol) { node->data.type.type = AstNodeTypeTypePrimitive; ast_buf_from_token(pc, token, &node->data.type.primitive_name); @@ -485,6 +487,20 @@ static AstNode *ast_parse_type(ParseContext *pc, int token_index, int *new_token } node->data.type.child_type = ast_parse_type(pc, token_index, &token_index); + } else if (token->id == TokenIdLBracket) { + node->data.type.type = AstNodeTypeTypeArray; + + node->data.type.child_type = ast_parse_type(pc, token_index, &token_index); + + Token *semicolon_token = &pc->tokens->at(token_index); + token_index += 1; + ast_expect_token(pc, semicolon_token, TokenIdSemicolon); + + node->data.type.array_size = ast_parse_expression(pc, &token_index, true); + + Token *rbracket_token = &pc->tokens->at(token_index); + token_index += 1; + ast_expect_token(pc, rbracket_token, TokenIdRBracket); } else { ast_invalid_token_error(pc, token); } @@ -494,8 +510,7 @@ static AstNode *ast_parse_type(ParseContext *pc, int token_index, int *new_token } /* -ParamDecl<node> : token(Symbol) token(Colon) Type { -}; +ParamDecl : token(Symbol) token(Colon) Type */ static AstNode *ast_parse_param_decl(ParseContext *pc, int token_index, int *new_token_index) { Token *param_name = &pc->tokens->at(token_index); |
