-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbayonet.d
63 lines (58 loc) · 1.64 KB
/
bayonet.d
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
import std.stdio, std.path, std.array, std.string, std.algorithm;
import file=std.file;
import lexer, parser, expression, declaration, error, util;
import scope_, semantic_;
string getActualPath(string path){
// TODO: search path
auto ext = path.extension;
if(ext=="") path = path.setExtension("bayonet");
//return file.getcwd().canFind("/test")?path:"test/"~path;
return path;
}
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){ return readCode(File(path)); }
int run(string path){
path = getActualPath(path);
auto ext = path.extension;
if(ext != ".bayonet"){
stderr.writeln(path~": unrecognized extension: "~ext);
return 1;
}
string code;
try code=readCode(path);
catch(Exception){
if(!file.exists(path)) stderr.writeln(path ~ ": no such file");
else stderr.writeln(path ~ ": error reading file");
return 1;
}
auto src=new Source(path, code);
auto err=new FormattingErrorHandler();
auto program=parseFile(src,err);
program=semantic(src,program,new TopScope(err));
//writeln(program);
if(!err.nerrors){
import translate_;
writeln(translate(program,new Builder()));
}
return !!err.nerrors;
}
int main(string[] args){
//import core.memory; GC.disable();
version(TEST) test();
if(args.length<2){
stderr.writeln("error: no input files");
return 1;
}
args.popFront();
args.sort!((a,b)=>a.startsWith("--")>b.startsWith("--"));
foreach(x;args){
if(auto r=run(x)) return r;
}
return 0;
}