-
Notifications
You must be signed in to change notification settings - Fork 0
/
constructs.js
5065 lines (4707 loc) · 206 KB
/
constructs.js
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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function () {
"use strict";
var evaluatorLibrariesLoaded = false;
var EVALUATOR_ROOT_URL="http://LoadBalancerEast-1672652775.us-east-1.elb.amazonaws.com/";
var loadEvaluatorLibraries = function(after) {
if (evaluatorLibrariesLoaded) {
return after();
} else {
jQuery.getScript(
EVALUATOR_ROOT_URL+"easyXDM.js",
function() {
jQuery.getScript(
EVALUATOR_ROOT_URL+"evaluator.js",
function() {
jQuery.getScript(EVALUATOR_ROOT_URL+"support.js",
function() {
evaluatorLibrariesLoaded = true;
after();
});
});
});
}
};
// If there is no window.console, we'll make one up.
if (!window.console) {
$(document).ready(function () {
/* var consoleDiv = $("<div id='console'/>");
$(document.body).append(consoleDiv);
consoleDiv.css("position", "absolute");
consoleDiv.css("right", "10px");
consoleDiv.css("top", "10px");
consoleDiv.css("width", "300px");
consoleDiv.css("height", "500px");
consoleDiv.css("background-color", "white");
consoleDiv.css("border", "1px solid red");
consoleDiv.css("overflow", "scroll");*/
window.console = {
/* log: function() {
var i;
for (i = 0; i < arguments.length; i++) {
consoleDiv.append($("<span/>").text(arguments[i]));
consoleDiv.append($("<br/>"));
}
}*/
};
});
}
/*====================================================================================
___ _ ___ __ _ _ _ _
| \ __ _| |_ __ _ | \ ___ / _(_)_ _ (_) |_(_)___ _ _ ___
| |) / _` | _/ _` | | |) / -_) _| | ' \| | _| / _ \ ' \(_-
|___/\__,_|\__\__,_| |___/\___|_| |_|_||_|_|\__|_\___/_||_/__/
=====================================================================================*/
/*
*makeID - generates a new unique ID
*/
function makeID() {
return String(ID++);
}
/*
* makeIDList - returns an array of unique id's
* @param num - the number of id's to be created
* @return - the array of id's of length num
*/
function makeIDList(num) {
var toReturn = [];
for (var i = 0; i < num; i++) {
toReturn[i] = makeID();
}
return toReturn;
}
/*
cloneProgram takes in an array of code objects (arr) and outputs a clone of arr
*/
var cloneProgram = function (arr) {
var tempArr = [];
for (var i = 0; i < arr.length; i++) {
tempArr[i] = arr[i].clone();
}
return tempArr;
};
/*
*flatten - turns a ExprBoolAnswer into a single level array of objects
*@param obj - an ExprBoolAnswer
*@return - a single level array of objects
*/
function flatten(obj) {
var toReturn = [];
if (obj.bool !== undefined) {
toReturn.push(obj.bool)
}
if (obj.answer !== undefined) {
toReturn.push(obj.answer)
}
return toReturn;
}
/**
*searchForIndex -finds the object with the given id in the given array of codeObjects
*@param id - the id being searched for
*@param array- the array of objects being searched
*@return: The object with the corresponding id.
*/
function searchForIndex(id, array) {
var toReturn = undefined;
for (var i = 0; i < array.length; i++) {
if (array[i] === undefined) {
//just skip
} else if (array[i].id === id) {
toReturn = array[i]
} else if (isDefine(array[i])) {
toReturn = searchForIndex(id, [array[i].expr]);
} else if (array[i] instanceof ExprApp) {
toReturn = searchForIndex(id, array[i].args);
} else if (array[i] instanceof ExprBoolAnswer) {
toReturn = searchForIndex(id, flatten(array[i]));
} else if (array[i] instanceof ExprCond) {
toReturn = searchForIndex(id, array[i].listOfBooleanAnswer);
}
if (toReturn !== undefined) {
return toReturn;
}
}
return undefined;
}
/**
*setChildInProgram adds or removes an embedded(not top level) obj from the given array of objects
* @param parentId - the id of the parent containing the child to be removed or space to be added to
* @param childId - the id of the object to be removed or the space to be filled
* @param obj - the obj being added (or undefined if removing)
* @param prog - an array of objects in which the the child is being added or removed
*/
function setChildInProgram(parentId, childId, obj, prog) {
var parent = searchForIndex(parentId, prog);
if (parent === undefined) {
throw new Error("setChildInProgram failure: parentId not found.");
}
if (isLiteral(parent) || parent instanceof ExprConst) {
throw new Error("setChildInProgram failure: parent was a literal, and cannot be added to");
}
if (parent.hasOwnProperty("funcIDList")) {
var index;
if (obj != undefined) {
index = getAddIndex(parent, childId);
} else {
index = getRemoveIndex(parent, childId);
}
if (index === -1) {
throw new Error("setChildInProgram failure: childId not found");
} else {
if (parent instanceof ExprApp) {
parent.args[index] = obj;
} else if (isDefine(parent)) {
parent.expr = obj;
} else if (parent instanceof ExprBoolAnswer) {
if (index === 0) {
parent.bool = obj;
} else {
parent.answer = obj;
}
} else if (parent instanceof ExprCond) {
throw new Error("setChildInProgram failure: parent was top level of cond, that doesn't work");
} else {
throw new Error("setChildInProgram failure: parent looked like: " + interpreter(parent));
}
}
}
}
/*
*getRemoveIndex - given a parent and an id, finds the index in the array of subexpressions of the parent at which to remove the child
*@param parent - the parent from which the child is being removed
*@param childID - the id of the child to be removed
*@return - an integer representing the index in the array of subexpressions of the parent at which to remove the child (0 if the expr of a definition)
*@return if not found -1
*/
function getRemoveIndex(parent, childID) {
var i;
var toReturn = -1;
if (isDefine(parent)) {
if (parent.expr != undefined && parent.expr.id === childID) {
toReturn = 0;
}
} else if (parent instanceof ExprApp) {
for (i = 0; i < parent.args.length; i++) {
if (parent.args[i] !== undefined) {
if (parent.args[i].id === childID) {
toReturn = i;
}
}
}
} else if (parent instanceof ExprBoolAnswer) {
if (parent.bool !== undefined && parent.bool.id === childID) {
toReturn = 0;
}
if (parent.answer !== undefined && parent.answer.id === childID) {
toReturn = 1;
}
}
return toReturn;
}
/*
*getAddIndex - given a parent and an id, finds the index in the array of subexpressions of the parent at which to add the child
*@param parent - the parent to which the child is being added
*@param childID - the id of the space at which the child is to be added
*@return - an integer representing the index in the array of subexpressions of the parent at which to add the child (0 if the expr of a definition)
*@return if not found -1
*/ function getAddIndex(parent, childID) {
for (var i = 0; i < parent.funcIDList.length; i++) {
if (parent.funcIDList[i] === childID) {
return i;
}
}
return -1;
}
/*
*The following are the data definitons for the objects used to represent the DrRacket Code
*selfType is the name of the data type, used for reconstruction after saving, since JSON stringify doesn't save the actual object type
*id - a unique id representing the element
*/
/*
* ExprDefineFunc
*Constucts the define function block
*contract - the contract of the define
*arguementNames - intialized to one empty argument name
*expr - intialized to undefined, holds the subexpression of the define
*funcIDList - an array of unique id's that point to empty spaces in the define. First position points to the expression, all others to the arguments
* EXAMPLE:
*(define (add2 x) (+ x 2)) =>
* ExprDefineFunc(contract1, ["x"], ExprApp("+", [ExprConst("x"), 2]))
* contract1 = ExprContract("add2", ["Numbers"], "Numbers")
*
*/
var ExprDefineFunc = function () {
this.contract = new ExprContract();
this.selfType = "ExprDefineFunc";
this.argumentNames = [""];
this.expr = undefined;
this.id = makeID();
this.funcIDList = makeIDList(2);
this.clone = function () {
var temp = new ExprDefineFunc();
temp.contract = this.contract.clone();
temp.argumentNames = this.argumentNames.slice(0);
if (this.expr != undefined) {
temp.expr = this.expr.clone();
}
temp.id = this.id;
temp.funcIDList = this.funcIDList.slice(0);
return temp;
};
};
/*
*ExprContract
* constructs the contract for a definition
* funcName - the name of the function
* argumentTypes - an array of types for each argument (since there must be at least one argument, the array is intialized to length 1 of undefined, which represents an unselected type)
* outputType - the output type of the definition, initialized to undefined
* funcIDList - id of output type followed by id's of arguments (need id's for drop down box to select type)
* purpose - a string representing the purpose of the funcion, initialized to an empty string
*/
var ExprContract = function () {
this.funcName = "";
this.selfType = "ExprContract"
this.argumentTypes = [undefined];
this.outputType = undefined;
this.id = makeID();
this.funcIDList = makeIDList(2);
this.purpose = "";
this.clone = function () {
var temp = new ExprContract();
temp.funcName = this.funcName;
temp.argumentTypes = this.argumentTypes.slice(0);
temp.funcIDList = this.funcIDList.slice(0);
temp.outputType = this.outputType;
temp.id = this.id;
temp.purpose = this.purpose;
return temp;
};
};
/*
* ExprDefineConst
* constructs the definition block for a constant
* constName - the name of the constant
* expr - the subexpression of the define block
* funcIDList - always has length one, only id points to the expression space, used for consistency
*EXAMPLE:
* (define x 3) =>
* ExprDefineConst("x", ExprNumber(3), "Numbers")
* (define y (+ 2 x)) =>
* ExprDefineConst("y", (ExprApp("+", [ExprNumber("2"), ExprConst("x")))
*/
var ExprDefineConst = function () {
this.constName = undefined;
this.selfType = "ExprDefineConst";
this.expr = undefined;
this.outputType = undefined; //MAKE SURE THIS WILL BE DEFINED!!!!
this.id = makeID();
this.funcIDList = makeIDList(1);
this.clone = function () {
var temp = new ExprDefineConst();
temp.constName = this.constName;
if (this.expr != undefined) {
temp.expr = this.expr.clone();
}
temp.outputType = this.outputType;
temp.id = this.id;
temp.funcIDList = this.funcIDList.slice(0);
return temp;
};
};
/*
* isLiteral - returns if the object is a literal or not
* @param obj - the object in question
* @return true if the obj is a literal, false otherwise
*/
function isLiteral(obj) {
return obj instanceof ExprString || obj instanceof ExprNumber || obj instanceof ExprBoolean;
}
/*
* isDefine - return if the object is a define function or define constant
* @param obj - the object in question
* @return true if the object is a definition
*/
function isDefine(obj) {
return (obj instanceof ExprDefineConst || obj instanceof ExprDefineFunc);
}
/*
* ExprApp
* Constructs an application of an operator
* funcName - the name of the function being applied
* funcIDList - a list of ID's with length equal to the number of arguments of the function, each id pointing to an empty argument space
* - if the argument space is filled, the id remains, just covered up
* args - an array of objects representing the arguments
* outputType - the output type of the function
*/
var ExprApp = function (funcName) {
this.funcName = funcName;
this.selfType = "ExprApp";
this.id = makeID();
var allFunctions = functions.concat(userFunctions);
this.funcIDList = makeIDList(allFunctions[containsName(allFunctions, funcName)].input.length);
this.args = [];
this.outputType = getOutput(funcName);
this.clone = function () {
var temp = new ExprApp(this.funcName);
temp.id = this.id;
temp.funcIDList = this.funcIDList.slice(0);
for (var i = 0; i < this.args.length; i++) {
if (this.args[i] != undefined) {
temp.args.push(this.args[i].clone());
} else {
temp.args.push(undefined);
}
}
temp.outputType = this.outputType;
return temp;
};
};
/*
* ExprString
* Constructs a string literal
* value - the actual string, initialized to "insert string here" to help the kids out
* outputType - "Strings"
*/
var ExprString = function () {
this.value = "insert string here";
this.selfType = "ExprString";
this.outputType = "Strings";
this.id = makeID();
this.clone = function () {
var temp = new ExprString();
temp.value = this.value;
temp.outputType = this.outputType;
temp.id = this.id;
return temp;
};
};
/*
* ExprNumber
* Constructs a number literal
* value - the actual number, initialized to 20
* outputType - "Numbers"
*/
var ExprNumber = function () {
this.value = 20;
this.selfType = "ExprNumber"
this.outputType = "Numbers";
this.id = makeID();
this.clone = function () {
var temp = new ExprNumber();
temp.value = this.value;
temp.outputType = this.outputType;
temp.id = this.id;
return temp;
};
};
/*
* ExprConst
* Constructs a constant or variable
* constName - the name of the constant or variable
* outputType - initialized to undefined, set by the contract (if in a define) or by the constant definition
*/
var ExprConst = function (constName) {
this.constName = constName;
this.selfType = "ExprConst"
this.outputType = getOutput(constName);
this.id = makeID();
this.clone = function () {
var temp = new ExprConst(this.constName);
temp.outputType = this.outputType;
temp.id = this.id;
return temp;
};
};
/*
* ExprBoolean
* Constructs a boolean literal
* value - the actual boolean, initialized to the input value (either true or false)
* outputType - "Booleans"
*/
var ExprBoolean = function (value) {
this.value = value;
this.selfType = "ExprBoolean";
this.outputType = "Booleans";
this.id = makeID();
this.clone = function () {
var temp = new ExprBoolean(this.value);
temp.outputType = this.outputType;
temp.id = this.id;
return temp;
};
};
/*
* ExprBoolAnswer
* Constructs a boolean answer pair of a cond
* bool - represents the boolean subexpression
* answer - represents the return subexpression
* outputType - I don't think this ever gets used, but its the last and we're cramming, so we aren't going to remove it now
*/
var ExprBoolAnswer = function () {
this.bool = undefined;
this.answer = undefined;
this.selfType = "ExprBoolAnswer";
this.outputType = undefined;
this.id = makeID();
this.funcIDList = makeIDList(2);
this.clone = function () {
var temp = new ExprBoolAnswer();
if (this.bool != undefined) {
temp.bool = this.bool.clone();
}
if (this.answer != undefined) {
temp.answer = this.answer.clone();
}
temp.outputType = this.outputType;
temp.id = this.id;
temp.funcIDList = this.funcIDList.slice(0);
return temp;
};
};
/*
* ExprCond
* Constructs a cond block
* listOfBooleanAnswer - an array of ExprBoolAnswer's
* outputType - the output type of the cond, only becomes defined if there are no errors in the cond, I don't think this is ever actually used either, but it is updated
* EXAMPLE:
* (cond
* [(true) 2]
* [(false) 1]) =>
* ExprCond(list1)
* list1 = [ExprBoolAnswer(ExprBoolean(true),ExprNumber(2)).ExprBoolAnswer(ExprBoolean(false),ExprNumber(1))]
*/
var ExprCond = function () {
this.listOfBooleanAnswer = [new ExprBoolAnswer()];
this.selfType = "ExprCond";
this.outputType = undefined;
this.id = makeID();
this.clone = function () {
var temp = new ExprCond();
for (var i = 0; i < this.listOfBooleanAnswer.length; i++) {
temp.listOfBooleanAnswer[i] = this.listOfBooleanAnswer[i].clone();
}
temp.outputType = this.outputType;
temp.id = this.id;
return temp;
};
};
/*
* objectArrayToProgram - turns an array of JSON objects (which is what our saved program is) back into DrBlocket data types
* @param JSONArrayObject - an array of JSON objects to turn back into DrBlocket data types
* @param arrayToPush - the array that stores the correctly formatted objects (the reason this is here is so you can load into program, userFunctions, etc. and have the whole program build)
*/
function objectArrayToProgram(JSONArrayObject, arrayToPush) {
for (var i = 0; i < JSONArrayObject.length; i++) {
arrayToPush.push(objectToCodeObject(JSONArrayObject[i]));
}
}
/*
* objectToCodeObject - turns the given JSON object into a DrBlocket object
* @param JSONObject - the object to be converted
* @return the DrBlocket version of the given object
*/
function objectToCodeObject(JSONObject) {
if (JSONObject == undefined) {
return undefined;
}
var curType = JSONObject.selfType;
var tempObject;
if (curType === "ExprNumber") {
tempObject = new ExprNumber();
tempObject.value = JSONObject.value;
} else if (curType === "ExprString") {
tempObject = new ExprString();
tempObject.value = JSONObject.value;
} else if (curType === "ExprBoolean") {
tempObject = new ExprBoolean();
tempObject.value = JSONObject.value;
} else if (curType === "ExprDefineFunc") {
tempObject = new ExprDefineFunc();
tempObject.argumentNames = JSONObject.argumentNames.slice(0);
tempObject.funcIDList = makeIDList(JSONObject.funcIDList.length)
tempObject.expr = objectToCodeObject(JSONObject.expr);
tempObject.contract = objectToCodeObject(JSONObject.contract);
} else if (curType === "ExprContract") {
tempObject = new ExprContract();
tempObject.funcName = JSONObject.funcName;
tempObject.funcIDList = makeIDList(JSONObject.funcIDList.length)
tempObject.argumentTypes = JSONObject.argumentTypes.slice(0);
tempObject.outputType = JSONObject.outputType;
} else if (curType === "ExprDefineConst") {
tempObject = new ExprDefineConst();
tempObject.constName = JSONObject.constName;
tempObject.expr = objectToCodeObject(JSONObject.expr);
tempObject.outputType = JSONObject.outputType;
} else if (curType === "ExprApp") {
tempObject = new ExprApp(JSONObject.funcName);
for (var j = 0; j < JSONObject.args.length; j++) {
tempObject.args[j] = objectToCodeObject(JSONObject.args[j]);
}
} else if (curType === "ExprConst") {
tempObject = new ExprConst(JSONObject.constName);
tempObject.outputType = JSONObject.outputType
} else if (curType === "ExprBoolAnswer") {
tempObject = new ExprBoolAnswer();
tempObject.bool = objectToCodeObject(JSONObject.bool);
tempObject.answer = objectToCodeObject(JSONObject.answer);
tempObject.outputType = JSONObject.outputType;
} else if (curType === "ExprCond") {
tempObject = new ExprCond();
for (var j = 0; j < JSONObject.listOfBooleanAnswer.length; j++) {
tempObject.listOfBooleanAnswer[j] = objectToCodeObject(JSONObject.listOfBooleanAnswer[j]);
}
tempObject.outputType = JSONObject.outputType;
}
return tempObject;
};
/*
*functions - an array of all pre-defined functions
* name - the function name
* input - an array of tuples of input type and input name
* - type - the type of the input, used in type inference and coloring
- name - the name of the argument, used to help kids know what to put in that argument place
* output - the output type of the function
*/
var functions = [];
functions[0] = {};
functions[0].name = "+";
functions[0].input = [{
type: "Numbers",
name: "Number"
}, {
type: "Numbers",
name: "Number"
}];
functions[0].output = "Numbers";
functions[1] = {};
functions[1].name = "-";
functions[1].input = [{
type: "Numbers",
name: "Number"
}, {
type: "Numbers",
name: "Number"
}];
functions[1].output = "Numbers";
functions[2] = {};
functions[2].name = "*";
functions[2].input = [{
type: "Numbers",
name: "Number"
}, {
type: "Numbers",
name: "Number"
}];
functions[2].output = "Numbers";
functions[3] = {};
functions[3].name = "/";
functions[3].input = [{
type: "Numbers",
name: "Dividend"
}, {
type: "Numbers",
name: "Divisor"
}];
functions[3].output = "Numbers";
functions[4] = {};
functions[4].name = "remainder";
functions[4].input = [{
type: "Numbers",
name: "Dividend"
}, {
type: "Numbers",
name: "Divisor"
}];
functions[4].output = "Numbers";
functions[5] = {};
functions[5].name = "sqrt";
functions[5].input = [{
type: "Numbers",
name: "Number"
}];
functions[5].output = "Numbers";
functions[6] = {};
functions[6].name = "sqr";
functions[6].input = [{
type: "Numbers",
name: "Number"
}];
functions[6].output = "Numbers";
functions[7] = {};
functions[7].name = "expt";
functions[7].input = [{
type: "Numbers",
name: "Base"
}, {
type: "Numbers",
name: "Exponent"
}];
functions[7].output = "Numbers";
functions[8] = {};
functions[8].name = "=";
functions[8].input = [{
type: "Numbers",
name: "Number"
}, {
type: "Numbers",
name: "Number"
}];
functions[8].output = "Booleans";
functions[9] = {};
functions[9].name = ">";
functions[9].input = [{
type: "Numbers",
name: "Number"
}, {
type: "Numbers",
name: "Number"
}];
functions[9].output = "Booleans";
functions[10] = {};
functions[10].name = "<";
functions[10].input = [{
type: "Numbers",
name: "Number"
}, {
type: "Numbers",
name: "Number"
}];
functions[10].output = "Booleans";
functions[11] = {};
functions[11].name = "<=";
functions[11].input = [{
type: "Numbers",
name: "Number"
}, {
type: "Numbers",
name: "Number"
}];
functions[11].output = "Booleans";
functions[12] = {};
functions[12].name = ">=";
functions[12].input = [{
type: "Numbers",
name: "Number"
}, {
type: "Numbers",
name: "Number"
}];
functions[12].output = "Booleans";
functions[13] = {};
functions[13].name = "even?";
functions[13].input = [{
type: "Numbers",
name: "Number"
}];
functions[13].output = "Booleans";
functions[14] = {};
functions[14].name = "odd?";
functions[14].input = [{
type: "Numbers",
name: "Number"
}];
functions[14].output = "Booleans";
functions[15] = {};
functions[15].name = "string-append";
functions[15].input = [{
type: "Strings",
name: "String"
}, {
type: "Strings",
name: "String"
}];
functions[15].output = "Strings";
functions[16] = {};
functions[16].name = "string-length";
functions[16].input = [{
type: "Strings",
name: "String"
}];
functions[16].output = "Numbers";
functions[17] = {};
functions[17].name = "string=?";
functions[17].input = [{
type: "Strings",
name: "String"
}, {
type: "Strings",
name: "String"
}];
functions[17].output = "Booleans";
functions[18] = {};
functions[18].name = "and";
functions[18].input = [{
type: "Booleans",
name: "Boolean"
}, {
type: "Booleans",
name: "Boolean"
}];
functions[18].output = "Booleans";
functions[19] = {};
functions[19].name = "or";
functions[19].input = [{
type: "Booleans",
name: "Boolean"
}, {
type: "Booleans",
name: "Boolean"
}];
functions[19].output = "Booleans";
functions[20] = {};
functions[20].name = "not";
functions[20].input = [{
type: "Booleans",
name: "Boolean"
}];
functions[20].output = "Booleans";
functions[21] = {};
functions[21].name = "rectangle";
functions[21].input = [{
type: "Numbers",
name: "Width"
}, {
type: "Numbers",
name: "Height"
}, {
type: "Strings",
name: "Outline"
}, {
type: "Strings",
name: "Color"
}];
functions[21].output = "Images";
functions[22] = {};
functions[22].name = "circle";
functions[22].input = [{
type: "Numbers",
name: "Radius"
}, {
type: "Strings",
name: "Outline"
}, {
type: "Strings",
name: "Color"
}];
functions[22].output = "Images";
functions[23] = {};
functions[23].name = "triangle";
functions[23].input = [{
type: "Numbers",
name: "Length"
}, {
type: "Strings",
name: "Outline"
}, {
type: "Strings",
name: "Color"
}];
functions[23].output = "Images";
functions[24] = {};
functions[24].name = "ellipse";
functions[24].input = [{
type: "Numbers",
name: "A"
}, {
type: "Numbers",
name: "B"
}, {
type: "Strings",
name: "Outline"
}, {
type: "Strings",
name: "Color"
}];
functions[24].output = "Images";
functions[25] = {};
functions[25].name = "star";
functions[25].input = [{
type: "Numbers",
name: "Side-Length"
}, {
type: "Strings",
name: "Outline"
}, {
type: "Strings",
name: "Color"
}];
functions[25].output = "Images";
functions[26] = {};
functions[26].name = "scale";
functions[26].input = [{
type: "Numbers",
name: "Multiple"
}, {
type: "Images",
name: "Image"
}];
functions[26].output = "Images";
functions[27] = {};
functions[27].name = "rotate";
functions[27].input = [{
type: "Numbers",
name: "Degrees"
}, {
type: "Images",
name: "Image"
}];
functions[27].output = "Images";
functions[28] = {};
functions[28].name = "place-image";
functions[28].input = [{
type: "Images",
name: "Image"
}, {
type: "Numbers",
name: "x"
}, {
type: "Numbers",
name: "y"
}, {
type: "Images",
name: "Background"
}];
functions[28].output = "Images";
//constants - an array of user defined variables containing their name and type
var constants = [];
//functionProgram - an array of user defined functions in object form (so the actual definiton object, not formatted like functions)
var functionProgram = [];
//constantProgram - an array of user defined constants in object form
var constantProgram =[];
//userFunctions - an array of user defined function in name, input, output form
var userFunctions = [];
//restricted contains a list of key words in racket that we aren't allowing the user to redefine
var restricted = ["lambda", "define", "list", "if", "else", "cond", "foldr", "foldl", "map", "let", "local"];
//pre-defined types (can be added to by user defined structs if they are ever implemented)
var types = ["Numbers", "Strings", "Booleans", "Images"];
//Colors is an object containing kv pairs of type to color
var colors = {};
colors.Numbers = "#33CCFF";
colors.Strings = "#FFA500";
colors.Images = "#66FF33";
colors.Booleans = "#CC33FF";
colors.Define = "#FFFFFF";
colors.Expressions = "#FFFFFF";
colors.Constants = "#FFFFFF";
/*
*historyarr - an array of tuples of program and storage (and eventually other things, but not currently)
* - updated every time a block is moved in the program or storage
* - used for undo, simply takes one step back and renders the program and storage
*/
var historyarr = [];
/*
* addToHistory - adds a tuple to the historyarr
* @param programState - the current program
* @param storageState- the current storage
*/
var addToHistory = function (programState, storageState) {
historyarr.push({
program: programState,
storage: storageState
});
$("#undoButton").removeAttr('disabled');
future = [];
};
//future - an array like historyarr, but only created by pressing undo
var future = [];
//program - an array of expressions (no definitions) that appear in the central coding area
var program = [];
//storageProgram - stores every object that is dropped into storage
var storageProgram = [];
/*====================================================================================
___ _ _ _ __ __ _ _ _
/ __| |___| |__ __ _| | \ \ / /_ _ _ _(_)__ _| |__| |___ ___
| (_ | / _ \ '_ \/ _` | | \ V / _` | '_| / _` | '_ \ / -_|_-<
\___|_\___/_.__/\__,_|_| _ \_/\__,_|_| |_\__,_|_.__/_\___/__/
/ _|___ | __| _ _ _ __| |_(_)___ _ _ ___
> _|_ _| | _| || | ' \/ _| _| / _ \ ' \(_-<
\_____| |_| \_,_|_||_\__|\__|_\___/_||_/__/
=====================================================================================*/
// These variables store what the height and width of the code div should be
var contentHeight;
var contentWidth;
//activated - which drawer types are currently being displayed
var activated = [];
// focused - which text block in the #code div is currently being focused
var focused = null;
//initvalue -the intial value of the focused
var initvalue = null;
//ID that matches together a code object and an HTML element
var ID = 0;
//adding - boolean of whether addind a message to an error report or output report
var adding = false;
//errorVal - used for form validation when an invalid input is in a number literal
var errorVal = false;
//timeout - delays syncing for function name
var timeout;
//onResize - resizes code div when the window is resized
function onResize() {
contentHeight = $(window).height() - $("#header").height();
contentWidth = $(window).width();