-
Notifications
You must be signed in to change notification settings - Fork 0
/
book_list.cl
132 lines (109 loc) · 3.43 KB
/
book_list.cl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
-- example of static and dynamic type differing for a dispatch
Class Book inherits IO {
title : String;
author : String;
initBook(title_p : String, author_p : String) : Book {
{
title <- title_p;
author <- author_p;
self;
}
};
print() : Book {
{
out_string("title: ").out_string(title).out_string("\n");
out_string("author: ").out_string(author).out_string("\n");
self;
}
};
};
Class Article inherits Book {
per_title : String;
initArticle(title_p : String, author_p : String,
per_title_p : String) : Article {
{
initBook(title_p, author_p);
per_title <- per_title_p;
self;
}
};
print() : Book {
{
out_string("periodical: ").out_string(per_title).out_string("\n");
self;
}
};
};
Class BookList inherits IO {
(* Since abort "returns" type Object, we have to add
an expression of type Bool here to satisfy the typechecker.
This code is unreachable, since abort() halts the program.
*)
isNil() : Bool { { abort(); true; } };
cons(hd : Book) : Cons {
(let new_cell : Cons <- new Cons in
new_cell.init(hd,self)
)
};
(* Since abort "returns" type Object, we have to add
an expression of type Book here to satisfy the typechecker.
This code is unreachable, since abort() halts the program.
*)
car() : Book { { abort(); new Book; } };
(* Since abort "returns" type Object, we have to add
an expression of type BookList here to satisfy the typechecker.
This code is unreachable, since abort() halts the program.
*)
cdr() : BookList { { abort(); new BookList; } };
print_list() : Object { abort() };
};
Class Cons inherits BookList {
xcar : Book; -- We keep the car and cdr in attributes.
xcdr : BookList; -- Because methods and features must have different names,
-- we use xcar and xcdr for the attributes and reserve
-- car and cdr for the features.
isNil() : Bool { false };
init(hd : Book, tl : BookList) : Cons {
{
xcar <- hd;
xcdr <- tl;
self;
}
};
car() : Book { xcar };
cdr() : BookList { xcdr };
print_list() : Object {
{
case xcar.print() of
dummy : Book => out_string("- dynamic type was Book -\n");
dummy : Article => out_string("- dynamic type was Article -\n");
esac;
xcdr.print_list();
}
};
};
Class Nil inherits BookList {
isNil() : Bool { true };
print_list() : Object { true };
};
Class Main {
books : BookList;
main() : Object {
(let a_book : Book <-
(new Book).initBook("Compilers, Principles, Techniques, and Tools",
"Aho, Sethi, and Ullman")
in
(let an_article : Article <-
(new Article).initArticle("The Top 100 CD_ROMs",
"Ulanoff",
"PC Magazine")
in
{
books <- (new Nil).cons(a_book).cons(an_article);
books.print_list();
}
) -- end let an_article
) -- end let a_book
};
};