diff options
author | Case Duckworth | 2022-07-08 16:38:49 -0500 |
---|---|---|
committer | Case Duckworth | 2022-07-08 16:38:49 -0500 |
commit | 5f6d17d3af8052da54afeb170c6986c8cec13d88 (patch) | |
tree | ec9812c55e207c4aa1e7a1445f3e8ed4c28e13b9 | |
parent | Add test (diff) | |
download | squ.awk-main.tar.gz squ.awk-main.zip |
-rwxr-xr-x | squ.awk | 58 |
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 @@ | |||
10 | BEGIN { | 10 | BEGIN { |
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 | ||
19 | END { | 22 | END { |
20 | ast = read(buffer) | 23 | read(buffer, ast) |
21 | eval(ast) | 24 | eval(ast) |
22 | } | 25 | } |
23 | 26 | ||
24 | 27 | ||
25 | function eval(buf) | 28 | function die(message, errcode) |
26 | { | 29 | { |
30 | eprint(PRGN " ERROR" (message ? ": " message : "")) | ||
31 | exit (errcode ? errcode : 1) | ||
27 | } | 32 | } |
28 | 33 | ||
29 | function read(buf) | 34 | function eprint(message) |
30 | { | 35 | { |
36 | # Print MESSAGE to STDERR. | ||
37 | print(message) > STDERR | ||
38 | } | ||
39 | |||
40 | function eval(ast) | ||
41 | { | ||
42 | # Evaluate multi-dimensional array AST. | ||
43 | for (w in ast) { | ||
44 | print ast[w] | ||
45 | } | ||
46 | } | ||
47 | |||
48 | function 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 | } |