blob: 2539786aeb7bdeca7763f91f1fa9937436e753f8 (
plain)
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
|
#!/usr/bin/awk -f
# SQU.AWK --- a lisp in awk
# (C) 2022 Case Duckworth <acdw@acdw.net>
### Commentary:
# Why am I doing this?
### Code:
BEGIN {
nested = 0
buffer = ""
OFS = "\t"
STDERR = "/dev/stderr"
PRGN = "squawk"
}
{
buffer = buffer $0 "\n"
}
END {
read(buffer, ast)
eval(ast)
}
function die(message, errcode)
{
eprint(PRGN " ERROR" (message ? ": " message : ""))
exit (errcode ? errcode : 1)
}
function eprint(message)
{
# Print MESSAGE to STDERR.
print(message) > STDERR
}
function eval(ast)
{
# Evaluate multi-dimensional array AST.
for (w in ast) {
print ast[w]
}
}
function read(buf, ast)
{
# Read string BUF into multi-dimensional array AST.
split(buf, b, "")
w = 1
word = ""
# Tokenize
for (c in b) {
# print c, b[c]
if (b[c] == "\\") {
word = word b[c++]
} else if (b[c] == "(") {
if (word) {
ast[w++] = word
word = ""
}
ast[w++] = "(" nested++
} else if (b[c] == ")") {
if (word) {
ast[w++] = word
word = ""
}
ast[w++] = ")" --nested
} else if (b[c] ~ /[ \t\r\n\f\v]/) {
if (word) {
ast[w++] = word
word = ""
}
} else {
word = word b[c]
}
if (nested < 0) {
die("Unmatched paren at " c)
}
}
}
|