Tweak data types to...work?

This commit is contained in:
Danila Fedorin 2019-06-12 08:35:12 -07:00
parent fe35695532
commit b2bb05d4b8
4 changed files with 15 additions and 8 deletions

View File

@ -171,6 +171,7 @@ namespace lily {
new_env->set_parent(env); new_env->set_parent(env);
std::vector<instruction*> new_branch; std::vector<instruction*> new_branch;
branch.expr->compile(mgr, new_branch, new_env); branch.expr->compile(mgr, new_branch, new_env);
new_branch.push_back(mgr.add_instruction<instruction_slide>(1));
ijump->instructions.push_back(std::move(new_branch)); ijump->instructions.push_back(std::move(new_branch));
for(int i = 0; i < ccount; i++) { for(int i = 0; i < ccount; i++) {
@ -179,7 +180,10 @@ namespace lily {
} }
} else { } else {
pattern_cons* cons_pattern = dynamic_cast<pattern_cons*>(pattern.get()); pattern_cons* cons_pattern = dynamic_cast<pattern_cons*>(pattern.get());
int constructor = std::distance(data->constructors.begin(), data->constructors.find(cons_pattern->name)); int constructor;
for(auto& pair : data->constructors) {
if(pair.first == cons_pattern->name) constructor = pair.second->id;
}
if(branched[constructor]) throw error("cannot branch on the same constructor twice"); if(branched[constructor]) throw error("cannot branch on the same constructor twice");
branched[constructor] = true; branched[constructor] = true;
@ -193,6 +197,7 @@ namespace lily {
std::vector<instruction*> into; std::vector<instruction*> into;
into.push_back(mgr.add_instruction<instruction_split>(vcount)); into.push_back(mgr.add_instruction<instruction_split>(vcount));
branch.expr->compile(mgr, into, env); branch.expr->compile(mgr, into, env);
into.push_back(mgr.add_instruction<instruction_slide>(vcount));
ijump->instructions.push_back(std::move(into)); ijump->instructions.push_back(std::move(into));
ijump->const_instructions[constructor] = ijump->instructions.size() - 1; ijump->const_instructions[constructor] = ijump->instructions.size() - 1;
} }

View File

@ -147,8 +147,8 @@ namespace lily {
void instruction_op::gen_llvm(llvm_context& ctx) { void instruction_op::gen_llvm(llvm_context& ctx) {
llvm::Value* stack = ctx.get_current_function()->arg_begin(); llvm::Value* stack = ctx.get_current_function()->arg_begin();
llvm::Value* param1 = builder.CreateCall(stack_pop_func, { stack }, "temp");
llvm::Value* param2 = builder.CreateCall(stack_pop_func, { stack }, "temp"); llvm::Value* param2 = builder.CreateCall(stack_pop_func, { stack }, "temp");
llvm::Value* param1 = builder.CreateCall(stack_pop_func, { stack }, "temp");
llvm::Value* param1_node_num = builder.CreatePointerCast(param1, llvm::PointerType::getUnqual(node_num_type), "temp"); llvm::Value* param1_node_num = builder.CreatePointerCast(param1, llvm::PointerType::getUnqual(node_num_type), "temp");
llvm::Value* param2_node_num = builder.CreatePointerCast(param2, llvm::PointerType::getUnqual(node_num_type), "temp"); llvm::Value* param2_node_num = builder.CreatePointerCast(param2, llvm::PointerType::getUnqual(node_num_type), "temp");
llvm::Value* param1_intptr = builder.CreateGEP(param1_node_num, { get_int32_constant(0), get_int32_constant(1) }, "temp"); llvm::Value* param1_intptr = builder.CreateGEP(param1_node_num, { get_int32_constant(0), get_int32_constant(1) }, "temp");

View File

@ -8,10 +8,12 @@ int main() {
try { try {
lily::program_ptr prog = lily::parse( lily::program_ptr prog = lily::parse(
"data Bool = { False, True }\n" "data IntPair = { MkPair(Int, Int) }\n"
"defn if c t e = { case not c of { True -> { t } False -> { e } } }\n" "data IntList = { Nil, Cons(Int, IntList) }"
"defn not b = { case b of { True -> { False } False -> { True } } }\n" "defn fst p = { case p of { MkPair(a, b) -> { a } } }\n"
"defn main = { if True 3 2 }" "defn snd p = { case p of { MkPair(a, b) -> { b } } }\n"
"defn sum l = { case l of { Nil -> { 0 } Cons(x, xs) -> { x + sum xs } } }\n"
"defn main = { let tl1 = { Cons 1 Nil } in { let tl2 = { Cons 2 tl1 } in { let tl3 = { Cons 3 tl2 } in { sum tl3 } } } }"
); );
prog->gen_llvm(); prog->gen_llvm();
} catch(lily::error& e) { } catch(lily::error& e) {

View File

@ -29,7 +29,6 @@ namespace lily {
type_data* type_manager::create_data_type(const std::string& name) { type_data* type_manager::create_data_type(const std::string& name) {
if(type_names.count(name)) throw error("redefinition of type"); if(type_names.count(name)) throw error("redefinition of type");
auto new_type = std::make_unique<type_data>(next_id++); auto new_type = std::make_unique<type_data>(next_id++);
type_data* raw_ptr = new_type.get(); type_data* raw_ptr = new_type.get();
types.push_back(std::move(new_type)); types.push_back(std::move(new_type));
@ -43,7 +42,8 @@ namespace lily {
} }
void type_manager::register_constructors(std::shared_ptr<type_env> env) { void type_manager::register_constructors(std::shared_ptr<type_env> env) {
for(auto& type_ref : types) { for(int i = 0; i < types.size(); i++) {
std::unique_ptr<type>& type_ref = types[i];
type_data* data_type = dynamic_cast<type_data*>(type_ref.get()); type_data* data_type = dynamic_cast<type_data*>(type_ref.get());
if(!data_type) continue; if(!data_type) continue;