author | wenzelm |
Fri, 23 Jun 2017 14:38:32 +0200 | |
changeset 66176 | b51a40281016 |
parent 65431 | 4a3e6cda3b94 |
child 66782 | 193c31b79a33 |
permissions | -rw-r--r-- |
62528 | 1 |
/* Title: Pure/ML/ml_syntax.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Concrete ML syntax for basic values. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
10 |
object ML_Syntax |
|
11 |
{ |
|
62709 | 12 |
/* int */ |
13 |
||
62717 | 14 |
private def signed_int(s: String): String = |
15 |
if (s(0) == '-') "~" + s.substring(1) else s |
|
62709 | 16 |
|
63805 | 17 |
def print_int(i: Int): String = signed_int(Value.Int(i)) |
18 |
def print_long(i: Long): String = signed_int(Value.Long(i)) |
|
62709 | 19 |
|
20 |
||
21 |
/* string */ |
|
22 |
||
62528 | 23 |
private def print_chr(c: Byte): String = |
24 |
c match { |
|
25 |
case 34 => "\\\"" |
|
26 |
case 92 => "\\\\" |
|
27 |
case 9 => "\\t" |
|
28 |
case 10 => "\\n" |
|
29 |
case 12 => "\\f" |
|
30 |
case 13 => "\\r" |
|
31 |
case _ => |
|
32 |
if (c < 0) "\\" + Library.signed_string_of_int(256 + c) |
|
33 |
else if (c < 32) new String(Array[Char](92, 94, (c + 64).toChar)) |
|
34 |
else if (c < 127) Symbol.ascii(c.toChar) |
|
35 |
else "\\" + Library.signed_string_of_int(c) |
|
36 |
} |
|
37 |
||
38 |
def print_char(s: Symbol.Symbol): String = |
|
39 |
if (s.startsWith("\\<")) s |
|
40 |
else UTF8.bytes(s).iterator.map(print_chr(_)).mkString |
|
41 |
||
42 |
def print_string(str: String): String = |
|
43 |
quote(Symbol.iterator(str).map(print_char(_)).mkString) |
|
44 |
||
62638 | 45 |
def print_string0(str: String): String = |
62528 | 46 |
quote(UTF8.bytes(str).iterator.map(print_chr(_)).mkString) |
62544 | 47 |
|
62709 | 48 |
|
65431
4a3e6cda3b94
provide session base for "isabelle build" and "isabelle console" ML process;
wenzelm
parents:
63805
diff
changeset
|
49 |
/* pair */ |
4a3e6cda3b94
provide session base for "isabelle build" and "isabelle console" ML process;
wenzelm
parents:
63805
diff
changeset
|
50 |
|
4a3e6cda3b94
provide session base for "isabelle build" and "isabelle console" ML process;
wenzelm
parents:
63805
diff
changeset
|
51 |
def print_pair[A, B](f: A => String, g: B => String)(pair: (A, B)): String = |
4a3e6cda3b94
provide session base for "isabelle build" and "isabelle console" ML process;
wenzelm
parents:
63805
diff
changeset
|
52 |
"(" + f(pair._1) + ", " + g(pair._2) + ")" |
4a3e6cda3b94
provide session base for "isabelle build" and "isabelle console" ML process;
wenzelm
parents:
63805
diff
changeset
|
53 |
|
4a3e6cda3b94
provide session base for "isabelle build" and "isabelle console" ML process;
wenzelm
parents:
63805
diff
changeset
|
54 |
|
62709 | 55 |
/* list */ |
56 |
||
62544 | 57 |
def print_list[A](f: A => String)(list: List[A]): String = |
58 |
"[" + commas(list.map(f)) + "]" |
|
62528 | 59 |
} |