-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
181 lines (166 loc) · 3.86 KB
/
db.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
function importPackageContents(db, pkg)
{
for (var op in pkg.operators)
{
if (op in db.env) throw 'Already defined: ' + op;
db.env[op] = pkg.operators[op];
}
for (var name in pkg.names)
{
if (name in db.env) throw 'Already defined: ' + name;
db.env[name] = pkg.names[name];
}
for (var special in pkg.specials)
{
if (special in db.env) throw 'Already defined: ' + special;
db.env[special] = pkg.specials[special];
}
}
function importPackage(db, pkg, remark)
{
if (!db.usesPackage(pkg))
{
db.packageNames[pkg.name] = remark;
importPackageContents(db, pkg);
for (var i = 0; i < pkg.depends.length; i++)
{
var dep = findPackage(pkg.depends[i]);
if (dep == undefined) throw 'Missing package dependency: ' + pkg.depends[i] + ' from ' + pkg.name;
importPackage(db, dep, remark);
}
}
}
function processFreshName(db, name)
{
var pkg = findPackage(name);
if (pkg != undefined)
{
if (db.usesPackage(pkg))
{
return ['already','already','Part of ' + db.packageNames[pkg.name]];
}
else
{
try {
importPackage(db, pkg, name);
return ['import','import',undefined];
} catch(e) {
return ['fail','fail',e];
}
}
}
return undefined;
}
function processDefinition(db, name, value)
{
if (name in db.env)
throw "Wasn't expecting name already to be in database (" + name + ")";
db.env[name] = value;
return ['def','definition',undefined];
}
function processHypothesis(db, obj)
{
if (is_definitely_true(obj))
return ['already','can_deduce',undefined];
else if (is_definitely_false(obj))
return ['false','false',undefined];
var remark = undefined;
for (var name in obj.names)
{
if (!(name in db.env))
{
db.env[name] = new IntroducedName(name);
if (remark == undefined)
remark = 'Introducing ' + name;
else
remark += ', ' + name;
}
}
return ['hyp','hypothesis',remark];
}
function processDeduction(db, obj)
{
if (is_definitely_true(obj))
return ['true','true',undefined];
else if (is_definitely_false(obj))
return ['false','false',undefined];
return null;
}
function processExpression(db, obj)
{
return ['expr','expression',obj.toString()];
}
function processObj(db, section, obj)
{
if (section == 'assume')
{
if (is_fresh_name(obj))
return processFreshName(db, obj.name);
else if (is_definition(obj))
return processDefinition(db, obj.name, obj.value);
else if (is_boolean(obj))
return processHypothesis(db, obj);
}
else if (section == 'deduce')
{
if (is_boolean(obj))
return processDeduction(db, obj);
else if (is_defined(obj))
return processExpression(db, obj);
}
return undefined;
}
function process(db, section, str)
{
if (str == '')
{
return [undefined,undefined,undefined];
}
else
{
try {
var ast = parser(str);
} catch(e) {
return ['?','unknown',undefined];
}
var obj = semantics(db.env, ast);
var line = processObj(db, section, obj);
if (line != undefined) return line;
}
return ['?','unknown',undefined];
}
function DB()
{
this.packageNames = {};
this.lines = [];
this.env = {};
}
DB.prototype.isEmpty = function()
{
return this.lines.length == 0;
}
DB.prototype.usesPackage = function(pkg)
{
return this.packageNames[pkg.name] != undefined;
};
DB.prototype.pushString = function(section, str)
{
var line = process(this, section, str);
this.lines.push({str:str, annotationString:line[0], annotationClass:line[1], remarkString:line[2]});
};
DB.prototype.annotationClass = function(i)
{
return this.lines[i].annotationClass;
}
DB.prototype.annotationString = function(i)
{
return this.lines[i].annotationString;
}
DB.prototype.remarkString = function(i)
{
return this.lines[i].remarkString;
}
DB.prototype.lineCount = function()
{
return this.lines.length;
}