-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompiler.js
More file actions
196 lines (178 loc) · 4.77 KB
/
compiler.js
File metadata and controls
196 lines (178 loc) · 4.77 KB
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
var compiler = (function (parser) {
function compile(string) {
var ast = parser.parse(string);
var js = ast.map(visitStatement).join("\n");
return js;
}
function visitStatement(statement) {
// Statements should finish with a semicolon
return visit(statement) + ";";
}
function visit(expr) {
// TODO(Richo): Maybe replace with an actual visitor...
switch (expr.type) {
case "Variable":
return visitVariable(expr);
break;
case "Number":
return visitNumber(expr);
break;
case "String":
return visitString(expr);
break;
case "Assignment":
return visitAssignment(expr);
break;
case "Array":
return visitArray(expr);
break;
case "MessageSend":
return visitMessageSend(expr);
break;
case "Method":
return visitMethod(expr);
break;
case "Javascript":
return visitJavascript(expr);
break;
}
}
function visitVariable(expr) {
return "context.lookup('" + expr.value + "').get()";
}
function visitNumber(expr) {
return "("+expr.value+")";
}
function visitString(expr) {
return "CreateString('" + expr.value + "')";
}
function visitAssignment(expr) {
return "context.lookup('" + expr.left.value + "').set("+visit(expr.right)+")";
}
function visitArray(expr) {
return "model.createList([" + expr.elements.map(visit).join(", ") + "])";
}
function visitMessageSend(expr) {
return visit(expr.rcvr) + ".receive('" +
expr.selector + "')(" +
expr.args.map(visit).join(", ") + ")";
}
function visitMethod(expr) {
if (expr.body.type && expr.body.type === "Javascript") {
return 'CreateMethod("' + expr.selector + '", "(function (' +
expr.args.join(", ") + ") {" +
expr.args.map(function(item){return "context.set('"+item+"',"+item+");";}).join("")+
expr.body.code.replace(/\\/g,'\\\\').replace(/"/g,'\\"') + '})", context)';
} else {
var body = expr.body.map(visit);
body.push("return ("+ body.pop()+")");
return `CreateMethod("` + expr.selector+
`","`+ "("+"function " + "(" +
expr.args.join(", ") + ") {" +
expr.args.map(function(item){return "context.set('"+item+"',"+item+");";}).join("")+
(expr.temps.map(function (tmp) {
return 'context.set("' + tmp + '", context.lookup("null").get());';
}).join(" ") +
body.join("; ")).replace(/\\/g,'\\\\').replace(/"/g,'\\"') + `; })",context)`;
}
}
function visitJavascript(expr) {
return expr.code;
}
function evaluate(string) {
return HiveEval(model.getRoot().get("context"), compiler.compile(string));
}
Object.prototype.receive = function (selector) {
var method = this.lookup(selector).get();
if (method != null) {
var currentExecutionContext=CreateContext(method.get("context"));
//currentExecutionContext.set('self',this);
return HiveEval(currentExecutionContext, method.get("source"));
}
return function () { return CreateString("DNU: " + selector); }
}
Object.prototype.lookup = function(selector) {
var me = this;
var myType = me.type;
switch (myType) {
case "List":
//Array
return staticLookup("List",me,selector);
break;
case "EditableString":
//String
return staticLookup("String",me,selector);
break;
}
if (this.keys().includes(selector)) {
return {
get: function () {
return me.get(selector);
},
set: function (value) {
me.set(selector,value);
return value;
},
found: true
};
}
if (this.keys().includes('parent')) {
var parentSlot = this.get('parent').lookup(selector);
if (parentSlot.found){ return parentSlot; }
}
return {
get: function() {
return me.get(selector);
},
set: function(value) {
me.set(selector,value);
return value;
},
found:false
};
};
function staticLookup (parnetName,selfValue,selector) {
var parent = model.getRoot().get('context').lookup(parnetName).get();
if (parent != null) {
var real = parent.lookup(selector).get();
var method = model.createMap();
if(real == null) {
return {
get: function() {
return parent.get(selector);
},
set: function(value) {
parent.set(selector,value);
return value;
},
found:false
};
}
var ct = CreateContext(real.get('context'));
ct.set('self',selfValue);
method.set('context',ct);
method.set('source',real.get('source'));
method.set("selector",selector);
method.set("value",method);
return {
get: function() {
return method;
},
set: function(value) {
parent.set(value);
return value;
},
found:true
};
} else {
return CreateString('DNU: ' + selector);
}
}
Number.prototype.lookup = function(selector) {
return staticLookup('Number',this.valueOf(),selector);
};
return {
compile: compile,
evaluate: evaluate
};
})(parser);