-
Notifications
You must be signed in to change notification settings - Fork 0
/
sjp32
executable file
·288 lines (272 loc) · 8.4 KB
/
sjp32
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
1: Program
2: Vars
3: n Int
4: EndVars
5: Function fibon(Val n Int) Return Int
6: Vars
7: retvalue Int
8: n1 Int
9: n2 Int
10: EndVars
11: Procedure recfibon(Val n Int,Ref n1 Int,Ref n2 Int)
12: Vars
13: aux Int
14: EndVars
15: If n=0 Or n=1 Then n1:=1 n2:=1
16: Else
17: recfibon(n-1,n1,n2)
18: aux:=n1 n1:=n2 n2:=aux+n2
19: EndIf
20: EndProcedure
21: recfibon(n,n1,n2)
22: Return n2
23: EndFunction
24: n:=1
25: While (n<10) Do
26: Write("Fibonacci(") Write(n) Write(")=") WriteLn(fibon(n))
27: n:=n+1
28: EndWhile
29: EndProgram
30:
program
\__list
| \__ident(n)
| \__int
\__list
| \__function
| \__ident(fibon)
| | \__list
| | | \__val
| | | \__ident(n)
| | | \__int
| | \__int
| \__list
| | \__ident(retvalue)
| | | \__int
| | \__ident(n1)
| | | \__int
| | \__ident(n2)
| | \__int
| \__list
| | \__procedure
| | \__ident(recfibon)
| | | \__list
| | | \__val
| | | | \__ident(n)
| | | | \__int
| | | \__ref
| | | | \__ident(n1)
| | | | \__int
| | | \__ref
| | | \__ident(n2)
| | | \__int
| | \__list
| | | \__ident(aux)
| | | \__int
| | \__list
| | \__list
| | \__if
| | \__or
| | | \__=
| | | | \__ident(n)
| | | | \__intconst(0)
| | | \__=
| | | \__ident(n)
| | | \__intconst(1)
| | \__list
| | | \__:=
| | | | \__ident(n1)
| | | | \__intconst(1)
| | | \__:=
| | | \__ident(n2)
| | | \__intconst(1)
| | \__list
| | \__(
| | | \__ident(recfibon)
| | | \__list
| | | \__-
| | | | \__ident(n)
| | | | \__intconst(1)
| | | \__ident(n1)
| | | \__ident(n2)
| | \__:=
| | | \__ident(aux)
| | | \__ident(n1)
| | \__:=
| | | \__ident(n1)
| | | \__ident(n2)
| | \__:=
| | \__ident(n2)
| | \__+
| | \__ident(aux)
| | \__ident(n2)
| \__list
| | \__(
| | \__ident(recfibon)
| | \__list
| | \__ident(n)
| | \__ident(n1)
| | \__ident(n2)
| \__ident(n2)
\__list
\__:=
| \__ident(n)
| \__intconst(1)
\__while
\__<
| \__ident(n)
| \__intconst(10)
\__list
\__write
| \__string("Fibonacci(")
\__write
| \__ident(n)
\__write
| \__string(")=")
\__writeln
| \__(
| \__ident(fibon)
| \__list
| \__ident(n)
\__:=
\__ident(n)
\__+
\__ident(n)
\__intconst(1)
Type Checking:
Generating code:
program
parameters
static_link
endparameters
variables
_n 4
endvariables
aload _n t0
iload 1 t1
stor t1 t0
etiq while_1
load _n t0
iload 10 t1
lesi t0 t1 t0
fjmp t0 endwhile_1
wris "Fibonacci("
load _n t0
wrii t0
wris ")="
pushparam 0
load _n t0
pushparam t0
aload static_link t0
pushparam t0
call program_fibon
killparam
killparam
popparam t0
wrii t0
wrln
aload _n t0
load _n t1
iload 1 t2
addi t1 t2 t1
stor t1 t0
ujmp while_1
etiq endwhile_1
stop
endprogram
subroutine program_fibon_recfibon
parameters
_n
_n1
_n2
static_link
endparameters
variables
_aux 4
endvariables
load _n t0
iload 0 t1
equi t0 t1 t0
load _n t1
iload 1 t2
equi t1 t2 t1
loor t0 t1 t0
fjmp t0 else_1
load _n1 t0
iload 1 t1
stor t1 t0
load _n2 t0
iload 1 t1
stor t1 t0
ujmp endif_1
etiq else_1
load _n t0
iload 1 t1
subi t0 t1 t0
pushparam t0
load _n1 t0
pushparam t0
load _n2 t0
pushparam t0
load static_link t0
pushparam t0
call program_fibon_recfibon
killparam
killparam
killparam
killparam
aload _aux t0
load _n1 t1
load t1 t1
stor t1 t0
load _n1 t0
load _n2 t1
load t1 t1
stor t1 t0
load _n2 t0
load _aux t1
load _n2 t2
load t2 t2
addi t1 t2 t1
stor t1 t0
etiq endif_1
retu
endsubroutine
subroutine program_fibon
parameters
returnvalue
_n
static_link
endparameters
variables
_retvalue 4
_n1 4
_n2 4
endvariables
load _n t0
pushparam t0
aload _n1 t0
pushparam t0
aload _n2 t0
pushparam t0
aload static_link t0
pushparam t0
call program_fibon_recfibon
killparam
killparam
killparam
killparam
load _n2 t0
stor t0 returnvalue
retu
endsubroutine
Executing code:
Fibonacci(1)=1
Fibonacci(2)=2
Fibonacci(3)=3
Fibonacci(4)=5
Fibonacci(5)=8
Fibonacci(6)=13
Fibonacci(7)=21
Fibonacci(8)=34
Fibonacci(9)=55