Initial import from FreeBSD RELENG_4:
[games.git] / sys / boot / ficl / softwords / softcore.awk
1 #!/usr/bin/awk -f
2 #
3 # Convert forth source files to a giant C string
4 #
5 # Joe Abley <jabley@patho.gen.nz>, 12 January 1999
6 #
7 # 02-oct-1999:  Cleaned up awk slightly; added some additional logic
8 #               suggested by dcs to compress the stored forth program.
9 #
10 # Note! This script uses strftime() which is a gawk-ism, and the
11 # POSIX [[:space:]] character class.
12 #
13 # $FreeBSD: src/sys/boot/ficl/softwords/softcore.awk,v 1.3.2.2 2001/03/04 04:55:10 obrien Exp $
14
15 BEGIN \
16 {
17   printf "/***************************************************************\n";
18   printf "** s o f t c o r e . c\n";
19   printf "** Forth Inspired Command Language -\n";
20   printf "** Words from CORE set written in FICL\n";
21   printf "** Author: John Sadler (john_sadler@alum.mit.edu)\n";
22   printf "** Created: 27 December 1997\n";
23   printf "** Last update: %s\n", datestamp;
24   printf "***************************************************************/\n";
25   printf "\n/*\n";
26   printf "** This file contains definitions that are compiled into the\n";
27   printf "** system dictionary by the first virtual machine to be created.\n";
28   printf "** Created automagically by ficl/softwords/softcore.awk\n";
29   printf "*/\n";
30   printf "\n#include \"ficl.h\"\n";
31   printf "\nstatic char softWords[] =\n";
32
33   commenting = 0;
34 }
35
36 # some general early substitutions
37 {
38   gsub(/\t/, "    ");                   # replace each tab with 4 spaces
39   gsub(/\"/, "\\\"");                   # escape quotes
40   gsub(/\\[[:space:]]+$/, "");          # toss empty comments
41 }
42
43 # strip out empty lines
44 /^ *$/ \
45 {
46   next;
47 }
48
49 # emit / ** lines as multi-line C comments
50 /^\\[[:space:]]\*\*/ \
51 {
52   sub(/^\\[[:space:]]/, "");
53   if (commenting == 0) printf "/*\n";
54   printf "%s\n", $0;
55   commenting = 1;
56   next;
57 }
58
59 # strip blank lines
60 /^[[:space:]]*$/ \
61 {
62   next;
63 }
64
65 # function to close a comment, used later
66 function end_comments()
67 {
68   commenting = 0;
69   printf "*/\n";
70 }
71
72 # pass commented preprocessor directives
73 /^\\[[:space:]]#/ \
74 {
75   if (commenting) end_comments();
76   sub(/^\\[[:space:]]/, "");
77   printf "%s\n", $0;
78   next;
79 }
80
81 # toss all other full-line \ comments
82 /^\\/ \
83 {
84   if (commenting) end_comments();
85   next;
86 }
87
88 # lop off trailing \ comments
89 /\\[[:space:]]+/ \
90 {
91   sub(/\\[[:space:]]+.*$/, "");
92 }
93
94 # expunge ( ) comments
95 /[[:space:]]+\([[:space:]][^)]*\)/ \
96 {
97   sub(/[[:space:]]+\([[:space:]][^)]*\)/, "");
98 }
99
100 # remove leading spaces
101 /^[[:space:]]+/ \
102 {
103   sub(/^[[:space:]]+/, "");
104 }
105
106 # removing trailing spaces
107 /[[:space:]]+$/ \
108 {
109   sub(/[[:space:]]+$/, "");
110 }
111
112 # strip out empty lines again (preceding rules may have generated some)
113 /^[[:space:]]*$/ \
114 {
115   if (commenting) end_comments();
116   next;
117 }
118
119 # emit all other lines as quoted string fragments
120 {
121   if (commenting) end_comments();
122
123   printf "    \"%s \"\n", $0;
124   next;
125 }
126
127 END \
128 {
129   if (commenting) end_comments();
130   printf "    \"quit \";\n";
131   printf "\n\nvoid ficlCompileSoftCore(FICL_VM *pVM)\n";
132   printf "{\n";
133   printf "    assert(ficlExec(pVM, softWords) != VM_ERREXIT);\n";
134   printf "}\n";
135 }