add try expression

See #83
This commit is contained in:
Andrew Kelley
2017-02-02 17:09:27 -05:00
parent 8b1c6d8b76
commit c0b37e8514
12 changed files with 344 additions and 103 deletions

View File

@@ -223,6 +223,8 @@ static const char *node_type_str(NodeType node_type) {
return "TypeLiteral";
case NodeTypeVarLiteral:
return "VarLiteral";
case NodeTypeTryExpr:
return "TryExpr";
}
zig_unreachable();
}
@@ -769,6 +771,25 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
}
break;
}
case NodeTypeTryExpr:
{
const char *var_str = node->data.try_expr.var_is_const ? "const" : "var";
const char *var_name = buf_ptr(node->data.try_expr.var_symbol);
const char *ptr_str = node->data.try_expr.var_is_ptr ? "*" : "";
fprintf(ar->f, "try (%s %s%s", var_str, ptr_str, var_name);
fprintf(ar->f, " = ");
render_node_grouped(ar, node->data.try_expr.target_node);
fprintf(ar->f, ") ");
render_node_grouped(ar, node->data.try_expr.then_node);
if (node->data.try_expr.else_node) {
fprintf(ar->f, " else ");
if (node->data.try_expr.err_symbol) {
fprintf(ar->f, "|%s| ", buf_ptr(node->data.try_expr.err_symbol));
}
render_node_grouped(ar, node->data.try_expr.else_node);
}
break;
}
case NodeTypeSwitchExpr:
{
AstNodeSwitchExpr *switch_expr = &node->data.switch_expr;