authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-12-26 15:43:40-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-12-26 15:43:40-07:00
log7ba99e9715c1fa3cd00fe6d9ac74b9a15eeac706
tree3ce60bcf77470440292beb7551ce0f0851e7a9a3
parent1f8e3871ee11c71b7965e8b9e97fa37a8e994d68

analyze if maybe var expressions


3 files changed, 61 insertions(+), 23 deletions(-)

example/guess_number/main.zig+1-1
......@@ -12,7 +12,7 @@ fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
1212 const answer = rand_int(&rand_state, 0, 100) + 1;
1313
1414 while true {
15 line = readline("\nGuess a number between 1 and 100: ");
15 const line = readline("\nGuess a number between 1 and 100: ");
1616
1717 if const guess ?= parse_number(line) {
1818 if (guess > answer) {
src/analyze.cpp+54-22
......@@ -661,6 +661,8 @@ static bool num_lit_fits_in_other_type(CodeGen *g, TypeTableEntry *literal_type,
661661 } else {
662662 return false;
663663 }
664 case TypeTableEntryIdMaybe:
665 return num_lit_fits_in_other_type(g, literal_type, other_type->data.maybe.child_type);
664666 }
665667 zig_unreachable();
666668}
......@@ -1220,11 +1222,11 @@ static TypeTableEntry *analyze_bin_op_expr(CodeGen *g, ImportTableEntry *import,
12201222 zig_unreachable();
12211223}
12221224
1223static VariableTableEntry *analyze_variable_declaration(CodeGen *g, ImportTableEntry *import,
1224 BlockContext *context, TypeTableEntry *expected_type, AstNode *node)
1225static VariableTableEntry *analyze_variable_declaration_raw(CodeGen *g, ImportTableEntry *import,
1226 BlockContext *context, AstNode *source_node,
1227 AstNodeVariableDeclaration *variable_declaration,
1228 bool expr_is_maybe)
12251229{
1226 AstNodeVariableDeclaration *variable_declaration = &node->data.variable_declaration;
1227
12281230 TypeTableEntry *explicit_type = nullptr;
12291231 if (variable_declaration->type != nullptr) {
12301232 explicit_type = resolve_type(g, variable_declaration->type);
......@@ -1238,19 +1240,28 @@ static VariableTableEntry *analyze_variable_declaration(CodeGen *g, ImportTableE
12381240 TypeTableEntry *implicit_type = nullptr;
12391241 if (variable_declaration->expr != nullptr) {
12401242 implicit_type = analyze_expression(g, import, context, explicit_type, variable_declaration->expr);
1241 if (implicit_type->id == TypeTableEntryIdUnreachable) {
1242 add_node_error(g, node,
1243 if (implicit_type->id == TypeTableEntryIdInvalid) {
1244 // ignore the poison value
1245 } else if (expr_is_maybe) {
1246 if (implicit_type->id == TypeTableEntryIdMaybe) {
1247 implicit_type = implicit_type->data.maybe.child_type;
1248 } else {
1249 add_node_error(g, source_node, buf_sprintf("expected maybe type"));
1250 implicit_type = g->builtin_types.entry_invalid;
1251 }
1252 } else if (implicit_type->id == TypeTableEntryIdUnreachable) {
1253 add_node_error(g, source_node,
12431254 buf_sprintf("variable initialization is unreachable"));
12441255 implicit_type = g->builtin_types.entry_invalid;
12451256 } else if (implicit_type->id == TypeTableEntryIdNumberLiteral) {
1246 add_node_error(g, node,
1257 add_node_error(g, source_node,
12471258 buf_sprintf("unable to infer variable type"));
12481259 implicit_type = g->builtin_types.entry_invalid;
12491260 }
12501261 }
12511262
12521263 if (implicit_type == nullptr && variable_declaration->is_const) {
1253 add_node_error(g, node, buf_sprintf("variables must have initial values or be declared 'mut'."));
1264 add_node_error(g, source_node, buf_sprintf("variables must have initial values or be declared 'mut'."));
12541265 implicit_type = g->builtin_types.entry_invalid;
12551266 }
12561267
......@@ -1259,7 +1270,7 @@ static VariableTableEntry *analyze_variable_declaration(CodeGen *g, ImportTableE
12591270
12601271 VariableTableEntry *existing_variable = find_local_variable(context, &variable_declaration->symbol);
12611272 if (existing_variable) {
1262 add_node_error(g, node,
1273 add_node_error(g, source_node,
12631274 buf_sprintf("redeclaration of variable '%s'", buf_ptr(&variable_declaration->symbol)));
12641275 } else {
12651276 VariableTableEntry *variable_entry = allocate<VariableTableEntry>(1);
......@@ -1267,13 +1278,20 @@ static VariableTableEntry *analyze_variable_declaration(CodeGen *g, ImportTableE
12671278 variable_entry->type = type;
12681279 variable_entry->is_const = variable_declaration->is_const;
12691280 variable_entry->is_ptr = true;
1270 variable_entry->decl_node = node;
1281 variable_entry->decl_node = source_node;
12711282 context->variable_table.put(&variable_entry->name, variable_entry);
12721283 return variable_entry;
12731284 }
12741285 return nullptr;
12751286}
12761287
1288static VariableTableEntry *analyze_variable_declaration(CodeGen *g, ImportTableEntry *import,
1289 BlockContext *context, TypeTableEntry *expected_type, AstNode *node)
1290{
1291 AstNodeVariableDeclaration *variable_declaration = &node->data.variable_declaration;
1292 return analyze_variable_declaration_raw(g, import, context, node, variable_declaration, false);
1293}
1294
12771295static TypeTableEntry *analyze_number_literal_expr(CodeGen *g, ImportTableEntry *import,
12781296 BlockContext *block_context, TypeTableEntry *expected_type, AstNode *node)
12791297{
......@@ -1397,37 +1415,51 @@ static TypeTableEntry *analyze_continue_expr(CodeGen *g, ImportTableEntry *impor
13971415 return g->builtin_types.entry_unreachable;
13981416}
13991417
1400static TypeTableEntry *analyze_if_bool_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,
1401 TypeTableEntry *expected_type, AstNode *node)
1418static TypeTableEntry *analyze_if_then_else(CodeGen *g, ImportTableEntry *import, BlockContext *context,
1419 TypeTableEntry *expected_type, AstNode *then_block, AstNode *else_node, AstNode *parent_node)
14021420{
1403 analyze_expression(g, import, context, g->builtin_types.entry_bool, node->data.if_bool_expr.condition);
1404
1405 TypeTableEntry *then_type = analyze_expression(g, import, context, expected_type,
1406 node->data.if_bool_expr.then_block);
1421 TypeTableEntry *then_type = analyze_expression(g, import, context, expected_type, then_block);
14071422
14081423 TypeTableEntry *else_type;
1409 if (node->data.if_bool_expr.else_node) {
1410 else_type = analyze_expression(g, import, context, expected_type, node->data.if_bool_expr.else_node);
1424 if (else_node) {
1425 else_type = analyze_expression(g, import, context, expected_type, else_node);
14111426 } else {
14121427 else_type = g->builtin_types.entry_void;
1413 else_type = resolve_type_compatibility(g, context, node, expected_type, else_type);
1428 else_type = resolve_type_compatibility(g, context, parent_node, expected_type, else_type);
14141429 }
14151430
14161431
14171432 if (expected_type) {
14181433 return (then_type->id == TypeTableEntryIdUnreachable) ? else_type : then_type;
14191434 } else {
1420 return resolve_peer_type_compatibility(g, context, node,
1421 node->data.if_bool_expr.then_block, node->data.if_bool_expr.else_node,
1435 return resolve_peer_type_compatibility(g, context, parent_node,
1436 then_block, else_node,
14221437 then_type, else_type);
14231438 }
14241439}
14251440
1441static TypeTableEntry *analyze_if_bool_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,
1442 TypeTableEntry *expected_type, AstNode *node)
1443{
1444 analyze_expression(g, import, context, g->builtin_types.entry_bool, node->data.if_bool_expr.condition);
1445
1446 return analyze_if_then_else(g, import, context, expected_type,
1447 node->data.if_bool_expr.then_block,
1448 node->data.if_bool_expr.else_node,
1449 node);
1450}
1451
14261452static TypeTableEntry *analyze_if_var_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,
14271453 TypeTableEntry *expected_type, AstNode *node)
14281454{
14291455 assert(node->type == NodeTypeIfVarExpr);
1430 zig_panic("TODO analyze_if_var_expr");
1456
1457 BlockContext *child_context = new_block_context(node, context);
1458
1459 analyze_variable_declaration_raw(g, import, child_context, node, &node->data.if_var_expr.var_decl, true);
1460
1461 return analyze_if_then_else(g, import, child_context, expected_type,
1462 node->data.if_var_expr.then_block, node->data.if_var_expr.else_node, node);
14311463}
14321464
14331465static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import, BlockContext *context,
src/analyze.hpp+6
......@@ -55,6 +55,10 @@ struct TypeTableEntryNumLit {
5555 NumLit kind;
5656};
5757
58struct TypeTableEntryMaybe {
59 TypeTableEntry *child_type;
60};
61
5862enum TypeTableEntryId {
5963 TypeTableEntryIdInvalid,
6064 TypeTableEntryIdVoid,
......@@ -66,6 +70,7 @@ enum TypeTableEntryId {
6670 TypeTableEntryIdArray,
6771 TypeTableEntryIdStruct,
6872 TypeTableEntryIdNumberLiteral,
73 TypeTableEntryIdMaybe,
6974};
7075
7176struct TypeTableEntry {
......@@ -84,6 +89,7 @@ struct TypeTableEntry {
8489 TypeTableEntryArray array;
8590 TypeTableEntryStruct structure;
8691 TypeTableEntryNumLit num_lit;
92 TypeTableEntryMaybe maybe;
8793 } data;
8894
8995 // use these fields to make sure we don't duplicate type table entries for the same type