forked from hdoan741/jstutor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReferenceParser.js
93 lines (84 loc) · 2.44 KB
/
ReferenceParser.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
var ReferenceParser = function() {
var getFuncName = function(src) {
return src.substring('function '.length, src.indexOf('{')).trim();
}
return {
// extract inner reference in a refValue
// happens on array, etc.
extractRef: function(refValue) {
var refs = [];
if (refValue.type == 'object') {
for (var i in refValue.properties) {
refs.push(refValue.properties[i].ref);
}
}
return refs;
},
extractFuncName: getFuncName,
// render format for online python tutor
renderOPTFormat: function(refMaps, variables) {
var heaps = {};
var primitives = {};
function render(index) {
if (primitives.hasOwnProperty(index)) {
return primitives[index];
}
if (heaps.hasOwnProperty(index)) {
return [
'REF',
index
];
}
var refValue = refMaps[index];
var renderedValue = null;
if (refValue.type == 'function') {
var func_name = getFuncName(refValue.source);
renderedValue = [
'FUNCTION',
func_name,
null
];
heaps[index] = renderedValue;
} else if (refValue.type == 'object') {
renderedValue = [];
var startIndex = 0;
if (refValue.className == 'Array') {
renderedValue = ['LIST'];
startIndex = 1;
} else
if (refValue.className == 'Object') {
renderedValue = ['DICT'];
}
for (var i = startIndex; i < refValue.properties.length; i++) {
var propertyValue = refValue.properties[i];
var renderedProperty = render(propertyValue.ref);
renderedValue.push(renderedProperty);
}
heaps[index] = renderedValue;
} else {
renderedValue = refValue.value || refValue.text;
primitives[index] = renderedValue;
}
return renderedValue;
}
for (i in refMaps) {
render(i);
}
var variableDicts = [];
for (i in variables) {
localVars = variables[i];
var varDict = {};
for (vname in localVars) {
var v = localVars[vname];
varDict[vname] = render(v.ref);
}
variableDicts.push(varDict);
}
return {
'heap': heaps,
'variableDicts': variableDicts
}
}
}
};
module.exports = ReferenceParser ();