-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.js
145 lines (128 loc) · 3.73 KB
/
env.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
var interpreter = require('./interpreter');
var forms = require('./forms');
var utils = require('./utils');
var defaultEnv = {
lambda: forms.lambda,
define: forms.define,
defmacro: forms.defmacro,
quote: forms.quote,
'if': forms.if,
'<': function (a, b) { return a < b; },
'>': function (a, b) { return a > b; },
'=': function (a, b) { return a == b; },
'+': function (a, b) { return a + b; },
'-': function (a, b) { return a - b; },
'*': function (a, b) { return a * b; },
'/': function (a, b) { return a / b; },
'%': function (a, b) { return a % b; },
and: function (a, b) { return a && b; },
or: function (a, b) { return a || b; },
not: function (expr) { return !expr; },
car: function (xs) { return xs[0]; },
cdr: function (xs) { return xs.slice(1); },
cons: function (x, xs) { return [x].concat(xs); },
concat: function(xs, ys) { return xs.concat(ys); },
size: function (xs) { return xs.length; },
list: function () { return Array.prototype.slice.call(arguments); }
};
/**
* Enrich the default environment with common definitions
*/
var buildEnv = [
/**
* Helper macro for defining functions:
*
* ['defun', 'fn', ['arg'],
* ['body', 'arg']]
*
* Will expand to:
*
* ['define', 'fn',
* ['lambda', ['arg'],
* ['body', 'arg']]]
*/
['defmacro', 'defun', ['name', 'args', 'body'],
['list',
['quote', 'define'],
'name',
['list',
['quote', 'lambda'],
'args',
'body']]],
// Check if argument is zero
['defun', 'zero', ['n'],
['=', 'n', 0]],
// Check if collection is empty
['defun', 'empty', ['coll'],
['zero', ['size', 'coll']]],
// Check if argument is even
['defun', 'even', ['n'],
['zero', ['%', 'n', 2]]],
// Check if argument is odd
['defun', 'odd', ['n'],
['not', ['even', 'n']]],
// Increase argument by 1
['defun', 'inc', ['n'],
['+', 'n', 1]],
// Decrease argument by 1
['defun', 'dec', ['n'],
['-', 'n', 1]],
// Get the element in collection by index
['defun', 'nth', ['coll', 'index'],
['if', ['zero', 'index'],
['car', 'coll'],
['nth', ['cdr', 'coll'], ['dec', 'index']]]],
// Drop first n elements from collection
['defun', 'drop', ['n', 'coll'],
['if', ['zero', 'n'],
'coll',
['drop', ['dec', 'n'], ['cdr', 'coll']]]],
// Take first n elements from collection
['defun', 'take-nth', ['n', 'coll'],
['if', ['empty', 'coll'],
'coll',
['cons',
['car', 'coll'],
['take-nth', 'n', ['drop', 'n', 'coll']]]]],
// Map a collection with function f
['defun', 'map', ['f', 'coll'],
['if', ['empty', 'coll'],
'coll',
['cons',
['f', ['car', 'coll']],
['map', 'f', ['cdr', 'coll']]]]],
// Filter a collection with predicate
['defun', 'filter', ['pred', 'coll'],
['if', ['empty', 'coll'],
'coll',
['if', ['pred', ['car', 'coll']],
['cons', ['car', 'coll'], ['filter', 'pred', ['cdr', 'coll']]],
['filter', 'pred', ['cdr', 'coll']]]]],
// Reduce a collection
['defun', 'reduce', ['coll', 'f', 'acc'],
['if', ['empty', 'coll'],
'acc',
['reduce', ['cdr', 'coll'], 'f', ['f', 'acc', ['car', 'coll']]]]],
// Create a lexical scope with bound arguments and execute body in context
['defmacro', 'let', ['bindings', 'body'],
['concat',
['list',
['list',
['quote', 'lambda'],
['take-nth', 2, 'bindings'],
'body']],
['take-nth', 2, ['cdr', 'bindings']]]]
];
/**
* Build default environment
*/
function getDefaultEnv() {
var env = utils.copy(defaultEnv);
buildEnv.forEach(function (expr) {
interpreter.evaluate(expr, env);
});
return env;
}
module.exports = {
getDefaultEnv: getDefaultEnv
};