add #typeof() compiler function

This commit is contained in:
Andrew Kelley
2016-01-03 18:17:50 -07:00
parent b453345554
commit fa6e3eec46
8 changed files with 148 additions and 29 deletions

View File

@@ -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");