Skip to content

Commit 0857ccf

Browse files
committed
wip
1 parent a468d67 commit 0857ccf

File tree

4 files changed

+97
-3
lines changed

4 files changed

+97
-3
lines changed

lib/Language/Bel/Compiler/Gensym.pm

+7-2
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@ sub gensym {
1919
return $GENSYM_PREFIX . sprintf("%04d", ++$unique_gensym_index);
2020
}
2121

22+
sub starts_with {
23+
my ($string, $prefix) = @_;
24+
25+
return substr($string, 0, length($prefix)) eq $prefix;
26+
}
27+
2228
sub is_gensym {
2329
my ($expr) = @_;
2430

2531
return is_symbol($expr)
26-
&& substr(symbol_name($expr), 0, length($GENSYM_PREFIX))
27-
eq $GENSYM_PREFIX;
32+
&& starts_with(symbol_name($expr), $GENSYM_PREFIX);
2833
}
2934

3035
our @EXPORT_OK = qw(

lib/Language/Bel/Compiler/Pass.pm

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,20 @@ sub translate {
1616
my ($self, $program) = @_;
1717

1818
$self->check_precondition($program);
19+
my $result = $self->do_translate($program);
20+
$self->check_postcondition($result);
1921

20-
return $self->do_translate($program);
22+
return $result;
2123
}
2224

2325
sub check_precondition {
2426
# do nothing by default
2527
}
2628

29+
sub check_postcondition {
30+
# do nothing by default
31+
}
32+
2733
# @abstract
2834
sub do_translate {
2935
my ($self) = @_;

lib/Language/Bel/Compiler/Pass/Alpha.pm

+49
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use Language::Bel::Core qw(
99
is_nil
1010
is_pair
1111
is_symbol
12+
is_symbol_of_name
1213
make_pair
1314
make_symbol
1415
symbol_name
@@ -23,6 +24,7 @@ use Language::Bel::Compiler::Primitives qw(
2324
);
2425
use Language::Bel::Compiler::Gensym qw(
2526
gensym
27+
is_gensym
2628
);
2729

2830
sub new {
@@ -94,4 +96,51 @@ sub do_translate {
9496
);
9597
}
9698

99+
# @override
100+
sub check_postcondition {
101+
my ($self, $ast) = @_;
102+
103+
my $args = car(cdr(cdr($ast)));
104+
my $body = cdr(cdr(cdr($ast)));
105+
106+
assure_meets_postcondition($body);
107+
}
108+
109+
sub is_among_symbol_names {
110+
my ($value, @names) = @_;
111+
112+
for my $name (@names) {
113+
return 1
114+
if is_symbol_of_name($value, $name);
115+
}
116+
return '';
117+
}
118+
119+
sub assure_meets_postcondition {
120+
my ($value) = @_;
121+
122+
if (is_nil($value)) {
123+
return;
124+
}
125+
elsif (is_pair($value) && is_symbol_of_name(car($value), "quote")) {
126+
return;
127+
}
128+
elsif (is_pair($value)) {
129+
assure_meets_postcondition(car($value));
130+
assure_meets_postcondition(cdr($value));
131+
}
132+
elsif (is_gensym($value)) {
133+
return;
134+
}
135+
elsif (is_among_symbol_names($value, "id", "type")) {
136+
return;
137+
}
138+
elsif (is_symbol_of_name($value, "no")) {
139+
return; # XXX: should be generalized to "known globals"
140+
}
141+
else {
142+
die "Doesn't meet precondition: '", _print($value), "'";
143+
}
144+
}
145+
97146
1;

lib/Language/Bel/Compiler/Pass/Flatten.pm

+34
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use Language::Bel::Reader qw(
2323
);
2424
use Language::Bel::Compiler::Gensym qw(
2525
gensym
26+
is_gensym
2627
);
2728
use Language::Bel::Compiler::Primitives qw(
2829
car
@@ -186,4 +187,37 @@ sub do_translate {
186187
);
187188
}
188189

190+
# @override
191+
sub check_postcondition {
192+
my ($self, $ast) = @_;
193+
194+
my $body = cdr(cdr($ast));
195+
196+
while (!is_nil($body)) {
197+
die "body not a pair"
198+
unless is_pair($body);
199+
200+
my $operation = car($body);
201+
print("OPERATION: ", _print($operation), "\n");
202+
die "operation is not a pair"
203+
unless is_pair($operation);
204+
205+
# XXX: first, check if the form is (gensym := ...)
206+
# (and in that case, only allow a subset of the op types)
207+
my $op_name = car($operation);
208+
if (is_symbol_of_name($op_name, "return")) {
209+
# XXX: handle checking of 'return' operands
210+
}
211+
# (gensym_0004 (compose =) ((prim (quote id)) gensym_0003 (quote pair)))
212+
elsif (is_gensym($op_name)) {
213+
# XXX: handle the above operation more in detail
214+
}
215+
else {
216+
die "unrecognized operation: ", _print($operation);
217+
}
218+
219+
$body = cdr($body);
220+
}
221+
}
222+
189223
1;

0 commit comments

Comments
 (0)