-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathlittlelisp.js
138 lines (122 loc) · 3.69 KB
/
littlelisp.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
;(function(exports) {
var library = {
first: function(x) {
return x[0];
},
rest: function(x) {
return x.slice(1);
},
print: function(x) {
console.log(x);
return x;
}
};
var Context = function(scope, parent) {
this.scope = scope;
this.parent = parent;
this.get = function(identifier) {
if (identifier in this.scope) {
return this.scope[identifier];
} else if (this.parent !== undefined) {
return this.parent.get(identifier);
}
};
};
var special = {
let: function(input, context) {
var letContext = input[1].reduce(function(acc, x) {
acc.scope[x[0].value] = interpret(x[1], context);
return acc;
}, new Context({}, context));
return interpret(input[2], letContext);
},
lambda: function(input, context) {
return function() {
var lambdaArguments = arguments;
var lambdaScope = input[1].reduce(function(acc, x, i) {
acc[x.value] = lambdaArguments[i];
return acc;
}, {});
return interpret(input[2], new Context(lambdaScope, context));
};
},
if: function(input, context) {
return interpret(input[1], context) ?
interpret(input[2], context) :
interpret(input[3], context);
}
};
var interpretList = function(input, context) {
if (input.length > 0 && input[0].value in special) {
return special[input[0].value](input, context);
} else {
var list = input.map(function(x) { return interpret(x, context); });
if (list[0] instanceof Function) {
return list[0].apply(undefined, list.slice(1));
} else {
return list;
}
}
};
var interpret = function(input, context) {
if (context === undefined) {
return interpret(input, new Context(library));
} else if (input instanceof Array) {
return interpretList(input, context);
} else if (input.type === "identifier") {
return context.get(input.value);
} else if (input.type === "number" || input.type === "string") {
return input.value;
}
};
var categorize = function(input) {
if (!isNaN(parseFloat(input))) {
return { type:'number', value: parseFloat(input) };
} else if (input[0] === '"' && input.slice(-1) === '"') {
return { type:'string', value: input.slice(1, -1) };
} else {
return { type:'identifier', value: input };
}
};
var parenthesize = function(input, list) {
if (list === undefined) {
return parenthesize(input, []);
} else {
var token = input.shift();
if (token === undefined) {
return list.pop();
} else if (token === "(") {
list.push(parenthesize(input, []));
return parenthesize(input, list);
} else if (token === ")") {
return list;
} else {
return parenthesize(input, list.concat(categorize(token)));
}
}
};
var tokenize = function(input) {
return input.split('"')
.map(function(x, i) {
if (i % 2 === 0) { // not in string
return x.replace(/\(/g, ' ( ')
.replace(/\)/g, ' ) ');
} else { // in string
return x.replace(/ /g, "!whitespace!");
}
})
.join('"')
.trim()
.split(/\s+/)
.map(function(x) {
return x.replace(/!whitespace!/g, " ");
});
};
var parse = function(input) {
return parenthesize(tokenize(input));
};
exports.littleLisp = {
parse: parse,
interpret: interpret
};
})(typeof exports === 'undefined' ? this : exports);