-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathb4a.pas
59 lines (52 loc) · 1.49 KB
/
b4a.pas
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
program b4a; { b4 assembler }
uses ub4, ub4asm, sysutils {leftstr};
type
tfmt = (json, b4x);
var
i : ub4.value;
procedure ParseParams(var ifn, ofn: string; var fmt: tfmt);
var a : byte = 0;
begin
fmt := b4x;
for i := 1 to paramcount do begin
if paramstr(i) = '-j' then fmt := json
else case a of
0: begin ifn := paramstr(i); inc(a) end;
1: begin ofn := paramstr(i); inc(a) end;
otherwise begin writeln('too many arguments'); halt end
end
end;
if a = 0 then begin writeln('usage: b4a [-j] infile.b4a [outfile]'); halt end
else if a = 1 then begin
a := lastdelimiter('.', ifn);
if a > 0 then ofn := leftstr(ifn, a) else ofn := ifn + '.';
if fmt = json then ofn += 'json' else ofn += 'b4x';
end
end;
procedure emit_b4x(ofn: string);
var out : file of byte;
begin assign(out, ofn); rewrite(out);
for i := 0 to ub4.maxcell do write(out, ub4.mem[i]);
close(out)
end;
procedure emit_json(ofn: string);
var out : text;
begin assign(out, ofn); rewrite(out); i:= 0;
write(out, '[');
while i < ub4.maxcell do begin
if i mod 10 = 0 then writeln(out);
write(out, ' ', ub4.mem[i] : 3);
inc(i); if i < ub4.maxcell then write(out, ',');
end;
writeln(out, ']');
close(out)
end;
var ifn, ofn : string; fmt: tfmt;
begin
parseParams(ifn, ofn, fmt);
assign(input, ifn); reset(input);
boot; ub4asm.b4as;
close(input);
if fmt = b4x then emit_b4x(ofn)
else emit_json(ofn)
end.