Fix bug from small improvements

This commit is contained in:
Danila Fedorin 2019-10-10 17:59:44 -07:00
parent 21f90d85c5
commit df0b819b0e
2 changed files with 10 additions and 9 deletions

View File

@ -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 case_type = mgr.resolve(of->typecheck(mgr, env), var);
type_ptr branch_type = mgr.new_type(); type_ptr branch_type = mgr.new_type();
if(!dynamic_cast<type_base*>(case_type.get())) {
throw type_error("attempting case analysis of non-data type");
}
for(auto& branch : branches) { for(auto& branch : branches) {
type_env new_env = env.scope(); type_env new_env = env.scope();
branch->pat->match(case_type, mgr, new_env); 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); mgr.unify(branch_type, curr_branch_type);
} }
case_type = mgr.resolve(case_type, var);
if(!dynamic_cast<type_base*>(case_type.get())) {
throw type_error("attempting case analysis of non-data type");
}
return branch_type; return branch_type;
} }

View File

@ -65,8 +65,8 @@ Finally, just like `ast_case::typecheck` called
We follow the same implementation strategy for patterns, We follow the same implementation strategy for patterns,
but we don't need indentation, or recursion: but we don't need indentation, or recursion:
{{< codelines "C++" "compiler/04/ast.cpp" 114 116 >}} {{< codelines "C++" "compiler/04/ast.cpp" 115 117 >}}
{{< codelines "C++" "compiler/04/ast.cpp" 122 127 >}} {{< codelines "C++" "compiler/04/ast.cpp" 123 128 >}}
In `main`, let's print the bodies of each function we receive from the parser: In `main`, let's print the bodies of each function we receive from the parser:
{{< codelines "C++" "compiler/04/main.cpp" 47 56 >}} {{< 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 the pattern matching method. However, since we don't want
case analysis on functions, we ensure that the type of the expression case analysis on functions, we ensure that the type of the expression
is `type_base`. If not, we report this: 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 The next exception is in `pattern_constr::match`. It occurs
when the pattern has a constructor we don't recognize, and when the pattern has a constructor we don't recognize, and
that's exactly what we report: 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 The next exception occurs in a loop, when we bind
types for each of the constructor pattern's variables. 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 this means that the pattern wants to apply the
constructor to more parameters than it actually constructor to more parameters than it actually
takes: 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`. We remove the last throw at the bottom of `pattern_constr::match`.
This is because once unification succeeds, we know This is because once unification succeeds, we know