-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhscript
More file actions
executable file
·102 lines (85 loc) · 1.94 KB
/
hscript
File metadata and controls
executable file
·102 lines (85 loc) · 1.94 KB
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
#!/usr/bin/env bash
# Usage: hscript input.hs
# Note: this file was partially generated with an LLM.
mkdir -p /tmp/hscript
awk '
function reset_block() {
block_len = 0;
}
function push_line(line) {
block[block_len++] = line;
}
# Heuristics for "IO / Template Haskell-ish" lines
function is_special(line) {
# Contains:
# - <-
# - $( ... )
# - declareColumns
# - D.derive ...
return (line ~ /<-/ ||
line ~ /[$][(]/ ||
line ~ /declareColumns/ ||
line ~ /D[.]derive/);
}
function flush_block() {
if (block_len == 0) return;
lang_or_import = 1; # all LANGUAGE/imports?
all_special = 1; # every line is IO/TH-ish?
any_special = 0; # at least one IO/TH-ish line?
for (i = 0; i < block_len; i++) {
line = block[i];
if (line !~ /^{-# LANGUAGE/ && line !~ /^import[[:space:]]+/) {
lang_or_import = 0;
}
if (is_special(line)) {
any_special = 1;
} else {
all_special = 0;
}
}
if (all_special && block_len > 0) {
# All IO/TH-ish: each gets its own :{ ... :}
for (i = 0; i < block_len; i++) {
print ":{";
print block[i];
print ":}";
}
} else if (lang_or_import || block_len > 1 || any_special) {
# Multi-line chunk, or imports/pragmas, or a single special line
print ":{";
for (i = 0; i < block_len; i++) {
print block[i];
}
print ":}";
} else {
# Single, non-special line (e.g. `default (Int, T.Text)`)
for (i = 0; i < block_len; i++) {
print block[i];
}
}
reset_block();
}
BEGIN {
reset_block();
}
# GHCi-style commands (e.g. :set, :load) — pass through
/^[[:space:]]*:/ {
flush_block();
print;
next;
}
# Blank line: end current chunk, keep blank
NF == 0 {
flush_block();
print "";
next;
}
# Any other line is part of the current Haskell chunk
{
push_line($0);
}
END {
flush_block();
}
' "$@" > /tmp/hscript/script.ghci
ghc -e ':script /tmp/hscript/script.ghci'