Skip to content

Commit fc2d482

Browse files
committed
Finish the fold drivers in fold.rs.
1 parent 5826a2e commit fc2d482

File tree

2 files changed

+110
-12
lines changed

2 files changed

+110
-12
lines changed

src/comp/front/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ tag stmt_ {
7777
type decl = spanned[decl_];
7878
tag decl_ {
7979
decl_local(ident, option[@ty], option[@expr]);
80-
decl_item(name, @item);
80+
decl_item(ident, @item);
8181
}
8282

8383
type expr = spanned[expr_];

src/comp/middle/fold.rs

Lines changed: 109 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import util.common.ty_mach;
1111
import front.ast;
1212
import front.ast.ident;
1313
import front.ast.name;
14+
import front.ast.path;
1415
import front.ast.ty;
1516
import front.ast.expr;
1617
import front.ast.lval;
@@ -38,6 +39,11 @@ type ast_fold[ENV] =
3839
(fn(&ENV e, &span sp) -> @ty) fold_ty_char,
3940
(fn(&ENV e, &span sp) -> @ty) fold_ty_str,
4041
(fn(&ENV e, &span sp, @ty t) -> @ty) fold_ty_box,
42+
(fn(&ENV e, &span sp, @ty t) -> @ty) fold_ty_vec,
43+
44+
(fn(&ENV e, &span sp,
45+
vec[tup(bool, @ty)] elts) -> @ty) fold_ty_tup,
46+
4147
(fn(&ENV e, &span sp, ast.path p,
4248
&option[referent] r) -> @ty) fold_ty_path,
4349

@@ -94,7 +100,7 @@ type ast_fold[ENV] =
94100
&option[@expr]) -> @decl) fold_decl_local,
95101

96102
(fn(&ENV e, &span sp,
97-
&name name, @item item) -> @decl) fold_decl_item,
103+
ident ident, @item item) -> @decl) fold_decl_item,
98104

99105

100106
// Stmt folds.
@@ -136,6 +142,7 @@ type ast_fold[ENV] =
136142
(fn(&ENV e, @ast.crate c) -> ENV) update_env_for_crate,
137143
(fn(&ENV e, @item i) -> ENV) update_env_for_item,
138144
(fn(&ENV e, @stmt s) -> ENV) update_env_for_stmt,
145+
(fn(&ENV e, @decl i) -> ENV) update_env_for_decl,
139146
(fn(&ENV e, @lval l) -> ENV) update_env_for_lval,
140147
(fn(&ENV e, @expr x) -> ENV) update_env_for_expr,
141148
(fn(&ENV e, @ty t) -> ENV) update_env_for_ty,
@@ -147,19 +154,94 @@ type ast_fold[ENV] =
147154

148155
//// Fold drivers.
149156

150-
// FIXME: Finish these.
151-
152-
fn fold_expr_name[ENV](&ENV env, ast_fold[ENV] fld, &name n,
153-
&option[referent] r) -> tup(name,option[referent]) {
154-
ret tup(n,r);
157+
fn fold_name[ENV](&ENV env, ast_fold[ENV] fld, &name n) -> name {
158+
let vec[@ast.ty] tys_ = vec();
159+
for (@ast.ty t in n.node.types) {
160+
append[@ast.ty](tys_, fold_ty(env, fld, t));
161+
}
162+
let ast.name_ n_ = rec(ident=n.node.ident, types=tys_);
163+
ret fld.fold_name(env, n.span, n_);
155164
}
156165

157166
fn fold_ty[ENV](&ENV env, ast_fold[ENV] fld, @ty t) -> @ty {
158-
ret t;
167+
let ENV env_ = fld.update_env_for_ty(env, t);
168+
169+
if (!fld.keep_going(env_)) {
170+
ret t;
171+
}
172+
173+
alt (t.node) {
174+
case (ast.ty_nil) { ret fld.fold_ty_nil(env_, t.span); }
175+
case (ast.ty_bool) { ret fld.fold_ty_bool(env_, t.span); }
176+
case (ast.ty_int) { ret fld.fold_ty_int(env_, t.span); }
177+
case (ast.ty_uint) { ret fld.fold_ty_uint(env_, t.span); }
178+
179+
case (ast.ty_machine(?m)) {
180+
ret fld.fold_ty_machine(env_, t.span, m);
181+
}
182+
183+
case (ast.ty_char) { ret fld.fold_ty_char(env_, t.span); }
184+
case (ast.ty_str) { ret fld.fold_ty_str(env_, t.span); }
185+
186+
case (ast.ty_box(?ty)) {
187+
auto ty_ = fold_ty(env, fld, ty);
188+
ret fld.fold_ty_box(env_, t.span, ty_);
189+
}
190+
191+
case (ast.ty_vec(?ty)) {
192+
auto ty_ = fold_ty(env, fld, ty);
193+
ret fld.fold_ty_vec(env_, t.span, ty_);
194+
}
195+
196+
case (ast.ty_tup(?elts)) {
197+
let vec[tup(bool, @ty)] elts_ = vec();
198+
for (tup(bool, @ty) elt in elts) {
199+
elts_ += tup(elt._0, fold_ty(env, fld, elt._1));
200+
}
201+
ret fld.fold_ty_tup(env_, t.span, elts);
202+
}
203+
204+
case (ast.ty_path(?pth, ?ref_opt)) {
205+
let vec[ast.name] path = vec();
206+
for (ast.name n in pth) {
207+
path += fold_name(env, fld, n);
208+
}
209+
ret fld.fold_ty_path(env_, t.span, path, ref_opt);
210+
}
211+
}
159212
}
160213

161214
fn fold_decl[ENV](&ENV env, ast_fold[ENV] fld, @decl d) -> @decl {
162-
ret d;
215+
let ENV env_ = fld.update_env_for_decl(env, d);
216+
217+
if (!fld.keep_going(env_)) {
218+
ret d;
219+
}
220+
221+
alt (d.node) {
222+
case (ast.decl_local(?id, ?ty_opt, ?expr_opt)) {
223+
auto ty_opt_ = none[@ast.ty];
224+
auto expr_opt_ = none[@ast.expr];
225+
alt (ty_opt) {
226+
case (some[@ast.ty](?t)) {
227+
ty_opt_ = some[@ast.ty](fold_ty(env, fld, t));
228+
}
229+
}
230+
alt (expr_opt) {
231+
case (some[@ast.expr](?e)) {
232+
expr_opt_ = some[@ast.expr](fold_expr(env, fld, e));
233+
}
234+
}
235+
ret fld.fold_decl_local(env_, d.span, id, ty_opt_, expr_opt_);
236+
}
237+
238+
case (ast.decl_item(?id, ?item)) {
239+
auto item_ = fold_item(env_, fld, item);
240+
ret fld.fold_decl_item(env_, d.span, id, item_);
241+
}
242+
}
243+
244+
fail;
163245
}
164246

165247
fn fold_lval[ENV](&ENV env, ast_fold[ENV] fld, @lval lv) -> @lval {
@@ -182,6 +264,7 @@ fn fold_lval[ENV](&ENV env, ast_fold[ENV] fld, @lval lv) -> @lval {
182264
}
183265

184266
case (ast.lval_name(?n, ?r)) {
267+
auto n_ = fold_name(env_, fld, n);
185268
ret fld.fold_lval_name(env_, lv.span, n, r);
186269
}
187270
}
@@ -424,7 +507,6 @@ fn identity_fold_name[ENV](&ENV env, &span sp, ast.name_ n) -> name {
424507
ret respan(sp, n);
425508
}
426509

427-
428510
// Type identities.
429511

430512
fn identity_fold_ty_nil[ENV](&ENV env, &span sp) -> @ty {
@@ -460,6 +542,15 @@ fn identity_fold_ty_box[ENV](&ENV env, &span sp, @ty t) -> @ty {
460542
ret @respan(sp, ast.ty_box(t));
461543
}
462544

545+
fn identity_fold_ty_vec[ENV](&ENV env, &span sp, @ty t) -> @ty {
546+
ret @respan(sp, ast.ty_vec(t));
547+
}
548+
549+
fn identity_fold_ty_tup[ENV](&ENV env, &span sp, vec[tup(bool,@ty)] elts)
550+
-> @ty {
551+
ret @respan(sp, ast.ty_tup(elts));
552+
}
553+
463554
fn identity_fold_ty_path[ENV](&ENV env, &span sp, ast.path p,
464555
&option[referent] r) -> @ty {
465556
ret @respan(sp, ast.ty_path(p, r));
@@ -549,8 +640,8 @@ fn identity_fold_decl_local[ENV](&ENV e, &span sp,
549640
}
550641

551642
fn identity_fold_decl_item[ENV](&ENV e, &span sp,
552-
&name n, @item i) -> @decl {
553-
ret @respan(sp, ast.decl_item(n, i));
643+
ident id, @item i) -> @decl {
644+
ret @respan(sp, ast.decl_item(id, i));
554645
}
555646

556647

@@ -627,6 +718,10 @@ fn identity_update_env_for_stmt[ENV](&ENV e, @stmt s) -> ENV {
627718
ret e;
628719
}
629720

721+
fn identity_update_env_for_decl[ENV](&ENV e, @decl d) -> ENV {
722+
ret e;
723+
}
724+
630725
fn identity_update_env_for_lval[ENV](&ENV e, @lval l) -> ENV {
631726
ret e;
632727
}
@@ -660,6 +755,8 @@ fn new_identity_fold[ENV]() -> ast_fold[ENV] {
660755
fold_ty_char = bind identity_fold_ty_char[ENV](_,_),
661756
fold_ty_str = bind identity_fold_ty_str[ENV](_,_),
662757
fold_ty_box = bind identity_fold_ty_box[ENV](_,_,_),
758+
fold_ty_vec = bind identity_fold_ty_vec[ENV](_,_,_),
759+
fold_ty_tup = bind identity_fold_ty_tup[ENV](_,_,_),
663760
fold_ty_path = bind identity_fold_ty_path[ENV](_,_,_,_),
664761

665762
fold_expr_vec = bind identity_fold_expr_vec[ENV](_,_,_),
@@ -698,6 +795,7 @@ fn new_identity_fold[ENV]() -> ast_fold[ENV] {
698795
update_env_for_crate = bind identity_update_env_for_crate[ENV](_,_),
699796
update_env_for_item = bind identity_update_env_for_item[ENV](_,_),
700797
update_env_for_stmt = bind identity_update_env_for_stmt[ENV](_,_),
798+
update_env_for_decl = bind identity_update_env_for_decl[ENV](_,_),
701799
update_env_for_lval = bind identity_update_env_for_lval[ENV](_,_),
702800
update_env_for_expr = bind identity_update_env_for_expr[ENV](_,_),
703801
update_env_for_ty = bind identity_update_env_for_ty[ENV](_,_),

0 commit comments

Comments
 (0)