interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / src / osh / osh_gen.sh

79 lines · 2551 bytes Raw Blame History
1#!/bin/sh
2# osh_gen.sh: generate an osh unit source (lexer, parser) from the one layout list.
3# osh_gen.sh gen LAYOUT TEMPLATE OUT_OSC OUT_H write the generated files
4# osh_gen.sh check LAYOUT TEMPLATE OUT_OSC OUT_H regenerate to a temp dir and require
5# byte-identical checked-in files
6# OSC has no constants or includes, so the template (osh_lex.osc.in) names every
7# workspace offset as @NAME@ and sed substitutes the number. The C header carries the
8# same names for the host. POSIX sh and sed only.
9set -eu
10export LC_ALL=C
11
12mode=${1:?mode}
13lst=${2:?layout}
14tpl=${3:?template}
15osc=${4:?out.osc}
16hdr=${5:?out.h}
17
18# NAME VALUE pairs, comments and blank lines dropped
19pairs() {
20 sed -e 's/#.*//' -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' -e '/^$/d' "$lst"
21}
22
23build() {
24 out_osc=$1
25 out_h=$2
26 tmp=$(mktemp -d "${TMPDIR:-/tmp}/osh_gen.XXXXXX")
27 trap 'rm -rf "$tmp"' EXIT
28 pairs > "$tmp/pairs"
29 # every line is exactly NAME VALUE; names unique; values decimal
30 if grep -Env '^[A-Z][A-Z0-9_]* [0-9]+$' "$tmp/pairs" > "$tmp/bad"; [ -s "$tmp/bad" ]; then
31 echo "osh_gen: bad layout line:" >&2
32 cat "$tmp/bad" >&2
33 exit 1
34 fi
35 dup=$(sed 's/ .*//' "$tmp/pairs" | sort | uniq -d)
36 if [ -n "$dup" ]; then
37 echo "osh_gen: duplicate layout name: $dup" >&2
38 exit 1
39 fi
40 sed 's/^\([A-Z0-9_]*\) \([0-9]*\)$/s|@\1@|\2|g/' "$tmp/pairs" > "$tmp/sub.sed"
41 {
42 echo "// GENERATED by src/osh/osh_gen.sh from osh_layout.lst and $(basename "$tpl"). Do not edit; edit the .in or the list."
43 sed -f "$tmp/sub.sed" "$tpl"
44 } > "$out_osc"
45 if grep -n '@[A-Z][A-Z0-9_]*@' "$out_osc" > "$tmp/left"; then
46 echo "osh_gen: unresolved @NAME@ in template:" >&2
47 cat "$tmp/left" >&2
48 exit 1
49 fi
50 {
51 echo "/* GENERATED by src/osh/osh_gen.sh from osh_layout.lst. Do not edit. */"
52 echo "#ifndef OSH_LAYOUT_H"
53 echo "#define OSH_LAYOUT_H"
54 sed 's/^\([A-Z0-9_]*\) \([0-9]*\)$/#define OSH_\1 \2ULL/' "$tmp/pairs"
55 echo "#endif /* OSH_LAYOUT_H */"
56 } > "$out_h"
57 rm -rf "$tmp"
58 trap - EXIT
59}
60
61case $mode in
62gen)
63 build "$osc" "$hdr"
64 ;;
65check)
66 t=$(mktemp -d "${TMPDIR:-/tmp}/osh_gen_chk.XXXXXX")
67 build "$t/o.osc" "$t/o.h"
68 rc=0
69 cmp -s "$t/o.osc" "$osc" || { echo "osh_gen: $osc is stale; run make osh-gen" >&2; rc=1; }
70 cmp -s "$t/o.h" "$hdr" || { echo "osh_gen: $hdr is stale; run make osh-gen" >&2; rc=1; }
71 rm -rf "$t"
72 [ $rc -eq 0 ] && echo "osh_gen: check OK (generated files match the layout list)"
73 exit $rc
74 ;;
75*)
76 echo "usage: osh_gen.sh gen|check LAYOUT TEMPLATE OUT_OSC OUT_H" >&2
77 exit 2
78 ;;
79esac