softcore.awk revision 68729
142807Smsmith#!/usr/bin/awk -f
260962Sdcs#
342807Smsmith# Convert forth source files to a giant C string
460962Sdcs#
542807Smsmith# Joe Abley <jabley@patho.gen.nz>, 12 January 1999
660962Sdcs#
760962Sdcs# 02-oct-1999:  Cleaned up awk slightly; added some additional logic
860962Sdcs#               suggested by dcs to compress the stored forth program.
960962Sdcs#
1060962Sdcs# Note! This script uses strftime() which is a gawk-ism, and the
1160962Sdcs# POSIX [[:space:]] character class.
1260962Sdcs#
1351789Sdcs# $FreeBSD: head/sys/boot/ficl/softwords/softcore.awk 68729 2000-11-14 21:02:49Z obrien $
1442807Smsmith
1542807SmsmithBEGIN \
1642807Smsmith{
1742807Smsmith  printf "/***************************************************************\n";
1842807Smsmith  printf "** s o f t c o r e . c\n";
1942807Smsmith  printf "** Forth Inspired Command Language -\n";
2042807Smsmith  printf "** Words from CORE set written in FICL\n";
2142807Smsmith  printf "** Author: John Sadler (john_sadler@alum.mit.edu)\n";
2242807Smsmith  printf "** Created: 27 December 1997\n";
2368729Sobrien  printf "** Last update: %s\n", datestamp;
2442807Smsmith  printf "***************************************************************/\n";
2542807Smsmith  printf "\n/*\n";
2642807Smsmith  printf "** This file contains definitions that are compiled into the\n";
2742807Smsmith  printf "** system dictionary by the first virtual machine to be created.\n";
2842807Smsmith  printf "** Created automagically by ficl/softwords/softcore.awk\n";
2942807Smsmith  printf "*/\n";
3042807Smsmith  printf "\n#include \"ficl.h\"\n";
3142807Smsmith  printf "\nstatic char softWords[] =\n";
3242807Smsmith
3342807Smsmith  commenting = 0;
3442807Smsmith}
3542807Smsmith
3642807Smsmith# some general early substitutions
3742807Smsmith{
3860962Sdcs  gsub(/\t/, "    ");			# replace each tab with 4 spaces
3960962Sdcs  gsub(/\"/, "\\\"");			# escape quotes
4060962Sdcs  gsub(/\\[[:space:]]+$/, "");		# toss empty comments
4142807Smsmith}
4242807Smsmith
4342807Smsmith# strip out empty lines
4442807Smsmith/^ *$/ \
4542807Smsmith{
4642807Smsmith  next;
4742807Smsmith}
4842807Smsmith
4942807Smsmith# emit / ** lines as multi-line C comments
5060962Sdcs/^\\[[:space:]]\*\*/ \
5142807Smsmith{
5260962Sdcs  sub(/^\\[[:space:]]/, "");
5360962Sdcs  if (commenting == 0) printf "/*\n";
5460962Sdcs  printf "%s\n", $0;
5542807Smsmith  commenting = 1;
5642807Smsmith  next;
5742807Smsmith}
5842807Smsmith
5960962Sdcs# strip blank lines
6060962Sdcs/^[[:space:]]*$/ \
6142807Smsmith{
6242807Smsmith  next;
6342807Smsmith}
6442807Smsmith
6542807Smsmith# function to close a comment, used later
6642807Smsmithfunction end_comments()
6742807Smsmith{
6842807Smsmith  commenting = 0;
6942807Smsmith  printf "*/\n";
7042807Smsmith}
7142807Smsmith
7242807Smsmith# pass commented preprocessor directives
7342807Smsmith/^\\[[:space:]]#/ \
7442807Smsmith{
7542807Smsmith  if (commenting) end_comments();
7660962Sdcs  sub(/^\\[[:space:]]/, "");
7742807Smsmith  printf "%s\n", $0;
7842807Smsmith  next;
7942807Smsmith}
8042807Smsmith
8160962Sdcs# toss all other full-line \ comments
8242807Smsmith/^\\/ \
8342807Smsmith{
8442807Smsmith  if (commenting) end_comments();
8542807Smsmith  next;
8642807Smsmith}
8742807Smsmith
8860962Sdcs# lop off trailing \ comments
8960962Sdcs/\\[[:space:]]+/ \
9060962Sdcs{
9160962Sdcs  sub(/\\[[:space:]]+.*$/, "");
9260962Sdcs}
9360962Sdcs
9460962Sdcs# expunge ( ) comments
9560962Sdcs/[[:space:]]+\([[:space:]][^)]*\)/ \
9660962Sdcs{
9760962Sdcs  sub(/[[:space:]]+\([[:space:]][^)]*\)/, "");
9860962Sdcs}
9960962Sdcs
10060962Sdcs# remove leading spaces
10160962Sdcs/^[[:space:]]+/ \
10260962Sdcs{
10360962Sdcs  sub(/^[[:space:]]+/, "");
10460962Sdcs}
10560962Sdcs
10660962Sdcs# removing trailing spaces
10760962Sdcs/[[:space:]]+$/ \
10860962Sdcs{
10960962Sdcs  sub(/[[:space:]]+$/, "");
11060962Sdcs}
11160962Sdcs
11260962Sdcs# strip out empty lines again (preceding rules may have generated some)
11360962Sdcs/^[[:space:]]*$/ \
11460962Sdcs{
11560962Sdcs  if (commenting) end_comments();
11660962Sdcs  next;
11760962Sdcs}
11860962Sdcs
11942807Smsmith# emit all other lines as quoted string fragments
12042807Smsmith{
12142807Smsmith  if (commenting) end_comments();
12242807Smsmith
12360962Sdcs  printf "    \"%s \"\n", $0;
12442807Smsmith  next;
12542807Smsmith}
12642807Smsmith
12742807SmsmithEND \
12842807Smsmith{
12942807Smsmith  if (commenting) end_comments();
13042807Smsmith  printf "    \"quit \";\n";
13142807Smsmith  printf "\n\nvoid ficlCompileSoftCore(FICL_VM *pVM)\n";
13242807Smsmith  printf "{\n";
13351789Sdcs  printf "    assert(ficlExec(pVM, softWords) != VM_ERREXIT);\n";
13442807Smsmith  printf "}\n";
13542807Smsmith}
136