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
wip
  • Loading branch information
wbbradley committed Jul 15, 2020
commit 3823f1fb132300a4523680dd717c34016930886a
24 changes: 12 additions & 12 deletions play/patterns.zion
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ type Pattern is {
}

fn pattern_from_type(t DataType) Pattern {
match t {
DataType(typename, ctors) {
ctor_patterns := new [CtorPatternValue]
for ctor in ctors {
arg_patterns := new [Pattern]
for arg_type in ctor.arg_types {
append(arg_patterns, AllOf(arg_type))
}
append(ctor_patterns, CtorPatternValue(typename, ctor.name, arg_patterns))
}
return CtorPatterns(ctor_patterns)
match t {
DataType(typename, ctors) {
ctor_patterns := new [CtorPatternValue]
for ctor in ctors {
arg_patterns := new [Pattern]
for arg_type in ctor.arg_types {
append(arg_patterns, AllOf(arg_type))
}
}
append(ctor_patterns, CtorPatternValue(typename, ctor.name, arg_patterns))
}
return CtorPatterns(ctor_patterns)
}
}
}

type Pair T S is Pair(lhs T, rhs S)
Expand Down
2 changes: 1 addition & 1 deletion runtime/zion_rt.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const char *zion_strerror(int errnum, char *buf, int64_t bufsize) {
}
void *zion_malloc(uint64_t cb) {
void *pb = GC_MALLOC(cb);
// printf("allocated %" PRId64 " bytes at 0x%08" PRIx64 "\n", cb, (uint64_t)pb);
printf("allocated %" PRId64 " bytes at 0x%08" PRIx64 "\n", cb, (uint64_t)pb);
return pb;
}

Expand Down
5 changes: 4 additions & 1 deletion src/match.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,10 @@ void difference(Location location,
} else if (lhs.args.size() == 0) {
send(theNothing);
} else {
assert(lhs.args.size() == rhs.args.size());
if (lhs.args.size() != rhs.args.size()) {
throw zion::user_error(location, "pattern %s does not match pattern %s",
lhs.str().c_str(), rhs.str().c_str());
}
size_t i = 0;
auto send_ctor_pattern = [location, &i, &lhs, &send](Pattern::ref arg) {
if (dyncast<const Nothing>(arg)) {
Expand Down
3 changes: 2 additions & 1 deletion src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1758,7 +1758,8 @@ const Predicate *parse_ctor_predicate(ParseState &ps,
} else {
return new CtorPredicate(
ctor_name.location,
{new TuplePredicate(location, params, maybe<Identifier>())}, ctor_name,
params,
ctor_name,
name_assignment);
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,16 @@ types::Refs get_ctor_param_terms(Location location,
assert(outer_ctor_terms.size() >= 1);

types::Refs ctor_param_terms = get_ctor_param_terms(outer_ctor_terms);
if (params_count > 1) {
/* no-unary-tuple: ugh, this is so hacky */
if (ctor_param_terms.size() == 1) {
if (auto tuple_type = dyncast<const types::TypeTuple>(ctor_param_terms.front())) {
ctor_param_terms = tuple_type->dimensions;
} else {
throw zion::user_error(location, "wrong number of params given to pattern");
}
}
}

if (ctor_param_terms.size() != params_count) {
throw zion::user_error(location,
Expand Down