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

Allow defaults in function signatures with syntax (..., p = e, ...). #30

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions include/artic/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1640,6 +1640,24 @@ struct ImplicitParamPtrn : public Ptrn {
void print(Printer&) const override;
};

struct DefaultParamPtrn : public Ptrn {
Ptr<Ptrn> underlying;
Ptr<Expr> default_expr;

DefaultParamPtrn(const Loc& loc, Ptr<Ptrn>&& underlying, Ptr<Expr>&& default_expr)
: Ptrn(loc), underlying(std::move(underlying)), default_expr(std::move(default_expr))
{}

bool is_trivial() const override;

void emit(Emitter&, const thorin::Def*) const override;
const artic::Type* infer(TypeChecker&) override;
const artic::Type* check(TypeChecker&, const artic::Type*) override;
void bind(NameBinder&) override;
void resolve_summons(Summoner&) override;
void print(Printer&) const override;
};

/// A pattern that matches against a structure field.
struct FieldPtrn : public Ptrn {
Identifier id;
Expand Down
4 changes: 4 additions & 0 deletions src/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,10 @@ bool ImplicitParamPtrn::is_trivial() const {
return underlying->is_trivial();
}

bool DefaultParamPtrn::is_trivial() const {
return underlying->is_trivial();
}

void FieldPtrn::collect_bound_ptrns(std::vector<const IdPtrn*>& bound_ptrns) const {
if (ptrn)
ptrn->collect_bound_ptrns(bound_ptrns);
Expand Down
5 changes: 5 additions & 0 deletions src/bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,11 @@ void ImplicitParamPtrn::bind(artic::NameBinder& binder) {
underlying->bind(binder);
}

void DefaultParamPtrn::bind(artic::NameBinder& binder) {
underlying->bind(binder);
default_expr->bind(binder);
}

void FieldPtrn::bind(NameBinder& binder) {
if (ptrn) binder.bind(*ptrn);
}
Expand Down
40 changes: 40 additions & 0 deletions src/check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,33 @@ const artic::Type* CallExpr::infer(TypeChecker& checker) {
auto [ref_type, callee_type] = remove_ref(checker.infer(*callee));
if (auto fn_type = callee_type->isa<artic::FnType>()) {
checker.coerce(callee, fn_type);

artic::ast::TupleExpr* args_tuple = arg->isa<TupleExpr>();

const artic::TupleType* from_tuple = fn_type->dom->isa<artic::TupleType>();
const artic::ast::PathExpr* path_expr = callee_path(callee.get());
const artic::ast::FnDecl* fn_decl = nullptr;
const artic::ast::FnExpr* decl = nullptr;
const artic::ast::TuplePtrn* fn_params = nullptr;

if (path_expr) {
fn_decl = path_expr->path.start_decl->isa<FnDecl>();
if (fn_decl) {
decl = fn_decl->fn.get();
fn_params = decl->param->isa<TuplePtrn>();

if (fn_params) {
for (int i = args_tuple->args.size(); i < fn_params->args.size(); i++) {
auto default_param = fn_params->args[i]->isa<DefaultParamPtrn>();
if (default_param) {
auto default_expr = default_param->default_expr.get();
args_tuple->args.push_back(std::unique_ptr<Expr>(default_expr));
}
}
}
}
}

checker.coerce(arg, fn_type->dom);
return fn_type->codom;
} else {
Expand Down Expand Up @@ -1882,6 +1909,19 @@ const artic::Type * ImplicitParamPtrn::check(artic::TypeChecker& checker, const
return checker.type_table.implicit_param_type(underlying->type);
}

//TODO: we can use the default expression to infer the type of this pattern, and need to check it as well.
const artic::Type* DefaultParamPtrn::infer(artic::TypeChecker& checker) {
checker.infer(*default_expr);
checker.check(*underlying, default_expr->type);
return default_expr->type;
}

const artic::Type *DefaultParamPtrn::check(artic::TypeChecker& checker, const artic::Type* expected) {
checker.check(*underlying, expected);
checker.check(*default_expr, expected);
return checker.type_table.implicit_param_type(underlying->type);
}

const artic::Type* FieldPtrn::check(TypeChecker& checker, const artic::Type* expected) {
return checker.check(*ptrn, expected);
}
Expand Down
4 changes: 4 additions & 0 deletions src/emit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1786,6 +1786,10 @@ void ImplicitParamPtrn::emit(artic::Emitter& emitter, const thorin::Def* value)
underlying->emit(emitter, value);
}

void DefaultParamPtrn::emit(artic::Emitter& emitter, const thorin::Def* value) const {
underlying->emit(emitter, value);
}

void FieldPtrn::emit(Emitter& emitter, const thorin::Def* value) const {
emitter.emit(*ptrn, value);
}
Expand Down
16 changes: 13 additions & 3 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ Ptr<ast::Ptrn> Parser::parse_ptrn(bool allow_types, bool allow_implicits) {
ahead().tag() == Token::LBracket ||
ahead().tag() == Token::LParen ||
ahead().tag() == Token::LBrace ||
(allow_types && ahead().tag() != Token::Colon && ahead().tag() != Token::As)) {
(allow_types && ahead().tag() != Token::Colon && ahead().tag() != Token::As && ahead().tag() != Token::Eq)) {
auto path = parse_path(std::move(id), true);
if (ahead().tag() == Token::LBrace)
ptrn = parse_record_ptrn(std::move(path));
Expand All @@ -311,8 +311,13 @@ Ptr<ast::Ptrn> Parser::parse_ptrn(bool allow_types, bool allow_implicits) {
return make_ptr<ast::TypedPtrn>(path.loc, Ptr<ast::Ptrn>(), std::move(type));
} else
ptrn = parse_ctor_ptrn(std::move(path));
} else
} else {
ptrn = parse_id_ptrn(std::move(id), false);
if (allow_implicits && accept(Token::Eq)) {
auto default_expr = parse_expr();
ptrn = make_ptr<ast::DefaultParamPtrn>(ptrn->loc, std::move(ptrn), std::move(default_expr));
}
}
}
break;
case Token::Mut:
Expand Down Expand Up @@ -349,7 +354,12 @@ Ptr<ast::Ptrn> Parser::parse_ptrn(bool allow_types, bool allow_implicits) {
ptrn = parse_error_ptrn();
break;
}
return parse_typed_ptrn(std::move(ptrn));
ptrn = parse_typed_ptrn(std::move(ptrn));
if (allow_implicits && accept(Token::Eq)) {
auto default_expr = parse_expr();
ptrn = make_ptr<ast::DefaultParamPtrn>(ptrn->loc, std::move(ptrn), std::move(default_expr));
}
return ptrn;
}

Ptr<ast::Ptrn> Parser::parse_typed_ptrn(Ptr<ast::Ptrn>&& ptrn) {
Expand Down
7 changes: 7 additions & 0 deletions src/print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,13 @@ void ImplicitParamPtrn::print(Printer& p) const {
underlying->print(p);
}

void DefaultParamPtrn::print(Printer& p) const {
p << log::keyword_style("default") << ' ';
underlying->print(p);
p << " = ";
default_expr->print(p);
}

void FieldPtrn::print(Printer& p) const {
if (is_etc()) {
p << "...";
Expand Down
6 changes: 6 additions & 0 deletions src/summoner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ void ImplicitParamPtrn::resolve_summons(artic::Summoner& summoner) {
summoner.insert(underlying->type, underlying->to_expr());
}

void DefaultParamPtrn::resolve_summons(artic::Summoner& summoner) {
default_expr->resolve_summons(summoner);

underlying->resolve_summons(summoner);
}

void FieldPtrn::resolve_summons(artic::Summoner& summoner) {
if (ptrn) ptrn->resolve_summons(summoner);
}
Expand Down
Loading