-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathutil.js
More file actions
84 lines (74 loc) · 2.08 KB
/
Copy pathutil.js
File metadata and controls
84 lines (74 loc) · 2.08 KB
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
// ******************************
// * PARSING
// ******************************
function is_leaf(node) { return (!node["children"]); }
var boolean_aliases = {
"#t": true,
"#T": true,
"true": true,
"#f": false,
"#F": false,
"false": false
}
function is_string(s) { return s != undefined && s[0] == "\""; }
function is_number(s) { return !isNaN(parseFloat(s)); }
function is_identifier(s) { return !(boolean_aliases[s]!=undefined || is_string(s) || is_number(s)); }
// ******************************
// * EVALUATION
// ******************************
function make_church_error(name, start, end, msg) {
return {name: "Church" + name, message: start + "-" + end + ": " + msg, start: start, end: end};
}
// TODO: update this for new list format
function format_result(obj) {
if (Array.isArray(obj)) {
// if we're a list
if (obj[obj.length-1] == null) {
return "(" + obj.slice(0,-1).map(format_result).join(" ") + ")";
}
// if we're just a pair
else {
var formatted = obj.map(format_result);
var most = formatted.slice(0,-1);
var last = formatted[formatted.length - 1]
return "(" +
most.join(" ") +
" . " +
last +
")";
}
} else {
if (typeof(obj) == "boolean") {
if (obj) {
return "#t";
} else {
return "#f";
}
} else if (typeof obj == "string") {
return obj
} else if (obj instanceof RegExp) {
return obj.toString();
} else if (typeof obj == "object") {
return JSON.stringify(obj);
} else {
return obj + "";
}
}
}
function log(obj, header) {
console.log(header||"*********************************");
if (typeof(obj) == "string") {
console.log(obj);
} else {
console.log(JSON.stringify(obj, undefined, 2));
}
}
module.exports = {
is_leaf: is_leaf,
boolean_aliases: boolean_aliases,
is_string: is_string,
is_identifier: is_identifier,
make_church_error: make_church_error,
format_result: format_result,
log: log
}