-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp_logo.adb
342 lines (312 loc) · 10.9 KB
/
p_logo.adb
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
WITH Ada.Text_IO;USE Ada.Text_IO;
WITH Ada.Integer_Text_IO; USE Ada.Integer_Text_IO;
WITH Ada.Numerics; USE Ada.Numerics;
With Ada.Numerics.Elementary_Functions; USE Ada.Numerics.Elementary_Functions;
with p_fenetre; use p_fenetre;
with Ada.Unchecked_Deallocation;
PACKAGE BODY P_Logo IS
procedure build(t:in out turtle; interpreterRules:in out rules; op:in out pt_command) is
BEGIN
Op.Action:=(OTHERS=>' ');
p_fenetre.Create_Graph_Window;
t.orientation:=180;
t.xCoord:=Get_window_Height/2;
t.yCoord:=Get_window_Width/2;
interpreterRules.penDown:=true;
End build;
-----------------
-- Interpreter --
-----------------
PROCEDURE Interpreter IS
Op:Pt_Command:=new t_command;
interpreterRules:rules;
turt:turtle;
BEGIN
Build(Turt,InterpreterRules,Op);
While NOT compareStrings(Op.action,textToAction("exit")) LOOP
op:=GetCommand;
Put(op.action);
Put(op.value);
Put_line(" ");
doCommand(Op,turt,interpreterRules);
if inBufferCommands(op.action) then
put_line("dealocatting buffer");
--emptyBuffer(Op.buffer);
put_line("done");
end if;
END LOOP;
END Interpreter;
--------------------
-- CompareStrings --
--------------------
FUNCTION compareStrings (
A,
B : t_action)
RETURN Boolean IS
BEGIN
return A=B;
END compareStrings;
---------------
-- getAction --
---------------
function getCommand return pt_command is
c : character;
op:pt_command:=new t_command ;
buff:pt_buffer;
BEGIN
op.action:=(others=>' ');
get(c);
while c=' ' OR c=';' loop
get(c);
end loop;
for i in op.action'first..op.action'last loop
Ada.Text_IO.put("got character:");
put(c);
Put_line(" ");
if c=' ' OR c=';' then
Ada.Text_IO.put_line("got space character");
exit;
end if;
op.action(i):=c;
get(c);
end loop;
Ada.Text_IO.put("got command:");
p_logo.put(op.action);
Put_line(" ");
if NOT inNoValueCommands(op.action) then
put_line("getting value");
get(op.value);
end if;
if inBufferCommands(op.action) then
buff:=getBufferCommand;
op.buffer:=buff;
end if;
put_line("got whole command");
return op;
END getCommand ;
function inNoValueCommands(action: t_action) return boolean is
BEGIN
IF compareStrings(action,textToAction("home"))
OR compareStrings(action,textToAction("penup"))
OR compareStrings(action,textToAction("pu"))
OR compareStrings(action,textToAction("pendown"))
OR compareStrings(action,textToAction("pd"))
OR compareStrings(action,textToAction("clear"))
OR compareStrings(action,textToAction("cs"))
OR compareStrings(action,textToAction("["))
OR compareStrings(action,textToAction("]"))
then
return true;
end if;
return false;
end inNoValueCommands;
function inBufferCommands(action: t_action) return boolean is
BEGIN
if compareStrings(action,textToAction("repeat")) then
return true;
end if;
return false;
end inBufferCommands;
function getBufferCommand return pt_buffer is
c:character;
Command:Pt_Command:=NEW T_Command;
CommandInBuffer:t_command_in_buffer;
buffer:pt_buffer:=new t_command_buffer;
BEGIN
buffer.length:=0;
command.action:=(others=>'#');
get(c);
while c=' ' loop
get(c);
end loop;
if c='[' then
Put_line("getting first command");
Command:=GetCommand;
Put_line("got first command");
CommandInBuffer.command:=command;
while not compareStrings(command.action,textToAction("]")) loop
addToBuffer(buffer,CommandInBuffer);
Put_line("getting next command");
Command:=GetCommand;
Put_line("got next command");
CommandInBuffer.command:=command;
END LOOP;
END IF;
return buffer;
end getBufferCommand;
PROCEDURE AddToBuffer(Buffer: IN OUT Pt_Buffer;Command: IN t_command_in_buffer) IS
bufferCommand: pt_command_in_buffer:= new t_command_in_buffer;
LastCommand:pt_Command_In_Buffer:= new t_command_in_buffer;
BEGIN
BufferCommand.Command:=Command.Command;
BufferCommand.NextCommand:=Command.NextCommand;
Put_line("adding command to buffer");
if buffer.first=NULL then
buffer.first:=bufferCommand;
ELSE
lastCommand:=LastCommandInBuffer(Buffer);
lastCommand.nextCommand:=bufferCommand;
end if;
buffer.length:=buffer.length+1;
listBufferCommands(Buffer);
end addToBuffer;
FUNCTION LastCommandInBuffer(Buffer:Pt_Buffer) RETURN pt_Command_In_Buffer IS
command:pt_command_in_buffer:=buffer.first;
BEGIN
while command.nextCommand/=NULL loop
command:=command.nextCommand;
end loop;
Put_line("got last command in buffer");
return command;
end lastCommandInBuffer;
procedure listBufferCommands(Buffer:Pt_Buffer) IS
command:pt_command_in_buffer:=buffer.first;
BEGIN
ada.Text_IO.put("buffer length:");
put(buffer.length);
put_line(" ");
p_logo.put(command.command.action);
ada.Text_IO.put_line(" ");
while not endOfBuffer(buffer,command) loop
p_logo.put(command.command.action);
ada.Text_IO.put_line(" ");
command:=command.nextCommand;
end loop;
put_line("end buffer list");
End listBufferCommands;
function endOfBuffer(buffer:Pt_Buffer;command:pt_command_in_buffer) return boolean is
BEGIN
return lastCommandInBuffer(buffer)=command;
end endOfBuffer;
function degreesToRad(d: Integer) return float is
BEGIN
return (Ada.Numerics.Pi*(float(d)))/float(180);
end degreesToRad;
function textToAction(S:in String) return t_action is
action:t_action:=(others=>' ');
BEGIN
for i in S'range loop
action(i):=S(i);
end loop;
return action;
end textToAction;
PROCEDURE emptyBuffer(Buff:Pt_Buffer) IS
procedure free is new Ada.Unchecked_Deallocation(
Object => t_command_in_buffer,
Name => pt_command_in_buffer);
temp:pt_command_in_buffer;
command:pt_command_in_buffer:=buff.first;
BEGIN
while not endOfBuffer(buff,command) loop
temp:=command;
if(command.command.buffer/=NULL) then
emptyBuffer(command.command.buffer);
end if;
command:=command.nextCommand;
free(temp);
put_line("deallocation");
end loop;
end emptyBuffer;
PROCEDURE put(act:in t_action) is
BEGIN
for i in act'range loop
put(act(i));
end loop;
end put;
procedure doCommand(comm:pt_command;turt:in out turtle;interpreterRules:in out rules) is
BEGIN
ada.Text_IO.put("doing command : ");
p_logo.put(comm.action);
put_line(" ");
if compareStrings(comm.action,textToAction("forward")) OR compareStrings(comm.action,textToAction("fw")) then
doForward(comm,turt,interpreterRules);
elsif compareStrings(comm.action,textToAction("backward")) OR compareStrings(comm.action,textToAction("bk")) then
doBackward(comm,turt,interpreterRules);
elsif compareStrings(comm.action,textToAction("left")) OR compareStrings(comm.action,textToAction("lt")) then
doLeft(comm,turt);
elsif compareStrings(comm.action,textToAction("right")) OR compareStrings(comm.action,textToAction("rt")) then
doRight(comm,turt);
elsif compareStrings(comm.action,textToAction("repeat")) then
doRepeat(comm,turt,interpreterRules);
elsif compareStrings(comm.action,textToAction("home")) then
doHome(comm,turt);
elsif compareStrings(comm.action,textToAction("penup")) OR compareStrings(comm.action,textToAction("pu")) then
doPenUp(comm,interpreterRules);
elsif compareStrings(comm.action,textToAction("pendown")) OR compareStrings(comm.action,textToAction("pd")) then
doPenDown(comm,interpreterRules);
elsif compareStrings(comm.action,textToAction("clear")) OR compareStrings(comm.action,textToAction("cs")) then
doClear(comm);
else
put_line("Cette commande n'existe pas");
end if;
end doCommand;
procedure doForward(comm:pt_command;turt:in out turtle;interpreterRules:in out rules) is
oldX:integer:=turt.xCoord;
oldY:integer:=turt.yCoord;
BEGIN
turt.xCoord:=turt.xCoord+integer(float(comm.value)*cos(degreesToRad(turt.orientation)));
turt.yCoord:=turt.yCoord+integer(float(comm.value)*sin(degreesToRad(turt.orientation)));
if Get_window_Height<turt.xCoord OR turt.yCoord<0 OR turt.xCoord<0 Or turt.yCoord>Get_window_Width then
raise turtleOutsideWindow;
end if;
if interpreterRules.penDown then
Draw_Line(oldY,oldX,turt.yCoord,turt.xCoord);
end if;
end doForward;
procedure doBackward(comm:pt_command;turt:in out turtle;interpreterRules:in out rules) is
oldX:integer:=turt.xCoord;
oldY:integer:=turt.yCoord;
BEGIN
turt.xCoord:=(turt.xCoord-integer(float(comm.value)*cos(degreesToRad(turt.orientation))));
turt.yCoord:=(turt.yCoord-integer(float(comm.value)*sin(degreesToRad(turt.orientation))));
if Get_window_Height<turt.xCoord OR turt.yCoord<0 OR turt.xCoord<0 Or turt.yCoord>Get_window_Width then
raise turtleOutsideWindow;
end if;
if interpreterRules.penDown then
Draw_Line(oldY,oldX,turt.yCoord,turt.xCoord);
end if;
end doBackward;
procedure doLeft(comm:in pt_command;turt:in out turtle)is
BEGIN
turt.orientation:=(turt.orientation+comm.value) mod 360;
end doLeft;
procedure doRight(comm:in pt_command;turt:in out turtle) is
BEGIN
turt.orientation:=(turt.orientation-comm.value) mod 360;
end doRight;
procedure doClear(comm:pt_command) is
BEGIN
Clear_Window;
end doClear;
procedure doHome(comm:pt_command;turt:in out turtle) is
BEGIN
turt.xCoord:=Get_window_Height/2;
turt.yCoord:=Get_window_Width/2;
end doHome;
procedure doPenDown(comm:pt_command;interpreterRules:in out rules) is
BEGIN
interpreterRules.pendown:=true;
end doPenDown;
procedure doPenUp(comm:pt_command;interpreterRules:in out rules) is
BEGIN
interpreterRules.pendown:=false;
end doPenUp;
procedure doRepeat(comm:in pt_command;turt: in out turtle;interpreterRules:in out rules) IS
BEGIN
Put_line("repeating...");
for i in 1..comm.value loop
doBuffer(comm.buffer,turt,interpreterRules);
end loop;
end doRepeat;
procedure doBuffer(buffer:in Pt_Buffer;turt:in out turtle;interpreterRules:in out rules ) IS
currentCommand:pt_command_in_buffer:=buffer.first;
BEGIN
Put_line("doing first command");
doCommand(currentCommand.command,turt,interpreterRules);
while not endOfBuffer(buffer,currentCommand) loop
Put_line("doing next command");
currentCommand:=currentCommand.nextCommand;
doCommand(currentCommand.command,turt,interpreterRules);
end loop;
end doBuffer;
END P_Logo;