Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tuple args #25

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wip
  • Loading branch information
wbbradley committed Jul 12, 2020
commit 602e30f86e031b40f107c41325bcbce61076aa8e
1 change: 1 addition & 0 deletions local.vimrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ let g:ale_cpp_clang_options=substitute(system("pkg-config bdw-gc --cflags") . '
\ system("llvm-config --cxxflags"), '\n', '', 'g')
let g:ale_c_clang_options=substitute(system("pkg-config bdw-gc --cflags") . ' ' .
\ system("llvm-config --cflags"), '\n', '', 'g')
set makeprg=make\ debug
4 changes: 1 addition & 3 deletions src/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,7 @@ tarjan::Vertices get_free_vars(
return get_free_vars(lambda->body, new_bound_vars);
} else if (auto application = dcast<const ast::Application *>(expr)) {
tarjan::Vertices free_vars = get_free_vars(application->a, bound_vars);
for (auto &param : application->params) {
set_merge(free_vars, get_free_vars(param, bound_vars));
}
set_merge(free_vars, get_free_vars(application->b, bound_vars));
return free_vars;
} else if (auto let = dcast<const ast::Let *>(expr)) {
tarjan::Vertices free_vars = get_free_vars(let->value, bound_vars);
Expand Down
8 changes: 4 additions & 4 deletions src/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,9 @@ std::shared_ptr<Compilation> merge_compilation(
for (auto pair : module_rebound->ctor_id_map) {
if (in(pair.first, ctor_id_map)) {
throw user_error(INTERNAL_LOC(),
"ctor_id %s already exists in ctor_id_map but is trying to be added by module %s!",
pair.first.c_str(),
module_rebound->name.c_str());
"ctor_id %s already exists in ctor_id_map but is "
"trying to be added by module %s!",
pair.first.c_str(), module_rebound->name.c_str());
}
ctor_id_map[pair.first] = pair.second;
}
Expand All @@ -302,7 +302,7 @@ std::shared_ptr<Compilation> merge_compilation(
program_filename, program_name,
new Program(program_decls, program_type_classes, program_instances,
new Application(new Var(make_iid("main")),
{unit_expr(INTERNAL_LOC())})),
unit_expr(INTERNAL_LOC()))),
comments, link_ins, DataCtorsMap{data_ctors_map, ctor_id_map}, type_env);
}

Expand Down
25 changes: 10 additions & 15 deletions src/gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ struct DeferGuard {
/* emit calls to deferred closures */
for (auto closure_iter = defer_closures.rbegin();
closure_iter != defer_closures.rend(); ++closure_iter) {
std::vector<llvm::Value *> args{
llvm::Constant::getNullValue(builder.getInt8Ty()->getPointerTo())};
llvm_create_closure_callsite(INTERNAL_LOC(), builder, *closure_iter,
args);
llvm::Value *arg = llvm::Constant::getNullValue(
builder.getInt8Ty()->getPointerTo());
llvm_create_closure_callsite(INTERNAL_LOC(), builder, *closure_iter, arg);
}

called = true;
Expand Down Expand Up @@ -162,9 +161,7 @@ void get_free_vars(const ast::Expr *expr,
#endif
} else if (auto application = dcast<const ast::Application *>(expr)) {
get_free_vars(application->a, typing, globals, locals, free_vars);
for (auto &param : application->params) {
get_free_vars(param, typing, globals, locals, free_vars);
}
get_free_vars(application->b, typing, globals, locals, free_vars);
} else if (auto let = dcast<const ast::Let *>(expr)) {
// TODO: allow let-rec
get_free_vars(let->value, typing, globals, locals, free_vars);
Expand Down Expand Up @@ -1097,22 +1094,20 @@ ResolutionStatus gen(std::string name,
debug_above(4, log("applying (%s :: %s) (%s :: ...TODO...)...",
application->a->str().c_str(),
typing.at(application->a)->str().c_str(),
join_str(application->params, ", ").c_str()));
typing.at(application->b)->str().c_str()));

llvm::Value *closure = gen(builder, llvm_module, defer_guard,
break_to_block, continue_to_block,
application->a, typing, type_env,
gen_env_globals, gen_env_locals, globals);

std::vector<llvm::Value *> args;
for (auto &param : application->params) {
args.push_back(gen(builder, llvm_module, defer_guard, break_to_block,
continue_to_block, param, typing, type_env,
gen_env_globals, gen_env_locals, globals));
}
llvm::Value *arg = gen(builder, llvm_module, defer_guard, break_to_block,
continue_to_block, application->b, typing,
type_env, gen_env_globals, gen_env_locals,
globals);

publish(llvm_create_closure_callsite(application->get_location(), builder,
closure, args));
closure, arg));
return rs_cache_resolution;
} else if (auto let = dcast<const ast::Let *>(expr)) {
llvm::Value *let_value = nullptr;
Expand Down
20 changes: 2 additions & 18 deletions src/import_rules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,6 @@ namespace {

using namespace ::zion::ast;

std::vector<const Expr *> rewrite_exprs(
const RewriteImportRules &rewrite_import_rules,
const std::vector<const Expr *> &exprs);

const Predicate *rewrite_predicate(
const RewriteImportRules &rewrite_import_rules,
const Predicate *predicate) {
Expand Down Expand Up @@ -190,9 +186,8 @@ PatternBlocks rewrite_pattern_blocks(
const Application *rewrite_application(
const RewriteImportRules &rewrite_import_rules,
const Application *application) {
return new Application(
rewrite_expr(rewrite_import_rules, application->a),
rewrite_exprs(rewrite_import_rules, application->params));
return new Application(rewrite_expr(rewrite_import_rules, application->a),
rewrite_expr(rewrite_import_rules, application->b));
}

const Expr *rewrite_expr(const RewriteImportRules &rewrite_import_rules,
Expand Down Expand Up @@ -277,17 +272,6 @@ const Expr *rewrite_expr(const RewriteImportRules &rewrite_import_rules,
return {};
}

std::vector<const Expr *> rewrite_exprs(
const RewriteImportRules &rewrite_import_rules,
const std::vector<const Expr *> &exprs) {
std::vector<const Expr *> new_exprs;
new_exprs.reserve(exprs.size());
for (auto expr : exprs) {
new_exprs.push_back(rewrite_expr(rewrite_import_rules, expr));
}
return new_exprs;
}

std::vector<const Decl *> rewrite_decls(
const RewriteImportRules &rewrite_import_rules,
const std::vector<const Decl *> &decls) {
Expand Down
24 changes: 9 additions & 15 deletions src/infer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,17 @@ types::Ref infer_core(const Expr *expr,
auto t1 = infer(application->a, data_ctors_map, return_type,
scheme_resolver, tracked_types, constraints,
instance_requirements);
types::Ref param_type;
param_type = infer(application->b, data_ctors_map, return_type,
scheme_resolver, tracked_types, constraints,
instance_requirements);
auto t2 = type_params(param_types);
auto t2 = infer(application->b, data_ctors_map, return_type,
scheme_resolver, tracked_types, constraints,
instance_requirements);
auto tv = type_variable(expr->get_location());
append_to_constraints(
constraints, t1, type_arrow(application->get_location(), t2, tv),
make_context(application->get_location(),
"(%s :: %s) applied to ((%s) :: %s) results in type %s",
"(%s :: %s) applied to (%s :: %s) results in type %s",
application->a->str().c_str(), t1->str().c_str(),
join_str(application->params, ", ").c_str(),
t2->str().c_str(), tv->str().c_str()));
application->b->str().c_str(), t2->str().c_str(),
tv->str().c_str()));
return tv;
} else if (auto let = dcast<const Let *>(expr)) {
auto t1 = infer(let->value, data_ctors_map, return_type, scheme_resolver,
Expand Down Expand Up @@ -142,13 +140,9 @@ types::Ref infer_core(const Expr *expr,
make_context(defer->get_location(),
"defer must call nullary function"));

if (defer->application->params.size() != 1) {
throw user_error(defer->get_location(),
"incorrect number of arguments passed to deferred call");
}
auto param_type = infer(defer->application->params[0], data_ctors_map,
return_type, scheme_resolver, tracked_types,
constraints, instance_requirements);
auto param_type = infer(defer->application->b, data_ctors_map, return_type,
scheme_resolver, tracked_types, constraints,
instance_requirements);
auto t2 = type_params({param_type});
append_to_constraints(
constraints, t1,
Expand Down
4 changes: 3 additions & 1 deletion src/llvm_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -728,10 +728,12 @@ void destructure_closure(llvm::IRBuilder<> &builder,
llvm::Value *llvm_create_closure_callsite(Location location,
llvm::IRBuilder<> &builder,
llvm::Value *closure,
std::vector<llvm::Value *> args) {
llvm::Value *arg) {
llvm::Value *llvm_function_to_call = nullptr;
destructure_closure(builder, closure, &llvm_function_to_call, nullptr);

std::vector<llvm::Value *> args;
args.push_back(arg);
args.push_back(builder.CreateBitCast(
closure, builder.getInt8Ty()->getPointerTo(), "closure_cast"));

Expand Down
2 changes: 1 addition & 1 deletion src/llvm_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,6 @@ std::vector<llvm::Type *> llvm_get_types(
llvm::Value *llvm_create_closure_callsite(Location location,
llvm::IRBuilder<> &builder,
llvm::Value *closure,
std::vector<llvm::Value *> args);
llvm::Value *arg);

} // namespace zion
5 changes: 2 additions & 3 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,7 @@ const Expr *parse_postfix_expr(ParseState &ps) {
true /*force_cast*/);
break;
case tk_lparen: {
/* function call or implicit partial application (implicit lambda) */
/* function call */
auto location = ps.token.location;
ps.advance();
if (ps.token.tk == tk_rparen) {
Expand All @@ -1089,8 +1089,7 @@ const Expr *parse_postfix_expr(ParseState &ps) {
expect_token(tk_rparen);
}
}
expr = new Application(expr, callsite_refs);

expr = new Application(expr, std::move(callsite_refs));
chomp_token(tk_rparen);
}
break;
Expand Down
2 changes: 1 addition & 1 deletion src/prefix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ const Application *prefix_application(const std::set<std::string> &bindings,
std::string pre,
const Application *application) {
return new Application(prefix(bindings, pre, application->a),
prefix(bindings, pre, application->params));
prefix(bindings, pre, application->b));
}

const Expr *prefix(const std::set<std::string> &bindings,
Expand Down
23 changes: 7 additions & 16 deletions src/translate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,7 @@ const Expr *texpr(const types::DefnId &for_defn_id,
} else if (auto application = dcast<const Application *>(expr)) {
types::Ref operator_type = get_tracked_type(tracked_types,
application->a);
types::Refs operand_types;
for (auto &param : application->params) {
operand_types.push_back(get_tracked_type(tracked_types, param));
}
types::Ref operand_type = type_params(operand_types);
types::Ref operand_type = get_tracked_type(tracked_types, application->b);

/* if we have unresolved types below us in the tree, we need to
* propagate our known types down into them */
Expand All @@ -164,17 +160,12 @@ const Expr *texpr(const types::DefnId &for_defn_id,
auto a = texpr(for_defn_id, application->a, data_ctors_map, bound_vars,
tracked_types, operator_type, type_env, typing,
needed_defns, returns);
std::vector<const Expr *> new_params;
assert(operand_types.size() == application->params.size());
for (size_t i = 0; i < application->params.size(); ++i) {
/* translate all the parameters */
auto &param = application->params[i];
new_params.push_back(
texpr(for_defn_id, param, data_ctors_map, bound_vars, tracked_types,
operand_types[i]->rebind(unification.bindings), type_env,
typing, needed_defns, returns));
}
auto new_app = new Application(a, {new_params});
/* translate the parameter */
const Expr *new_param = texpr(for_defn_id, application->b, data_ctors_map,
bound_vars, tracked_types,
operand_type->rebind(unification.bindings),
type_env, typing, needed_defns, returns);
auto new_app = new Application(a, new_param);
typing[new_app] = type;
return new_app;
} else if (auto let = dcast<const Let *>(expr)) {
Expand Down
13 changes: 3 additions & 10 deletions src/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,16 +795,6 @@ types::Ref type_map(types::Ref a, types::Ref b) {
type_operator(type_id(Identifier{"Map", a->get_location()}), a), b);
}

types::Ref type_params(const types::Refs &params) {
#ifdef ZION_DEBUG
for (auto &param : params) {
assert(!dyncast<const types::TypeParams>(param));
}
#endif
assert(params.size() > 0);
return std::make_shared<types::TypeParams>(params[0]->get_location(), params);
}

types::TypeTuple::Ref type_tuple(types::Refs dimensions) {
assert(dimensions.size() != 0);
return type_tuple(dimensions[0]->get_location(), dimensions);
Expand Down Expand Up @@ -835,6 +825,8 @@ types::Ref type_arrows(types::Refs types) {
}

types::Refs unfold_arrows(types::Ref type) {
assert(false);
#if 0
auto op = dyncast<const types::TypeOperator>(type);
if (op != nullptr) {
auto nested_op = dyncast<const types::TypeOperator>(op->oper);
Expand All @@ -849,6 +841,7 @@ types::Refs unfold_arrows(types::Ref type) {
}
}
}
#endif
return {type};
}

Expand Down