From 56f0dbd02f6ba1f8279decd94551a9ade767179c Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 10 Sep 2020 12:53:55 -0700 Subject: [PATCH] Prevent case compilation from crashing and burning. --- code/compiler/13/ast.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/code/compiler/13/ast.cpp b/code/compiler/13/ast.cpp index 693cafd..a59e995 100644 --- a/code/compiler/13/ast.cpp +++ b/code/compiler/13/ast.cpp @@ -204,10 +204,6 @@ type_ptr ast_case::typecheck(type_mgr& mgr, type_env_ptr& env) { input_type = mgr.resolve(case_type, var); type_app* app_type; - // if(!(app_type = dynamic_cast(input_type.get())) || - // !dynamic_cast(app_type->constructor.get())) { - // throw type_error("attempting case analysis of non-data type", of->loc); - // } return branch_type; } @@ -407,17 +403,17 @@ void ast_case::compile(const env_ptr& env, std::vector& into) c of->compile(env, into); into.push_back(instruction_ptr(new instruction_eval())); - if((data = dynamic_cast(app_type->constructor.get()))) { + if(app_type && (data = dynamic_cast(app_type->constructor.get()))) { compile_case(*this, env, data, into); return; - } else if((internal = dynamic_cast(app_type->constructor.get()))) { + } else if(app_type && (internal = dynamic_cast(app_type->constructor.get()))) { if(internal->name == "Bool") { compile_case(*this, env, data, into); return; } } - throw std::runtime_error("no known way to compile case expression"); + throw type_error("attempting unsupported case analysis", of->loc); } void ast_let::print(int indent, std::ostream& to) const {