Roll back optimization changes.
This commit is contained in:
@@ -212,6 +212,10 @@ 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<type_app*>(input_type.get())) ||
|
||||
!dynamic_cast<type_data*>(app_type->constructor.get())) {
|
||||
throw type_error("attempting case analysis of non-data type");
|
||||
}
|
||||
|
||||
return branch_type;
|
||||
}
|
||||
@@ -223,219 +227,60 @@ void ast_case::translate(global_scope& scope) {
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
struct case_mappings {
|
||||
using tag_type = typename T::tag_type;
|
||||
std::map<tag_type, std::vector<instruction_ptr>> defined_cases;
|
||||
std::optional<std::vector<instruction_ptr>> default_case;
|
||||
|
||||
std::vector<instruction_ptr>& make_case_for(tag_type tag) {
|
||||
if(default_case)
|
||||
throw compiler_error("attempted pattern match after catch-all");
|
||||
return defined_cases[tag];
|
||||
}
|
||||
|
||||
std::vector<instruction_ptr>& make_default_case() {
|
||||
if(default_case)
|
||||
throw compiler_error("attempted repeated use of catch-all");
|
||||
default_case.emplace(std::vector<instruction_ptr>());
|
||||
return *default_case;
|
||||
}
|
||||
|
||||
std::vector<instruction_ptr>& get_specific_case_for(tag_type tag) {
|
||||
auto existing_case = defined_cases.find(tag);
|
||||
assert(existing_case != defined_cases.end());
|
||||
return existing_case->second;
|
||||
}
|
||||
|
||||
std::vector<instruction_ptr>& get_default_case() {
|
||||
assert(default_case);
|
||||
return *default_case;
|
||||
}
|
||||
|
||||
std::vector<instruction_ptr>& get_case_for(tag_type tag) {
|
||||
if(case_defined_for(tag)) return get_specific_case_for(tag);
|
||||
return get_default_case();
|
||||
}
|
||||
|
||||
bool case_defined_for(tag_type tag) {
|
||||
return defined_cases.find(tag) != defined_cases.end();
|
||||
}
|
||||
|
||||
bool default_case_defined() { return default_case.has_value(); }
|
||||
|
||||
size_t defined_cases_count() { return defined_cases.size(); }
|
||||
};
|
||||
|
||||
|
||||
struct case_strategy_bool {
|
||||
using tag_type = bool;
|
||||
using repr_type = bool;
|
||||
|
||||
case_strategy_bool(const type* type) {}
|
||||
|
||||
tag_type tag_from_repr(repr_type b) { return b; }
|
||||
|
||||
repr_type repr_from_pattern(const pattern_ptr& pt) {
|
||||
pattern_constr* cpat;
|
||||
if(!(cpat = dynamic_cast<pattern_constr*>(pt.get())) ||
|
||||
(cpat->constr != "True" && cpat->constr != "False") ||
|
||||
cpat->params.size() != 0)
|
||||
throw compiler_error(
|
||||
"pattern cannot be converted to a boolean",
|
||||
pt->loc);
|
||||
return cpat->constr == "True";
|
||||
}
|
||||
|
||||
void compile_branch(
|
||||
const branch_ptr& branch,
|
||||
const env_ptr& env,
|
||||
repr_type repr,
|
||||
std::vector<instruction_ptr>& into) {
|
||||
branch->expr->compile(env_ptr(new env_offset(1, env)), into);
|
||||
into.push_back(instruction_ptr(new instruction_slide(1)));
|
||||
}
|
||||
|
||||
size_t case_count() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
void into_instructions(
|
||||
case_mappings<case_strategy_bool>& ms,
|
||||
std::vector<instruction_ptr>& into) {
|
||||
if(ms.defined_cases_count() == 0) {
|
||||
for(auto& instruction : ms.get_default_case())
|
||||
into.push_back(std::move(instruction));
|
||||
return;
|
||||
}
|
||||
|
||||
instruction_ijump* ijump = new instruction_ijump();
|
||||
instruction_ptr inst(ijump);
|
||||
ijump->branches.push_back(std::move(ms.get_case_for(false)));
|
||||
ijump->tag_mappings[0] = 0;
|
||||
ijump->branches.push_back(std::move(ms.get_case_for(true)));
|
||||
ijump->tag_mappings[1] = 1;
|
||||
into.push_back(std::move(inst));
|
||||
}
|
||||
};
|
||||
|
||||
struct case_strategy_data {
|
||||
using tag_type = int;
|
||||
using repr_type = std::pair<const type_data::constructor*, const std::vector<std::string>*>;
|
||||
|
||||
const type_data* arg_type;
|
||||
|
||||
case_strategy_data(const type* t) {
|
||||
arg_type = dynamic_cast<const type_data*>(t);
|
||||
assert(arg_type);
|
||||
}
|
||||
|
||||
tag_type tag_from_repr(const repr_type& repr) { return repr.first->tag; }
|
||||
|
||||
repr_type repr_from_pattern(const pattern_ptr& pt) {
|
||||
pattern_constr* cpat;
|
||||
if(!(cpat = dynamic_cast<pattern_constr*>(pt.get())))
|
||||
throw compiler_error(
|
||||
"pattern cannot be interpreted as constructor.",
|
||||
pt->loc);
|
||||
return std::make_pair(
|
||||
&arg_type->constructors.find(cpat->constr)->second,
|
||||
&cpat->params);
|
||||
}
|
||||
|
||||
void compile_branch(
|
||||
const branch_ptr& branch,
|
||||
const env_ptr& env,
|
||||
const repr_type& repr,
|
||||
std::vector<instruction_ptr>& into) {
|
||||
env_ptr new_env = env;
|
||||
for(auto it = repr.second->rbegin(); it != repr.second->rend(); it++) {
|
||||
new_env = env_ptr(new env_var(*it, new_env));
|
||||
}
|
||||
|
||||
into.push_back(instruction_ptr(new instruction_split(repr.second->size())));
|
||||
branch->expr->compile(new_env, into);
|
||||
into.push_back(instruction_ptr(new instruction_slide(repr.second->size())));
|
||||
}
|
||||
|
||||
size_t case_count() {
|
||||
return arg_type->constructors.size();
|
||||
}
|
||||
|
||||
void into_instructions(
|
||||
case_mappings<case_strategy_data>& ms,
|
||||
std::vector<instruction_ptr>& into) {
|
||||
instruction_jump* jump_instruction = new instruction_jump();
|
||||
instruction_ptr inst(jump_instruction);
|
||||
|
||||
for(auto& constr : arg_type->constructors) {
|
||||
if(!ms.case_defined_for(constr.second.tag)) continue;
|
||||
jump_instruction->branches.push_back(
|
||||
std::move(ms.get_specific_case_for(constr.second.tag)));
|
||||
jump_instruction->tag_mappings[constr.second.tag] =
|
||||
jump_instruction->branches.size() - 1;
|
||||
}
|
||||
|
||||
if(ms.default_case_defined()) {
|
||||
jump_instruction->branches.push_back(
|
||||
std::move(ms.get_default_case()));
|
||||
for(auto& constr : arg_type->constructors) {
|
||||
if(ms.case_defined_for(constr.second.tag)) continue;
|
||||
jump_instruction->tag_mappings[constr.second.tag] =
|
||||
jump_instruction->branches.size();
|
||||
}
|
||||
}
|
||||
|
||||
into.push_back(std::move(inst));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void compile_case(const ast_case& node, const env_ptr& env, const type* type, std::vector<instruction_ptr>& into) {
|
||||
T strategy(type);
|
||||
case_mappings<T> cases;
|
||||
for(auto& branch : node.branches) {
|
||||
pattern_var* vpat;
|
||||
if((vpat = dynamic_cast<pattern_var*>(branch->pat.get()))) {
|
||||
if(cases.defined_cases_count() == strategy.case_count())
|
||||
throw compiler_error("redundant catch-all pattern", branch->pat->loc);
|
||||
auto& branch_into = cases.make_default_case();
|
||||
env_ptr new_env(new env_var(vpat->var, env));
|
||||
branch->expr->compile(new_env, branch_into);
|
||||
branch_into.push_back(instruction_ptr(new instruction_slide(1)));
|
||||
} else {
|
||||
auto repr = strategy.repr_from_pattern(branch->pat);
|
||||
auto& branch_into = cases.make_case_for(strategy.tag_from_repr(repr));
|
||||
strategy.compile_branch(branch, env, repr, branch_into);
|
||||
}
|
||||
}
|
||||
|
||||
if(!(cases.defined_cases_count() == strategy.case_count() ||
|
||||
cases.default_case_defined()))
|
||||
throw compiler_error("incomplete patterns", node.loc);
|
||||
|
||||
strategy.into_instructions(cases, into);
|
||||
}
|
||||
|
||||
void ast_case::compile(const env_ptr& env, std::vector<instruction_ptr>& into) const {
|
||||
type_app* app_type = dynamic_cast<type_app*>(input_type.get());
|
||||
type_data* data;
|
||||
type_base *base;
|
||||
type_data* type = dynamic_cast<type_data*>(app_type->constructor.get());
|
||||
|
||||
of->compile(env, into);
|
||||
into.push_back(instruction_ptr(new instruction_eval()));
|
||||
|
||||
if(app_type && (data = dynamic_cast<type_data*>(app_type->constructor.get()))) {
|
||||
compile_case<case_strategy_data>(*this, env, data, into);
|
||||
return;
|
||||
} else if (app_type && (base = dynamic_cast<type_base *>(app_type->constructor.get()))) {
|
||||
if (base->name == "Bool") {
|
||||
compile_case<case_strategy_bool>(*this, env, data, into);
|
||||
return;
|
||||
}
|
||||
instruction_jump* jump_instruction = new instruction_jump();
|
||||
into.push_back(instruction_ptr(jump_instruction));
|
||||
for(auto& branch : branches) {
|
||||
std::vector<instruction_ptr> branch_instructions;
|
||||
pattern_var* vpat;
|
||||
pattern_constr* cpat;
|
||||
|
||||
if((vpat = dynamic_cast<pattern_var*>(branch->pat.get()))) {
|
||||
branch->expr->compile(env_ptr(new env_offset(1, env)), branch_instructions);
|
||||
|
||||
for(auto& constr_pair : type->constructors) {
|
||||
if(jump_instruction->tag_mappings.find(constr_pair.second.tag) !=
|
||||
jump_instruction->tag_mappings.end())
|
||||
break;
|
||||
|
||||
jump_instruction->tag_mappings[constr_pair.second.tag] =
|
||||
jump_instruction->branches.size();
|
||||
}
|
||||
jump_instruction->branches.push_back(std::move(branch_instructions));
|
||||
} else if((cpat = dynamic_cast<pattern_constr*>(branch->pat.get()))) {
|
||||
env_ptr new_env = env;
|
||||
for(auto it = cpat->params.rbegin(); it != cpat->params.rend(); it++) {
|
||||
new_env = env_ptr(new env_var(*it, new_env));
|
||||
}
|
||||
|
||||
branch_instructions.push_back(instruction_ptr(new instruction_split(
|
||||
cpat->params.size())));
|
||||
branch->expr->compile(new_env, branch_instructions);
|
||||
branch_instructions.push_back(instruction_ptr(new instruction_slide(
|
||||
cpat->params.size())));
|
||||
|
||||
int new_tag = type->constructors[cpat->constr].tag;
|
||||
if(jump_instruction->tag_mappings.find(new_tag) !=
|
||||
jump_instruction->tag_mappings.end())
|
||||
throw type_error("technically not a type error: duplicate pattern");
|
||||
|
||||
jump_instruction->tag_mappings[new_tag] =
|
||||
jump_instruction->branches.size();
|
||||
jump_instruction->branches.push_back(std::move(branch_instructions));
|
||||
}
|
||||
}
|
||||
|
||||
throw type_error("attempting unsupported case analysis", of->loc);
|
||||
for(auto& constr_pair : type->constructors) {
|
||||
if(jump_instruction->tag_mappings.find(constr_pair.second.tag) ==
|
||||
jump_instruction->tag_mappings.end())
|
||||
throw type_error("non-total pattern");
|
||||
}
|
||||
}
|
||||
|
||||
void ast_let::print(int indent, std::ostream& to) const {
|
||||
|
||||
Reference in New Issue
Block a user