From df0b819b0e23c762508f51a3199f6a1e49d6ca1e Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 10 Oct 2019 17:59:44 -0700 Subject: [PATCH] Fix bug from small improvements --- code/compiler/04/ast.cpp | 9 +++++---- content/blog/04_compiler_improvements.md | 10 +++++----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/code/compiler/04/ast.cpp b/code/compiler/04/ast.cpp index f8a88df..8880df2 100644 --- a/code/compiler/04/ast.cpp +++ b/code/compiler/04/ast.cpp @@ -97,10 +97,6 @@ type_ptr ast_case::typecheck(type_mgr& mgr, const type_env& env) const { type_ptr case_type = mgr.resolve(of->typecheck(mgr, env), var); type_ptr branch_type = mgr.new_type(); - if(!dynamic_cast(case_type.get())) { - throw type_error("attempting case analysis of non-data type"); - } - for(auto& branch : branches) { type_env new_env = env.scope(); branch->pat->match(case_type, mgr, new_env); @@ -108,6 +104,11 @@ type_ptr ast_case::typecheck(type_mgr& mgr, const type_env& env) const { mgr.unify(branch_type, curr_branch_type); } + case_type = mgr.resolve(case_type, var); + if(!dynamic_cast(case_type.get())) { + throw type_error("attempting case analysis of non-data type"); + } + return branch_type; } diff --git a/content/blog/04_compiler_improvements.md b/content/blog/04_compiler_improvements.md index 7ece0d9..b7ea851 100644 --- a/content/blog/04_compiler_improvements.md +++ b/content/blog/04_compiler_improvements.md @@ -65,8 +65,8 @@ Finally, just like `ast_case::typecheck` called We follow the same implementation strategy for patterns, but we don't need indentation, or recursion: -{{< codelines "C++" "compiler/04/ast.cpp" 114 116 >}} -{{< codelines "C++" "compiler/04/ast.cpp" 122 127 >}} +{{< codelines "C++" "compiler/04/ast.cpp" 115 117 >}} +{{< codelines "C++" "compiler/04/ast.cpp" 123 128 >}} In `main`, let's print the bodies of each function we receive from the parser: {{< codelines "C++" "compiler/04/main.cpp" 47 56 >}} @@ -160,12 +160,12 @@ we simply pass the type of the expression to be case analyzed into the pattern matching method. However, since we don't want case analysis on functions, we ensure that the type of the expression is `type_base`. If not, we report this: -{{< codelines "C++" "compiler/04/ast.cpp" 100 102 >}} +{{< codelines "C++" "compiler/04/ast.cpp" 107 110 >}} The next exception is in `pattern_constr::match`. It occurs when the pattern has a constructor we don't recognize, and that's exactly what we report: -{{< codelines "C++" "compiler/04/ast.cpp" 131 133 >}} +{{< codelines "C++" "compiler/04/ast.cpp" 132 134 >}} The next exception occurs in a loop, when we bind types for each of the constructor pattern's variables. @@ -174,7 +174,7 @@ constructor type to a `type_arr`. Conceptually, this means that the pattern wants to apply the constructor to more parameters than it actually takes: -{{< codelines "C++" "compiler/04/ast.cpp" 137 137 >}} +{{< codelines "C++" "compiler/04/ast.cpp" 138 138 >}} We remove the last throw at the bottom of `pattern_constr::match`. This is because once unification succeeds, we know