-
Notifications
You must be signed in to change notification settings - Fork 52
/
silq.d
150 lines (146 loc) · 4.45 KB
/
silq.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
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
// Written in the D programming language
// License: http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0
import util.path;
import std.array, std.string, std.algorithm, std.conv;
import util, util.io;
import ast.lexer, ast.parser, ast.expression, ast.declaration, ast.error, help;
import astopt;
import options, ast.scope_, ast.modules, ast.summarize;
static this(){
opt.importPath ~= buildPath(dirName(file.thisExePath),"library");
static if(language==psi) opt.importPath ~= buildPath(dirName(file.thisExePath),"..","..","..","..","ras","psi","library"); // TODO: remove
}
int run(string path){
path = getActualPath(path);
auto ext = path.extension;
if(ext != (language==astopt.silq?".slq":".psi")){ // TODO: support only language==silq
stderr.writeln(path~": unrecognized extension: "~ext);
return 1;
}
auto err=makeErrorHandler(opt.errorFormat);
scope(exit) err.finalize();
auto sc=new TopScope(err);
Expression[] exprs;
if(auto r=importModule(path,err,exprs,sc))
return r;
if(err.nerrors) return 1;
FunctionDef[string] functions;
foreach(expr;exprs){
if(cast(ErrorExp)expr) continue;
if(auto fd=cast(FunctionDef)expr){
functions[fd.name.name]=fd;
}else if(!cast(Declaration)expr&&!cast(DefineExp)expr&&!cast(CommaExp)expr) err.error("top level expression must be declaration",expr.loc);
}
if(opt.summarize.length){
try{
foreach(expr;exprs)
if(auto fd=cast(FunctionDef)expr)
writefln(getSummary(fd,opt.summarize).join(","));
}catch(Exception e){
stderr.writeln("error: ",e.msg);
return 1;
}
return 0;
}
version(CHECK_AST){
foreach(expr;exprs){
if(auto fd=cast(FunctionDef)expr){
import ast.checker:checkFunction;
checkFunction(fd);
}
}
}
// TODO: add some backends
if(err.nerrors) return 1;
if(opt.backend==BackendType.run){
import qsim;
auto be=new QSim(path);
if("main" in functions){
auto fun=functions["main"];
foreach(i;0..opt.numRuns){
auto qstate=be.run(fun,err);
if("`value" in qstate.vars)
writeln(qstate.formatQValue(qstate.vars["`value"]));
if(astopt.projectForget){
auto total=qstate.totalProb();
if(total>zeroThreshold)
writefln("Pr[error] = %f",1.0L-total);
}
}
}
}
return !!err.nerrors;
}
int main(string[] args){
//import core.memory; GC.disable();
version(TEST) test();
if(args.length) args.popFront();
auto idx=args.countUntil("--read-args");
if(idx<args.length){
args=args[0..idx]~args[idx+1..$];
foreach(filename;args){
if(filename.startsWith("--")) continue;
auto firstLine=File(filename).readln().strip();
if(firstLine.startsWith("// args: ")) args=firstLine["// args: ".length..$].split~args;
else if(firstLine.startsWith("//args: ")) args=firstLine["//args: ".length..$].split~args;
break;
}
}
args.sort!((a,b)=>a.startsWith("--")>b.startsWith("--"));
bool hasInputFile=false;
foreach(x;args){
switch(x){
case "--help": writeln(help.help); return 0;
case "--noboundscheck": opt.noBoundsCheck=true; break;
case "--trace": opt.trace=true; break;
case "--dump-reverse": opt.dumpReverse=true; break;
case "--error-json": opt.errorFormat=ErrorFormat.json; break;
case "--run": opt.backend=BackendType.run; break;
case "--unsafe-capture-const": astopt.allowUnsafeCaptureConst=true; break;
case "--project-forget": astopt.projectForget=true; break;
default:
if(x.startsWith("--summarize=")){
auto rest=x["--summarize=".length..$];
import std.regex: regex, match;
auto r=regex(r"^\[(([-a-z])*,)*([-a-z])*,?\]$");
if(match(rest,r)){
rest=rest[1..$-1];
if(rest.endsWith(",")) rest=rest[0..$-1];
opt.summarize=rest.split(',');
}else{
stderr.writeln("error: summary specification needs to be of format [key1,key2,...]");
return 1;
}
continue;
}
if(x.startsWith("--run=")){
auto rest=x["--run=".length..$];
try{
opt.backend=BackendType.run;
opt.numRuns=to!ulong(rest);
}catch(Exception){
stderr.writeln("error: number of samples needs to be 64-bit unsigned integer");
return 1;
}
continue;
}
if(x.startsWith("--seed=")){
import std.random;
try{
rndGen.seed(to!uint(x["--seed=".length..$]));
}catch(Exception){
stderr.writeln("error: random seed must be 32-bit unsigned integer");
return 1;
}
continue;
}
hasInputFile=true;
if(auto r=run(x)) return r;
}
}
if(!hasInputFile){
stderr.writeln("error: no input files");
return 1;
}
return 0;
}