Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
wbbradley committed Jul 15, 2020
1 parent 500b9ea commit 3823f1f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
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

0 comments on commit 3823f1f

Please sign in to comment.