forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMIntBuiltin.java
More file actions
79 lines (68 loc) · 2.55 KB
/
MIntBuiltin.java
File metadata and controls
79 lines (68 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) 2014-2016 K Team. All Rights Reserved.
package org.kframework.compile;
import org.apache.commons.lang3.tuple.Pair;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MIntBuiltin {
private static Map<Pair<BigInteger, Integer>, MIntBuiltin> tokenCache = new HashMap<>();
public static MIntBuiltin of(BigInteger value, int precision) {
assert value != null;
Pair<BigInteger, Integer> key = Pair.of(value, precision);
MIntBuiltin mintBuiltin = tokenCache.get(key);
if (mintBuiltin == null) {
mintBuiltin = new MIntBuiltin(value, precision);
tokenCache.put(key, mintBuiltin);
}
return mintBuiltin;
}
public static MIntBuiltin of(String value) {
Pair<BigInteger, Integer> pair = MIntBuiltin.parseKMInt(value);
return of(pair.getLeft(), pair.getRight());
}
private final BigInteger value;
private final int precision;
private MIntBuiltin(BigInteger value, int precision) {
this.value = value;
this.precision = precision;
}
private static Pattern pattern = Pattern.compile("(\\d+*)[pP](\\d+)");
public static Pair<BigInteger, Integer> parseKMInt(String s) {
try {
Matcher m = pattern.matcher(s);
int precision;
String value;
if (m.matches()) {
precision = Integer.parseInt(m.group(2));
value = m.group(1);
} else if (s.endsWith("i") || s.endsWith("I")) {
precision = 32;
value = s.substring(0, s.length() - 1);
} else if (s.endsWith("l") || s.endsWith("L")) {
precision = 64;
value = s.substring(0, s.length() - 1);
} else if (s.endsWith("s") || s.endsWith("S")) {
precision = 16;
value = s.substring(0, s.length() - 1);
} else if (s.endsWith("b") || s.endsWith("B")) {
precision = 8;
value = s.substring(0, s.length() - 1);
} else {
throw new NumberFormatException();
}
BigInteger result = new BigInteger(value);
return Pair.of(result, precision);
} catch (IllegalArgumentException e) {
e.printStackTrace();
throw new NumberFormatException();
}
}
public int precision() {
return precision;
}
public BigInteger value() {
return value;
}
}