forked from fubark/cyber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.zig
229 lines (208 loc) · 6.14 KB
/
main.zig
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
const std = @import("std");
const builtin = @import("builtin");
const stdx = @import("stdx");
const cy = @import("cyber.zig");
const log = stdx.log.scoped(.main);
const build_options = @import("build_options");
const TraceEnabled = build_options.trace;
const fmt = @import("fmt.zig");
var verbose = false;
var reload = false;
var pc: ?u32 = null;
const CP_UTF8 = 65001;
var prevWinConsoleOutputCP: u32 = undefined;
pub fn main() !void {
if (builtin.os.tag == .windows) {
prevWinConsoleOutputCP = std.os.windows.kernel32.GetConsoleOutputCP();
_ = std.os.windows.kernel32.SetConsoleOutputCP(CP_UTF8);
}
defer {
if (builtin.os.tag == .windows) {
_ = std.os.windows.kernel32.SetConsoleOutputCP(prevWinConsoleOutputCP);
}
}
const alloc = cy.heap.getAllocator();
defer cy.heap.deinitAllocator();
const args = try std.process.argsAlloc(alloc);
defer std.process.argsFree(alloc, args);
var cmd = Command.none;
var arg0: ?[]const u8 = null;
var i: usize = 1;
while (i < args.len) : (i += 1) {
const arg = args[i];
if (arg[0] == '-') {
if (std.mem.eql(u8, arg, "-v")) {
verbose = true;
} else if (std.mem.eql(u8, arg, "-r")) {
reload = true;
} else if (std.mem.eql(u8, arg, "-pc")) {
i += 1;
if (i < args.len) {
pc = try std.fmt.parseInt(u32, args[i], 10);
} else {
std.debug.print("Missing pc arg.\n", .{});
exit(1);
}
}
} else {
if (cmd == .none) {
if (std.mem.eql(u8, arg, "compile")) {
cmd = .compile;
} else if (std.mem.eql(u8, arg, "version")) {
cmd = .version;
} else if (std.mem.eql(u8, arg, "help")) {
cmd = .help;
} else {
cmd = .eval;
if (arg0 == null) {
arg0 = arg;
}
}
} else {
if (arg0 == null) {
arg0 = arg;
}
}
}
}
switch (cmd) {
.eval => {
if (arg0) |path| {
try evalPath(alloc, path);
} else {
return error.MissingFilePath;
}
},
.compile => {
if (arg0) |path| {
try compilePath(alloc, path);
} else {
return error.MissingFilePath;
}
},
.help => {
help();
},
.version => {
version();
},
.none => {
help();
exit(1);
},
}
}
fn exit(code: u8) noreturn {
if (builtin.os.tag == .windows) {
_ = std.os.windows.kernel32.SetConsoleOutputCP(prevWinConsoleOutputCP);
}
std.os.exit(code);
}
const Command = enum {
eval,
compile,
help,
version,
none,
};
fn compilePath(alloc: std.mem.Allocator, path: []const u8) !void {
const src = try std.fs.cwd().readFileAlloc(alloc, path, 1e10);
defer alloc.free(src);
const vm = cy.getUserVM();
try vm.init(alloc);
defer vm.deinit();
var trace: cy.TraceInfo = undefined;
vm.setTrace(&trace);
const res = vm.compile(path, src) catch |err| {
fmt.panic("unexpected {}\n", &.{fmt.v(err)});
};
if (res.err) |err| {
switch (err) {
.tokenize,
.parse,
.compile, => {
if (!cy.silentError) {
const report = try vm.allocLastErrorReport();
defer alloc.free(report);
fmt.printStderr(report, &.{});
}
exit(1);
},
}
}
try cy.debug.dumpBytecode(vm.constInternal(), pc);
}
fn evalPath(alloc: std.mem.Allocator, path: []const u8) !void {
const src = try std.fs.cwd().readFileAllocOptions(alloc, path, 1e10, 4096, @alignOf(u8), null);
defer alloc.free(src);
cy.verbose = verbose;
const vm = cy.getUserVM();
try vm.init(alloc);
defer vm.deinit();
var trace: cy.TraceInfo = undefined;
vm.setTrace(&trace);
defer {
if (TraceEnabled) {
trace.deinit(alloc);
}
}
_ = vm.eval(path, src, .{
.singleRun = builtin.mode == .ReleaseFast,
.enableFileModules = true,
.reload = reload,
}) catch |err| {
switch (err) {
error.Panic,
error.TokenError,
error.ParseError,
error.CompileError => {
if (!cy.silentError) {
const report = try vm.allocLastErrorReport();
defer alloc.free(report);
fmt.printStderr(report, &.{});
}
exit(1);
},
else => {
fmt.panic("unexpected {}\n", &.{fmt.v(err)});
},
}
};
if (verbose) {
std.debug.print("\n==VM Info==\n", .{});
try vm.dumpInfo();
if (TraceEnabled) {
vm.dumpStats();
}
}
if (cy.TrackGlobalRC) {
vm.internal().compiler.deinitRtObjects();
vm.internal().deinitRtObjects();
try cy.arc.checkGlobalRC(vm.internal());
}
}
fn help() void {
std.debug.print(
\\Cyber {s}
\\
\\Usage: cyber [source]
\\ cyber [command] ...
\\
\\Commands:
\\ cyber [source] Compile and run a script.
\\ cyber compile [source] Compile script and dump the bytecode.
\\ cyber help Print usage.
\\ cyber version Print version number.
\\
\\General Options:
\\ -r Refetch url imports and cached assets.
\\ -v Verbose.
\\
\\cyber compile Options:
\\ -pc Next arg is the pc to dump detailed bytecode at.
\\
, .{build_options.version});
}
fn version() void {
std.debug.print("{s}\n", .{build_options.full_version});
}