From fa6e3eec464227a2c20ca949e2fd12f3ab649960 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 3 Jan 2016 18:17:50 -0700 Subject: add #typeof() compiler function --- src/parser.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) (limited to 'src/parser.cpp') diff --git a/src/parser.cpp b/src/parser.cpp index ab0abd5285..afacd6bdb8 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -142,6 +142,8 @@ const char *node_type_str(NodeType node_type) { return "StructValueExpr"; case NodeTypeStructValueField: return "StructValueField"; + case NodeTypeCompilerFnCall: + return "CompilerFnCall"; } zig_unreachable(); } @@ -233,6 +235,12 @@ void ast_print(AstNode *node, int indent) { ast_print(node->data.type.child_type, indent + 2); break; } + case AstNodeTypeTypeCompilerExpr: + { + fprintf(stderr, "CompilerExprType\n"); + ast_print(node->data.type.compiler_expr, indent + 2); + break; + } } break; case NodeTypeReturnExpr: @@ -402,6 +410,9 @@ void ast_print(AstNode *node, int indent) { fprintf(stderr, "%s '%s'\n", node_type_str(node->type), buf_ptr(&node->data.struct_val_field.name)); ast_print(node->data.struct_val_field.expr, indent + 2); break; + case NodeTypeCompilerFnCall: + fprintf(stderr, "%s\n", node_type_str(node->type)); + break; } } @@ -985,16 +996,48 @@ static void ast_parse_type_assume_amp(ParseContext *pc, int *token_index, AstNod } /* -Type : token(Symbol) | token(Unreachable) | token(Void) | PointerType | ArrayType | MaybeType +CompileTimeFnCall : token(NumberSign) token(Symbol) token(LParen) Expression token(RParen) +*/ +static AstNode *ast_parse_compiler_fn_call(ParseContext *pc, int *token_index, bool mandatory) { + Token *token = &pc->tokens->at(*token_index); + + if (token->id == TokenIdNumberSign) { + *token_index += 1; + } else if (mandatory) { + ast_invalid_token_error(pc, token); + } else { + return nullptr; + } + + Token *name_symbol = ast_eat_token(pc, token_index, TokenIdSymbol); + ast_eat_token(pc, token_index, TokenIdLParen); + + AstNode *node = ast_create_node(pc, NodeTypeCompilerFnCall, token); + ast_buf_from_token(pc, name_symbol, &node->data.compiler_fn_call.name); + node->data.compiler_fn_call.expr = ast_parse_expression(pc, token_index, true); + + ast_eat_token(pc, token_index, TokenIdRParen); + return node; +} + +/* +Type : token(Symbol) | token(Unreachable) | token(Void) | PointerType | ArrayType | MaybeType | CompileTimeFnCall PointerType : token(Ampersand) option(token(Const)) Type ArrayType : token(LBracket) Type token(Semicolon) token(Number) token(RBracket) */ static AstNode *ast_parse_type(ParseContext *pc, int *token_index) { Token *token = &pc->tokens->at(*token_index); - *token_index += 1; - AstNode *node = ast_create_node(pc, NodeTypeType, token); + AstNode *compiler_fn_call = ast_parse_compiler_fn_call(pc, token_index, false); + if (compiler_fn_call) { + node->data.type.type = AstNodeTypeTypeCompilerExpr; + node->data.type.compiler_expr = compiler_fn_call; + return node; + } + + *token_index += 1; + if (token->id == TokenIdKeywordUnreachable) { node->data.type.type = AstNodeTypeTypePrimitive; buf_init_from_str(&node->data.type.primitive_name, "unreachable"); -- cgit v1.2.3