src/Tools/Code/lib/Tools/codegen
author haftmann
Fri, 24 Sep 2010 11:56:14 +0200
changeset 39674 dacf4bad3954
parent 39646 64fdbee67135
child 39714 56c98c820a90
permissions -rwxr-xr-x
load theory explicitly
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21884
7df02627898e added isatool codegen
haftmann
parents:
diff changeset
     1
#!/usr/bin/env bash
7df02627898e added isatool codegen
haftmann
parents:
diff changeset
     2
#
7df02627898e added isatool codegen
haftmann
parents:
diff changeset
     3
# Author: Florian Haftmann, TUM
7df02627898e added isatool codegen
haftmann
parents:
diff changeset
     4
#
7df02627898e added isatool codegen
haftmann
parents:
diff changeset
     5
# DESCRIPTION: issue code generation from shell
7df02627898e added isatool codegen
haftmann
parents:
diff changeset
     6
7df02627898e added isatool codegen
haftmann
parents:
diff changeset
     7
7df02627898e added isatool codegen
haftmann
parents:
diff changeset
     8
PRG="$(basename "$0")"
7df02627898e added isatool codegen
haftmann
parents:
diff changeset
     9
7df02627898e added isatool codegen
haftmann
parents:
diff changeset
    10
function usage()
7df02627898e added isatool codegen
haftmann
parents:
diff changeset
    11
{
7df02627898e added isatool codegen
haftmann
parents:
diff changeset
    12
  echo
39646
64fdbee67135 improved and tuned external codegen tool
haftmann
parents: 39121
diff changeset
    13
  echo "Usage: isabelle $PRG [OPTIONS] IMAGE THYNAME CMD"
32483
522f04b719c8 added -q switch for run in qnd mode
haftmann
parents: 32482
diff changeset
    14
  echo
522f04b719c8 added -q switch for run in qnd mode
haftmann
parents: 32482
diff changeset
    15
  echo "  Options are:"
522f04b719c8 added -q switch for run in qnd mode
haftmann
parents: 32482
diff changeset
    16
  echo "    -q    run in quick'n'dirty mode"
21884
7df02627898e added isatool codegen
haftmann
parents:
diff changeset
    17
  echo
7df02627898e added isatool codegen
haftmann
parents:
diff changeset
    18
  echo "  Issues code generation using image IMAGE,"
39646
64fdbee67135 improved and tuned external codegen tool
haftmann
parents: 39121
diff changeset
    19
  echo "  theory THYNAME,"
25611
c0deb7307732 isatool codegen now returns exit value
haftmann
parents: 25451
diff changeset
    20
  echo "  with Isar command 'export_code CMD'"
21884
7df02627898e added isatool codegen
haftmann
parents:
diff changeset
    21
  echo
7df02627898e added isatool codegen
haftmann
parents:
diff changeset
    22
  exit 1
7df02627898e added isatool codegen
haftmann
parents:
diff changeset
    23
}
7df02627898e added isatool codegen
haftmann
parents:
diff changeset
    24
39646
64fdbee67135 improved and tuned external codegen tool
haftmann
parents: 39121
diff changeset
    25
21884
7df02627898e added isatool codegen
haftmann
parents:
diff changeset
    26
## process command line
7df02627898e added isatool codegen
haftmann
parents:
diff changeset
    27
39646
64fdbee67135 improved and tuned external codegen tool
haftmann
parents: 39121
diff changeset
    28
QUICK_AND_DIRTY="false"
32483
522f04b719c8 added -q switch for run in qnd mode
haftmann
parents: 32482
diff changeset
    29
522f04b719c8 added -q switch for run in qnd mode
haftmann
parents: 32482
diff changeset
    30
while getopts "q" OPT
522f04b719c8 added -q switch for run in qnd mode
haftmann
parents: 32482
diff changeset
    31
do
522f04b719c8 added -q switch for run in qnd mode
haftmann
parents: 32482
diff changeset
    32
  case "$OPT" in
522f04b719c8 added -q switch for run in qnd mode
haftmann
parents: 32482
diff changeset
    33
    q)
39646
64fdbee67135 improved and tuned external codegen tool
haftmann
parents: 39121
diff changeset
    34
      QUICK_AND_DIRTY="true"
32483
522f04b719c8 added -q switch for run in qnd mode
haftmann
parents: 32482
diff changeset
    35
      ;;
522f04b719c8 added -q switch for run in qnd mode
haftmann
parents: 32482
diff changeset
    36
    \?)
522f04b719c8 added -q switch for run in qnd mode
haftmann
parents: 32482
diff changeset
    37
      usage
522f04b719c8 added -q switch for run in qnd mode
haftmann
parents: 32482
diff changeset
    38
      ;;
522f04b719c8 added -q switch for run in qnd mode
haftmann
parents: 32482
diff changeset
    39
  esac
522f04b719c8 added -q switch for run in qnd mode
haftmann
parents: 32482
diff changeset
    40
done
522f04b719c8 added -q switch for run in qnd mode
haftmann
parents: 32482
diff changeset
    41
522f04b719c8 added -q switch for run in qnd mode
haftmann
parents: 32482
diff changeset
    42
shift $(($OPTIND - 1))
522f04b719c8 added -q switch for run in qnd mode
haftmann
parents: 32482
diff changeset
    43
522f04b719c8 added -q switch for run in qnd mode
haftmann
parents: 32482
diff changeset
    44
[ "$#" -ne 3 ] && usage
21884
7df02627898e added isatool codegen
haftmann
parents:
diff changeset
    45
7df02627898e added isatool codegen
haftmann
parents:
diff changeset
    46
IMAGE="$1"; shift
39646
64fdbee67135 improved and tuned external codegen tool
haftmann
parents: 39121
diff changeset
    47
THYNAME="$1"; shift
64fdbee67135 improved and tuned external codegen tool
haftmann
parents: 39121
diff changeset
    48
CODE_EXPR=$(echo "$1" | perl -pe 's/\\/\\\\/g; s/"/\\\"/g')
21884
7df02627898e added isatool codegen
haftmann
parents:
diff changeset
    49
7df02627898e added isatool codegen
haftmann
parents:
diff changeset
    50
39646
64fdbee67135 improved and tuned external codegen tool
haftmann
parents: 39121
diff changeset
    51
## invoke code generation
21884
7df02627898e added isatool codegen
haftmann
parents:
diff changeset
    52
39674
dacf4bad3954 load theory explicitly
haftmann
parents: 39646
diff changeset
    53
FORMAL_CMD="Toplevel.program (fn () => use_thy thyname; ML_Context.eval_text_in \
39646
64fdbee67135 improved and tuned external codegen tool
haftmann
parents: 39121
diff changeset
    54
  (SOME (ProofContext.init_global (Thy_Info.get_theory thyname))) false Position.none ml_cmd) \
64fdbee67135 improved and tuned external codegen tool
haftmann
parents: 39121
diff changeset
    55
  handle _ => OS.Process.exit OS.Process.failure;"
32483
522f04b719c8 added -q switch for run in qnd mode
haftmann
parents: 32482
diff changeset
    56
39646
64fdbee67135 improved and tuned external codegen tool
haftmann
parents: 39121
diff changeset
    57
ACTUAL_CMD="val thyname = \"$THYNAME\"; \
64fdbee67135 improved and tuned external codegen tool
haftmann
parents: 39121
diff changeset
    58
  val qnd = $QUICK_AND_DIRTY; \
64fdbee67135 improved and tuned external codegen tool
haftmann
parents: 39121
diff changeset
    59
  val cmd_expr = \"$CODE_EXPR\"; \
64fdbee67135 improved and tuned external codegen tool
haftmann
parents: 39121
diff changeset
    60
  val ml_cmd = \"Code_Target.codegen_tool thyname qnd cmd_expr\"; \
64fdbee67135 improved and tuned external codegen tool
haftmann
parents: 39121
diff changeset
    61
  $FORMAL_CMD"
32483
522f04b719c8 added -q switch for run in qnd mode
haftmann
parents: 32482
diff changeset
    62
39646
64fdbee67135 improved and tuned external codegen tool
haftmann
parents: 39121
diff changeset
    63
"$ISABELLE_PROCESS" -r -q -e "$ACTUAL_CMD" "$IMAGE" || exit 1