-
Notifications
You must be signed in to change notification settings - Fork 8
/
util.js
143 lines (112 loc) · 3.37 KB
/
util.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
// Copyright 2015 Ryan Marcus
// This file is part of vulcan.
//
// vulcan is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// vulcan is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with vulcan. If not, see <http://www.gnu.org/licenses/>.
"use strict";
const parser = require("./parse.js");
const lexer = require("./lex.js");
Array.prototype.peek = function () {
return this[this.length - 1];
};
if (!Array.prototype.includes) {
Array.prototype.includes = function(searchElement /*, fromIndex*/ ) {
'use strict';
var O = Object(this);
var len = parseInt(O.length) || 0;
if (len === 0) {
return false;
}
var n = parseInt(arguments[1]) || 0;
var k;
if (n >= 0) {
k = n;
} else {
k = len + n;
if (k < 0) {k = 0;}
}
var currentElement;
while (k < len) {
currentElement = O[k];
if (searchElement === currentElement ||
(searchElement !== searchElement && currentElement !== currentElement)) {
return true;
}
k++;
}
return false;
};
}
if (!String.prototype.startsWith) {
String.prototype.startsWith = function(searchString, position) {
position = position || 0;
return this.indexOf(searchString, position) === position;
};
}
function hoistNullActions(tree) {
if (!tree || tree.action == "substitution" || tree.action == "literal")
return tree;
if (tree.action == null)
return hoistNullActions(tree.args[0]);
return { action: tree.action,
args: tree.args.map(hoistNullActions) };
}
module.exports.negate = negate;
function negate(a) {
return { action: "negation",
args: [a] };
}
module.exports.treeToExpr = treeToExpr;
function treeToExpr(tree) {
if (tree.action == "substitution") {
return tree.args[0];
}
if (tree.action == "literal") {
return tree.args[0];
}
if (tree.action == "negation") {
if (tree.args[0].action == "substitution") {
return "!" + tree.args[0].args[0];
}
return "(!" + treeToExpr(tree.args[0]) + ")";
}
if (tree.action == "conjunction") {
return "(" + treeToExpr(tree.args[0]) + " & " + treeToExpr(tree.args[1]) + ")";
}
if (tree.action == "disjunction") {
return "(" + treeToExpr(tree.args[0]) + " | " + treeToExpr(tree.args[1]) + ")";
}
if (tree.action == "implication") {
return "(" + treeToExpr(tree.args[0]) + " -> " + treeToExpr(tree.args[1]) + ")";
}
if (tree.action == "equivalence") {
return "(" + treeToExpr(tree.args[0]) + " <-> " + treeToExpr(tree.args[1]) + ")";
}
return "";
}
module.exports.proofToString = proofToString;
function proofToString(proof) {
proof = proof.map(function(i) {
if (i.label == "sep")
return "------------------------------\n";
if (i.tree) {
return i.idx + "\t" + i.tree + "\t" + i.label + "\n";
}
return i.label + "\n";
});
return proof.join("");
}
module.exports.buildTree = buildTree;
function buildTree(string) {
return hoistNullActions(parser.parse(lexer.lex(string)));
}