Another travisCI attempt for JS and python (matrix).
[jackhill/mal.git] / js / core.js
CommitLineData
ea81a808
JM
1// Node vs browser behavior
2var core = {};
3if (typeof module === 'undefined') {
4 var exports = core;
5} else {
6 var types = require('./types'),
8cb5cda4
JM
7 readline = require('./node_readline'),
8 reader = require('./reader'),
ea81a808
JM
9 printer = require('./printer');
10}
11
12// Errors/Exceptions
13function mal_throw(exc) { throw exc; }
14
15
16// String functions
17function pr_str() {
18 return Array.prototype.map.call(arguments,function(exp) {
19 return printer._pr_str(exp, true);
20 }).join(" ");
21}
22
23function str() {
24 return Array.prototype.map.call(arguments,function(exp) {
25 return printer._pr_str(exp, false);
26 }).join("");
27}
28
29function prn() {
31b44161 30 printer.println.apply({}, Array.prototype.map.call(arguments,function(exp) {
ea81a808
JM
31 return printer._pr_str(exp, true);
32 }));
33}
34
35function println() {
31b44161 36 printer.println.apply({}, Array.prototype.map.call(arguments,function(exp) {
ea81a808
JM
37 return printer._pr_str(exp, false);
38 }));
39}
40
8cb5cda4 41function slurp(f) {
b58698b2
JM
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 }
8cb5cda4
JM
54}
55
ea81a808 56
db4c329a
JM
57// Number functions
58function time_ms() { return new Date().getTime(); }
59
60
ea81a808
JM
61// Hash Map functions
62function 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
68function 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
74function get(hm, key) {
7e9a2883 75 if (hm != null && key in hm) {
ea81a808
JM
76 return hm[key];
77 } else {
78 return null;
79 }
80}
81
82function contains_Q(hm, key) {
83 if (key in hm) { return true; } else { return false; }
84}
85
86function keys(hm) { return Object.keys(hm); }
87function vals(hm) { return Object.keys(hm).map(function(k) { return hm[k]; }); }
88
89
90// Sequence functions
91function cons(a, b) { return [a].concat(b); }
92
93function concat(lst) {
94 lst = lst || [];
95 return lst.concat.apply(lst, Array.prototype.slice.call(arguments, 1));
96}
97
b8ee29b2
JM
98function nth(lst, idx) {
99 if (idx < lst.length) { return lst[idx]; }
100 else { throw new Error("nth: index out of range"); }
101}
ea81a808
JM
102
103function first(lst) { return lst[0]; }
104
105function rest(lst) { return lst.slice(1); }
106
107function empty_Q(lst) { return lst.length === 0; }
108
109function count(s) {
110 if (Array.isArray(s)) { return s.length; }
b8ee29b2
JM
111 else if (s === null) { return 0; }
112 else { return Object.keys(s).length; }
ea81a808
JM
113}
114
115function 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
125function 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
130function map(f, lst) {
131 return lst.map(function(el){ return f(el); });
132}
133
134
135// Metadata functions
136function with_meta(obj, m) {
137 var new_obj = types._clone(obj);
138 new_obj.__meta__ = m;
139 return new_obj;
140}
141
142function 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
154function deref(atm) { return atm.val; }
155function reset_BANG(atm, val) { return atm.val = val; }
156function 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
164var 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,
b8ee29b2
JM
172 'keyword': types._keyword,
173 'keyword?': types._keyword_Q,
8cb5cda4 174
ea81a808
JM
175 'pr-str': pr_str,
176 'str': str,
177 'prn': prn,
178 'println': println,
8cb5cda4
JM
179 'readline': readline.readline,
180 'read-string': reader.read_str,
181 'slurp': slurp,
ea81a808
JM
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;},
db4c329a 190 "time-ms": time_ms,
ea81a808
JM
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,
ea81a808
JM
213 'apply': apply,
214 'map': map,
6301e0b6 215 'conj': conj,
ea81a808
JM
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
225exports.ns = core.ns = ns;