@@ -76,48 +76,41 @@ For that reason, let us give you a strong hint for a potential design
7676of the ASTs and examples of how parsing could work. You are not
7777required to follow this design, but you certainly may.
7878
79- Here's a potential AST definition for the added primitives,
80- @racket[cond ], and @racket[case ]:
79+ Here's the AST definition for @racket[case ]:
8180
8281@#reader scribble/comment-reader
8382(racketblock
84- ;; type Expr =
85- ;; ...
86- ;; | (Case Expr [Listof CaseClause] Expr)
87-
88- ;; type CaseClause = (Clause [Listof Datum] Expr)
83+ ;; type Expr = ...
84+ ;; | (Case Expr [Listof [Listof Datum]] [Listof Expr] Expr)
8985
9086;; type Datum = Integer | Boolean
9187
92- (struct Case (e cs el) #:prefab )
88+ (struct Case (e ds cs el) #:prefab )
9389)
9490
9591There is one new kind of expression constructor: @racket[Case]. A
96- @racket[Case] AST node contains three things: an expression that is
97- the subject of the dispatch (i.e. the expression that is evaluated to
98- determine which clause should be taken), a list of case-clauses (not
99- to be confused with cond-clauses), and an @racket[else ]-clause
100- expression. Each case-clause, like a cond-clause, consists of two
101- things. Hence we re-use the @racket[Clause] structure, but with
102- different types of elements. The first element is a list of
103- @emph{datums}, each being either an integer or a boolean.
92+ @racket[Case] AST node contains four things: an expression that is the
93+ subject of the dispatch (i.e. the expression that is evaluated to
94+ determine which clause should be taken), a list of lists of datums, an
95+ equal length list of expressions, and an @racket[else ]-clause
96+ expression. A @emph{datum} is either an integer or a boolean.
10497
10598Here are some examples of how concrete expressions are parsed into
10699ASTs using this representation:
107100
108101@itemlist[
109102
110103@item{@racket[(case (add1 3 ) [else 2 ])] parses as @racket[(Case (Prim1
111- 'add1 (Lit 3 )) '() (Lit 2 ))].}
104+ 'add1 (Lit 3 )) '() '() (Lit 2 ))].}
112105
113106@item{@racket[(case 4 [(4 ) 1 ] [else 2 ])] parses as @racket[(Case (Lit
114- 4 ) (list (Clause ( list 4 ) ( Lit 1 ) )) (Lit 2 ))],}
107+ 4 ) (list (list 4 )) (list ( Lit 1 )) (Lit 2 ))],}
115108
116109@item{@racket[(case 4 [(4 5 6 ) 1 ] [else 2 ])] parses as @racket[(Case (Lit
117- 4 ) (list (Clause (list 4 5 6 ) ( Lit 1 ) )) (Lit 2 ))], and }
110+ 4 ) (list (list 4 5 6 )) (list ( Lit 1 )) (Lit 2 ))], and }
118111
119112@item{@racket[(case 4 [(4 5 6 ) 1 ] [(#t #f ) 7 ] [else 2 ])] parses as @racket[(Case (Lit
120- 4 ) (list (Clause ( list 4 5 6 ) (Lit 1 )) (Clause (list #t #f ) (Lit 7 ) )) (Lit 2 ))].}
113+ 4 ) (list (list 4 5 6 ) (list #t #f )) (list (Lit 1 ) (Lit 7 )) (Lit 2 ))].}
121114]
122115
123116
0 commit comments