-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy path4.scrbl
More file actions
151 lines (103 loc) · 5.38 KB
/
4.scrbl
File metadata and controls
151 lines (103 loc) · 5.38 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#lang scribble/manual
@(require "../defns.rkt")
@title[#:tag "Assignment 4" #:style 'unnumbered]{Assignment 4: Case}
@(require (for-label a86/ast (except-in racket ...)))
@bold{Due: @assign-deadline[4]}
The goal of this assignment is to extend the language developed in
@secref{a3-dupe-plus} with another new form of control flow expressions:
@racket[case]-expressions.
@bold{Note:} you will need to carry forward all of the changes you
implemented for @secref{a3-dupe-plus}.
@section[#:tag-prefix "a4-" #:style 'unnumbered]{Dupe++}
The Dupe++ language extends @secref{a3-dupe-plus} in the follow ways:
@itemlist[
@item{adding @racket[case].}
]
@subsection[#:tag-prefix "a4-" #:style 'unnumbered]{Case expressions}
The following new case form is included in Dupe++:
@racketblock[
(case _ev
[(_d1 ...) _e1]
...
[else _en])
]
The @racket[case] expression form is a mechanism for dispatching
between a number of possible expressions based on a value, much like
C's notion of a @tt{switch}-statement.
The meaning of a @racket[case] expression is computed by evaluating
the expression @racket[_ev] and then proceeding in order through each
clause until one is found that has a datum @racket[_di] equal to
@racket[_ev]'s value. Once such a clause is found, the corresponding
expression @racket[_ei] is evaluated and its value is the value of the
@racket[case] expression. If no such clause exists, expression
@racket[_en] is evaluated and its value is the value of the
@racket[case] expression.
Note that each clause consists of a parenthesized list of
@emph{datums}, which in the setting of Dupe means either integer or
boolean literals.
For the purposes of this assignment, we will assume every
@racket[case] expression ends in an @racket[else] clause, even though
this is not true in general for Racket. The parser will reject any
@racket[case]-expression that does not end in @racket[else].
@section[#:tag-prefix "a4-" #:style 'unnumbered]{Implementing Dupe++}
You must extend the interpreter and compiler to implement Dupe++. You
are given a file @tt{dupe-plus-plus.zip} on ELMS with a starter
compiler based on the @secref{Dupe} language we studied in class.
You may use any a86 instructions you'd like, however it is possible to
complete the assignment using @racket[Cmp], @racket[Je], @racket[Jg],
@racket[Jmp], @racket[Label], @racket[Mov], and @racket[Sub].
@section[#:tag-prefix "a4-" #:style 'unnumbered #:tag "parse"]{Parsing Dupe++}
In the past, designing the AST type and structure definitions has
given students some grief. Getting stuck at this point means you
can't make any progress on the assignment and making a mistake at this
level can cause real trouble down the line for your compiler.
For that reason, let us give you a strong hint for a potential design
of the ASTs and examples of how parsing could work. You are not
required to follow this design, but you certainly may.
Here's the AST definition for @racket[case]:
@#reader scribble/comment-reader
(racketblock
;; type Expr = ...
;; | (Case Expr [Listof [Listof Datum]] [Listof Expr] Expr)
;; type Datum = Integer | Boolean
(struct Case (e ds cs el) #:prefab)
)
There is one new kind of expression constructor: @racket[Case]. A
@racket[Case] AST node contains four things: an expression that is the
subject of the dispatch (i.e. the expression that is evaluated to
determine which clause should be taken), a list of lists of datums, an
equal length list of expressions, and an @racket[else]-clause
expression. A @emph{datum} is either an integer or a boolean.
Here are some examples of how concrete expressions are parsed into
ASTs using this representation:
@itemlist[
@item{@racket[(case (add1 3) [else 2])] parses as @racket[(Case (Prim1
'add1 (Lit 3)) '() '() (Lit 2))].}
@item{@racket[(case 4 [(4) 1] [else 2])] parses as @racket[(Case (Lit
4) (list (list 4)) (list (Lit 1)) (Lit 2))],}
@item{@racket[(case 4 [(4 5 6) 1] [else 2])] parses as @racket[(Case (Lit
4) (list (list 4 5 6)) (list (Lit 1)) (Lit 2))], and}
@item{@racket[(case 4 [(4 5 6) 1] [(#t #f) 7] [else 2])] parses as @racket[(Case (Lit
4) (list (list 4 5 6) (list #t #f)) (list (Lit 1) (Lit 7)) (Lit 2))].}
]
@section[#:tag-prefix "a4-" #:style 'unnumbered]{Implementing case}
Implement the @racket[case] expression form as described earlier.
To do this, you should:
@itemlist[
@item{Study @tt{ast.rkt} to understand how this new form of expression is represented.}
@item{Add test cases to @tt{test/test-runner.rkt}. These will be
tested with both the interpreter and compiler.}
@item{Update @tt{interp.rkt} to correctly interpret @racket[case] expressions.}
@item{Bring forward all of the changes you made to the interpreter from @secref{a3-dupe-plus}.}
@item{Test your interpreter with @tt{raco test test/interp.rkt}.}
@item{Make examples of @racket[case]-expressions and potential translations of them
to assembly.}
@item{Update @tt{compile.rkt} to correctly compile @racket[case] expressions based on your examples.}
@item{Bring forward all of the changes you made to the compiler from @secref{a3-dupe-plus}.}
@item{Test your interpreter with @tt{raco test test/compile.rkt}.}
]
Note that only a small number of tests are given to you, so you should
write additional test cases.
@section[#:tag-prefix "a4-" #:style 'unnumbered]{Submitting}
To submit, use @tt{make} from within the @tt{dupe-plus-plus} directory to
create a zip file containing your work and submit it to Gradescope.