/
eval.js
123 lines (107 loc) · 2.9 KB
/
eval.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
var _ = require('underscore');
var helpers = require('./util/helpers');
var car = helpers.car;
var cdr = helpers.cdr;
// forward declarations
var loopEval;
var loopEvalCollection = function(array) {
return _.map(array, function(node) {
return loopEval(node);
});
};
var loopEval = function(syntaxTree) {
var contents = syntaxTree.contents;
switch (syntaxTree.type) {
case 'list':
if (contents.length === 0) {
throw new Error("list of length 0!");
}
var firstToken = car(contents);
var remainingTokens = cdr(contents);
if (firstToken.type === 'list') {
return {
type: 'funcall',
function: loopEval(car(contents)),
arguments: {
type: 'list',
contents: loopEvalCollection(cdr(contents))
}
};
}
var body;
if (firstToken.type === 'id') {
if (firstToken.contents === 'lambda' || firstToken.contents === 'function') {
var formalArgs = car(remainingTokens);
body = loopEvalCollection(cdr(remainingTokens));
return {
type: 'funcall',
function: firstToken,
arguments: {
type: 'list',
contents: [
formalArgs,
{
type: 'list',
contents: body
}
]
}
};
} else { // user defined or built in function
body = loopEvalCollection(remainingTokens);
return {
type: 'funcall',
function: firstToken,
arguments: {
type: 'list',
contents: body
}
};
}
}
if (firstToken.type === 'prop-access') {
body = loopEvalCollection(remainingTokens);
return {
type: 'funcall',
function: loopEval(firstToken),
arguments: {
type: 'list',
contents: body
}
};
}
var errorMessage = "Parse error near: `" + firstToken.contents + "`";
if (firstToken.sourceInfo) {
errorMessage += [
' ',
'-- line: ',
firstToken.sourceInfo.first_line,
" -- col: ",
firstToken.sourceInfo.first_column,
"-",
firstToken.sourceInfo.last_column
].join('');
}
throw new Error(errorMessage);
case 'prop-access':
var key = contents.key;
var value = contents.value;
return {
type: 'prop-access',
key: loopEval(key),
value: loopEval(value)
};
case 'id':
case 'string':
case 'number':
case 'comment':
return syntaxTree;
default:
throw new Error('unknown type of syntaxTree, type: ' + syntaxTree.type);
}
};
module.exports = function(branches) {
return _.map(branches, function(branch) {
return loopEval(branch);
});
};