OCaml: put macro flag in metadata rather than special type field
[jackhill/mal.git] / js / core.js
1 // Node vs browser behavior
2 var core = {};
3 if (typeof module === 'undefined') {
4 var exports = core;
5 } else {
6 var types = require('./types'),
7 readline = require('./node_readline'),
8 reader = require('./reader'),
9 printer = require('./printer');
10 }
11
12 // Errors/Exceptions
13 function mal_throw(exc) { throw exc; }
14
15
16 // String functions
17 function pr_str() {
18 return Array.prototype.map.call(arguments,function(exp) {
19 return printer._pr_str(exp, true);
20 }).join(" ");
21 }
22
23 function str() {
24 return Array.prototype.map.call(arguments,function(exp) {
25 return printer._pr_str(exp, false);
26 }).join("");
27 }
28
29 function prn() {
30 printer.println.apply({}, Array.prototype.map.call(arguments,function(exp) {
31 return printer._pr_str(exp, true);
32 }));
33 }
34
35 function println() {
36 printer.println.apply({}, Array.prototype.map.call(arguments,function(exp) {
37 return printer._pr_str(exp, false);
38 }));
39 }
40
41 function slurp(f) {
42 if (typeof require !== 'undefined') {
43 return require('fs').readFileSync(f, 'utf-8');
44 } else {
45 var req = new XMLHttpRequest();
46 req.open("GET", f, false);
47 req.send();
48 if (req.status == 200) {
49 return req.responseText;
50 } else {
51 throw new Error("Failed to slurp file: " + f);
52 }
53 }
54 }
55
56
57 // Number functions
58 function time_ms() { return new Date().getTime(); }
59
60
61 // Hash Map functions
62 function assoc(src_hm) {
63 var hm = types._clone(src_hm);
64 var args = [hm].concat(Array.prototype.slice.call(arguments, 1));
65 return types._assoc_BANG.apply(null, args);
66 }
67
68 function dissoc(src_hm) {
69 var hm = types._clone(src_hm);
70 var args = [hm].concat(Array.prototype.slice.call(arguments, 1));
71 return types._dissoc_BANG.apply(null, args);
72 }
73
74 function get(hm, key) {
75 if (hm != null && key in hm) {
76 return hm[key];
77 } else {
78 return null;
79 }
80 }
81
82 function contains_Q(hm, key) {
83 if (key in hm) { return true; } else { return false; }
84 }
85
86 function keys(hm) { return Object.keys(hm); }
87 function vals(hm) { return Object.keys(hm).map(function(k) { return hm[k]; }); }
88
89
90 // Sequence functions
91 function cons(a, b) { return [a].concat(b); }
92
93 function concat(lst) {
94 lst = lst || [];
95 return lst.concat.apply(lst, Array.prototype.slice.call(arguments, 1));
96 }
97
98 function nth(lst, idx) {
99 if (idx < lst.length) { return lst[idx]; }
100 else { throw new Error("nth: index out of range"); }
101 }
102
103 function first(lst) { return lst[0]; }
104
105 function rest(lst) { return lst.slice(1); }
106
107 function empty_Q(lst) { return lst.length === 0; }
108
109 function count(s) {
110 if (Array.isArray(s)) { return s.length; }
111 else if (s === null) { return 0; }
112 else { return Object.keys(s).length; }
113 }
114
115 function conj(lst) {
116 if (types._list_Q(lst)) {
117 return Array.prototype.slice.call(arguments, 1).reverse().concat(lst);
118 } else {
119 var v = lst.concat(Array.prototype.slice.call(arguments, 1));
120 v.__isvector__ = true;
121 return v;
122 }
123 }
124
125 function apply(f) {
126 var args = Array.prototype.slice.call(arguments, 1);
127 return f.apply(f, args.slice(0, args.length-1).concat(args[args.length-1]));
128 }
129
130 function map(f, lst) {
131 return lst.map(function(el){ return f(el); });
132 }
133
134
135 // Metadata functions
136 function with_meta(obj, m) {
137 var new_obj = types._clone(obj);
138 new_obj.__meta__ = m;
139 return new_obj;
140 }
141
142 function meta(obj) {
143 // TODO: support symbols and atoms
144 if ((!types._sequential_Q(obj)) &&
145 (!(types._hash_map_Q(obj))) &&
146 (!(types._function_Q(obj)))) {
147 throw new Error("attempt to get metadata from: " + types._obj_type(obj));
148 }
149 return obj.__meta__;
150 }
151
152
153 // Atom functions
154 function deref(atm) { return atm.val; }
155 function reset_BANG(atm, val) { return atm.val = val; }
156 function swap_BANG(atm, f) {
157 var args = [atm.val].concat(Array.prototype.slice.call(arguments, 2));
158 atm.val = f.apply(f, args);
159 return atm.val;
160 }
161
162
163 // types.ns is namespace of type functions
164 var ns = {'type': types._obj_type,
165 '=': types._equal_Q,
166 'throw': mal_throw,
167 'nil?': types._nil_Q,
168 'true?': types._true_Q,
169 'false?': types._false_Q,
170 'symbol': types._symbol,
171 'symbol?': types._symbol_Q,
172 'keyword': types._keyword,
173 'keyword?': types._keyword_Q,
174
175 'pr-str': pr_str,
176 'str': str,
177 'prn': prn,
178 'println': println,
179 'readline': readline.readline,
180 'read-string': reader.read_str,
181 'slurp': slurp,
182 '<' : function(a,b){return a<b;},
183 '<=' : function(a,b){return a<=b;},
184 '>' : function(a,b){return a>b;},
185 '>=' : function(a,b){return a>=b;},
186 '+' : function(a,b){return a+b;},
187 '-' : function(a,b){return a-b;},
188 '*' : function(a,b){return a*b;},
189 '/' : function(a,b){return a/b;},
190 "time-ms": time_ms,
191
192 'list': types._list,
193 'list?': types._list_Q,
194 'vector': types._vector,
195 'vector?': types._vector_Q,
196 'hash-map': types._hash_map,
197 'map?': types._hash_map_Q,
198 'assoc': assoc,
199 'dissoc': dissoc,
200 'get': get,
201 'contains?': contains_Q,
202 'keys': keys,
203 'vals': vals,
204
205 'sequential?': types._sequential_Q,
206 'cons': cons,
207 'concat': concat,
208 'nth': nth,
209 'first': first,
210 'rest': rest,
211 'empty?': empty_Q,
212 'count': count,
213 'apply': apply,
214 'map': map,
215 'conj': conj,
216
217 'with-meta': with_meta,
218 'meta': meta,
219 'atom': types._atom,
220 'atom?': types._atom_Q,
221 "deref": deref,
222 "reset!": reset_BANG,
223 "swap!": swap_BANG};
224
225 exports.ns = core.ns = ns;