Repository navigation
Expand file tree
/
Copy pathmodules.d
More file actions
251 lines (233 loc) · 6.74 KB
/
Copy pathmodules.d
File metadata and controls
251 lines (233 loc) · 6.74 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
module ast.modules;
import util.tuple: Q=Tuple, q=tuple;
import util: mallocAppender;
import astopt;
import ast.parser;
import ast.error: ErrorHandler;
import ast.lexer: Source, Location;
import ast.scope_: Scope, TopScope;
import ast.declaration;
import ast.expression: Expression;
private TopScope preludeScope=null;
private Source preludeSrc=null;
private TopScope operatorScope=null;
private static Q!(Expression[],TopScope)[string] modules;
import util.io;
string readCode(File f){
// TODO: use memory-mapped file with 4 padding zero bytes
auto app=mallocAppender!(char[])();
foreach(r;f.byChunk(1024)){app.put(cast(char[])r);}
app.put("\0\0\0\0"); // insert 4 padding zero bytes
return cast(string)app.data;
}
string readCode(string path){
version(WebAssembly){
if(path=="/dev/stdin") return readCode(File(path));
stderr.writeln("error: reading from file `",path,"` not supported in WebAssembly, use `--stdin` flag");
import std.exception:enforce;
enforce(0);
assert(0);
}else{
return readCode(File(path));
}
}
string readBuiltin(string[] paths)(int index){
string path = paths[index];
version(WebAssembly) string binPath="/home/silq";
else{
import std.file: thisExePath;
import util.path: dirName;
string binPath = dirName(thisExePath());
foreach(tryPath; [
binPath ~ "/library/" ~ path,
binPath ~ "/../share/silq/library/" ~ path,
]) {
try return readCode(tryPath);
catch(Exception) {}
}
}
switch(index){
static foreach(i,p;paths){
case i:
// 4 padding bytes required by lexer
return import(p) ~ "\0\0\0\0";
}
default: assert(0);
}
}
Scope getPreludeScope(ErrorHandler err, Location loc){
import ast.semantic_: semantic;
import ast.expression;
import ast.type;
if(preludeScope) return preludeScope;
preludeScope = new TopScope(".prelude",err);
foreach(name, value; [
"𝔹": Bool(false),
"⊥": bottom(),
"𝟙": unit(),
"*": typeTy(),
"ctype": ctypeTy(),
"etype": etypeTy(),
"qtype": qtypeTy(),
"utype": utypeTy(),
"qnumeric": qnumericTy(),
"ℕ": ℕt(false),
"ℤ": ℤt(false),
"ℚ": ℚt(false),
"ℝ": ℝ(false),
"ℂ": ℂ(false),
]) {
auto id = new Identifier(name);
auto vd = new VarDecl(id);
id.meaning = vd;
vd.initializer = value;
id.type = vd.dtype = vd.vtype = value.type;
id.sstate = vd.sstate = SemState.completed;
preludeScope.insert(vd);
}
preludeSrc = new Source(".prelude", readBuiltin!preludePaths(preludeIndex));
int nerr = err.nerrors;
auto exprs = parseSource(preludeSrc, err);
exprs = semantic(exprs, preludeScope);
if(nerr != err.nerrors) {
if(loc.line) preludeScope.error("failed to import prelude", loc);
else stderr.writeln("error: failed to import prelude");
}
return preludeScope;
}
bool isInPrelude(Declaration decl){
if(!preludeScope) return false;
return decl.scope_.isNestedIn(preludeScope);
}
bool isInPrelude(Scope sc){
if(!preludeScope) return false;
return sc.isNestedIn(preludeScope);
}
bool isPreludeSource(Source src){
return src is preludeSrc;
}
string getActualPath(string path){
import util.path;
import util.io:file;
auto ext = path.extension;
import std.string:startsWith;
if(!path.startsWith("/dev/") && ext=="") path = path.setExtension(astopt.defaultExtension);
if(file.exists(path)) return path;
foreach_reverse(p;astopt.importPath){
auto candidate=buildPath(p,path);
if(file.exists(candidate))
return candidate;
}
return path;
}
string getShortPath(string filePath){
import std.path: relativePath;
string result=filePath;
foreach_reverse(p;astopt.importPath){
auto candidate=relativePath(filePath,p);
if(candidate.length<result.length)
result=filePath;
}
return result;
}
string moduleName(string filePath){
import std.string:replace;
import util.path;
auto moduleName=getShortPath(filePath).setExtension("");
moduleName=moduleName.replace("../","..");
moduleName=moduleName.replace("./",".");
moduleName=moduleName.replace("/",".");
return moduleName.replace("/",".");
}
int importModule(Source src,ErrorHandler err,out Expression[] exprs,out TopScope sc,Location loc){
int nerr=err.nerrors;
exprs=parseSource(src,err);
if(nerr!=err.nerrors) return 1;
sc=new TopScope(moduleName(src.name),err);
sc.import_(getPreludeScope(err, loc));
import ast.semantic_: semantic;
exprs=semantic(exprs,sc);
if(nerr!=err.nerrors){
if(loc.line) sc.error("errors in imported file",loc);
return 1;
}
return 0;
}
int importModule(string path,ErrorHandler err,out Expression[] exprs,out TopScope sc,Location loc){
path = getActualPath(path);
if(path in modules){
// only the main module doesn't have an import location
assert(loc.line);
auto exprssc=modules[path];
exprs=exprssc[0],sc=exprssc[1];
if(!sc){
err.error("circular imports not supported",loc);
return 1;
}
return 0;
}
modules[path]=q(Expression[].init,TopScope.init);
scope(success) modules[path]=q(exprs,sc);
if(auto r=parseFile(getActualPath(path),err,exprs,loc))
return r;
sc=new TopScope(moduleName(path),err);
sc.import_(getPreludeScope(err, loc));
int nerr=err.nerrors;
import ast.semantic_: semantic;
exprs=semantic(exprs,sc);
if(nerr!=err.nerrors){
if(loc.line) sc.error("errors in imported file",loc);
return 1;
}
return 0;
}
int parseFile(string path,ErrorHandler err,ref Expression[] r,Location loc=Location.init){
string code;
try code=readCode(path);
catch(Exception){
string error;
if(!file.exists(path)){
// bake prelude into binary as a fallback
void noSuchFile(){ error = path ~ ": no such file"; }
static if(language==psi){
if(path=="prelude.psi") code=import("prelude.psi") ~ "\0\0\0\0";
else if(path=="prelude-nocheck.psi") code=import("prelude-nocheck.psi") ~ "\0\0\0\0";
else noSuchFile();
}else noSuchFile();
}else error = path ~ ": error reading file";
if(error){
if(loc.line) err.error(error,loc);
else stderr.writeln("error: "~error);
return 1;
}
}
auto src=new Source(path, code);
auto nerr=err.nerrors;
r=parseSource(src,err);
return nerr!=err.nerrors;
}
static if(language==silq)
Scope getOperatorScope(ErrorHandler err, Location loc){
import ast.semantic_: semantic;
if(operatorScope) return operatorScope;
operatorScope = new TopScope(".operators",err);
operatorScope.import_(getPreludeScope(err, loc));
auto src = new Source(".operators", readBuiltin!([operatorsPath])(0));
int nerr = err.nerrors;
auto exprs = parseSource(src, err);
exprs = semantic(exprs,operatorScope);
if(nerr != err.nerrors) {
if(loc.line) operatorScope.error("failed to import operators", loc);
else stderr.writeln("error: failed to import operators");
}
return operatorScope;
}
static if(language==silq)
bool isInOperators(Declaration decl){
static if(operatorLowering) {
if(!operatorScope) return false;
return decl.scope_.isNestedIn(operatorScope);
} else {
return false;
}
}