-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment.cpp
664 lines (536 loc) · 18.4 KB
/
environment.cpp
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
#include "environment.hpp"
#include "expression.hpp"
#include <cassert>
#include <cmath>
//#include <complex>
#include <iostream>
#include "environment.hpp"
#include "semantic_error.hpp"
/***********************************************************************
Helper Functions
**********************************************************************/
// predicate, the number of args is nargs
bool nargs_equal(const std::vector<Expression> & args, unsigned nargs) {
return args.size() == nargs;
}
/***********************************************************************
Each of the functions below have the signature that corresponds to the
typedef'd Procedure function pointer.
**********************************************************************/
// the default procedure always returns an expresison of type None
Expression default_proc(const std::vector<Expression> & args) {
args.size(); // make compiler happy we used this parameter
return Expression();
};
// constants
const double PI = std::atan2(0, -1);
const double EXP = std::exp(1);
const std::complex<double> I(0, 1);
Expression add(const std::vector<Expression> & args) {
// check all aruments are numbers, while adding
double realSum = 0, imagSum = 0;
bool flag = false;
for (auto & a : args) {
if (a.isHeadSymbol()) {
throw SemanticError("Error in call to add, argument not a number");
}
else {
if (a.isHeadNumber())
realSum += a.head().asNumber();
else if (a.head().isComplexNumber()) {
flag = true;
realSum += real(a.head().asComplexNumber());
imagSum += imag(a.head().asComplexNumber());
}
}
}
if (flag == false)
return Expression(realSum);
std::complex<double> result(realSum, imagSum);
return Expression(result);
};
Expression mul(const std::vector<Expression> & args) {
// check all aruments are numbers, while multiplying
std::complex<double> result(1, 0);
bool flag = false;
if (nargs_equal(args, 1))
throw SemanticError("Error in call to mul, invalid number of arguments");
for (auto & a : args) {
if (a.isHeadSymbol())
throw SemanticError("Error in call to mul, argument not a number");
else
{
if (a.head().isComplexNumber())
{
flag = true;
result *= a.head().asComplexNumber();
}
else
result *= a.head().asNumber();
}
}
if (flag == false)
return Expression(result.real());
else
return Expression(result);
};
Expression subneg(const std::vector<Expression> & args) {
double realResult = 0, imagResult = 0;
bool flag = true;
// preconditions
if (nargs_equal(args, 1)) {
if (args[0].isHeadNumber()) {
flag = false;
realResult = -args[0].head().asNumber();
}
else if (args[0].head().isComplexNumber()) {
realResult = -real(args[0].head().asComplexNumber());
imagResult = -imag(args[0].head().asComplexNumber());
}
else {
throw SemanticError("Error in call to negate: invalid argument.");
}
}
else if (nargs_equal(args, 2)) {
if ((args[0].isHeadSymbol()) || (args[1].isHeadSymbol())) {
throw SemanticError("Error in call to subtraction: invalid argument.");
}
else {
if (args[0].isHeadNumber() && args[1].head().isComplexNumber())
{
realResult = args[0].head().asNumber() - real(args[1].head().asComplexNumber());
imagResult = -std::imag(args[1].head().asComplexNumber());
}
else if (args[1].isHeadNumber() && args[0].head().isComplexNumber())
{
realResult = std::real(args[0].head().asComplexNumber()) - args[1].head().asNumber();
imagResult = -std::imag(args[0].head().asComplexNumber());
}
else if (args[0].head().isComplexNumber() && args[1].head().isComplexNumber())
{
realResult = std::real(args[0].head().asComplexNumber()) - real(args[1].head().asComplexNumber());
imagResult = std::imag(args[0].head().asComplexNumber()) - imag(args[1].head().asComplexNumber());
}
else
{
flag = false;
realResult = args[0].head().asNumber() - args[1].head().asNumber();
}
}
}
else {
throw SemanticError("Error in call to subtraction or negation: invalid number of arguments.");
}
std::complex<double> result(realResult, imagResult);
if (!flag)
return Expression(realResult);
else
return Expression(result);
};
Expression div(const std::vector<Expression> & args) {
std::complex<double> result(0, 0);
bool flag = true;
if (nargs_equal(args,1))
{
if (args[0].isHeadNumber())
{
flag = false;
result = 1.0 / args[0].head().asNumber();
}
else if (args[0].head().isComplexNumber())
result = 1.0 / args[0].head().asComplexNumber();
}
else if (nargs_equal(args, 2))
{
if (args[0].isHeadNumber() && args[1].head().isComplexNumber())
{
result = args[0].head().asNumber() / args[1].head().asComplexNumber();
}
else if (args[1].isHeadNumber() && args[0].head().isComplexNumber())
{
result = args[0].head().asComplexNumber() / args[1].head().asNumber();
}
else if (args[0].head().isComplexNumber() && args[1].head().isComplexNumber())
{
result = args[0].head().asComplexNumber() / args[1].head().asComplexNumber();
}
else
{
flag = false;
result = args[0].head().asNumber() / args[1].head().asNumber();
}
}
else
throw SemanticError("Error in call to div: incorrect num of args");
if (!flag)
return Expression(result.real());
else
return Expression(result);
};
//compute the exponent. 1st input is base and 2nd is the exponent
Expression exponent(const std::vector<Expression>& args) {
std::complex<double> result(0, 0);
bool flag = true;
if (nargs_equal(args, 2))
{
if ((args[0].isHeadNumber()) && (args[1].isHeadNumber()))
{
flag = false;
result = std::pow(args[0].head().asNumber(), args[1].head().asNumber());
}
else if (args[1].isHeadNumber() && args[0].head().isComplexNumber())
{
result = std::pow(args[0].head().asComplexNumber(), args[1].head().asNumber());
}
else if (args[0].head().isComplexNumber() && args[1].head().isComplexNumber())
{
result = std::pow(args[0].head().asComplexNumber(), args[1].head().asComplexNumber());
}
else if (args[0].isHeadNumber() && args[1].head().isComplexNumber())
{
result = std::pow(args[0].head().asNumber(), args[1].head().asComplexNumber());
}
else
throw SemanticError("Error in call to exponent: invalid argument.");
}
else
throw SemanticError("Error in call to exponent: invalid number of arguments.");
if (!flag)
return Expression(result.real());
else
return Expression(result);
};
Expression sqrt(const std::vector<Expression> & args) {
std::complex<double> result(0,0);
bool flag = true;
if (nargs_equal(args, 1))
{
if (args[0].isHeadNumber())
{
if (args[0].head().asNumber() >= 0)
{
flag = false;
result = std::sqrt(args[0].head().asNumber());
}
else
result = std::sqrt(-args[0].head().asNumber()) * I;
}
else if (args[0].head().isComplexNumber())
result = std::sqrt(args[0].head().asComplexNumber());
else
throw SemanticError("Error in call to sqaure root: invalid argument.");
}
else
throw SemanticError("Error in call to sqaure root: number argument must be one.");
if (!flag)
return Expression(result.real());
else
return Expression(result);
};
// computes the natural log
Expression ln(const std::vector<Expression> & args) {
double result = 0;
if (nargs_equal(args, 1))
{
if (args[0].isHeadNumber())
{
if (args[0].head().asNumber() >= 0)
result = std::log(args[0].head().asNumber());
else
throw SemanticError("Error in call to natural log: input has to be positive.");
}
else
throw SemanticError("Error in call to natural log: invalid argument.");
}
else
throw SemanticError("Error in call to natural log: number of argument must be one.");
return Expression(result);
};
// computes the sine
Expression sin(const std::vector<Expression> & args) {
double result = 0;
if (nargs_equal(args, 1))
{
if (args[0].isHeadNumber())
result = std::sin(args[0].head().asNumber());
else
throw SemanticError("Error in call to sine: invalid argument");
}
else
throw SemanticError("Error in call to sine: invalid argument.");
return Expression(result);
};
// computes the cosine
Expression cos(const std::vector<Expression> & args) {
double result = 0;
if (nargs_equal(args, 1))
{
if (args[0].isHeadNumber())
result = std::cos(args[0].head().asNumber());
else
throw SemanticError("Error in call to cosine: invalid argument.");
}
else
throw SemanticError("Error in call to cosine: invalid argument.");
return Expression(result);
};
// computes the tangent
Expression tan(const std::vector<Expression> & args) {
double result = 0;
if (nargs_equal(args, 1))
{
if (args[0].isHeadNumber())
result = std::tan(args[0].head().asNumber());
else
throw SemanticError("Error in call to tangent: invalid argument.");
}
else
throw SemanticError("Error in call to tangent: invalid argument.");
return Expression(result);
};
// returns real part of a complex number
Expression real(const std::vector<Expression> & args) {
if (nargs_equal(args, 1) && args[0].head().isComplexNumber())
return Expression(std::real(args[0].head().asComplexNumber()));
else
throw SemanticError("Error in call to real: argument not a complex number.");
};
// returns imaginary part of a complex number
Expression imaginary(const std::vector<Expression> & args) {
if (nargs_equal(args, 1) && args[0].head().isComplexNumber())
return Expression(std::imag(args[0].head().asComplexNumber()));
else
throw SemanticError("Error in call to real: argument not a complex number.");
};
// returns phase angle of a complex number in radians
Expression arg(const std::vector<Expression> & args) {
if (nargs_equal(args, 1) && args[0].head().isComplexNumber())
return Expression(std::arg(args[0].head().asComplexNumber()));
else
throw SemanticError("Error in call to real: argument not a complex number.");
};
// returns the conjugate of a complex number
Expression conj(const std::vector<Expression> & args) {
if (nargs_equal(args, 1) && args[0].head().isComplexNumber())
return Expression(std::conj(args[0].head().asComplexNumber()));
else
throw SemanticError("Error in call to real: argument not a complex number.");
};
// returns the magnitude of a complex number
Expression mag(const std::vector<Expression> & args) {
if (nargs_equal(args, 1) && args[0].head().isComplexNumber())
return Expression(std::abs(args[0].head().asComplexNumber()));
else
throw SemanticError("Error in call to real: argument not a complex number.");
};
// Milestone 1 task 2
// returns list of the arguments
Expression list(const std::vector<Expression> & args) {
Atom list_head("list");
Expression list(list_head);
for (unsigned int i = 0; i < args.size(); i++) {
list.addToTail(args[i]);
}
return list;
};
// this functions returns the first expression of the list
Expression first(const std::vector<Expression> &args)
{
Expression firstList(Atom("list"));
if (nargs_equal(args, 1)) {
if (args[0].tailConstBegin() != args[0].tailConstEnd()) {
if (args[0].head().asSymbol() == "list")
firstList = *(args[0].tailConstBegin());
else
throw SemanticError("Error in call to first: argument not a list");
}
else
throw SemanticError("Error: argument to first empty list");
}
else
throw SemanticError("Error: invalid number of arguments");
return firstList;
}
Expression rest(const std::vector<Expression> &args) {
if (args[0].head().asSymbol() != "list")
{
throw SemanticError("Error: argument to rest is not a list.");
}
if (nargs_equal(args, 1))
{
if (args[0].head().asSymbol() != "list")
throw SemanticError("Error: argument to rest is not a list.");
if (args[0].isListEmpty())
throw SemanticError("Error: argument to rest is an empty list.");
Expression rest(Atom("list"));
for (auto a = args[0].tailConstBegin() + 1; a != args[0].tailConstEnd(); ++a)
rest.addToTail(*a);
return rest;
}
else
throw SemanticError("Error: more than one argument in call to rest.");
}
Expression length(const std::vector<Expression> &args)
{
if (nargs_equal(args, 1))
{
if (args[0].head() != Expression(Atom("list")))
throw SemanticError("Error: argument to length is not a list.");
return Expression(Atom(args[0].listLength()));
}
else
throw SemanticError("Error: more than one argument in call to length.");
}
Expression append(const std::vector<Expression> &args)
{
if (nargs_equal(args, 2))
{
if (args[0].head() != Expression(Atom("list")))
throw SemanticError("Error: first argument to append not a list.");
Expression newList(Atom("list"));
for (auto a = args[0].tailConstBegin(); a != args[0].tailConstEnd(); ++a)
newList.addToTail(*a);
newList.addToTail(args[1]);
return newList;
}
else
throw SemanticError("Error in append: invalid number of arguments");
}
Expression join(const std::vector<Expression> &args)
{
if (args[0].head() != Expression(Atom("list")) || args[1].head() != Expression(Atom("list")))
throw SemanticError("Error: first argument to join not a list.");
else
{
Expression newList(Atom("list"));
for (auto a = args[0].tailConstBegin(); a != args[0].tailConstEnd(); ++a)
newList.addToTail(*a);
for (auto a = args[1].tailConstBegin(); a != args[1].tailConstEnd(); ++a)
newList.addToTail(*a);
return newList;
}
}
Expression range(const std::vector<Expression> &args)
{
if (!args[0].isHeadNumber() || !args[1].isHeadNumber() || !args[2].isHeadNumber())
throw SemanticError("Error: argument not a number.");
else
{
if (args[0].head().asNumber() > args[1].head().asNumber())
throw SemanticError("Error: begin greater than end in range.");
else if (args[2].head().asNumber() <= 0)
throw SemanticError("Error: negative or zero increment in range.");
else
{
Expression rangeList(Atom("list"));
for (double a = args[0].head().asNumber(); a <= args[1].head().asNumber(); a += args[2].head().asNumber())
{
rangeList.addToTail(Expression(a));
//a += args[2].head().asNumber();
}
return rangeList;
}
}
}
Environment::Environment() {
reset();
}
bool Environment::is_known(const Atom & sym) const {
if (!sym.isSymbol()) return false;
return envmap.find(sym.asSymbol()) != envmap.end();
}
bool Environment::is_exp(const Atom & sym) const {
if (!sym.isSymbol()) return false;
auto result = envmap.find(sym.asSymbol());
return (result != envmap.end()) && (result->second.type == ExpressionType);
}
Expression Environment::get_exp(const Atom & sym) const {
Expression exp;
if (sym.isSymbol()) {
auto result = envmap.find(sym.asSymbol());
if ((result != envmap.end()) && (result->second.type == ExpressionType)) {
exp = result->second.exp;
}
}
return exp;
}
void Environment::add_exp(const Atom & sym, const Expression & exp) {
if (!sym.isSymbol()) {
throw SemanticError("Attempt to add non-symbol to environment");
}
// overwrite the symbol map if lambda is used
if (envmap.find(sym.asSymbol()) != envmap.end()) {
envmap[sym.asSymbol()] = EnvResult(ExpressionType, exp);
}
envmap.emplace(sym.asSymbol(), EnvResult(ExpressionType, exp));
}
bool Environment::is_proc(const Atom & sym) const {
if (!sym.isSymbol()) return false;
auto result = envmap.find(sym.asSymbol());
return (result != envmap.end()) && (result->second.type == ProcedureType);
}
bool Environment::is_list(const std::vector<Expression> & exp) const
{
return (Expression(exp[0].head()) == Expression(Atom("list")));
}
Procedure Environment::get_proc(const Atom & sym) const {
//Procedure proc = default_proc;
if (sym.isSymbol()) {
auto result = envmap.find(sym.asSymbol());
if ((result != envmap.end()) && (result->second.type == ProcedureType)) {
return result->second.proc;
}
}
return default_proc;
}
/*void Environment::add_MessageQueue(MessageQueue<std::string> *msg) {
}*/
/*
Reset the environment to the default state. First remove all entries and
then re-add the default ones.
*/
void Environment::reset() {
envmap.clear();
// Built-In value of pi
envmap.emplace("pi", EnvResult(ExpressionType, Expression(PI)));
envmap.emplace("-pi", EnvResult(ExpressionType, Expression(-PI)));
// Procedure: add;
envmap.emplace("+", EnvResult(ProcedureType, add));
// Procedure: subneg;
envmap.emplace("-", EnvResult(ProcedureType, subneg));
// Procedure: mul;
envmap.emplace("*", EnvResult(ProcedureType, mul));
// Procedure: div;
envmap.emplace("/", EnvResult(ProcedureType, div));
//milestone 0
// task 3-1 Built-in value of e = exp(1)
envmap.emplace("e", EnvResult(ExpressionType, Expression(EXP)));
envmap.emplace("-e", EnvResult(ExpressionType, Expression(-EXP)));
// task 3-2 procedure: sqrt
envmap.emplace("sqrt", EnvResult(ProcedureType, sqrt));
// task 3-3 precedure: exponent
envmap.emplace("^", EnvResult(ProcedureType, exponent));
// task 3-4 procedure: natural log
envmap.emplace("ln", EnvResult(ProcedureType, ln));
// task 3-5~7 procedure: trig functions
envmap.emplace("sin", EnvResult(ProcedureType, sin));
envmap.emplace("cos", EnvResult(ProcedureType, cos));
envmap.emplace("tan", EnvResult(ProcedureType, tan));
//task 4-1 create built-in expression I
envmap.emplace("I", EnvResult(ExpressionType, Expression(I)));
envmap.emplace("-I", EnvResult(ExpressionType, Expression(-I)));
//task 4-4 real, imaginary, arg, conj procedures implemented
envmap.emplace("real", EnvResult(ProcedureType, real));
envmap.emplace("imag", EnvResult(ProcedureType, imaginary));
envmap.emplace("arg", EnvResult(ProcedureType, arg));
envmap.emplace("conj", EnvResult(ProcedureType, conj));
envmap.emplace("mag", EnvResult(ProcedureType, mag));
//milestone 1 task 1 creating a list
envmap.emplace("list", EnvResult(ProcedureType, list));
envmap.emplace("first", EnvResult(ProcedureType, first));
envmap.emplace("rest", EnvResult(ProcedureType, rest));
envmap.emplace("length", EnvResult(ProcedureType, length));
envmap.emplace("append", EnvResult(ProcedureType, append));
envmap.emplace("join", EnvResult(ProcedureType, join));
envmap.emplace("range", EnvResult(ProcedureType, range));
}