-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhammer.wls
69 lines (49 loc) · 2 KB
/
hammer.wls
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
#!/usr/bin/env wolframscript
(* ::Package:: *)
(* ::Subsection:: *)
(*Imports and setup*)
SetDirectory[DirectoryName[$InputFileName /. "" :> NotebookFileName[]]];
Get["Sledgehammer`", Path -> Directory[]];
On@Assert
flags = $ScriptCommandLine[[2]] // Characters;
(* check flags *)
validFlags = Characters@"wcbdhxn";
badFlags = Complement[flags, validFlags];
Assert[Length@badFlags == 0, "Invalid flags" <> StringJoin@badFlags];
printDebug = MemberQ[flags, "d"];
If[MemberQ[flags, "h"], Print["Print history not implemented"]];
wform = MemberQ[flags, "w"];
cform = MemberQ[flags, "c"];
bform = MemberQ[flags, "b"];
If[MemberQ[flags, "x"], Print["Explanation not implemented"]];
noExecute = MemberQ[flags, "n"];
Assert[BooleanCountingFunction[1, 3][wform, cform, bform], "Must have exactly one of wcb flags!"];
(* ::Subsection:: *)
(*Execution*)
codeFile = $ScriptCommandLine[[3]];
code = Which[
wform, ToExpression[ReadString@codeFile, InputForm, HoldComplete],
cform, postfixToW@decompress@bytesToBits@BinaryReadList@codeFile,
bform, postfixToW@decompress@brailleToBits@Import[codeFile,"Text", CharacterEncoding->"UTF8"],
True, Assert[False, "Must have one of wcb flags!"]];
(* compression should be reversible *)
pfCode = wToPostfix@code;
Assert[decompress@padTo8@compress@pfCode == pfCode];
debugHistory = {};
input = If[Length@$ScriptCommandLine >= 4,
ReadList@$ScriptCommandLine[[4]],
{}
];
If[!noExecute, Print@eval[code, input]];
If[printDebug,
Print@code;
Export["_tokenized.mx", {Length@tokenToBits@#, If[AssociationQ@#, Values@#,#]} &/@code, "Table"];
Export["_compressed.mx", compressed = compress@code, "String"];
BinaryWrite["_.hmrc", bitsToBytes@compressed];
WriteString[
f = OpenWrite["_braille.hmrb", CharacterEncoding->"UTF-8"],
bitsToBraille@compressed]; Close[f];
Print["Compressed code exported to debug files"];
codeLength = Length@compressed;
Print["Code length: ", codeLength, " bits = ", N@codeLength/8, " bytes"];
Export["_debugHistory.mx", debugHistory // Map[OutputForm], "Table"] ]