summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rwxr-xr-xsqu.awk58
1 files changed, 55 insertions, 3 deletions
diff --git a/squ.awk b/squ.awk index d4753a0..2539786 100755 --- a/squ.awk +++ b/squ.awk
@@ -10,6 +10,9 @@
10BEGIN { 10BEGIN {
11 nested = 0 11 nested = 0
12 buffer = "" 12 buffer = ""
13 OFS = "\t"
14 STDERR = "/dev/stderr"
15 PRGN = "squawk"
13} 16}
14 17
15{ 18{
@@ -17,15 +20,64 @@ BEGIN {
17} 20}
18 21
19END { 22END {
20 ast = read(buffer) 23 read(buffer, ast)
21 eval(ast) 24 eval(ast)
22} 25}
23 26
24 27
25function eval(buf) 28function die(message, errcode)
26{ 29{
30 eprint(PRGN " ERROR" (message ? ": " message : ""))
31 exit (errcode ? errcode : 1)
27} 32}
28 33
29function read(buf) 34function eprint(message)
30{ 35{
36 # Print MESSAGE to STDERR.
37 print(message) > STDERR
38}
39
40function eval(ast)
41{
42 # Evaluate multi-dimensional array AST.
43 for (w in ast) {
44 print ast[w]
45 }
46}
47
48function read(buf, ast)
49{
50 # Read string BUF into multi-dimensional array AST.
51 split(buf, b, "")
52 w = 1
53 word = ""
54 # Tokenize
55 for (c in b) {
56 # print c, b[c]
57 if (b[c] == "\\") {
58 word = word b[c++]
59 } else if (b[c] == "(") {
60 if (word) {
61 ast[w++] = word
62 word = ""
63 }
64 ast[w++] = "(" nested++
65 } else if (b[c] == ")") {
66 if (word) {
67 ast[w++] = word
68 word = ""
69 }
70 ast[w++] = ")" --nested
71 } else if (b[c] ~ /[ \t\r\n\f\v]/) {
72 if (word) {
73 ast[w++] = word
74 word = ""
75 }
76 } else {
77 word = word b[c]
78 }
79 if (nested < 0) {
80 die("Unmatched paren at " c)
81 }
82 }
31} 83}