-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC.awk
More file actions
347 lines (314 loc) · 8.22 KB
/
C.awk
File metadata and controls
347 lines (314 loc) · 8.22 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!awk -f
#
# decisionTableCompiler - generate optimal pseudocode for decision tables
# Copyright (C) 1993-2025 G. David Butler <gdb@dbSystems.com>
#
# This file is part of decisionTableCompiler
#
# decisionTableCompiler is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# decisionTableCompiler is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Generates C header and implementation from decision table CSV pseudocode
# Usage: awk -f C.awk file.psu
# Generates file.h and file.c
BEGIN {
# Extract name from filename or use "stdin"
if (ARGC > 1 && ARGV[1] != "-") {
fname = ARGV[1]
# Remove path
sub(/^.*\//, "", fname)
# Remove .psu extension
sub(/\.psu$/, "", fname)
name = fname
} else {
name = "stdin"
}
# Set output filenames
hfile = name ".h"
cfile = name ".c"
# Convert name to uppercase for header guard
name_upper = toupper(name)
# Function name is nameCamelCase
func_name = name "Evaluate"
}
# Parse CSV fields from a line into fields[] array, return count
# Handles quoted fields with embedded commas and doubled quotes
function parse_csv(line, fields, n, i, c, in_quote, field) {
n = 0
i = 1
while (i <= length(line)) {
field = ""
in_quote = 0
c = substr(line, i, 1)
# Check if field starts with quote
if (c == "\"") {
in_quote = 1
i++
while (i <= length(line)) {
c = substr(line, i, 1)
if (c == "\"") {
# Check for doubled quote
if (substr(line, i + 1, 1) == "\"") {
field = field "\""
i += 2
} else {
# End of quoted field
i++
break
}
} else {
field = field c
i++
}
}
# Skip comma after quoted field
if (substr(line, i, 1) == ",")
i++
} else {
# Unquoted field - read until comma or end
while (i <= length(line)) {
c = substr(line, i, 1)
if (c == ",") {
i++
break
}
field = field c
i++
}
}
fields[++n] = field
}
return n
}
# Convert a string to valid C identifier
# Replace non-alphanumeric with underscore, ensure doesn't start with digit
function to_c_ident(s, result, i, c) {
result = ""
for (i = 1; i <= length(s); i++) {
c = substr(s, i, 1)
if (c ~ /[A-Za-z0-9]/)
result = result c
else
result = result "_"
}
# Ensure doesn't start with digit
if (result ~ /^[0-9]/)
result = "_" result
return result
}
# Parse input variable metadata: I,var,val
/^I,/ {
parse_csv($0, f)
var = f[2]
val = f[3]
if (!seen_input[var]++) {
input_vars[++num_inputs] = var
}
input_vals[var, ++input_val_count[var]] = val
all_vals[val]++
next
}
# Parse output variable metadata: O,var,val
/^O,/ {
parse_csv($0, f)
var = f[2]
val = f[3]
if (!seen_output[var]++) {
output_vars[++num_outputs] = var
}
output_vals[var, ++output_val_count[var]] = val
all_vals[val]++
next
}
# Parse depth: D,n
/^D,/ {
parse_csv($0, f)
depth = f[2]
next
}
# Store body lines for two-pass processing
/^[LJTR],/ {
body[++num_body] = $0
}
END {
if (num_body > 0) {
# First pass: collect jump targets
for (i = 1; i <= num_body; i++) {
line = body[i]
if (line ~ /^T,/) {
# Test: T,var,val,label
parse_csv(line, f)
label = f[4]
jump_target[label] = 1
} else if (line ~ /^J,/) {
# Jump: J,label
parse_csv(line, f)
label = f[2]
jump_target[label] = 1
}
}
emit_header()
# Second pass: emit code, filtering unused labels
for (i = 1; i <= num_body; i++) {
line = body[i]
if (line ~ /^L,/) {
# Label: L,n
parse_csv(line, f)
label = f[2]
if (label != "0" && jump_target[label])
print "L" label ":" > cfile
} else if (line ~ /^T,/) {
# Test: T,var,val,label
parse_csv(line, f)
var = f[2]
val = f[3]
label = f[4]
c_var = to_c_ident(var)
c_val = to_c_ident(val)
print " if (" c_var " == " name "_" c_var "_" c_val ")" > cfile
print " goto L" label ";" > cfile
} else if (line ~ /^J,/) {
# Jump: J,label
parse_csv(line, f)
label = f[2]
print " goto L" label ";" > cfile
} else if (line ~ /^R,/) {
# Resolve: R,var,val
parse_csv(line, f)
var = f[2]
val = f[3]
c_var = to_c_ident(var)
c_val = to_c_ident(val)
print " *" c_var " = " name "_" c_var "_" c_val ";" > cfile
}
}
emit_footer()
}
}
function emit_header( i, j, k, var, val, vals, n, c_var, c_val) {
# === HEADER FILE ===
# Header guard start
print "#ifndef " name_upper "_H" > hfile
print "#define " name_upper "_H" > hfile
print "" > hfile
# Emit enum for each input variable
for (i = 1; i <= num_inputs; i++) {
var = input_vars[i]
c_var = to_c_ident(var)
print "enum " name "_" c_var "_e {" > hfile
# Sort values for this variable
n = input_val_count[var]
for (j = 1; j <= n; j++) {
vals[j] = input_vals[var, j]
}
for (j = 1; j < n; j++) {
for (k = j + 1; k <= n; k++) {
if (vals[j] > vals[k]) {
val = vals[j]
vals[j] = vals[k]
vals[k] = val
}
}
}
# Emit enum values with name and variable prefix
for (j = 1; j <= n; j++) {
c_val = to_c_ident(vals[j])
if (j == 1)
print " " name "_" c_var "_" c_val > hfile
else
print "," name "_" c_var "_" c_val > hfile
}
print "};" > hfile
print "" > hfile
}
# Emit enum for each output variable
for (i = 1; i <= num_outputs; i++) {
var = output_vars[i]
c_var = to_c_ident(var)
print "enum " name "_" c_var "_e {" > hfile
# Sort values for this variable
n = output_val_count[var]
for (j = 1; j <= n; j++) {
vals[j] = output_vals[var, j]
}
for (j = 1; j < n; j++) {
for (k = j + 1; k <= n; k++) {
if (vals[j] > vals[k]) {
val = vals[j]
vals[j] = vals[k]
vals[k] = val
}
}
}
# Emit enum values with name and variable prefix
for (j = 1; j <= n; j++) {
c_val = to_c_ident(vals[j])
if (j == 1)
print " " name "_" c_var "_" c_val > hfile
else
print "," name "_" c_var "_" c_val > hfile
}
print "};" > hfile
print "" > hfile
}
# Emit function declaration
print "void" > hfile
print func_name "(" > hfile
for (i = 1; i <= num_inputs; i++) {
var = input_vars[i]
c_var = to_c_ident(var)
if (i == 1)
print " enum " name "_" c_var "_e " c_var > hfile
else
print " ,enum " name "_" c_var "_e " c_var > hfile
}
for (i = 1; i <= num_outputs; i++) {
var = output_vars[i]
c_var = to_c_ident(var)
print " ,enum " name "_" c_var "_e *" c_var > hfile
}
print ");" > hfile
# === C FILE ===
# Include header
print "#include \"" name ".h\"" > cfile
print "" > cfile
# Function definition
print "void" > cfile
print func_name "(" > cfile
for (i = 1; i <= num_inputs; i++) {
var = input_vars[i]
c_var = to_c_ident(var)
if (i == 1)
print " enum " name "_" c_var "_e " c_var > cfile
else
print " ,enum " name "_" c_var "_e " c_var > cfile
}
for (i = 1; i <= num_outputs; i++) {
var = output_vars[i]
c_var = to_c_ident(var)
print " ,enum " name "_" c_var "_e *" c_var > cfile
}
print "){" > cfile
if (depth != "") {
print " /* " depth " */" > cfile
}
}
function emit_footer() {
# Close header guard
print "" > hfile
print "#endif" > hfile
# Close function in C file with single exit point
print "L0:" > cfile
print " return;" > cfile
print "}" > cfile
}