Skip to content

Commit 3823f1f

Browse files
committed
wip
1 parent 500b9ea commit 3823f1f

File tree

5 files changed

+29
-15
lines changed

5 files changed

+29
-15
lines changed

play/patterns.zion

+12-12
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ type Pattern is {
2222
}
2323

2424
fn pattern_from_type(t DataType) Pattern {
25-
match t {
26-
DataType(typename, ctors) {
27-
ctor_patterns := new [CtorPatternValue]
28-
for ctor in ctors {
29-
arg_patterns := new [Pattern]
30-
for arg_type in ctor.arg_types {
31-
append(arg_patterns, AllOf(arg_type))
32-
}
33-
append(ctor_patterns, CtorPatternValue(typename, ctor.name, arg_patterns))
34-
}
35-
return CtorPatterns(ctor_patterns)
25+
match t {
26+
DataType(typename, ctors) {
27+
ctor_patterns := new [CtorPatternValue]
28+
for ctor in ctors {
29+
arg_patterns := new [Pattern]
30+
for arg_type in ctor.arg_types {
31+
append(arg_patterns, AllOf(arg_type))
3632
}
37-
}
33+
append(ctor_patterns, CtorPatternValue(typename, ctor.name, arg_patterns))
34+
}
35+
return CtorPatterns(ctor_patterns)
36+
}
37+
}
3838
}
3939

4040
type Pair T S is Pair(lhs T, rhs S)

runtime/zion_rt.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ const char *zion_strerror(int errnum, char *buf, int64_t bufsize) {
114114
}
115115
void *zion_malloc(uint64_t cb) {
116116
void *pb = GC_MALLOC(cb);
117-
// printf("allocated %" PRId64 " bytes at 0x%08" PRIx64 "\n", cb, (uint64_t)pb);
117+
printf("allocated %" PRId64 " bytes at 0x%08" PRIx64 "\n", cb, (uint64_t)pb);
118118
return pb;
119119
}
120120

src/match.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,10 @@ void difference(Location location,
458458
} else if (lhs.args.size() == 0) {
459459
send(theNothing);
460460
} else {
461-
assert(lhs.args.size() == rhs.args.size());
461+
if (lhs.args.size() != rhs.args.size()) {
462+
throw zion::user_error(location, "pattern %s does not match pattern %s",
463+
lhs.str().c_str(), rhs.str().c_str());
464+
}
462465
size_t i = 0;
463466
auto send_ctor_pattern = [location, &i, &lhs, &send](Pattern::ref arg) {
464467
if (dyncast<const Nothing>(arg)) {

src/parser.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1758,7 +1758,8 @@ const Predicate *parse_ctor_predicate(ParseState &ps,
17581758
} else {
17591759
return new CtorPredicate(
17601760
ctor_name.location,
1761-
{new TuplePredicate(location, params, maybe<Identifier>())}, ctor_name,
1761+
params,
1762+
ctor_name,
17621763
name_assignment);
17631764
}
17641765
}

src/types.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,16 @@ types::Refs get_ctor_param_terms(Location location,
10061006
assert(outer_ctor_terms.size() >= 1);
10071007

10081008
types::Refs ctor_param_terms = get_ctor_param_terms(outer_ctor_terms);
1009+
if (params_count > 1) {
1010+
/* no-unary-tuple: ugh, this is so hacky */
1011+
if (ctor_param_terms.size() == 1) {
1012+
if (auto tuple_type = dyncast<const types::TypeTuple>(ctor_param_terms.front())) {
1013+
ctor_param_terms = tuple_type->dimensions;
1014+
} else {
1015+
throw zion::user_error(location, "wrong number of params given to pattern");
1016+
}
1017+
}
1018+
}
10091019

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

0 commit comments

Comments
 (0)